Wednesday, March 21, 2018

Setting up SFTP server on Ubuntu

Overview


In this example we are using Ubuntu application running on Windows 10, but the instructions can be used to set up an SFTP server on any Ubuntu system.

Instructions


On Windows 10 get Ubuntu from the Microsoft Store



After installing Ubuntu go to the terminal:



1. Make a copy of the original config file

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults

sudo chmod a-w /etc/ssh/sshd_config.factory-defaults

2. Add the 'sftponly' group


sudo groupadd sftponly

3. Edit the /etc/ssh/sshd_config -file


sudo nano /etc/ssh/sshd_config

In the /etc/ssh/sshd_config -file find a row "PasswordAuthentication" and set it to 'Yes'



Comment out the existing Subsystem sftp command.

To the end of the /etc/ssh/sshd_config add the rows:

Subsystem sftp internal-sftp
Match Group sftponly
ChrootDirectory %h
ForceCommand internal-sftp
AllowTcpForwarding no

Save the changes: CTRL + O + [ENTER]
Exit nano: CTRL + X



4. Create the sftptest -user, put it to the sftponly -group, create directories and set the rights:


Do not use users already existing in the system.
Use the command cut -d : -f 1 /etc/passwd  to get a list of existing users.




sudo mkdir /home/sftptest
sudo useradd -d /home/sftptest -M -N -g users sftptest
sudo passwd sftptest

[enter password] abcABC123!!!

sudo chown root:root /home/sftptest
cd /home/sftptest
sudo mkdir files
sudo chown sftptest:users /home/sftptest/files
sudo usermod sftptest -g sftponly
sudo usermod sftptest -s /bin/false

5. Restart the ssh service


sudo service ssh restart

In the end this is what you should see:



6. Get the IP address for the Ubuntu instance


Enter the command: ifconfig

In this case the IP address we want is the one for eth1



Now you can test connection using telnet:



If the connection works you should see something like this:



Use WinSCP or such to connet to the sftp server:



You should now see a 'files' -directory that you can use to store data.



When you close the Ubuntu application and start it again you must restart the ssh service with the command "sudo service ssh restart".




1 comment: