Cheat sheets

Cheat sheet

ssh, scp and sftp cheat sheet

Remote login and file copies on one page: ssh, scp and sftp.

ssh

Opens an encrypted shell on another device, directly or through a jump host, and forwards ports through it.

ssh admin@10.0.1.10 hostname
Logs in to 10.0.1.10 as admin, runs hostname, and returns.
ssh -p 2222 admin@10.0.2.2
Logs in through port 2222, which the router forwards to the inside.
ssh -J admin@bastion.lab admin@10.0.3.10
Reaches an inside server by going through the bastion host.
ssh -L 8080:10.0.3.10:80 admin@bastion.lab
Makes local port 8080 lead to the inside web server, through the bastion.
grep 'Failed password' /var/log/netlab-sshd.log | tail
Shows the latest failed logins recorded by the SSH server.
ssh -i ~/.ssh/lab_ed25519 admin@10.0.1.10
Logs in with that private key instead of the default ones.
ssh-keygen -t ed25519 -C ana@laptop
Makes a new key pair, ~/.ssh/id_ed25519 and its .pub, the type to choose today.
ssh-copy-id admin@10.0.1.10
Adds your public key to the server's ~/.ssh/authorized_keys, so later logins need no password.

Every ssh option, with real output

scp

Copies files to or from another machine over SSH: logged in and encrypted.

scp /etc/frr/frr.conf backup@10.0.9.10:officeRouter.conf
Copies the saved config into the backup account's home.
scp backup@10.0.9.10:officeRouter.conf /tmp/restore.conf
Fetches it back.
scp report.txt ana@10.0.1.10:/tmp/
Copies a local file into /tmp on 10.0.1.10.
scp -r configs/ backup@10.0.9.10:
Copies a whole directory into the backup account's home.
scp -P 2222 report.txt admin@10.0.2.2:
Uses the server's port 2222: capital -P, where ssh takes -p.
scp -i ~/.ssh/backup_ed25519 frr.conf backup@10.0.9.10:
Logs in with that private key instead of the default ones.
scp -p frr.conf backup@10.0.9.10:
Keeps the file's modification time and mode on the copy.
scp 'admin@10.0.1.10:/var/log/*.log' logs/
Copies every .log file; the quotes keep your own shell from expanding the *.

Every scp option, with real output

sftp

An interactive file session over SSH: ls, get, put, rm, as with FTP but logged in and encrypted.

printf 'ls -l\n' | sftp -b - backup@10.0.9.10
Lists the backup account's files.
sftp backup@10.0.9.10
Opens a session: then ls, cd, get FILE, put FILE, and lcd to move on your own side.
sftp -P 2222 admin@10.0.2.2
Uses the server's port 2222: capital -P, as with scp.
sftp -i ~/.ssh/backup_ed25519 backup@10.0.9.10
Logs in with that private key.
sftp backup@10.0.9.10:officeRouter.conf
Downloads one file into the current directory and quits, without a session.
printf 'put frr.conf\n' | sftp -b - backup@10.0.9.10
Uploads a file from a script, failing if the upload fails.
printf 'get -R configs\n' | sftp -b - backup@10.0.9.10
Downloads a whole directory: -R for recursive.
sftp -b nightly.txt backup@10.0.9.10
Runs the commands in nightly.txt and stops at the first that fails; a line starting with - may fail.

Every sftp option, with real output

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab