In this article, you will learn how to use SFTP for Secure Transfer Files via the command-line interface.
The SSH File Transfer Protocol is a network protocol that provides file access, file transfer, and file management over any reliable data stream.
First of all, make sure that you are able to connect your server through an SSH connection.
You can verify the SSH connection by running the following command:
ssh username@Server’s_IP -p port
e.g: ssh email@example.com -p 51821
Set UP SSH Keys
We recommend generating SSH keys and transferring your public key to the system you wish to access. If you wish to use SFTP by authenticating using passwords you may skip this section.
Generate SSH Keys
You can set up an RSA token by running the following command:
ssh-keygen -t rsa
Then, you will be asked a couple of questions such as the path file of your SSH key.
We recommend using the default settings by pressing Enter to all of the above steps.
However, if you wish for more enhanced security you may set a passphrase and every time you try to log in to a remote server, you will be asked for a password
Once the process is completed there will be two keys saved in the relative directory, one Private(id_rsa) and one Public(id_rsa.pub).
Copy SSH Keys to Remote Server
You can copy the public key file by running the following command:
ssh-copy-id -p port username@Server’s_IP
Use the username & IP address of the remote server you wish to copy the key.
Type yes in the command prompt and press ENter in order to continue.
Type the remote’s server’s password and press Enter.
Now you can log in to the remote server by running the following command:
ssh -p port username@Server’s_IP
Connect through SFTP
Now, you may establish an SFTP connection by running the following command:
sftp -oPort=port username@Server’s_
To view a list of the available commands, type help command.
Navigate through SFTP
First of all, we need to check which directory we are currently connected to by running the following command:
For example, if wish to navigate to /testdir/ directory we will run the below command:
In order to view the content of your selected folder you may run the following command:
Transfer Files through SFTP
If you wish to download files from the remote host that you are currently connected to, type the following command:
The transfer of the file is now completed. Note that the file will be transferred with the same name.
In case you wish to transfer a file and simultaneously change the file name you must run the following command:
get filename newfilename
For downloading multiple files:
For downloading a whole directory with its contents using the following command:
get -r directoryname
You can also download a whole directory with its contents and maintain the relative permissions and access time by running the command:
get -Pr directoryname
If you wish to upload files from the local system to the remote host that you are currently connected to, type the following command:
Note: If you encounter the error Permission Denied you should also check the file’s permissions that you are trying to upload.
Change file’s permission:
chmod 755 filename
Change directory’s permission:
chmod 644 directoryname
For transferring a whole directory with its contents using the following command:
put -r directoryname
If you wish to temporarily go back to your local system you can type: !
For example, this command is useful if we need to check the available disk capacity of our local system by using the following command:
Then type exit in order to return to your previously SFTP connection:
Lastly, if you wish to exit the SFTP session type the bye command.
Connect through FTP client
For this section, we will use the FileZilla client.
Step1: Navigate to the File Tab and select Site Manager
Step2: Select the New site option in order to create a new SFTP connection
Step3: Fill out your connection details as shown in the example below
Step 4: Press
Step 5: Press
OK regarding the Unknown host key notification
Note: You may view also the status logs that will appear for possible errors.
Congratulations! You have successfully connected to your server via SFTP through Filezilla!