SSH
By Greg Gallardo
SSH
I use SSH all the time for work and my own projects. This post is just a collection of SSH related commands that I find useful.
Basic Usage
Log into a machine as a specific user with ssh.
ssh username@example.com
Specify a port other than the default (22) with -p.
ssh username@example.com -p 5555
ssh ssh://username@example.com:5555
Specify a SSH key with -i followed by the path to the key.
ssh -i ~/.ssh/my_key username@example.com
Copy Files
SCP
I occasionally use scp to copy files over the network (but I usually use SFTP)
scp -P 5555 -i ~/.ssh/my_id <FILE_NAME> username@example.com:/home/username/<FILE_NAME>
Note:
- For
scpthe port option-Pis upper caseP. This is different fromsshwhich uses lower casep. -iis still the same.
To copy an entire folder use the -r option
scp -r host:path/directory .
SFTP
sftp lets you interactively copy files to and from a server.
sftp -oPort=5555 -i ~/.ssh/my_id username@example.com
The basic commands are:
pwd: Show the name of the remote working directory.ls: list the contents of the remote directory.cd <path> : Change the remote directory to` put <filename>: Upload a local file to the remote machineget <filename>: Download a remote file to the local machine. adding-rwill recursively copy files from the server.lpwd: Show the name of the local working directory.lls: list the contents of the local directory.lcd <path> : Change the local directory to` Generate SSH Keys
Use
ssh-keygento create SSH keys.ssh-keygen -t rsa-t rsasets the algorithm to RSA. You can set the number of bytes (more is better) with the -b optionssh-keygen -t rsa -b 4096You will be asked for a passphrase. I typically enter one, but you can just press return to leave it blank:
fred5617@homebox:~$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/fred5617/.ssh/id_rsa): /home/fred5617/.ssh/id_rsa_eraseme Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/fred5617/.ssh/id_rsa_eraseme Your public key has been saved in id_rsa_eraseme.pub The key fingerprint is: SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX fred5617@homebox The key's randomart image is: +---[RSA 3072]----+ |.XXXXXXXXXX | |= XXX.XXX. XXX X | |-XXX X X XXXXX | | .X. XX. XX | | XXXA. | | , .XX * | | XX . X | | * >.XX . | | * X XX | +----[SHA256]-----+Copy SSH keys to a Server
Once you have a new key, you can upload its public key to a server with
ssh-copy-id.ssh-copy-id -i ~/.ssh/id_rsa_eraseme fred@example.comFor this to work, you must already have SSH and password access to the server.
ssh-agent
Sometimes you want to use SSH multiple times without entering your passphrase each time. To do this, use
ssh-agent.eval $(ssh-agent)Then add the key you want to use with
ssh-addssh-add /path/to/id_rsa_erasemeSSHFS
Mounting a remote filesystem with
sshfssshfs fred5617@10.10.50.1:/home/fred5617/SEGA/sgdkrocksnet ~/sgdkrocksnet/
Copy Code