SSH Notes
These notes apply to OpenSSH
- SSH sessions
- Initiate a session
- Use
ssh [email protected]to initiate an ssh session (can alternatively usessh -l user domain.com)
- Use
- Files stored locally in
~/.ssh- Known hosts
- The first time you connect to a remote server via SSH, you are asked if you want to continue--if you do, that server is added to the known_hosts file
- Permissions: recommended is
700for the.sshfolder and600for ssh files
- Known hosts
- Useful commands
- Type
ssh --help,scp --help, orssh-keygen -helpto see possible options -vfor verbose output-Ccompress a session (for slow connections) - Ex: ssh -C domain.com-oPubkeyAuthentication=no- Useful for when you receive the following error: Too many authentication failures for username
- Bypasses key authentication and goes straight to username/password
-oPreferredAuthentications=hostbased,publickey,keyboard-interactive,password- List the order of preferred authentication methods
- Type
- Escape character
- Alerts ssh that there are special commands to follow, which aren't sent immediately to the server
- Usage
- Default is
~ - Must be the first character on the command line to be treated as an escape character
- Next character determines the effect--Ex:
Ctrl-Zwill suspend ssh, like any other shell job -e "new-character"will change the escape character, when initializing an ssh session; Ex -ssh -e "%" [email protected]
- Default is
- Initiate a session
- SSH keys
- Generate a new DSA or RSA key:
ssh-keygen-tlets you specify either a dsa or rsa key (rsa is default and preferred) -- Ex:ssh-keygen -t rsa-bspecifies the number of bits in the key (default is 2048) -- Ex:ssh-keygen -t dsa -b 2048-fspecifies the name relative to your current directory- Ex:
ssh-keygen -t dsa -f mykey - If you omit,
-f, you are prompted for a name - Default name is
id_dsaandid_rsa
- Ex:
-Nspecifies the passphrase- Ex:
ssh-keygen -t dsa -N secretword - If you omit this, you'll be prompted later
- Ex:
-Cspecifies a comment- Defaults to
username@hostwhere host is the local host name - Ex:
ssh-keygen -t rsa -C "This is a comment"
- Defaults to
- Be careful using the above flags if your command line is logged!!!
- Passphrase (password) - always provide this because it encrypts the key; should be 10-15 characters long and not a grammatical sentence
-pto change the passphrase of an existing key- Also specify the filename with
-fand the old and new passphrases with-Pand-N - Ex:
ssh-keygen -t dsa -p -f mykey -P secretword -N newword - (If you omit the flags, you'll be prompted to enter the information)
- Benefit of this is that it doesn't change the file, just the passphrase for encryption, so no new public key uploads are needed
- Also specify the filename with
ssh-add [key-name]adds private key identities to the authentication agent (necessary for some OS programs)-dremoves specified identities from the agent-Ddeletes all identities from the agent-llists fingerprints of all registered identities-Llists public key parameters of all registered identities-Kadd passphrase in your keychain when adding identity (Mac OS)-csubjects added identities to confirmation
- Authorized keys
- Public keys go in a file called
~/.ssh/authorized_keys--transfer the contents of your public key into this file, all on one line - When using ssh2, the name of this file can be
authorized_keys2 ssh-copy-idis a script that copies local key files onto a server in~/.ssh./authorized_keysssh-copy-id -i key_file [email protected]- Ex:
ssh-copy-id -i mykey [email protected](don't need .pub extension for key file) - Creates a new
authorized_keysfile on the server or appends a new line to one - Does not create a new line before the key, so make sure there's one there already
- Does create a new line at the end of the key, to prepare for the next one
- Public keys go in a file called
- SSH agent
- Tracks keys for you so you don't have to repeatedly enter a passphrase
ssh-agent $SHELL- Invokes ssh agent with the specified shell
- Ex:
ssh-agent $bash
- ssh-add
- Generate a new DSA or RSA key:
- Configuration files
- Syntax
- Set with a combination of keywords and values
- Can use Keyword value or Keyword = value
- Can use the
-oflag to set individual configuration options- Ex:
ssh -o "User sally"or ssh-o User=sally - Use multiple
-o "Keyword value"inline to set multiple options
- Ex:
- File usually stored as
~/.ssh/config -Fwill specify a different configuration file than the default- Ex:
ssh -F /usr/local/ssh/other_config
- Ex:
- Host myserver
- Allows you to set host specific configuration
- Indent configuration values under this line (not required, but easier to read)
- myserver can be a server name, an ip address, or a nickname
- Wildcards are allowed - Ex:
Host *.example.com- If multiple wildcards match the same server, all settings that match apply; if there are duplicate settings, the earliest setting is applied
- Host settings keywords
UserHostNameIdentityFileForwardAgentPortCipher
- Syntax
- Transferring files
scp [source] [destination]scp [email protected]:filename new-local-filename- Leaving the user name off of the source domain indicates that the remote name is the same as the local one
- SSH server configuration
- Configuration file located in
/etc/ssh/sshd_config - Change port number to something other than the default
- Restart
ssh deamon /etc/init.d/ssh reload
- Configuration file located in