SFTP, also known as an “SSH File Transfer Protocol” or “Secure File Transfer Protocol,” is a protocol used for transferring large files over the internet. It is built on FTP and includes Secure Shells for sharing files securely. SSH and SFTP protocols were both designed by the Internet Engineering Task Force (IETF) to improve web security.
SFTP is very useful when you want to transfer any sensitive data over the internet. SFTP uses SSH and encrypts all commands to avoid password sniffing and exposing sensitive information in plain text.
Why is SFTP different from FTP and FTPS?
- It transmits data in binary format, so file transfer is much faster than other protocols.
- It doesn't need any dedicated data channel because it uses only one connection.
- It lists the directory in a machine-readable format.
- SFTP connection is encrypted and secure as it uses SSH protocol.
In this post, we will show you how to set up an SFTP server on Ubuntu.
Install SSH Server
SFTP uses SSH protocol to transfer files. So you will need to install the SSH server on your system. You can install it by running the following command:
apt-get install openssh-server -y
After installing the SSH server, start the SSH service and enable it to start at system reboot with the following command:
systemctl start ssh
systemctl enable ssh
Next, verify the status of the SSH service with the following command:
systemctl status ssh
If everything is fine, you should get the following output:
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-07-18 09:06:45 UTC; 3min 25s ago
Process: 646 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 647 (sshd)
Tasks: 1 (limit: 2353)
└─647 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
Jul 18 09:06:44 ubuntu2004 systemd: Starting OpenBSD Secure Shell server...
Jul 18 09:06:45 ubuntu2004 sshd: Server listening on 0.0.0.0 port 22.
Jul 18 09:06:45 ubuntu2004 sshd: Server listening on :: port 22.
Jul 18 09:06:45 ubuntu2004 systemd: Started OpenBSD Secure Shell server.
Jul 18 09:08:40 ubuntu2004 sshd: Accepted password for root from 184.108.40.206 port 64650 ssh2
Jul 18 09:08:40 ubuntu2004 sshd: pam_unix(sshd:session): session opened for user root by (uid=0)
At this point, SSH service is started and listening on port 22. You can check the SSH listening port with the following command:
ss -antpl | grep 22
You should see the following output:
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=647,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=647,fd=4))
Configure SSH Server
Next, you will need to edit the SSH main configuration file and configure it for SFTP. You can edit it with the following command:
Add the following lines at the end of the file:
Match group sftp
Save and close the file when you are finished. Then, restart the SSH service to apply the configuration changes:
systemctl restart ssh
Create SFTP User Account
Next, you will need to create a user account for SFTP access. First, create a new group named sftp with the following command:
You should see the following output:
Adding group `sftp' (GID 1000) ...
Next, create a new user named sftpuser and add it to the sftp group:
useradd -m sftpuser -g sftp
Next, set a password for sftpuser with the following command:
You will be asked to set a password as shown below:
Retype new password:
passwd: password updated successfully
Next, grant full access to the sftpuser of /home/sftpuser directory and deny access for all other users with the following command:
chmod 700 /home/sftpuser/
Connect SFTP From Command-line
You can now connect to the SFTP server from the remote system using the command-line interface.
First, log in to the remote system, open the command-line interface, and run the following command to connect to the SFTP server:
You will be asked to provide a password for sftpuser as shown below:
Provide your password and hit Enter. Once you are connected, you should get the SFTP shell as shown below:
Connected to 220.127.116.11.
Now, change the directory to sftpuser home directory with the following command:
sftp> cd sftpuser/
Next, list all files and directories with the following command:
sftp> ls -l
You should get the following output:
drwxr-xr-x 2 1000 1000 4096 Jul 18 09:23 documents
-rw-r--r-- 1 1000 1000 0 Jul 18 09:24 file1.txt
-rw-r--r-- 1 1000 1000 0 Jul 18 09:24 file2.txt
-rw-r--r-- 1 1000 1000 0 Jul 18 09:24 hitesh.jpeg
-rw-r--r-- 1 1000 1000 0 Jul 18 09:24 image.png
If you want to create any directory, run the following command:
sftp> mkdir mydir
To disconnect from the SFTP shell, run the following command:
Connect SFTP From GUI
You can also connect to your SFTP server from the Ubuntu graphical desktop environment.
First, open the File Manager from the Application menu as shown below:
Now, click on the Connect to Server. You should see the following screen:
Now, type your SFTP server address in sftp://your-server-ip format and click on the Connect button. You should see the following screen:
Provide your sftp username, password and click on the Connect button. Once you are connected to the SFTP server, you should see the following screen:
Now, click on the sftpuser directory. You should see all content of this directory on the following screen:
In the above guide, we explained how to set up an SFTP server on Ubuntu using SSH protocol. We also explained how to connect to the SFTP server from the command-line and GUI methods. You can also use any FTP client to connect to the SFTP server from any operating system and transfer files and directories securely over the internet.