Command Line SSH User Guide
SSH is a secure protocol that encrypts all data sent between the client computer and the computer it is connecting to. SSH applications usually allow both interactive terminal sessions on the remote machine and the ability to transfer files securely.
This guide is intended as a very basic introduction to using command line SSH commands. A lot more information on using SSH can be found online, in books, and in the man pages for each individual SSH command.
The command line SSH command to open a terminal on a remote machine is just called ssh. The syntax to connect to a remote machine is:
where username is the name of your account on the remote system and hostname is the hostname of the remote system that is being connected to.
The following is an example of the user abc123 connecting to the Lion-X cluster lionxj.rcc.psu.edu.
[abc123@funkmachine ~]$ ssh firstname.lastname@example.org email@example.com's password: password [abc123@lionxj ~]$
[abc123@lionxj ~]$" prompt now shows that user abc123
is logged in to lionxj.
There are two main command-line SSH commands to transfer files: scp and sftp. scp is a non-interactive command that takes a set of files to copy on the command line, copies them, and exits. sftp is an interactive command that opens a persistent connection that multiple copying commands can be performed through.
To copy one or more local files up to a remote server, the scp syntax would be:
scp local_file(s) user@hostname:destination_directory
Example: copy individual files to a remote system
For user abc123 to copy the local files foo.c and foo.h into their home directory on the host lionxj.rcc.psu.edu, the following command would be used:
[abc123@funkmachine ~]$ scp foo.c foo.h firstname.lastname@example.org:. email@example.com's password: password foo.c 100% 122 0.1KB/s 0.1KB/s 00:00 foo.h 100% 14 0.0KB/s 0.1KB/s 00:00
Example: copy a whole directory to a remote system
For a user to copy a whole directory called srcdir including all of its contents into their home directory on the host hammer.rcc.psu.edu, the
-rflag can be added to the scp command. The
-rflag tells scp to recursively descend into a directory and copy all of its contents.
[abc123@funkmachine ~]$ scp -r srcdir firstname.lastname@example.org:. email@example.com's password: password bork.c 100% 388 0.4KB/s 22.0KB/s 00:00 swap2.c 100% 199 0.2KB/s 22.0KB/s 00:00 list.c 100% 1029 1.0KB/s 22.0KB/s 00:00 array.c 100% 536 0.5KB/s 22.0KB/s 00:00 foo.c 100% 122 0.1KB/s 22.0KB/s 00:00 acid.c 100% 446 0.4KB/s 22.0KB/s 00:00 blah.c 100% 112 0.1KB/s 22.0KB/s 00:00
Example: copy files down from a remote system
To copy files from a remote system down to a local machine, the order of the scp command just needs to be reversed so that the first argument (the source argument) is the remote system and the second argument (the destination argument) is a directory on the local system.
For user abc123 to copy the remote file results.txt on the server lionxi.rcc.psu.edu into their current directory on their local machine, the following command would be used:
scp firstname.lastname@example.org:results.txt . email@example.com's password: password results.txt 100% 8 0.0KB/s 0.0KB/s 00:00
sftp is an interactive command that uses the same syntax as a standard command-line ftp client. It differs from a standard ftp client in that the authentication and the data transfer happen through the SSH protocol rather than the FTP protocol. The SSH protocol is encrypted whereas the FTP protocol is not.
There are a number of basic commands that are used inside of stfp:
put filename: uploads the file filename
get filename: downloads the file filename
ls: lists the contents of the current remote directory
lls: lists the contents of the current local directory
pwd: returns the current remote directory
lpwd: returns the current local directory
cd directory: changes the current remote directory to directory
lcd directory: changes the current local directory to directory
The syntax for calling sftp is:sftp username@hostname
Example sftp session
[abc123@funkmachine ~]$ sftp firstname.lastname@example.org Connecting to lionxi.rcc.psu.edu... abc123@lionxi's password: password sftp> pwd Remote working directory: /gpfs/home/jwh128 sftp> lpwd Local working directory: /home/jwh128 sftp> cd work/depot sftp> pwd Remote working directory: /gpfs/work/jwh128/depot sftp> lcd results sftp> lpwd Local working directory: /home/jwh128/results sftp> ls -l -rw-r--r-- 1 root root 5 Mar 3 12:08 dump sftp> lls -l total 0 sftp> get dump Fetching /gpfs/work/jwh128/depot/dump to dump /gpfs/work/jwh128/depot/dump 100% 5 0.0KB/s 0.0KB/s 00:00 sftp> lls -l total 4 -rw-r--r-- 1 jwh128 jwh128 5 Mar 3 12:09 dump sftp>