Why Should You Use SFTP over FTP?

The safety of your dataโ€”and your userโ€™s dataโ€”is paramount in todayโ€™s landscape. With the amount of cyberattacks on the rise in the last few years and the increasing need for companies to collect and use data, keeping it safe should be one of your highest priorities.6

This is where the choice between FTP and SFTP becomes crucial. FTP, the older of the two, provides basic file transfer capabilities. However, it lacks encryption, exposing your data to potential interception during transfers. This is where SFTP steps in, enhancing security by integrating file transfer capabilities with SSH encryption. This added layer of security ensures that any data transferred is encrypted end-to-end, protecting sensitive information from unauthorized access.

Beyond security, SFTP simplifies server management and supports more robust authentication methods, including public key authentication. This makes it a comprehensive solution for businesses aiming to bolster their server infrastructure and strengthen their security processes.

Log into a Remote Server using SSH

SSH creates a secure connection between two systems. To initiate an SSH session, youโ€™ll need to open your command-line interfaceโ€”this could be Terminal on Linux or PowerShell on Windows. For this example, you would need a local and remote system in place. If you donโ€™t have a remote system, quickly provision a remote server on Windows or Linux and log in using your root (user name) and password for authentication:

ssh root@172.105.186.216
root@172.105.186.216's password:
Last login: Fri Feb 26 14:28:52 2021 from 180.150.39.150
[root@sm2134-216 ~]#

Alternatively, you can generate SSH keys (public and private key pair) for authentication and log into the remote server using that. To ensure that no files exist on your remote server, check your current directory and list the content of the directory:

[root@sm2134-216 ~]# pwd
/root
[root@sm2134-216 ~]# ls

This secure login process is crucial for maintaining the integrity and confidentiality of the data being accessed and transferred. SSH encrypts all data sent between the client and server, protecting sensitive information from interception or eavesdropping.

If you have provisioned a new server, there will be no files in the root directory. Exit the remote machine, hop back to your local machine, and list the files and subdirectories in the current directory:

[root@sm2134-216 ~]# exit
โ–‘โ–’โ–“ ~/Projects/ServerMania โ–“โ–’โ–‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘โ–’โ–“ took 6m 1s โ‰ก at 12:10:39 โ–“โ–’โ–‘
โฏ
netflix_titles.csv.zip

The directory contains one file netflix_titles.csv.zip, which we will transfer to the remote machine using the SFTP command. In this example, we have used the root user, but please make sure that your user has the โ€˜writeโ€™ permission on the remote server. Learn more about how to SSH on our blog.

Log into a Remote Server using SFTP

Building upon the secure foundation provided by SSH, SFTP offers an encrypted channel for transferring files, ensuring that your data remains confidential and intact during transit. To begin, ensure you have SSH access to the server youโ€™re connecting to, as SFTP uses that SSH protocol for secure file transfers.

Letโ€™s login to the remote machine using the SFTP command and start an SFTP session and run the ? or help command:

โฏ sftp root@172.105.186.216
root@172.105.186.216's password:
Connected to 172.105.186.216.
sftp> ?

This will list all the possible SFTP commands, but we want to look at only a couple of them. Most of these commands resemble basic shell commands for navigation, file and directory creation, etc. Letโ€™s look at some of the examples of SFTP commands:

  • ls: Lists the files and directories in the current remote directory.
  • cd [directory_name]: Changes the current remote directory to directory_name.
  • lcd [directory_name]: Changes the current local directory; useful for setting where files will be downloaded to or uploaded from.
  • get [filename]: Downloads filename from the remote server to the local machine.
  • put [filename]: Uploads filename from the local machine to the remote server.
  • mkdir [directory_name]: Creates a new directory on the remote server.
  • rmdir [directory_name]: Removes a directory on the remote server (the directory must be empty).
  • exit or quit: Closes the connection to the remote server and exits SFTP.

In the following code snippet, you can see the examples of some of the commands mentioned above run on the SFTP prompt:

โ–‘โ–’โ–“ ~/Projects/ServerMania
โ–“โ–’โ–‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘โ–’โ–“ โ‰ก at 20:51:54 โ–“โ–’โ–‘
โฏ sftp root@172.105.186.216
root@172.105.186.216's password:
Connected to 172.105.186.216.
sftp> pwd
Remote working directory: /root
sftp> ls
sftp> lpwd
Local working directory: /Users/kovid/Projects/ServerMania
sftp>
sftp> mkdir files
sftp> cd files
sftp> pwd
Remote working directory: /root/files
sftp> cd ..
sftp> rmdir files
sftp>

Now that you know how to navigate the file system of both the local machine and the remote server, you will learn how to transfer files from one to another.

Transfer Files from a Local Machine to a Remote Server

Transferring files from your local machine (such as your dedicated server) to a remote server is a common task that can be securely accomplished using SFTP commands. First, let us see how a file can be transferred from a local machine to a remote machine using the secure file transfer protocol. Log in to the server to access the SFTP prompt and navigate to the local directory, which has the file to be transferred:

Connected to 172.105.186.216.
sftp> lpwd
Local working directory: /Users/kovid
sftp> lcd /Users/kovid/Projects/ServerMania
sftp> lpwd
Local working directory: /Users/kovid/Projects/ServerMania
sftp> lls
netflix_titles.csv.zip
sftp> put netflix_titles.csv.zip
Uploading netflix_titles.csv.zip to root/netflix_titles.csv.zipnetflix_titles.csv.zip 100% 1207KB 1.5MB/s 00:00
sftp>

As you can see, using the put command, we have successfully transferred the file netflix_titles.csv.zip from our local machine to the remote machine. Notice that we did not provide any path on the remote machine, so the file got copied into the remote machineโ€™s current directory. Verify if the file has been copied or not by running the ls command:

sftp> pwd
Remote working directory: /root
sftp> ls
netflix_titles.csv.zip
sftp> bye

The bye command closes our SFTP connection, letting us safely disconnect from our server once weโ€™ve finished our secure file transfer.

Transfer Files from a Remote Server to a Local Machine

Now, let us delete the local file from our local machine and copy it back from the remote server fetching the remote file using the get command:

โฏ pwd
/Users/kovid/Projects/ServerMania
โฏ ls
netflix_titles.csv.zip
โ–‘โ–’โ–“ ~/Projects/ServerMania โ–“โ–’โ–‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘โ–’โ–“ โ‰ก at 21:16:22 โ–“โ–’โ–‘
โฏ rm netflix_titles.csv.zip
โ–‘โ–’โ–“ ~/Projects/ServerMania โ–“โ–’โ–‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘โ–’โ–“ โ‰ก at 21:16:32 โ–“โ–’โ–‘
โฏ ls
โ–‘โ–’โ–“ ~/Projects/ServerMania โ–“โ–’โ–‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘โ–’โ–“ โ‰ก at 21:16:34 โ–“โ–’โ–‘
โฏ

After removing the file from the local system, establish an SFTP connection with the remote machine again:

โฏ sftp root@172.105.186.216
root@172.105.186.216's password:
Connected to 172.105.186.216.
sftp> get netflix_titles.csv.zip
Fetching /root/netflix_titles.csv.zip to netflix_titles.csv.zip/root/netflix_titles.csv.zip 100% 1207KB 4.8MB/s 00:00
sftp>
sftp> lpwd
Local working directory: /Users/kovid/Projects/ServerMania
sftp> lls
netflix_titles.csv.zip
sftp> bye

Alternatively, you can also copy the remote file to your local system using the SFTP command without establishing a persistent connection from your local command line. This doesnโ€™t have to use the get command. Look at the following command:

โ–‘โ–’โ–“ ~/Projects/ServerMania โ–“โ–’โ–‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘โ–’โ–“ โ‰ก at 21:25:12 โ–“โ–’โ–‘
โฏ sftp root@172.105.186.216:netflix_titles.csv.zip.
root@172.105.186.216's password:
Connected to 172.105.186.216.
Fetching /root/netflix_titles.csv.zip to ./netflix_titles.csv.zip/root/netflix_titles.csv.zip 100% 1207KB 4.8MB/s 00:00
โ–‘โ–’โ–“ ~/Projects/ServerMania โ–“โ–’โ–‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–‘โ–’โ–“ took 4s โ‰ก at 21:25:22 โ–“โ–’โ–‘
โฏ

Next Steps

The SFTP protocol, essential for both Windows and Linux users, encrypts files in transit, ensuring that sensitive information remains out of reach from unauthorized access. Weโ€™ve covered the fundamental commands necessary for secure file transfer, shown you how to transfer files to and from local and remote servers. In an era where data breaches are costly, integrating SFTP into your server infrastructure is not just an optionโ€”itโ€™s a necessity for maintaining trust and integrity in your digital transactions.

Still need help getting all this running on your ServerMania servers? Contact our support team.

ServerMania not only supports businesses with technical tutorials, but also through enterprise focused solutions such as colocation and server cluster configurations. For these advanced partnership opportunities, please book a free consultation with one of our account executives today.