{"id":4553,"date":"2021-02-17T15:17:35","date_gmt":"2021-02-17T15:17:35","guid":{"rendered":"https:\/\/linuxways.net\/?p=4553"},"modified":"2021-02-17T23:22:37","modified_gmt":"2021-02-17T23:22:37","slug":"how-to-create-sftp-user-with-specified-directory-permissions-in-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-create-sftp-user-with-specified-directory-permissions-in-ubuntu-20-04\/","title":{"rendered":"How to Create SFTP User with Specified Directory Permissions in Ubuntu 20.04"},"content":{"rendered":"<p>SFTP stands for Secure File Transfer Protocol that we used for secure data transfer to and from your local system. It works over SSH Protocol and hence is considered to be more secure than the simple FTP (File Transfer Protocol). Data transferring is a routine based task of every system administrator and other developers who need to make changes in their code.<\/p>\n<p>So, in today&#8217;s article, we are going to show the detailed steps to configure SFTP Server and create users with their specified directory permissions.<\/p>\n<p>For the demonstration, we are using the Ubuntu 20.04 LTS for the SFTP setup, but similar steps can be performed in case you are using the RHEL\/CentOS Operating system.<\/p>\n<h2>Prerequisites:<\/h2>\n<p>Starting from the prerequisites, we need to make sure that we have sudo or root user privileges on the server where we need to create SFTP Users.<\/p>\n<p>Let&#8217;s start by login into the system using ssh for which you can use the Putty or Shell terminal of your Linux Desktop.<\/p>\n<pre># ssh -i key.pem ubuntu@your_server_ip<\/pre>\n<pre># sudo -i<\/pre>\n<p>You can also directly login to your system as well, but make sure to use your own username and credentials.<\/p>\n<h2>Step 1: Creating New SFTP Only User<\/h2>\n<p>Creating a new user for SFTP is as similar as we do for adding any other general users. But in order to restrict that to be used for SFTP purposes only, we will assign it to no login shell.<\/p>\n<p>Run the command below to create an SFTP user by specifying its custom home directory.<\/p>\n<pre># useradd -m -d \/home\/example.com sftp_user<\/pre>\n<p>Set the password of the newly created user using \u2018passwd\u2019 command as below.<\/p>\n<pre># passwd sftp_user<\/pre>\n<p>Make sure to set a complex password for its security. Then assign it a nologin shell by modifying the \u2018passwd\u2019 file as below.<\/p>\n<pre># vim \/etc\/passwd<\/pre>\n<pre>sftp_user:x:1001:1001::\/home\/example.com:\/usr\/sbin\/nologin<\/pre>\n<p>Save and close using :wq! To apply changes.<\/p>\n<h2>Step 2: Setup Directory Permissions<\/h2>\n<p>After creating the new user, we need to set up the right directory permissions and ownership to the user&#8217;s directory that we have created in the first step.<\/p>\n<pre># chmod 755 \/home\/example.com\/<\/pre>\n<pre># chown root:root \/home\/example.com<\/pre>\n<h2>Step 3: Restrict Directory Access<\/h2>\n<p>Now we have a new SFTP only user in place, next we need to restrict its directory access to be accessible to its specified home folder that we created.<\/p>\n<p>In order to do so, we are going to modify the ssh configuration file by adding the following parameters.<\/p>\n<pre># vim \/etc\/ssh\/sshd_config<\/pre>\n<pre>subsystem sftp internal-sftp\n\nMatch User sftp_user\n\nChrootDirectory %h\n\nAllowTCPForwarding no\n\nX11Forwarding no\n\nPasswordAuthentication yes\n\nForceCommand internal-sftp<\/pre>\n<p>Before saving the made changes, make sure to add your username against \u2018Match User\u2019 , whereas \u2018%h\u2019 represents the home directory of your newly created user within the restricted environment. This \u2018ChrootDirectory\u2019 will ensure that your SFTP user won\u2019t have access to any other directory.<br \/>\nSave the ssh configuration file, verify the syntax if there is an issue and then restart its services and confirm if it&#8217;s running using the below commands.<\/p>\n<pre># ssh -t<\/pre>\n<pre># systemctl restart sshd<\/pre>\n<pre># systemctl status sshd<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"746\" height=\"333\" class=\"wp-image-4554\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-296.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-296.png 746w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-296-300x134.png 300w\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" \/><\/p>\n<p>Here, you can see that first there was an error in the ssh configuration file that we fixed by commenting out the additional entry and restart \u2018sshd service.<\/p>\n<h2>Step 4: Testing SFTP Login<\/h2>\n<p>At this point, we are ready to use our SFTp user but before that let&#8217;s test it to make sure it has only SFTP access by using the ssh command.<\/p>\n<pre># <a href=\"mailto:ssh@xx.xx.xx.xx\">ssh@xx.xx.xx.xx<\/a><\/pre>\n<p>Whereas xx needs to be replaced with your own server IP. As a result, you should get the connection failure as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"589\" height=\"118\" class=\"wp-image-4555\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-297.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-297.png 589w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-297-300x60.png 300w\" sizes=\"auto, (max-width: 589px) 100vw, 589px\" \/><\/p>\n<h2>Step 5: Using SFTP Client<\/h2>\n<p>There are a couple of SFTP clients available to use for the cross-platform operating system and you can use your favorite one. We are going to test it using FileZilla which is one of the best and most useful FTP\/SFTP clients available to use. You can easily download it from their official link.<\/p>\n<p>Let&#8217;s open it in your system and give the IP address of your SFTP server along with your created username and password.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1199\" height=\"734\" class=\"wp-image-4556\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-298.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-298.png 1199w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-298-300x184.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-298-1024x627.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-298-768x470.png 768w\" sizes=\"auto, (max-width: 1199px) 100vw, 1199px\" \/><\/p>\n<p>Upon successful credentials, you will be able to have access to your specified directory only where you can upload or download the data.<\/p>\n<h2>Conclusion:<\/h2>\n<p>In this article we have covered the detailed steps to setup and new SFTP server by making the ssh configuration changes, adding new users, and assigning the required directory permissions. You can add as many users as you want or simply create a new group and make new users part of that group.<\/p>","protected":false},"excerpt":{"rendered":"<p>SFTP stands for Secure File Transfer Protocol that we used for secure data transfer to and from your local system. It works over SSH Protocol and hence is&hellip;<\/p>","protected":false},"author":14,"featured_media":4558,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[200,199,100],"class_list":["post-4553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-directory-permissions","tag-sftp-user","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4553"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4553\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/4558"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}