SFTP, or SSH File Transfer Protocol is a secure protocol to transfer and access files over a encrypted secure connection. This tutorial explains how to use sftp command in Linux.
As you know FTP is a popular method of transferring files but it’s insecure protocol that should only be used in limited cases or on networks you trust secure. SFTP provides all the functionality of FTP, and it is easier to setup.
You must have write permission on remote system to transfer files using SFTP.
Make an SFTP connection
By default, SFTP uses the SSH protocol to authenticate and establish a secure connection. Because of this, the same authentication methods are available that are present in SSH.
Although passwords authentication method is easy to use and set up by default, if you need to connect your server often we recommend you create SSH keys and set up a passwordless SFTP login.
To establish an SSH connection with remote system and open up and SFTP session, run the
sftp command followed by the remote server username and the IP address or domain name:
Your prompt will change to an
sftp prompt once you are connected to the remote server.
Connected to remote_username@server_ip_or_hostname. sftp>
If you have different SSH port then default
22, use the
-oPort option to specify the alternate port:
sftp -oPort=custom_port remote_username@server_ip_or_hostname
SFTP commands are very similar to the Linux shell prompt commands. To get a list of all available SFTP commands type,
Available commands: bye Quit sftp cd path Change remote directory to 'path' ... ... version Show SFTP version !command Execute 'command' in local shell ! Escape to local shell ? Synonym for help
Navigating with SFTP
By default, once you are logged in to the remote server, your current working directory is the remote user home directory. You can check that by typing:
Remote working directory: /home/remote_username
You can get list of the files and directories of current working directory using ls command:
To go to another directory, use the
cd command. For example, to navigate to the
/var directory type:
sftp shell also provides commands for local navigation, information and file management. The local commands are prefixed with the letter
For example, to get the local working directory, type:
Local working directory: /home/local_username
To list the contents of the current directory on the local machine:
Navigate to another directory use lcd command:
Transferring Files with SFTP
As we discussed above usign SFTP you can transfer files securely between two systems.
Downloading Files with the SFTP Command
If we would like download files from our remote host, we can do so by issuing the following command:
To download the files from remote server using
sftp, run following command:
Fetching /home/remote_username/filename to filename /home/remote_username/filename 100% 5MB 1.2MB/s 00:05
If you want to save the downloaded file with a different name, specify the new name as the second argument:
You can save the downloaded file with a different name by specify new name as the second argument:
get filename new_filename
To download a directory and contents of it from the remote system, use the recursive
get -r remote_directory
If a file transfer fails or is interrupted, you can resume it using the
Uploading Files with the SFTP Command
To upload or transfer local files to remote host, use
It should show output something like this:
Uploading filename to /home/remote_username/filename filename 100% 12MB 1.7MB/s 00:06
To upload a whole local directory and it’s contents, you would type:
put -r locale_directory
The same flags that work with
get apply to
To resume an interrupted upload:
File Manipulations with SFTP
SFTP allows you to perform some basic file manipulation commands. Following are some examples to use the SFTP shell:
To Get information about disk usage of the remote system:
Size Used Avail (root) %Capacity 20616252 1548776 18002580 19067476 7%
Create a new directory on the remote server:
Rename a file on the remote server:
rename file_name new_file_name
Delete a file on the remote server:
Delete a directory on the remote server:
Change the permissions of a file on the remote system:
chmod 644 file_name
Change the owner of a file on the remote system:
chown user_id file_name
You must supply the user ID to the
Change the group owner of a remote file with:
chgrp group_id file_name
Close the connection by typing
In this tutorial we explained how to use sftp command to download and upload files to your remote SFTP server.
Do not hesitate to leave a comment if you have any question or feedback.