How to transfer files in Linux using scp and sftp

Transfering files using Secure Copy (scp)

OpenSSH is useful for securely running shell commands on remote systems. The Secure Copy command, scp, which is part of the OpenSSH suite, copies files from a remote system to the local system or from the local system to a remote system. The command uses the SSH server for authentication and encrypts data when it is being transferred.

You can specify a remote location for the source or destination of the files you are copying. The format of the remote location should be in the form [[email protected]]host:/path. The [email protected] portion of the argument is optional. If it is missing, your current local username will be used. When you run the command, your scp client will authenticate to the remote SSH server just like ssh, using key-based authentication or prompting you for your password.

The following example demonstrates how to copy the local /etc/yum.conf and /etc/hosts files on host, to the remoteuser’s home directory on the remotehost remote system:

[[email protected] ~]$ scp /etc/yum.conf /etc/hosts [email protected]:/home/remoteuser
[email protected]'s password: password
yum.conf                                   100%  813     0.8KB/s   00:00
hosts                                      100%  227     0.2KB/s   00:00

You can also copy a file in the other direction, from a remote system to the local file system. In this example, the file /etc/hostname on remotehost is copyed to the local directory /home/user. The scp command authenticates to remotehost as the user remoteuser.

[[email protected] ~]$ scp [email protected]:/etc/hostname /home/user
[email protected]'s password: password
hostname                                   100%   22     0.0KB/s   00:00

To copy a whole directory tree recursively, use the -r option. In the following example, the remote directory /var/log on remotehost is copied recursively to the local directory /tmp/ on host. You must connect to the remote system as root to make sure you can read all files in the remote /var/log directory.

[[email protected] ~]$ scp -r [email protected]:/var/log /tmp
[email protected]'s password: password
...output omitted...

Transfering files Secure File Transfer Program (sftp)

To interactively upload or download files from a SSH server, use the Secure File Transfer Program, sftp. A session with the sftp command uses the secure authentication mechanism and encrypted data transfer to and from the SSH server.

Just like the scp command, the sftp command uses [[email protected]]host to identify the target system and user name. If you do not specify a user, the command will attempt to log in using your local user name as the remote user name. You will then be presented with an sftp> prompt.

[[email protected] ~]$ sftp [email protected]
[email protected]'s password: password
Connected to remotehost.

The interactive sftp session accepts various commands that work the same way on the remote file system as they do in the local file system, such as ls, cd, mkdir, rmdir, and pwd. The put command uploads a file to the remote system. The get command downloads a file from the remote system. The exit command exits the sftp session.

To upload the /etc/hosts file on the local system to the newly created directory /home/ remoteuser/hostbackup on remotehost. The sftp session always assumes that the put command is followed by a file on the local file system and starts in the connecting user’s home directory; in this case, /home/remoteuser:

sftp> mkdir hostbackup
sftp> cd hostbackup
sftp> put /etc/hosts
Uploading /etc/hosts to /home/remoteuser/hostbackup/hosts
/etc/hosts                                 100%  227     0.2KB/s   00:00

To download /etc/yum.conf from the remote host to the current directory on the local system, execute the command get /etc/yum.conf and exit the sftp session with the exit command.

sftp> get /etc/yum.conf
Fetching /etc/yum.conf to yum.conf
/etc/yum.conf                              100%  813     0.8KB/s   00:00

sftp> exit
[[email protected] ~]$