Linux Training : 3. ssh, scp and rsync

ssh, scp and rsync

Secure SHell, ssh, is the default remote command line tool for all Linux users. It replaces the old, totally insecure, telnet. Secure CoPy, scp, uses the same encryption process as ssh to securely copy files between machines. rsync can tunnel through ssh to securely synchronize files or entire directories between multiple systems.

ssh uses

  • remote command line

    • The most basic use, ssh <username>@<hostname> will open an encrypted tunnel, prompt for a password, and then the user is effectively logged into the remote machine at a command prompt.
    • There are many flags that are available to allow specific additional functions. A particularly useful one is to use ssh to execute a remote command and return the output to the controlling host. ssh <username>@<hostname> 'ls -la /home' will give a listing of the /home directory from the remote machine on the screen of the connecting machine. In IO parlance, the remote machine STDOUT is redirected through the ssh tunnel to the connecting machine STDOUT.
  • tunneling applications

    --ssh (with the -X parameter) typically will set the DISPLAY variable. This allows remote X application to have their display sent back through the ssh tunnel. Simply ssh to the remote machine and start the application with it's normal executable launcher.
  • port redirection with secure tunneling

    --Port redirection is a convenient way to run a local app data stream through a secure tunnel to a remote machine. ssh -L <localport>:localhost:<remote port> <username>@<hostname> will open an ssh connection and bind <localport> to the tunnel on the local end and <remote port> to the tunnel on the remote end. Now run the local-app and connect to localhost:<local port> and the application is local but the data stream is from the remote machine (which thinks the connection is local to itself).

scp uses

  • scp from local to remote

    • scp /path/to/file <username>@<hostname>: will copy file to the home directory of <username> on <hostname>
    • scp /path/to/files/ <username>@<hostname>:/path/to/new/files/ will copy all of the files to the /path/to/new/files/ location on the <hostname> system.
      • To copy everything from that point down, use the -r flag for recursive, i.e. scp -r /path/to/files/
      • Be aware that scp -r will follow symlinks back up through the entire filesystem.
  • scp from remotehost1 to remotehost2

    • scp <user1>@<host1>:<path>/file <user2>@<host2>:<path> will copy a file from host1 to host even though you are working on a third host locally.
  • scp overwrites

    • Be aware that scp will overwrite *_without notice_* a file of the same name as the copying file on the receiving machine.

rsync uses

  • sync remote file to local file

    • rsync -avz -e "ssh -l <remoteuser>" <remoteuser>@<remote host>:/path/to/file local/path/to/file will sync remote file to local file
  • sync remote directory to local system

    • rsync -avz -e "ssh -l <remoteuser>" <remoteuser>@<remote host>:/path/to/directory/ local/path/to/directory will sync the contents of the remote directory.
  • sync two remote files to local directory

    • rsync -avz -e "ssh -l <remoteuser>" <remoteuser>@<remote host>:'path1/file1 path2/file2' local/path will sync only file1 and file2 into local/path directory.

      rsync can be used with flags that direct removing of receiving files no longer in source directories. Be sure this is what you want before adding any --delete flags