Remote Access with ssh and scp
ssh lets you log in to another computer over the network and work on its command line, with everything encrypted. scp uses the same connection to copy files between computers.
Connecting
ssh [email protected]Logs in to server.example.com as the user roger. Type exit to disconnect.
ssh -p 2222 [email protected]Connects to a server that uses a different port.
Logging in with a key instead of a password
SSH keys are more secure than passwords and save you typing. You only need to set them up once.
ssh-keygen -t ed25519Creates a new key pair in ~/.ssh. Set a passphrase to protect it.
ssh-copy-id [email protected]Copies your public key to the server so you can log in without a password.
Keep your private key private. Only the file ending in .pub should ever be copied to another computer.
Shortcuts for servers you use often
Add entries to ~/.ssh/config to save typing:
Host myserver
HostName server.example.com
User roger
Port 2222
Now ssh myserver connects with all of these settings.
Copying files with scp
scp report.pdf [email protected]:/home/roger/Copies report.pdf to the server.
scp [email protected]:/var/log/syslog .Copies a file from the server to the current directory.
scp -r photos/ [email protected]:~/Copies a whole folder (-r) to your home directory on the server.
Tunnels
ssh -L 8080:localhost:80 [email protected]Makes the server's web page available on your computer at http://localhost:8080, through the encrypted connection.
Quick reference
| Option | Meaning |
|---|---|
ssh user@host | Log in to another computer |
-p 2222 | Use a different port |
ssh-keygen | Create an SSH key |
ssh-copy-id | Install your key on a server |
scp file user@host:path | Copy a file to a server |
scp -r | Copy a folder |
~/.ssh/config | Save settings for each server |