Options can be either given on the command line when invoking ssh (see man ssh) or in a user configuration file at ~/.ssh/config (see man ssh_config).
All SSH utilities (see https://www.openssh.org/manual.html)
ssh— The basic rlogin/rsh-like client programsshd— The daemon that permits you to log inssh-agent— An authentication agent that can store private keysssh-add— Tool which adds keys to in the above agentssh-keygen— Key generation toolssh-keyscan— Utility for gathering public host keys from a number of hostsssh-keysign— Helper program for host-based authenticationsftp— FTP-like program that works over SSH1 and SSH2 protocolscp— File copy program that acts like rcpsftp-server— SFTP server subsystem (started automatically by sshd)
Additional manuals (ssh_config is not a program; man ssh_config will display the manual for the client configuration file)
ssh_config— The client configuration filesshd_config— The daemon configuration file
See man ssh_config.
Order of priority
- command-line options
- user's configuration file (~/.ssh/config)
- system-wide configuration file (/etc/ssh/ssh_config)
-
Generate private-public key pair:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519-caltech- This creates 2 files:
~/.ssh/id_ed25519-caltech.pubcontaining the public key~/.ssh/id_ed25519-caltechcontaining the private key
ssh-keygenwill ask for a passphrase to encrypt the private key.- If no passphrase is given, then the private key is stored in plaintext in the file
~/.ssh/id_ed25519-caltech. - If a passphrase is given, then
~/.ssh/id_ed25519-caltechcontains the encrypted private key.
- If no passphrase is given, then the private key is stored in plaintext in the file
- This creates 2 files:
-
Copy the public key to the server:
ssh-copy-id -i ~/.ssh/id_ed25519-caltech.pub btyeh@caltech- This adds the public key (i.e., the contents of
~/.ssh/id_ed25519-caltech.pub) tobtyeh@caltech:/home/btyeh/.ssh/authorized_keys ~/.ssh/authorized_keysin a user's home directory lists public keys that can be used for logging in as the user using a corresponding private key instead of the user password.
- This adds the public key (i.e., the contents of
-
Update the SSH client configuration file (
~/.ssh/config) to point to the private key. Note thatUseKeychainis a macOS-specific configuration keyword.Host caltech Hostname login.hpc.caltech.edu IdentityFile ~/.ssh/id_ed25519-caltech Host * AddKeysToAgent yes UseKeychain yes
Consider setting up an SSH connection: ssh btyeh@caltech
-
The
IdentityFile ~/.ssh/id_ed25519-caltechline directs thesshclient to ask thecaltechserver if it recognizes the public key at~/.ssh/id_ed25519-caltech.pub(i.e., if the same public key is atbtyeh@caltech:~/.ssh/authorized_keys). -
The server SSH daemon
sshdrecognizes the client's public key, then asks for verification that the client "owns" the key. -
The client uses the local private key to sign a "challenge" from the server and sends the signature back.
a. The
sshclient needs the passphrase to access the private key. TheUseKeychain yesconfiguration directs thesshclient to get the passphrase from the system keychain (e.g., macOS Keychain) instead of prompting the user.b. The
AddKeysToAgent yesconfiguration directs thessh clientto give the decrypted private key tossh-agent, which holds it in volatile memory (RAM).c. Steps 3a and 3b are analogous to manually running
ssh-add ~/.ssh/id_ed25519-caltech, which will prompt for the passphrase, then add the private key tossh-agent.- The
ssh-add --apple-load-keychainflag is analogous to theUseKeychain yesconfiguration: look for the passphrase from the macOS Keychain first; only prompt the user if it is not found. - The passphrase can be manually added to the macOS keychain by using the
--apple-use-keychainflag. (If only using thessh clientwith the above configuration file without usingssh-add -apple-use-keychainmanually, will the passphrase ever get added to the macOS Keychain?)
- The
ssh options
-L [bind:]<loc_port>:<host>:<host_port>: forwards all network requests on the local machine's<loc_port>port to the<host>:<host_port>on<remote>[bind:]<loc_port>= the local port from which to forward network requests. Ifbindis not set, then anyone with access to the local machine's<loc_port>(e.g. over the same Wi-Fi network) can also see the forwarded requests. To restrict<loc_port>access to the local machine itself, setbind=localhost<host>:<host_port>= the host port (from<remote>'s perspective) that is receiving forwarded network requests- Typically
<host>=localhost <host>may also be a network address (e.g., if<remote>is a login node and<host>is a compute node)- Firewalls may limit the range of accessible
<host_port>numbers (e.g., on wheat and oat Farmshare nodes,<host_port>needs to be > 32768)
- Firewalls may limit the range of accessible
- Typically
-N: do not execute a remote command-f: go to background just before command execution
Direct port forwarding: ssh -N -L [bind:]<loc_port>:<host>:<host_port> <user>@<remote>
- Example: forward localhost:8889 to rice05:50000
ssh -Nf -L localhost:8889:localhost:50000 bentyeh@rice05.stanford.edu - Example: forward localhost:8889 to wheat16:50000 through rice05
ssh -Nf -L localhost:8889:wheat16.stanford.edu:50000 bentyeh@rice05.stanford.edu
Multiple hop port forwarding: ssh -L [bind:]<loc_port>:<middle_host>:<middle_port> <user>@<middle_host> ssh -N -L <middle_port>:<host>:<host_port> <remote>
- Does not work if two-factor authentication is required between
<middle_host>and<remote>(e.g., on Farmshare) - Example: forward localhost:8889 through login.sherlock.stanford.edu:40000 to sh-08-25.int:50000
ssh -f -L localhost:8889:localhost:40000 bentyeh@login.sherlock.stanford.edu ssh -N -L 40000:localhost:50000 sh-08-25.int
How to avoid authentication each time you log in?
- Add the following lines to
~/.ssh/configon your local machine (i.e., laptop):Host <hostname, e.g., login.sherlock.stanford.edu> ControlMaster auto ControlPersist yes ControlPath ~/.ssh/%l%r@%h:%p - To control an active connection multiplexing process:
ssh -O <ctl_cmd> <host><ctl_cmd>check: check that the master process is runningstop: request the master to stop accepting further multiplexing requestsexit: request the master to exit
- Example:
ssh -O check sherlock
- References: