OpenSSH - Restrict users to one or multiple folders












1














I want to know how I can restrict user access using OpenSSH Server.
Let's say I want




  • user 1 to only access /Media, /Documents, and his home folder,

  • User 2 should only access /Folder21, and his homefolder,

  • User 3 should only have access to /Documents, /Folder21 and his home folder,


How can I do such a thing?



I found how to do it with one folder, but that was for all users, I think?
description here: https://bensmann.no/restrict-sftp-users-to-home-folder/



(copied from link I gave above:

Can I just do this:



$ usermod User1 -s /bin/false
$ usermod User2 -s /bin/false


And set the user’s home directory:



$ usermod User1 -d /folder
$ usermod User2 -d /folder


then make all the other folders subfolders of /folder, and restrict access to those folders accordingly to my wishes using the chmod command, or is there a better solution??



Addition and clarification:
Adding it to their homedirectory is not an option, as it are shared folders, some users need access to the same content,while it is restricted to other users,



This is what I had in mind:




  • user 1

  • user 2

  • user 3

  • user 4


  • folder "Network documentation"


  • folder "application documentation"

  • folder "Downloads"

  • folder "Media"


  • User 1 needs full access to everything, but Read-only access to application documentation List item


  • User 2 needs full access to both documentation folders

  • User 3 only needs access to the "Media" folder

  • User 4 only needs access to the folder "Downloads" and "Application Documentation"










share|improve this question





























    1














    I want to know how I can restrict user access using OpenSSH Server.
    Let's say I want




    • user 1 to only access /Media, /Documents, and his home folder,

    • User 2 should only access /Folder21, and his homefolder,

    • User 3 should only have access to /Documents, /Folder21 and his home folder,


    How can I do such a thing?



    I found how to do it with one folder, but that was for all users, I think?
    description here: https://bensmann.no/restrict-sftp-users-to-home-folder/



    (copied from link I gave above:

    Can I just do this:



    $ usermod User1 -s /bin/false
    $ usermod User2 -s /bin/false


    And set the user’s home directory:



    $ usermod User1 -d /folder
    $ usermod User2 -d /folder


    then make all the other folders subfolders of /folder, and restrict access to those folders accordingly to my wishes using the chmod command, or is there a better solution??



    Addition and clarification:
    Adding it to their homedirectory is not an option, as it are shared folders, some users need access to the same content,while it is restricted to other users,



    This is what I had in mind:




    • user 1

    • user 2

    • user 3

    • user 4


    • folder "Network documentation"


    • folder "application documentation"

    • folder "Downloads"

    • folder "Media"


    • User 1 needs full access to everything, but Read-only access to application documentation List item


    • User 2 needs full access to both documentation folders

    • User 3 only needs access to the "Media" folder

    • User 4 only needs access to the folder "Downloads" and "Application Documentation"










    share|improve this question



























      1












      1








      1


      1





      I want to know how I can restrict user access using OpenSSH Server.
      Let's say I want




      • user 1 to only access /Media, /Documents, and his home folder,

      • User 2 should only access /Folder21, and his homefolder,

      • User 3 should only have access to /Documents, /Folder21 and his home folder,


      How can I do such a thing?



      I found how to do it with one folder, but that was for all users, I think?
      description here: https://bensmann.no/restrict-sftp-users-to-home-folder/



      (copied from link I gave above:

      Can I just do this:



      $ usermod User1 -s /bin/false
      $ usermod User2 -s /bin/false


      And set the user’s home directory:



      $ usermod User1 -d /folder
      $ usermod User2 -d /folder


      then make all the other folders subfolders of /folder, and restrict access to those folders accordingly to my wishes using the chmod command, or is there a better solution??



      Addition and clarification:
      Adding it to their homedirectory is not an option, as it are shared folders, some users need access to the same content,while it is restricted to other users,



      This is what I had in mind:




      • user 1

      • user 2

      • user 3

      • user 4


      • folder "Network documentation"


      • folder "application documentation"

      • folder "Downloads"

      • folder "Media"


      • User 1 needs full access to everything, but Read-only access to application documentation List item


      • User 2 needs full access to both documentation folders

      • User 3 only needs access to the "Media" folder

      • User 4 only needs access to the folder "Downloads" and "Application Documentation"










      share|improve this question















      I want to know how I can restrict user access using OpenSSH Server.
      Let's say I want




      • user 1 to only access /Media, /Documents, and his home folder,

      • User 2 should only access /Folder21, and his homefolder,

      • User 3 should only have access to /Documents, /Folder21 and his home folder,


      How can I do such a thing?



      I found how to do it with one folder, but that was for all users, I think?
      description here: https://bensmann.no/restrict-sftp-users-to-home-folder/



      (copied from link I gave above:

      Can I just do this:



      $ usermod User1 -s /bin/false
      $ usermod User2 -s /bin/false


      And set the user’s home directory:



      $ usermod User1 -d /folder
      $ usermod User2 -d /folder


      then make all the other folders subfolders of /folder, and restrict access to those folders accordingly to my wishes using the chmod command, or is there a better solution??



      Addition and clarification:
      Adding it to their homedirectory is not an option, as it are shared folders, some users need access to the same content,while it is restricted to other users,



      This is what I had in mind:




      • user 1

      • user 2

      • user 3

      • user 4


      • folder "Network documentation"


      • folder "application documentation"

      • folder "Downloads"

      • folder "Media"


      • User 1 needs full access to everything, but Read-only access to application documentation List item


      • User 2 needs full access to both documentation folders

      • User 3 only needs access to the "Media" folder

      • User 4 only needs access to the folder "Downloads" and "Application Documentation"







      permissions directory openssh groups






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 3 '14 at 19:04

























      asked Oct 2 '14 at 17:34









      FrankB

      613




      613






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Could you not just put whatever folders you want each user to access in their home directory? If not, I believe that ssh uses the same file permissions as the file system on the computer. So you can just use the same file permissions modifiers you would use to give a user access to a folder outside their home directory, like this:
          https://superuser.com/questions/280994/give-write-permissions-to-multiple-users-on-a-folder-in-ubuntu






          share|improve this answer























          • Assuming "access" is to be understood broadly, this solution would be nontrivial to implement and possibly undesirable. Users have read and even execute access in many places outside their home folder (and the ability to create new files in some areas outside it, such as /tmp). I think it's complicated to change this just with UNIX permissions, ACLs, and/or AppArmor, and would break many important aspects of an interactive SSH session. For example, they must be able to execute their shell but it's usually located outside their home folder. And they must have access to some nodes in /dev.
            – Eliah Kagan
            Oct 2 '14 at 20:45





















          0














          You could do a simple permissions control system.



          First off, you need a directory tree, so let's do this:



          ~ (Home Folder
          |
          |---> Their Files
          |
          |---> Symlink to /opt/public/


          In /opt/public/, you have all of your folders. In this case,




          • Media

          • Documents

          • Folder51


          As you only have three users (and three folders), you could create groups.



          A simple way to do this is to create a group for each folder. For instance:




          • groupadd opt-public-media-access

          • groupadd opt-public-documents-access

          • groupadd opt-public-folder51-access


          Now, add the users to the specific groups (in this case, user1 is going to the media group.)



          usermod -a -G opt-public-media-access user1


          Finally, run these commands on each folder to secure read/write perms to the specific group only:



          chown -R root:opt-public-media-access /opt/public/Media
          chmod 770 -R /opt/public/Media/


          Add and remove groups/folders at will. A few things to note, though:




          • The command to remove groups is groupdel. You should delete the group when you delete the folder.

          • Other users on the system not in the proper group will not be able to get into those folders, regardless.

          • Give each user their own home folder. Do not place it into something like /opt/public.

          • Root can go wherever he wants. Make sure none of the users have sudo rights.


          • All commands in this post are meant to be run as root, unless otherwise specified.






          share|improve this answer























          • Okay, this sounds very easy.. I will try it and see how far I get, Thanks for your answer
            – FrankB
            Oct 3 '14 at 17:34












          • Would the following help in setting it up? Then add the following stanza at the end of the file(etc/ssh/sshd_config) (add such a stanza for each group that you want to chroot): Match Group users ChrootDirectory /home AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
            – FrankB
            Oct 3 '14 at 18:32












          • I don't think that it'll matter.
            – Kaz Wolfe
            Oct 3 '14 at 18:58













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f531490%2fopenssh-restrict-users-to-one-or-multiple-folders%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Could you not just put whatever folders you want each user to access in their home directory? If not, I believe that ssh uses the same file permissions as the file system on the computer. So you can just use the same file permissions modifiers you would use to give a user access to a folder outside their home directory, like this:
          https://superuser.com/questions/280994/give-write-permissions-to-multiple-users-on-a-folder-in-ubuntu






          share|improve this answer























          • Assuming "access" is to be understood broadly, this solution would be nontrivial to implement and possibly undesirable. Users have read and even execute access in many places outside their home folder (and the ability to create new files in some areas outside it, such as /tmp). I think it's complicated to change this just with UNIX permissions, ACLs, and/or AppArmor, and would break many important aspects of an interactive SSH session. For example, they must be able to execute their shell but it's usually located outside their home folder. And they must have access to some nodes in /dev.
            – Eliah Kagan
            Oct 2 '14 at 20:45


















          0














          Could you not just put whatever folders you want each user to access in their home directory? If not, I believe that ssh uses the same file permissions as the file system on the computer. So you can just use the same file permissions modifiers you would use to give a user access to a folder outside their home directory, like this:
          https://superuser.com/questions/280994/give-write-permissions-to-multiple-users-on-a-folder-in-ubuntu






          share|improve this answer























          • Assuming "access" is to be understood broadly, this solution would be nontrivial to implement and possibly undesirable. Users have read and even execute access in many places outside their home folder (and the ability to create new files in some areas outside it, such as /tmp). I think it's complicated to change this just with UNIX permissions, ACLs, and/or AppArmor, and would break many important aspects of an interactive SSH session. For example, they must be able to execute their shell but it's usually located outside their home folder. And they must have access to some nodes in /dev.
            – Eliah Kagan
            Oct 2 '14 at 20:45
















          0












          0








          0






          Could you not just put whatever folders you want each user to access in their home directory? If not, I believe that ssh uses the same file permissions as the file system on the computer. So you can just use the same file permissions modifiers you would use to give a user access to a folder outside their home directory, like this:
          https://superuser.com/questions/280994/give-write-permissions-to-multiple-users-on-a-folder-in-ubuntu






          share|improve this answer














          Could you not just put whatever folders you want each user to access in their home directory? If not, I believe that ssh uses the same file permissions as the file system on the computer. So you can just use the same file permissions modifiers you would use to give a user access to a folder outside their home directory, like this:
          https://superuser.com/questions/280994/give-write-permissions-to-multiple-users-on-a-folder-in-ubuntu







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 20 '17 at 10:18









          Community

          1




          1










          answered Oct 2 '14 at 20:39









          Termhn

          40127




          40127












          • Assuming "access" is to be understood broadly, this solution would be nontrivial to implement and possibly undesirable. Users have read and even execute access in many places outside their home folder (and the ability to create new files in some areas outside it, such as /tmp). I think it's complicated to change this just with UNIX permissions, ACLs, and/or AppArmor, and would break many important aspects of an interactive SSH session. For example, they must be able to execute their shell but it's usually located outside their home folder. And they must have access to some nodes in /dev.
            – Eliah Kagan
            Oct 2 '14 at 20:45




















          • Assuming "access" is to be understood broadly, this solution would be nontrivial to implement and possibly undesirable. Users have read and even execute access in many places outside their home folder (and the ability to create new files in some areas outside it, such as /tmp). I think it's complicated to change this just with UNIX permissions, ACLs, and/or AppArmor, and would break many important aspects of an interactive SSH session. For example, they must be able to execute their shell but it's usually located outside their home folder. And they must have access to some nodes in /dev.
            – Eliah Kagan
            Oct 2 '14 at 20:45


















          Assuming "access" is to be understood broadly, this solution would be nontrivial to implement and possibly undesirable. Users have read and even execute access in many places outside their home folder (and the ability to create new files in some areas outside it, such as /tmp). I think it's complicated to change this just with UNIX permissions, ACLs, and/or AppArmor, and would break many important aspects of an interactive SSH session. For example, they must be able to execute their shell but it's usually located outside their home folder. And they must have access to some nodes in /dev.
          – Eliah Kagan
          Oct 2 '14 at 20:45






          Assuming "access" is to be understood broadly, this solution would be nontrivial to implement and possibly undesirable. Users have read and even execute access in many places outside their home folder (and the ability to create new files in some areas outside it, such as /tmp). I think it's complicated to change this just with UNIX permissions, ACLs, and/or AppArmor, and would break many important aspects of an interactive SSH session. For example, they must be able to execute their shell but it's usually located outside their home folder. And they must have access to some nodes in /dev.
          – Eliah Kagan
          Oct 2 '14 at 20:45















          0














          You could do a simple permissions control system.



          First off, you need a directory tree, so let's do this:



          ~ (Home Folder
          |
          |---> Their Files
          |
          |---> Symlink to /opt/public/


          In /opt/public/, you have all of your folders. In this case,




          • Media

          • Documents

          • Folder51


          As you only have three users (and three folders), you could create groups.



          A simple way to do this is to create a group for each folder. For instance:




          • groupadd opt-public-media-access

          • groupadd opt-public-documents-access

          • groupadd opt-public-folder51-access


          Now, add the users to the specific groups (in this case, user1 is going to the media group.)



          usermod -a -G opt-public-media-access user1


          Finally, run these commands on each folder to secure read/write perms to the specific group only:



          chown -R root:opt-public-media-access /opt/public/Media
          chmod 770 -R /opt/public/Media/


          Add and remove groups/folders at will. A few things to note, though:




          • The command to remove groups is groupdel. You should delete the group when you delete the folder.

          • Other users on the system not in the proper group will not be able to get into those folders, regardless.

          • Give each user their own home folder. Do not place it into something like /opt/public.

          • Root can go wherever he wants. Make sure none of the users have sudo rights.


          • All commands in this post are meant to be run as root, unless otherwise specified.






          share|improve this answer























          • Okay, this sounds very easy.. I will try it and see how far I get, Thanks for your answer
            – FrankB
            Oct 3 '14 at 17:34












          • Would the following help in setting it up? Then add the following stanza at the end of the file(etc/ssh/sshd_config) (add such a stanza for each group that you want to chroot): Match Group users ChrootDirectory /home AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
            – FrankB
            Oct 3 '14 at 18:32












          • I don't think that it'll matter.
            – Kaz Wolfe
            Oct 3 '14 at 18:58


















          0














          You could do a simple permissions control system.



          First off, you need a directory tree, so let's do this:



          ~ (Home Folder
          |
          |---> Their Files
          |
          |---> Symlink to /opt/public/


          In /opt/public/, you have all of your folders. In this case,




          • Media

          • Documents

          • Folder51


          As you only have three users (and three folders), you could create groups.



          A simple way to do this is to create a group for each folder. For instance:




          • groupadd opt-public-media-access

          • groupadd opt-public-documents-access

          • groupadd opt-public-folder51-access


          Now, add the users to the specific groups (in this case, user1 is going to the media group.)



          usermod -a -G opt-public-media-access user1


          Finally, run these commands on each folder to secure read/write perms to the specific group only:



          chown -R root:opt-public-media-access /opt/public/Media
          chmod 770 -R /opt/public/Media/


          Add and remove groups/folders at will. A few things to note, though:




          • The command to remove groups is groupdel. You should delete the group when you delete the folder.

          • Other users on the system not in the proper group will not be able to get into those folders, regardless.

          • Give each user their own home folder. Do not place it into something like /opt/public.

          • Root can go wherever he wants. Make sure none of the users have sudo rights.


          • All commands in this post are meant to be run as root, unless otherwise specified.






          share|improve this answer























          • Okay, this sounds very easy.. I will try it and see how far I get, Thanks for your answer
            – FrankB
            Oct 3 '14 at 17:34












          • Would the following help in setting it up? Then add the following stanza at the end of the file(etc/ssh/sshd_config) (add such a stanza for each group that you want to chroot): Match Group users ChrootDirectory /home AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
            – FrankB
            Oct 3 '14 at 18:32












          • I don't think that it'll matter.
            – Kaz Wolfe
            Oct 3 '14 at 18:58
















          0












          0








          0






          You could do a simple permissions control system.



          First off, you need a directory tree, so let's do this:



          ~ (Home Folder
          |
          |---> Their Files
          |
          |---> Symlink to /opt/public/


          In /opt/public/, you have all of your folders. In this case,




          • Media

          • Documents

          • Folder51


          As you only have three users (and three folders), you could create groups.



          A simple way to do this is to create a group for each folder. For instance:




          • groupadd opt-public-media-access

          • groupadd opt-public-documents-access

          • groupadd opt-public-folder51-access


          Now, add the users to the specific groups (in this case, user1 is going to the media group.)



          usermod -a -G opt-public-media-access user1


          Finally, run these commands on each folder to secure read/write perms to the specific group only:



          chown -R root:opt-public-media-access /opt/public/Media
          chmod 770 -R /opt/public/Media/


          Add and remove groups/folders at will. A few things to note, though:




          • The command to remove groups is groupdel. You should delete the group when you delete the folder.

          • Other users on the system not in the proper group will not be able to get into those folders, regardless.

          • Give each user their own home folder. Do not place it into something like /opt/public.

          • Root can go wherever he wants. Make sure none of the users have sudo rights.


          • All commands in this post are meant to be run as root, unless otherwise specified.






          share|improve this answer














          You could do a simple permissions control system.



          First off, you need a directory tree, so let's do this:



          ~ (Home Folder
          |
          |---> Their Files
          |
          |---> Symlink to /opt/public/


          In /opt/public/, you have all of your folders. In this case,




          • Media

          • Documents

          • Folder51


          As you only have three users (and three folders), you could create groups.



          A simple way to do this is to create a group for each folder. For instance:




          • groupadd opt-public-media-access

          • groupadd opt-public-documents-access

          • groupadd opt-public-folder51-access


          Now, add the users to the specific groups (in this case, user1 is going to the media group.)



          usermod -a -G opt-public-media-access user1


          Finally, run these commands on each folder to secure read/write perms to the specific group only:



          chown -R root:opt-public-media-access /opt/public/Media
          chmod 770 -R /opt/public/Media/


          Add and remove groups/folders at will. A few things to note, though:




          • The command to remove groups is groupdel. You should delete the group when you delete the folder.

          • Other users on the system not in the proper group will not be able to get into those folders, regardless.

          • Give each user their own home folder. Do not place it into something like /opt/public.

          • Root can go wherever he wants. Make sure none of the users have sudo rights.


          • All commands in this post are meant to be run as root, unless otherwise specified.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 3 '14 at 17:42

























          answered Oct 3 '14 at 17:32









          Kaz Wolfe

          25.9k1374134




          25.9k1374134












          • Okay, this sounds very easy.. I will try it and see how far I get, Thanks for your answer
            – FrankB
            Oct 3 '14 at 17:34












          • Would the following help in setting it up? Then add the following stanza at the end of the file(etc/ssh/sshd_config) (add such a stanza for each group that you want to chroot): Match Group users ChrootDirectory /home AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
            – FrankB
            Oct 3 '14 at 18:32












          • I don't think that it'll matter.
            – Kaz Wolfe
            Oct 3 '14 at 18:58




















          • Okay, this sounds very easy.. I will try it and see how far I get, Thanks for your answer
            – FrankB
            Oct 3 '14 at 17:34












          • Would the following help in setting it up? Then add the following stanza at the end of the file(etc/ssh/sshd_config) (add such a stanza for each group that you want to chroot): Match Group users ChrootDirectory /home AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
            – FrankB
            Oct 3 '14 at 18:32












          • I don't think that it'll matter.
            – Kaz Wolfe
            Oct 3 '14 at 18:58


















          Okay, this sounds very easy.. I will try it and see how far I get, Thanks for your answer
          – FrankB
          Oct 3 '14 at 17:34






          Okay, this sounds very easy.. I will try it and see how far I get, Thanks for your answer
          – FrankB
          Oct 3 '14 at 17:34














          Would the following help in setting it up? Then add the following stanza at the end of the file(etc/ssh/sshd_config) (add such a stanza for each group that you want to chroot): Match Group users ChrootDirectory /home AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
          – FrankB
          Oct 3 '14 at 18:32






          Would the following help in setting it up? Then add the following stanza at the end of the file(etc/ssh/sshd_config) (add such a stanza for each group that you want to chroot): Match Group users ChrootDirectory /home AllowTCPForwarding no X11Forwarding no ForceCommand internal-sftp
          – FrankB
          Oct 3 '14 at 18:32














          I don't think that it'll matter.
          – Kaz Wolfe
          Oct 3 '14 at 18:58






          I don't think that it'll matter.
          – Kaz Wolfe
          Oct 3 '14 at 18:58




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f531490%2fopenssh-restrict-users-to-one-or-multiple-folders%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?