In this article, we will show you how to securely transfer files between your local machine and a remote machine, using SFTP (Secure File Transfer Protocol), also known as SSH File Transfer Protocol.
The command line provides other alternative file transfer capabilities, such as SCP, which also uses the SSH (secure shell) under the hood. In the examples, we will use a sample file netflix_titles.csv.zip hosted on our local machine and transfer it securely using SFTP.
See also: (Live Webinar) Meet ServerMania: Transform Your Server Hosting Experience
Log in to the remote server using
SSH
SSH creates a secure connection between two systems. For this example, you would need a local system and a remote system instead. If you don’t have a remote system, quickly provision a remote server on Windows or Linux
and log in with your root password (username) 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-private key pair) for authentication and log in to the remote server with that. To ensure that no files exist on the remote server, check the current directory and list the contents of the directory:
[root@sm2134-216 ~]# pwd /root [root@sm2134-216 ~]# ls
If you have provisioned a new server, there will be no files in the root directory. Exit the remote machine and return 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 a file netflix_titles.csv.zip, which we will transfer to the remote machine using the SFTP command. In this example, we used the root user, but make sure your user has write permission on the remote server. Learn more about how to SSH on our blog.
Logging on to the remote server using SFTP SFTP
or Secure File Transfer Protocol
is an upgrade to traditional FTP (File Transfer Protocol). SFTP uses SSH (secure shell) to secure the connection.
Let’s log in to the remote machine using the SFTP command and start an SFTP session and run the command ? or help
: ❯ sftp root@172.105.186.216 root@172.105.186.216’s password: Connected to 172.105.186.216. sftp> ?
This will list all possible SFTP commands, but we want to see just a couple of them. Most of these commands resemble basic shell commands for navigation, creating files and directories, and so on. Let’s look at some of the examples of SFTP commands:
sftp> put – Upload file sftp> get – Download sftp file> cd path – Change remote directory to ‘path’sftp> pwd – Show remote working directory sftp> lcd path – Change local directory
- to ‘path’sftp>
- lpwd – Show local
- sftp
- > ls
- > lls – Display the contents of the local working directory
working directory
– Display the contents of the remote working directory sftp
In the following code snippet, you can see examples of some of the above-mentioned commands that are executed at the SFTP command prompt
: ░▒▓ ~/Projects/ServerMania ▓▒░─────────────────────────────────────────────────────────────────────────≡────────────────────────────────────────────────────────────────────────────────────────────��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 files> RMDIR SFTP>
Now that you know how to navigate the file system of both the local computer and the remote server, you will learn how
to transfer files from one to the other. Transfer files from local machine to
remote server
First, let’s 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 Upload 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. Note that we do not provide any path on the remote machine, so the file was copied to the current directory of the remote machine. Check if the file has been copied or not by running the command ls: sftp> pwd Remote working directory: /root sftp> ls netflix_titles.csv.zip sftp> bye Use the bye command to close the connection (SFTP session). Transfer files from the remote server to the local computer Now, let’s delete the local file from our local machine and copy it back from the remote server looking for the remote file using the
command get:
❯ pwd /Users/kovid/Projects/ServerMania ❯ ls netflix_titles.csv.zip ░▒▓ ~/Projects/ServerMania ▓▒░────────────────────────────────░▒▓ ≡ a las 21:16:22 ▓▒░ ❯ rm netflix_titles.csv.zip ░▒▓ ~/Projects/ServerMania ▓▒░────────────────────────────────────────────────────────≡─────────────────────────────────────────────────────────────────────────────────�32 ▓▒░ ❯ ls ░▒▓ ~/Projects/ServerMania ▓▒░─────────────────────────────░▒▓ ≡ at 21:16:34 ▓▒░ ❯
After deleting the file from the local system, re-establish an SFTP connection to the remote computer
: ❯ sftp password root@172.105.186.216 root@172.105.186.216’s: Connected to 172.105.186.216. sftp> get netflix_titles.csv.zip Get /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 does not 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. Getting /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 command is an easy and secure method to transfer files directly between two servers. Now you have everything that is required to use this command with ease!
Are you still looking for help running all of this on your ServerMania servers? Contact our support team or share your feedback in the comments below!
ServerMania not only supports enterprises with technical tutorials, but also through enterprise-centric solutions such as server cluster placement and configurations. For these advanced partnership opportunities, book a free consultation with our account executives today.