How to: Use SFTP to Securely Transfer Files on Linux
SFTP (Secure File Transfer Protocol or SSH File Transfer Protocol) is a is a secure way to transfer files to and from your VPS. Unlike FTP, which sends usernames and passwords in plain text, SFTP secures the data in transit. SFTP utilizes the SSH protocol. If you have SSH access to your VPS, you can use SFTP to transfer files. There are many SFTP clients with intuitive graphical interfaces, but in this guide we will be covering how to transfer files using a text based command-line.
For this guide we'll be using an Ubuntu VPS.
Installing an SSH server on an Ubuntu VPS
SSH is the main way to connect to SkySilk VPS, so we are ready to utilize SFTP from the start. If you do not have an SSH server already you can install one with
apt-get install openssh-server
Note: It is strongly advisable to harden your SSH security when implementing an SFTP server. There are a number of ways to harden your SSH settings for enhanced security, however they are out of the scope of this guide. A link to a guide on hardening SSH is provided below.
Connecting to an SFTP Server
Connecting to the SFTP server is very similar to connecting with SSH. In Unix based operating systems you can connect via the terminal using the following command
For command-line access using Windows you can utilize the PuTTY SFTP client PSFTP. If you installed PuTTY you can locate it by hitting the windows key and typing “PSFTP” in the search field.
You can access your server with PSFTP using the following command
You will be prompted to choose the user and enter a password.
Once you are connected to your VPS using SFTP you can begin transferring files.
To see a list of available commands and a short description of each you can enter
Navigating the File System
Navigating the file system is very similar to getting around in a Linux terminal.
Begin by using pwd to identify which directory you are currently working with.
Navigate to the desired directory you wish to transfer files to or from using cd
List directory contents using ls
Note: You can run the SFTP commands locally by prepending them with “l”
Examples of the previous commands but prepending "l" to operate them on the local host
lpwd lcd <Local_Directory_Path> lls
Now that we are in the correct directories on the local and remote hosts we can begin transferring files.
To copy a file from a remote directory to the local directory you can use
To copy a file from the local directory to the remote directory you can use
Closing the SFTP Session
You can close the SFTP session by entering "bye" or "exit"
Now that you have learned the basics of navigating directories and copying files between remote and local hosts we will dive a little deeper into more advanced commands and their options.
Changing File Names
You can change the filename with
get <Remote_Filename> <New_Filename> put <Local_Filename> <New_Filename>
You can create a directory with
To recursively copy all the files in a directory and its subdirectories
get <Remote_Directory_Name> -r put <Local_Directory_Name> -r
Preserving File Permissions
To preserve file permissions between transfers
get <Remote_Filename> -p put <Local_Filename> -p
Now you know how to securely transfer files using SFTP in the command-line!