Besides providing a secure remote shell, the ssh service also provides the scp and sftp as secure ways to transfer files from and to a remote system running the SSH server.
After completing this section, students should be able to copy files securely to or from a remote system running sshd.
The ssh command is useful for securely running shell commands on remote systems. It can also be used to securely copy files from one machine to another. The scp command transfers files from a remote host to the local system or from the local system to a remote host. It utilizes the SSH server for authentication and encrypted data transfer.
Remote file system locations are always specified in the format [user@]host:/path for either the source or target location of the files to be transferred. The user@ portion is optional and, if it is missing, the current local user that invokes the scp command is used. Before the transfer is initiated, the user must authenticate with the SSH server by password or SSH keys.
This example shows how to copy the local files on desktopX, /etc/yum.conf and /etc/hosts, securely to the account student on the remote system serverX into the directory /home/student/:
[student@desktopX ~]$scp /etc/yum.conf /etc/hosts serverX:/home/studentstudent@serverX's password:studentyum.conf 100% 813 0.8KB/s 00:00 hosts 100% 227 0.2KB/s 00:00
A user can copy a file from a remote account on a remote machine to the local file system with scp. In this example, copy the file /etc/hostname from the account student on the serverX machine to the local directory /home/student/.
[student@desktopX ~]$scp serverX:/etc/hostname /home/student/student@serverX's password:studenthostname 100% 22 0.0KB/s 00:00
To copy a whole directory tree recursively, the -r option is available. In the following example, the remote directory /var/log on serverX is copied recursively to the local directory /tmp/ on desktopX. To be able to read all files that have been copied to the /tmp directory, the user must connect to the remote location as root.
[student@desktopX ~]$scp -r root@serverX:/var/log /tmproot@serverX's password:redhat...
If an interactive tool is preferred when uploading or downloading files to a SSH server, the sftp command can be used. A session with sftp is similar to a classic FTP session, but uses the secure authentication mechanism and encrypted data transfer of the SSH server.
To initiate a sftp session, the sftp expects a remote location in the format [user@]host, where the user@ portion is optional and, if it is missing, the user invoking the sftp command is used. To establish the sftp session, authenticating with any of the methods the SSH server accepts is necessary.
[student@desktopX ~]$sftp serverXstudent@serverX's password:studentConnected to serverX.sftp>
The 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. In addition, there are the put and get commands for uploading and downloading files. The exit command exits the sftp session.
Upload the local file /etc/hosts to the newly created directory /home/student/hostbackup on the remote host serverX. 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/student:
sftp>mkdir hostbackupsftp>cd hostbackupsftp>put /etc/hostsUploading /etc/hosts to /home/student/hostbackup/hosts /etc/hosts 100% 227 0.2KB/s 00:00sftp>
To download the remote file /etc/yum.conf from the remote host to the current directory on the local file system, execute the command get /etc/yum.conf and exit the sftp session with the exit command.
sftp>get /etc/yum.confFetching /etc/yum.conf to yum.conf /etc/yum.conf 100% 813 0.8KB/s 00:00sftp>exit[student@desktopX ~]$
scp(1) and sftp(1) man pages