SSH (Secure Shell) is one of the most useful tools in the Unix arsenal. It provides a secure way of logging in and connecting to a remote server, allowing users to access and control the remote system through the network.
This article will enumerate some of the common SSH use cases and potential problems as well as solutions for each.
Normally, when you SSH to a remote server, you will be required to enter your password to that server:
~$ ssh [email protected] Password:
If you are running automated scripts like Capistrano that needs SSH access to a remote system, this can pose a problem as it pauses the execution of your script. To solve this or if you just get tired of providing your password every time you SSH, then its time to utilize SSH public keys.
Public keys are used to identify your system with the remote system you are logging into. First you need to generate your public key using this command:
ssh-keygen -t rsa
~$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/marvs/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/marvs/.ssh/id_rsa. Your public key has been saved in /home/marvs/.ssh/id_rsa.pub. The key fingerprint is: 5b:ee:0a:c6:46:4e:3f:ed:27:38:8c:74:55:4d:93:78 [email protected]
You can press Enter for every prompt in the process and it should be ok. This will generate the public key in:
Then copy the generated key into the remote server’s authorized_keys file. SSH provides a convenience method for this (assuming you are connecting to the “REMOTE” server with username “marvs”:
ssh-copy-id -i [email protected]
You can also manually copy the key using SSH:
cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Which is basically a shortcut for:
[email protected]> scp ~/.ssh/id_rsa.pub [email protected]:~/[email protected] [email protected]> ssh [email protected] Password: windowphonebookchivas [email protected]> mkdir -p .ssh [email protected]> cat [email protected] >> ~/.ssh/authorized_keys [email protected]> rm [email protected]