Unix / Linux: sftp File From One Server To Another

How do I security copy file from one server to another under Unix like operating system using secret file transfer protocol (sftp)?

sftp is an interactive file transfer program, similar to ftp, which performs all operations over an encrypted ssh transport. The syntax is as follows to copy file from one server to another.

Step #1: Starting sftp

The command for starting sftp session is as follows:

sftp username@server1


sftp username@

The first time you connect to server, sftp command will report that “The authenticity of the host can’t be established.”. This is normal and expected behavior. This means that sftp doesn’t have server in its database of known hosts. Answer yes at the prompt to connect to the server. Next, it will ask for your account password. Enter your password, and sftp will log in and present you with the sftp prompt as follows:


Step #2: sftp commands

sftp commands are similar to the Unix / Linux shell commands that you use for navigating files. You will be working with two servers or computers. So sftp will give you local and remote version of each command. Usually, all local commands are prefixed by an “l”.

Common sftp commands

Sftp Command Description
cd dir Change directory on the ftp server to dir.
lcd dir Change directory on your machine to dir.
ls List files in the current directory on the ftp server.
lls List files in the current directory on your machine.
pwd Print the current directory on the ftp server.
lpwd Print the current directory on your machine.
get file Download the file from the ftp server to current directory.
put file Upload the file from your machine to the ftp server.
exit Exit from the sftp program.

How do I get (download) file?

The get command in sftp allows you to download files from the sftp server. The syntax is:

get filename

To download a file called data.tar.gz, enter:

sftp> get data.tar.gz

Sample outputs:

Fetching /home/vivek/data.tar.gz to data.tar.gz
/home/vivek/data.tar.gz                                    100% 1960     1.9KB/s   00:01 

How do I put (upload) file?

The syntax is as follows to upload a file called file from local machine to the ftp server:

put file

In this example, upload a file called Screenshot.png, enter:

sftp> put Screenshot.png

Sample outputs:

Uploading Screenshot.png to /home/vivek/Screenshot.png
Screenshot.png                                                    100%   12KB  12.2KB/s   00:00 

How do I sftp from one server A to another Unix based server B?

First login to server A using the ssh command:
$ ssh user@serverA
Next, sftp to serverB:
$ sftp user@serverB
Finally, upload or download file to serverB:

sftp> put foo
sftp> ls
sftp> get bar
sftp> lls
sftp> exit

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • pranav Apr 19, 2013 @ 12:15

    how to copy file one mac to another in exam

  • Brandi Jun 10, 2013 @ 21:25

    How can I copy a whole directory or folder over the servers?

  • TusharK Dec 19, 2013 @ 6:20

    How can one put files from Server A on AIX platform to server B on RHEL using a shell script

  • TusharK Dec 19, 2013 @ 6:37

    hello NIX,

    following is done in the shell script

    sftp user_name@server_name

    But it is not accepting the password and shell is stopped and ask for Password.
    could you please suggest how can this be done


  • tanya Feb 6, 2014 @ 6:25

    ok then how to stop sftp mode? :D

  • Jesse Parker Feb 7, 2014 @ 18:42

    Try this
    sftp user_name:password@server_name

  • Arjun Jul 22, 2020 @ 12:03

    How to write shell script these steps

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz