{"id":281,"date":"2019-11-01T15:37:31","date_gmt":"2019-11-01T18:37:31","guid":{"rendered":"http:\/\/xaxowareti.com.br\/?p=281"},"modified":"2019-11-01T15:37:37","modified_gmt":"2019-11-01T18:37:37","slug":"how-to-restrict-sftp-users-to-home-directories-using-chroot-jail","status":"publish","type":"post","link":"https:\/\/xaxowareti.com.br\/?p=281","title":{"rendered":"How to Restrict SFTP Users to Home Directories Using chroot Jail"},"content":{"rendered":"\n<p>In this tutorial, we will be discussing how to restrict&nbsp;<strong>SFTP<\/strong>&nbsp;users to their home directories or specific directories. It means the user can only access his\/her respective home directory, not the entire file system.<\/p>\n\n\n\n<p>Restricting users home directories is vital, especially in a shared server environment, so that an unauthorized user won\u2019t sneak peek into the other user\u2019s files and folders.<\/p>\n\n\n\n<p><strong>Important<\/strong>: Please also note that the purpose of this article is to provide SFTP access only, not SSH logins, by following this article will have the permissions to do file transfer, but not allowed to do a remote SSH session.<\/p>\n\n\n\n<p><strong>Suggested Read:<\/strong>&nbsp;<a target=\"_blank\" href=\"https:\/\/www.tecmint.com\/restrict-ssh-user-to-directory-using-chrooted-jail\/\" rel=\"noreferrer noopener\">Restrict SSH User Access to Certain Directory Using Chrooted Jail<\/a><\/p>\n\n\n\n<p>The simplest way to do this, is to create a chrooted jail environment for SFTP access. This method is same for all Unix\/Linux operating systems. Using chrooted environment, we can restrict users either to their home directory or to a specific directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Restrict Users to Home Directories<\/h3>\n\n\n\n<p>In this section, we will create new group called&nbsp;<strong>sftpgroup<\/strong>&nbsp;and assign correct ownership and permissions to user accounts. There are two choices to restrict users to home or specific directories, we will see both way in this article.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create or Modify Users and Groups<\/h4>\n\n\n\n<p>Let us restrict the existing user, for example&nbsp;<code>tecmint<\/code>, to his\/her home directory named&nbsp;<code>\/home\/tecmint<\/code>. For this, you need to create a new&nbsp;<strong>sftpgroup<\/strong>&nbsp;group using&nbsp;<strong>groupadd<\/strong>&nbsp;command as shown:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># groupadd sftpgroup\n<\/pre>\n\n\n\n<p>Next, assign the user&nbsp;<strong>\u2018tecmint\u2019<\/strong>&nbsp;to&nbsp;<strong>sftpgroup<\/strong>&nbsp;group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># usermod -G sftpgroup tecmint\n<\/pre>\n\n\n\n<p>You can also create a new user using&nbsp;<a href=\"https:\/\/www.tecmint.com\/add-users-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">useradd command<\/a>, for example&nbsp;<code>senthil<\/code>&nbsp;and assign the user to&nbsp;<strong>sftpusers<\/strong>&nbsp;group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># adduser senthil -g sftpgroup -s \/sbin\/nologin\n# passwd tecmint\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Modify SSH Configuration File<\/h4>\n\n\n\n<p>Open and add the following lines to&nbsp;<code>\/etc\/ssh\/sshd_config<\/code>&nbsp;configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Subsystem sftp internal-sftp\n \n   Match Group sftpgroup\n   ChrootDirectory \/home\n   ForceCommand internal-sftp\n   X11Forwarding no\n   AllowTcpForwarding no\n<\/pre>\n\n\n\n<p>Save and exit the file, restart sshd service to take new changes into effect.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># systemctl restart sshd\nOR\n# service sshd restart\n<\/pre>\n\n\n\n<p>If you chroot multiple users to the same directory, you should change the permissions of each user\u2019s home directory in order to prevent all users to browse the home directories of the each other users.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># chmod 700 \/home\/tecmint\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Verify SSH and SFTP Users Login<\/h4>\n\n\n\n<p>Now, it\u2019s time to check the login from a local system. Try to ssh your remote system from your local system.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># ssh tecmint@192.168.1.150\n<\/pre>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>tecmint<\/strong>&nbsp;\u2013 remote system\u2019s username.<\/li><li><strong>192.168.1.150<\/strong>&nbsp;\u2013 Remote system\u2019s IP address.<\/li><\/ol>\n\n\n\n<h5 class=\"wp-block-heading\">Sample output:<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">tecmint@192.168.1.150's password: \nCould not chdir to home directory \/home\/tecmint: No such file or directory\nThis service allows sftp connections only.\nConnection to 192.168.1.150 closed.\n<\/pre>\n\n\n\n<p>Then, access remote system using SFTP.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># sftp tecmint@192.168.1.150\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Sample output:<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">tecmint@192.168.1.150's password: \nConnected to 192.168.1.150.\nsftp&gt;\n<\/pre>\n\n\n\n<p>Let us check the current working directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>sftp&amp;gt pwd<\/strong>\nRemote working directory: \/\n\n<strong>sftp&amp;gt ls<\/strong>\ntecmint  \n<\/pre>\n\n\n\n<p>Here,&nbsp;<code>tecmint<\/code>&nbsp;is the home directory. Cd to the&nbsp;<strong>tecmint<\/strong>&nbsp;directory and create the files or folders of your choice.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>sftp&amp;gt cd tecmint<\/strong>\nRemote working directory: \/\n\n<strong>sftp&amp;gt mkdir test<\/strong>\ntecmint  \n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Restrict Users to a Specific Directory<\/h3>\n\n\n\n<p>In our previous example, we restrict the existing users to the home directory. Now, we will see how to restrict a new user to a custom directory.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create Group and New Users<\/h4>\n\n\n\n<p>Create a new group&nbsp;<code>sftpgroup<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># groupadd sftpgroup\n<\/pre>\n\n\n\n<p>Next, create a directory for SFTP group and assign permissions for the root user.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># mkdir -p \/sftpusers\/chroot\n# chown root:root \/sftpusers\/chroot\/\n<\/pre>\n\n\n\n<p>Next, create new directories for each user, to which they will have full access. For example, we will create&nbsp;<code>tecmint<\/code>&nbsp;user and it\u2019s new home directory with correct group permission using following series of commands.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># adduser tecmint -g sftpgroup -s \/sbin\/nologin\n# passwd tecmint\n# mkdir \/sftpusers\/chroot\/tecmint\n# chown tecmint:sftpgroup \/sftpusers\/chroot\/tecmint\/\n# chmod 700 \/sftpusers\/chroot\/tecmint\/\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure SSH for SFTP Access<\/h4>\n\n\n\n<p>Modify or add the following lines at the end of the file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#Subsystem  \tsftp\t\/usr\/libexec\/openssh\/sftp-server\nSubsystem sftp  internal-sftp\n \nMatch Group sftpgroup\n   ChrootDirectory \/sftpusers\/chroot\/\n   ForceCommand internal-sftp\n   X11Forwarding no\n   AllowTcpForwarding no\n<\/pre>\n\n\n\n<p>Save and exit the file. Restart sshd service to take effect the saved changes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># systemctl restart sshd\nOR\n# service sshd restart\n<\/pre>\n\n\n\n<p>That\u2019s it, you can check by logging into the your remote SSH and SFTP server by using the step provided above at&nbsp;<a href=\"https:\/\/www.tecmint.com\/restrict-sftp-user-home-directories-using-chroot\/#verifysftp\">Verify SSH and SFTP login<\/a>.<\/p>\n\n\n\n<p>Be mindful that this method will disable the shell access, i.e you can\u2019t access the remote system\u2019s shell session using SSH. You can only access the remote systems via SFTP and do file transfer to and from the local and remote systems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Now you know how to restrict users home directories using a Chroot environment in Linux. If you find this useful, share this article on your social networks and let us know in the comment section below if there is any other methods to restrict users home directories.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will be discussing how to restrict&nbsp;SFTP&nbsp;users to their home directories or specific directories. It means the user can only access his\/her respective home directory, not the entire file system. Restricting users home directories is vital, especially in a shared server environment, so that an unauthorized user won\u2019t sneak peek into the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-281","post","type-post","status-publish","format-standard","hentry","category-sem-categoria"],"_links":{"self":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=281"}],"version-history":[{"count":1,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":282,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/281\/revisions\/282"}],"wp:attachment":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}