opscape

Networking and remote cheat sheet

The most useful networking and remote commands on one printable page, every line checked by running it.

curl Transfer data to or from a URL; the standard tool for HTTP requests, API calls and downloads from the shell.

curl -s http://localhost:8080/api/status.json
Fetch a URL and print the body output
curl -s -o servers.json http://localhost:8080/api/servers.json curl -O http://localhost:8080/files/report.csv ls -l servers.json report.csv
Save to a file with -o or -O output
curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' http://localhost:8080/old-page curl -sL http://localhost:8080/old-page
Follow redirects with -L output
curl -s http://localhost:8080/status/404; echo "exit: $?" curl -sSf http://localhost:8080/status/404; echo "exit: $?"
Make HTTP errors fail with -f output
curl -s --json '{"name": "web03", "cpus": 2}' http://localhost:8080/api/servers
Send JSON with --json output

hostname Print the system's host name, its short or fully qualified form, or its IP addresses.

hostname
Print the host name output
hostname -s
The short name output
hostname -I
All IP addresses of the machine output

ip Show and change network interfaces, addresses, routes and neighbors on Linux (iproute2).

ip addr
Show all addresses output
ip -br addr
A one-line summary per interface output
ip route get 127.0.0.1
Which route a packet would take output
ip route echo "--- local table:" ip route show table local
The routing tables output

nc Open raw TCP or UDP connections, or listen for them; for testing ports, talking to services by hand and piping data between hosts.

nc -zv 127.0.0.1 22
Check whether a port is open output
nc -w 1 127.0.0.1 22 < /dev/null
See what a service says first output
nc -l 127.0.0.1 9000 > received.txt & sleep 0.3 echo "hello over tcp" | nc -N 127.0.0.1 9000 wait cat received.txt
Listen and connect output
nc -l 127.0.0.1 9000 > copy.csv & sleep 0.3 nc -N 127.0.0.1 9000 < data/sales.csv wait cmp data/sales.csv copy.csv && echo "identical, $(wc -c < copy.csv) bytes"
Send a file between machines output

ping Send ICMP echo requests to a host and report replies and round-trip times, to test reachability and latency.

ping -c 3 127.0.0.1
Send a fixed number of pings output
ping -c 10 -i 0.2 -q 127.0.0.1
Only the summary, sent quickly output
if ping -c 1 -W 1 127.0.0.1 > /dev/null; then echo "host is up"; else echo "host is down"; fi
Check reachability in a script output

rsync Copy and synchronize files and directories, locally or over ssh, transferring only what changed.

rsync -av src/ backup/
Copy a directory, keeping its attributes output
rsync -a src with-dir/ rsync -a src/ contents-only/ find with-dir contents-only -maxdepth 2 | sort
The trailing slash on the source output
rsync -avn --delete src/ mirror/
Rehearse a mirror with --delete and -n output
rsync -av --exclude='*.py' --exclude=lib/ src/ backup/
Leave files out with --exclude output
rsync -av data logs server:backup/ ssh server 'ls backup'
Copy to a remote host over ssh output

scp Copy files between hosts over ssh, with cp-like syntax where a remote path is written HOST:PATH.

scp todo.txt server:/tmp/ ssh server 'ls -l /tmp/todo.txt'
Copy a file to a remote host output
scp server:demo/data/hosts.txt ./hosts-copy.txt cat hosts-copy.txt
Copy a file from a remote host output
scp -r src server:src-copy ssh server 'find src-copy -type f | sort'
Copy a directory with -r output
scp -P 22 todo.txt server:/tmp/ echo "copied, exit $?"
Use a different port with -P output

ss List network sockets on Linux; above all, which ports are listening and which connections are open.

ss -tln
Which TCP ports are listening output
ss -tlnp
Show which process owns each socket output
ss -tln 'sport = :8080'
Filter by port output

ssh Log in to a remote machine or run a command on it over an encrypted connection; also forwards ports and jumps through hosts.

ssh server hostname
Run a single command on a remote host output
ssh server 'cd demo && wc -l todo.txt notes.txt'
Run several commands, quoted output
grep ' 404 ' access.log | ssh server 'wc -l' ssh server 'cat demo/data/hosts.txt' | sort -k2 | head -n 2
Pipe data into and out of a remote command output
ssh -F ssh_config web 'echo connected to $(hostname) as $USER'
Give a host a short name in an ssh config output
ssh -f -N -L 9090:localhost:8080 server curl -s http://localhost:9090/api/status.json
Forward a local port to a remote service output

ssh-copy-id Install your public SSH key in a remote account's authorized_keys so you can log in without a password.

ssh-copy-id server
Running it when the key is already installed output
ssh-copy-id -n -i deploy_key.pub server
Preview which keys would be installed output
ssh-copy-id -i deploy_key.pub server ssh -i deploy_key -o IdentitiesOnly=yes server 'tail -n 1 ~/.ssh/authorized_keys'
Install a new key and log in with it output

ssh-keygen Create, inspect and manage SSH keys, and maintain known_hosts entries.

ssh-keygen -t ed25519 -C "dev@laptop" -f ./id_demo -N ''
Generate an Ed25519 key pair output
ssh-keygen -lf id_demo.pub
Show a key's fingerprint output
ssh-keygen -y -f id_demo
Recreate a lost public key output
ssh-keygen -R server -f ./known_hosts
Remove a host's old key from known_hosts output

wget Download files over HTTP, HTTPS and FTP, non-interactively, with retries, resuming and recursive mirroring.

wget http://127.0.0.1:8080/files/report.csv
Download a file output
wget -q -O servers.json http://127.0.0.1:8080/api/servers.json head -c 120 servers.json; echo
Choose the file name with -O output
wget -qO- http://127.0.0.1:8080/api/status.json | jq -r .version
Print to standard output for a pipeline output
wget -nv -i urls.txt
Download every URL listed in a file output
wget -r -np -nH -nv http://127.0.0.1:8080/api/ find api -type f
Mirror a directory recursively output