SFTP is also known as Secure File Transfer Protocol that offers you a secured file transfer facility based on the FTP (File Transfer Protocol). Earlier, all the transfers were done via unencrypted and insecure FTP. That is why we need a secure way to transfer all the files and develop the SFTP process.
SFTP works on the SSH protocol using the TCP port number 22, offering similar security and encryption functionalities as SSH. There is always a default SSH daemon that runs as a part of the OpenSSH server on the Linux systems, supporting SFTP protocol features.
With SFTP, you can benefit from a secure and encrypted connection for transferring the files from the local system to the remote system and vice versa. Most companies prefer SFTP because of the obvious security advantages SFTP has over FTP. In this article, we will go through the working of SFTP, its advantages, and various SFTP commands for Linux systems.
Working of SFTP
Well, there is no doubt that SFTP (SSH File Transfer Protocol or Secure File Transfer Protocol) ensures more security while securing the server to server file transfers between your clients and you. Before moving forward, you should understand the working of SFTP.
SFTP uses the SSH (Secure Shell) data stream for establishing a secure connection and ensuring a high level of protection during file transfer. SFTP uses various encryption algorithms that allow you to move your data to the server securely and keep the file encrypted during the transfer. It also provides authentication to avoid unauthorized access to the files during transfer and protect them from hackers.
SFTP does not ensure two-factor authentication; thus, for creating a more secure connection, you will require both a username and password along with SSH keys. By implementing SSH keys will keep away the hackers from being connected to the SFTP server. For this, you need to generate the SSH keys beforehand.
SFTP is implemented on a client-server architecture. The server is used to store data and is located at multiple places that the clients can access by sending requests. The client’s request traversed through the network and reached the server. Then the server responds to the user’s server by sending the required file.
Throughout the process, SFTP makes sure that your data is encrypted, and SSH keys will transfer the public key for providing the access.
Features of SFTP
Below are some features of using SFTP.
- It offers secure and encrypted file transfer.
- It provides complete support for TMUX and screen sessions.
- It is capable of implementing the SSH3, IPv6 HTTP protocols.
- You will be able to run various commands using the SSH terminal.
- It comes with a password and public-key authentication.
- It allows you to handle the server key re-exchanges functionality.
Advantages of SFTP
Below are some advantages that you can leverage using SFTP.
- You can easily install the software and use it without the requirement of root privileges.
- SFTP protocol works on the secure channel, which means the data will be transferred in an encrypted way keeping the hackers away.
- It ensures fast and efficient transfer of even large files. You can even transfer multiple files simultaneously.
- SFTP enhances productivity by allowing the users to work with server and server-to-server facilities.
Disadvantages of SFTP
Below are some disadvantages that you may face using SFTP-
- The communication via SFTP is binary, and you cannot log the transfer.
- It may be challenging for managing and validating the SSH keys.
- SFTP has some standards, providing you with some optional and recommended fields. It may create compatibility problems if you use different software from different vendors.
SFTP Commands
There are various scenarios where we can use SFTP commands.
Copying files
You can use SFTP as an alternative to SCP (secure copy) command. Earlier, the SCP command was used for pushing and pulling files from a remote server in a simple step. First we will look at how SCP works and then moves to the SFTP command for copying files.
SCP command for uploading the file.
$ scp {local-path} {user}@{remote-host}:{remote-path}
SCP command for downloading the file.
$ scp {user}@{remote-host}:{remote-file-path} {local-path}
The alternative SFTP command for uploading the file to a remote server is given below.
$ sftp {user}@{host}:{remote-path} <<< $'put {local-path}'
You can consider the below example:
The alternative SFTP command for downloading the file from a remote server is given below:
$ sftp {user}@{remote-host}:{remote-file-name} {local-file-name}
You can consider the below example:
Connecting to the SFTP server
For establishing a connection to the remote server, you can use the sftp command followed by the user name and the remote’s hostname or IP address. By default, TCP port 22 will be opened for this, or you need to mention the port using the -oPort flag explicitly.
If you are connecting to any server for the first time, you will be asked to confirm the SSH for the server. After you confirm, you will be asked for the password. Only then you can enter the sftp prompt as shown below.
Checking Version
Using the SFTP command, you can check the SFTP version as shown below.
Showing working directory
You can navigate to the current working directory of the remote server by using the pwd command on the SFTP prompt, as shown below.
- lpwd – display the current directory on your local system
- pwd – display the current directory on the FTP server
Listing files
To list all the files available in a remote working directory, you can use the ‘ls’ command using the sftp prompt, as shown below.
For Remote Working Directory
sftp> ls
Output
sftp> ls rem_file1 rem_file2 rem_file3 remote_test_dir sftp>
For Local Working Directory
sftp> lls
Output
sftp> lls bin file1 file2 file3 lib sys_info.sh test.tgz testdir sftp>
Switching directories
You can use the cd command for switching the remote working directory, as shown below.
If you want to switch the local working directory, you can use the ‘lcd’ command on the SFTP prompt, as shown below.
Uploading files
If you want to upload a single file to the remote server, you can use the ‘put’ command. You can see the working using the below example-
Uploading Single File
sftp> pwd
Remote working directory: /home/lo_user
sftp> lpwd Local working directory: /home/user sftp> ls rem_file1 rem_file2 rem_file3 remote_test_dir sftp> lls bin file1 file2 file3 lib sys_info.sh test.tgz testdir sftp> put file1 Uploading file1 to /home/lo_user/file1 file1 100% 6 6.0KB/s 00:00 sftp> ls file1 rem_file1 rem_file2 rem_file3 remote_test_dir sftp>
Uploading Multiple Files
sftp> pwd Remote working directory: /home/lo_user sftp> lpwd Local working directory: /home/user sftp> ls file1 rem_file1 rem_file2 rem_file3 remote_test_dir sftp> lls bin file1 file2 file3 lib sys_info.sh test.tgz testdir sftp> mput file[23] Uploading file2 to /home/lo_user/file2 file2 100% 6 6.5KB/s 00:00 Uploading file3 to /home/lo_user/file3 file3 100% 6 5.3KB/s 00:00 sftp> ls file1 file2 file3 rem_file1 rem_file2 rem_file3 remote_test_dir sftp>
Downloading files
You can download as single or multiple files from the remote server to the local server.
Downloading a Single File
sftp> pwd Remote working directory: /home/lo_user/remote_test_dir sftp> lpwd Local working directory: /home/user/testdir sftp> ls Remote_file4 sftp> lls File4 sftp> get remote_file4 Fetching /home/lo_user/remote_test_dir/remote_file4 to remote_file4 /home/lo_user/remote_test_dir/remote_file4 100% 13 5.2KB/s 00:00 sftp> lls file4 remote_file4 sftp>
Downloading Multiple Files
sftp> pwd Remote working directory: /home/lo_user sftp> lpwd Local working directory: /home/user/testdir sftp> ls rem_file1 rem_file2 rem_file3 remote_test_dir sftp> lls file4 remote_file4 sftp> mget remote_file* Fetching /home/lo_user/rem_file1 to rem_file1 /home/lo_user/rem_file1 100% 12 5.9KB/s 00:00 Fetching /home/lo_user/rem_file2 to rem_file2 /home/lo_user/rem_file2 100% 13 5.8KB/s 00:00 Fetching /home/lo_user/rem_file3 to rem_file3 /home/lo_user/rem_file3 100% 13 7.3KB/s 00:00 sftp> lls file4 rem_file1 rem_file2 rem_file3 remote_file4 sftp>
Creating directory
You can use the ‘mkdir’ command for creating a new directory on a remote server and local server.
Creating Directory on the Remote Server
sftp> pwd Remote working directory: /home/lo_user sftp> ls file1 file2 file rem_file1 rem_file2 rem_file3 remote_test_dir sftp> mkdir new_dir sftp> ls file1 file2 file3 new_dir rem_file1 rem_file2 rem_file3 remote_test_dir sftp>
Creating Directory on the Local Current Working Directory
sftp> lpwd Local working directory: /home/user sftp> lls bin file1 file2 file3 lib sys_info.sh test.tgz testdir sftp> lmkdir new_local_dir sftp> lls bin file1 file2 file3 lib new_local_dir sys_info.sh test.tgz testdir sftp>
Removing directory
You can remove any empty directory using the ‘rmdir’ command. If the directory is not empty, you may experience an error.
sftp> pwd Remote working directory: /home/lo_user sftp> ls file1 file2 file3 new_dir rem_file1 rem_file2 rem_file3 remote_test_dir sftp> rmdir new_dir sftp> ls file1 file2 file3 rem_file1 rem_file2 rem_file3 remote_test_dir sftp>
Conclusion
SFTP is one of the commonly used options for securely transferring files from one server to another. It comes with CLI and GUI features that can be easily supported across various platforms. We have mentioned various commands that are frequently used by any person working with servers and transferring files. You can go through the commands. These are easy to remember and implement. If you want to start practicing the SFTP commands, these are the basic ones to start with.