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.