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@remotehostremoteuser@remotehost's password:passwordConnected 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>pwdRemote working directory: /home/remoteuser sftp>lpwdLocal 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 hostbackupsftp>cd hostbackupsftp>put /etc/hostsUploading /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 directoryUploading 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 -ldrwxr-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.confFetching /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:00The scp command, which system administrators widely use to copy files to and from remote systems, is based on a historical rcp protocol that was not designed with security considerations.
The scp command has a known code injection issue such that an attacker could execute arbitrary commands on the remote server.
For this reason, scp is not covered in this course.
Although some vulnerabilities were fixed in recent years, not all can be fixed while maintaining backward compatibility.
For this reason, Red Hat recommends no longer using the scp command in new applications or scripts, and instead using other utilities such as the sftp or rsync commands to copy files to or from a remote host.
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.
The command uses the SSH server to authenticate and encrypt data during transfer.
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.
When you run the command, your scp client authenticates to the remote SSH server as with the ssh command, by using key-based authentication or by prompting you for your password.