Bookmark this page

Transfer Files Between Systems Securely

Objectives

  • Transfer files to or from a remote system securely with SSH.

Transfer Remote Files with the Secure File Transfer Program

The OpenSSH suite securely runs shell commands on remote systems. Use the Secure File Transfer Program (SFTP) to interactively upload to or download files from an SSH server. This program is part of the OpenSSH suite. A session with the sftp command uses the secure authentication mechanism and encrypted data transfer to and from the SSH server.

Specify a remote location for the source or destination of the files to copy. For the format of the remote location, use [user@]host:/path. The user@ part of the argument is optional. If this part is missing, then the sftp command uses your current local username. When you run the sftp command, your terminal provides an sftp> prompt.

[user@host ~]$ sftp remoteuser@remotehost
remoteuser@remotehost's password: password
Connected to remotehost.
sftp>

The interactive sftp session accepts various commands that work the same way in the remote file system as in the local file system, such as the ls, cd, mkdir, rmdir, and pwd commands. 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.

List the available sftp commands by using the help command in the sftp session:

sftp> help
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp [-h] grp path                Change group of file 'path' to 'grp'
chmod [-h] mode path               Change permissions of file 'path' to 'mode'
chown [-h] own path                Change owner of file 'path' to 'own'
...output omitted...

In an sftp session, you might run some commands on your local host. For most available commands, add the l character before the command. For example, the pwd command prints the current working directory on the remote host. To print the current working directory on your local host, use the lpwd command.

sftp> pwd
Remote working directory: /home/remoteuser
sftp> lpwd
Local working directory: /home/user

The next example uploads the /etc/hosts file on the local system to the newly created /home/remoteuser/hostbackup directory on the remotehost machine. The sftp session expects that the put command is followed by a local file in the connecting user's home directory, in this case the /home/remoteuser directory:

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 copy a whole directory tree recursively, use the sftp command -r option. The following example recursively copies the /home/user/directory local directory to the remotehost machine.

sftp> put -r directory
Uploading directory/ to /home/remoteuser/directory
Entering directory/
file1                                      100%    0     0.0KB/s   00:00
file2                                      100%    0     0.0KB/s   00:00
sftp> ls -l
drwxr-xr-x    2 student  student        32 Mar 21 07:51 directory

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

sftp> get /etc/yum.conf
Fetching /etc/yum.conf to yum.conf
/etc/yum.conf                              100%  813     0.8KB/s   00:00
sftp> exit
[user@host ~]$

To get a remote file with the sftp command on a single command line, without opening an interactive session, use the following syntax. You cannot use single command-line syntax to put files on a remote host.

[user@host ~]$ sftp remoteuser@remotehost:/home/remoteuser/remotefile
Connected to remotehost.
Fetching /home/remoteuser/remotefile to remotefile
remotefile                                                       100%    7    15.7KB/s   00:00

Transfer Files with Secure Copy Protocol

Warning

In versions prior to RHEL 9, the scp command was based on a historical rcp protocol that was not designed with security considerations. The scp protocol has a known code injection issue such that an attacker could execute arbitrary commands on the remote server. For this reason, the scp protocol is not covered in this course.

Although some vulnerabilities with the scp protocol were fixed in recent years, not all can be fixed while maintaining backward compatibility. For this reason, Red Hat recommends no longer using the legacy scp protocol in new applications or scripts.

You can find more information about this issue in https://access.redhat.com/security/cve/cve-2020-15778.

The scp Secure Copy command, which is also part of the OpenSSH suite, copies files from a remote system to the local system, or from the local system to a remote system. Since RHEL 9, the scp command uses the sftp protocol to transfer files.

You can specify a remote location for the source or destination of the files that you are copying. As with the sftp command, the scp command uses [user@]host to identify the target system and username. If you do not specify a user, then the command attempts to log in with your local username as the remote username.

To revert to the legacy scp protocol, you can include the -O flag. Due to the code injection vulnerability with the scp protocol, Red Hat recommends that you do not use the -O flag when you use the scp command.

References

sftp(1) man pages

Revision: rh134-9.3-5fd2368