How to Create SFTP Only User in Ubuntu & Debian

Channel: Linux
Abstract: create the directory structure to be accessible by the SFTP user. sudo mkdir -p /var/sftp/filessftpuser ForceCommand internal-sftp enforce the SFTP on

SFTP (SSH File Transfer Protocol) is a secure file protocol used to access, manage, and transfer files over an encrypted SSH transport session. Here SFTP only user means to create an account to access the server via SFTP only. That user doesn’t have SSH shell access. This allows you a secure channel to provide limited access to specific files and directories.

This guide describes you to create SFTP only users without shell access on Ubuntu and Debian systems.

Step 1- Creating a New User

First of all, create a user account in your system to use as an SFTP user. The following command will create a new account named sftpuser with no shell access. You can change the username of your choice

sudo adduser --shell /bin/false sftpuser 

The command will prompt for the password to set for a new account.

Step 2 – Create Directory for SFTP

Now, create the directory structure to be accessible by the SFTP user.

sudo mkdir -p /var/sftp/files 

Here we will allow users to access the 「files」 directory only.

Now, change the ownership of the files directory to the sftpuser. So that SFTP users can read and write on this directory only. No files outside of this directory will be accessible.

sudo chown sftpuser:sftpuser /var/sftp/files 

And set the owner and group owner of the /var/sftp to root. The root user has read/write access on this access. Group members and other accounts have only read and execute permissions.

sudo chown root:root /var/sftp 
sudo chmod 755 /var/sftp 

It will restrict SFTP users from writing files under /var/sftp directory.

Step 3 – Configure sshd for SFTP Only

/etc/ssh/sshd_config is the main configuration file of the OpenSSH server. Be careful with changing in this configuration file, because any mistake can lead connection lost.

Eit the SSH configuration file in a text editor:

sudo vim /etc/ssh/sshd_config 

and add the following settings at end of file.

Match User sftpuser
	ForceCommand internal-sftp
	PasswordAuthentication yes
	ChrootDirectory /var/sftp
	PermitTunnel no
	AllowAgentForwarding no
	AllowTcpForwarding no
	X11Forwarding no

Save the file and close.

The directives are:

  • Match User Defines the username, on which the SFTP only configurations applied. In our case it is: sftpuser
  • ForceCommand internal-sftp enforce the SFTP only access to user and restrict for the shell access.
  • PasswordAuthentication yes allows password authentication for the user.
  • ChrootDirectory /var/sftp Restrict user to access directories under this directory only. Here /var/sftp is act as root directory of the user.
  • AllowAgentForwarding no Specifies whether ssh-agent forwarding is permitted. The default is yes.
  • AllowTcpForwarding no Specifies whether TCP forwarding is permitted. The default is yes.
  • X11Forwarding no Specified where the graphical application is permitted for not

Restart SSH service to apply new settings:

sudo systemctl restart ssh 

That’s it. You have successfully completed the instructions to create an SFTP only user on Debian-based systems.

Step 4 – Security Tips (Options)

Here are some basic but important security tips for SFTP accounts in a productions environment.

  1. Run SSH server on a non-standard port
  2. Disallow the password authentication and configure key based authentication
  3. Make sure the firewall is restricted for specific IP addresses only
  4. And keep the openssh package up to date
Conclusion

This tutorial describes you create SFTP only users in the Ubuntu system. It will disabled shell access for the same users to restrict to a specified directory only.

Ref From: tecadmin
Channels: SSHSFTPFTP

Related articles