mkaz.com home photography web dev about

My SSH Notes

Marcus Kazmierczak
Created On: August 31st, 2004
Last Updated On: March 18th, 2006
1.0 - Summary

SSH is a secure replacement for telnet and rsh.

This document covers how to use SSH to connect to remote computers. This includes the new Subversion connection method and how to setup the public key authentication for password-less connections.

2.0 - Basic SSH and SCP
To connect to a remote computer:
ssh dev1.domain.com


To connect to remote computer using username:
ssh -l USERNAME dev1.domain.com


To connect to remote computer with X11 forwarding:
ssh -l USERNAME -X dev1.domain.com


Use SCP to copy files
You can also use scp, a utility included with OpenSSH, to copy files directly to a remote computer, like rcp.


Copy local file to remote computer:
scp ~/local/file.txt dev1.domain.com:/home/USERNAME/local/file.txt


Copy remote file to local computer:
scp dev1.domain.com:/home/USERNAME/local/file.txt ./file.txt


If your local username is different than your remote username:
scp ~/local/file.txt USERNAME@dev1.domain.com:/home/USERNAME/local/file.txt


Copy directories at a time:
scp -r ~/local dev1.domain.com:/home/USERNAME/local


Automatic Passwords
As you have probably noticed already, you have to enter your password after each command. This can become tiring and time consuming. You can use a public key for automatic authentication. Here's how you do that:

Creating Keys

The following will generate the public and private keys and prompt you to enter a pass phrase for the keys
ssh-keygen -q -f ~/.ssh/id_rsa -t rsa

Two keys are generated a public and private key, the public key has a .pub extension. This is what needs to be copied out to the server you wish to connect to. Use scp (see above) and copy the file to your home directory on the remote server.

On the remote server, append the id_rsa.pub file to your authorized_keys file:
cat id_rsa.pub >>~/.ssh/authorized_keys


You know should be able to login to the remote box and it will prompt you for your certificate passphrase instead of your password.


Using ssh-agent for Automatic Passwords

Start ssh-agent:
ssh-agent -s > ~/.ssh/ssh_agents

Source Profile:
. ~/.ssh/ssh_agents

Add Key to Agent (add pass phrase):
ssh-add

You can now login to remote server and you will not be prompted for your password.

You can remove a key from the ssh-agent using the following:
ssh-add -D

You can also add a timeout for the key when adding:
ssh-add -t SECS
Notes

One gotcha that I came across which was causing problems with the automatic login was the permissions on my ~/.ssh directory. It must be set to only be read by myself, in unix terms thats chmod 700

Related Links