Learn
Answers to the things people look up while they work on servers over SSH. Every command ran in a Linux container first, and the output is what it printed.
Last updated
SSH config
-
SSH config file (~/.ssh/config): examples that work
The file ~/.ssh/config stores the address, user, port and key of each server under a short name. After that, ssh app does the work of a long command.
-
How do I fix "REMOTE HOST IDENTIFICATION HAS CHANGED" in SSH?
The server now shows a different host key than the one saved in your known_hosts file. Check the new fingerprint with the server's owner, remove the old entry with ssh-keygen -R, and connect again.
-
How do I SSH through a jump host (bastion)?
Add -J user@bastion to the ssh command, or write ProxyJump bastion in the block of the server. ssh connects to the bastion first and opens the connection to the server through it.
-
How do I stop SSH from dropping with "Broken pipe"?
Add ServerAliveInterval 30 to the Host * block of ~/.ssh/config. ssh then sends a small message every 30 seconds, so a router that forgets idle connections sees traffic, and ssh notices when the server stops answering.
-
How does SSH port forwarding work? Examples of -L, -R and -D
ssh -L 9000:127.0.0.1:8080 app opens port 9000 on your machine and sends every connection to port 8080 on the server app. -R does the reverse, and -D turns the connection into a SOCKS proxy.
Security
-
How do I generate an SSH key and put it on a server?
Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
-
How do I disable SSH password login safely?
Put PasswordAuthentication no in a file in /etc/ssh/sshd_config.d/ whose name sorts first, run sudo sshd -t, then reload ssh. Keep your current session open and test a new login in a second window before you close it.
-
How do I use rm safely on a server?
List what a command will remove before you run it, with ls, find or echo. Use rm -I so that rm asks once before a recursive delete, and write variables as "${dir:?}" so that an empty one stops the command. The built-in --preserve-root protects only the path /.
-
How do I let a user run one command with sudo?
Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
-
How do I get rid of "Using a password on the command line interface can be insecure"?
The warning appears because you gave the password as a value, as in -ps3cret, and then it sits in the command line where ps can show it. Move it into a [client] section of ~/.my.cnf with mode 600, or into a login path made by mysql_config_editor, and run mysql without it.
-
How do I run psql without typing the password?
Put one line in ~/.pgpass in the form hostname:port:database:username:password and run chmod 600 on the file. psql then finds the password itself, and psql -w makes a script fail with an error instead of waiting for a prompt that nobody can answer.
-
Can I save an SSH password in the config file?
No. ssh_config has no option for a password, and ssh stops with an error when a config file contains one. Load a key into ssh-agent and nothing is asked; for a device that takes only a password, use ControlMaster to ask once per session or sshpass to answer for a script.
-
How do I run sudo over SSH when it says "a terminal is required"?
Add -t, as in ssh -t app sudo whoami, so ssh gives the remote command a terminal and sudo can ask for the password. In a script, give the account a NOPASSWD rule for that one command instead of sending the password into sudo -S.
Server diagnostics
-
How do I check disk space on a Linux server?
Run df -h to see how full each file system is, then run du -h --max-depth=1 on the full one to find the folder that uses the space. df reads the file system itself and du adds up the files it can reach, so the two can disagree.
-
How do I find the largest files on a Linux server?
Run find /path -type f -size +100M to list the files above 100 MiB, or du -ah /path | sort -h | tail to see the biggest files and folders together. Add -xdev so find stays on one disk. If df says the disk is full and du cannot find the space, look for deleted files that a program still holds open with lsof +L1.
-
How do I check memory and CPU usage on a Linux server?
Run free -h and read the available column. It is the memory that new programs can still get without swapping. Then run ps -eo pid,user,rss,comm --sort=-rss to see which process holds the most, and sort by -pcpu to see which one burns the CPU.
-
How do I find which process is using a port on Linux?
Run sudo ss -tulpn and look for the port in the Local Address column. The last column names the program and its PID. lsof -i :PORT answers the same question for one port. Without sudo
ssstill lists every listener but names only your own processes. -
How do I read logs on a Linux server?
Use tail -f to watch a log file live, grep -C 3 to see a match with the lines around it, and zgrep for the rotated copies that are compressed. For a service that logs to systemd, run journalctl -u name --since "1 hour ago". You need sudo or the right group for the journal.
Files
-
How do I copy files to and from a server over SSH?
Use scp notes.txt user@host:/tmp/ to upload and scp user@host:/var/log/app.log . to download. Add -r for a folder and -P for a port other than 22. For a transfer you may need to repeat, use rsync.
-
How do I change permissions recursively on Linux?
Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.
-
How do I use rsync over SSH, and see what it will do first?
Run rsync -ai --dry-run source/ user@host:/dest/ first. It lists what would change and changes nothing. Run the same command without --dry-run to do it. A slash after the source folder means its contents, and --delete removes files from the destination, so read the preview before you use it.
Many servers
-
How do I run one command on many servers over SSH?
Put the host names in a file and run ssh -n -o BatchMode=yes -o ConnectTimeout=5 host command for each one, in a loop for a few servers or with xargs -P for many. Keep each host's output and exit status, so one host that fails never hides behind the others.
-
How do I keep a command running after I close SSH?
Start it inside tmux (tmux new -s name, then Ctrl-b and d to leave). If tmux is not there, put nohup in front of the command and an ampersand after it. When a terminal closes, the programs started in it get a hangup signal and stop, and both tools get around that.
AI agents on servers
-
How do I give an AI agent SSH access with the least damage?
Give the agent its own account on the server, with no sudo, no password in the prompt and nothing secret in its reach. Let it read first, allow root for one exact command at most, make every change wait for a person, and keep a record of what ran.
-
How do I keep a record of what an AI agent ran on a server?
Do not count on shell history. Give the agent its own account and key, log sshd at VERBOSE so each login shows the key fingerprint, and let sudo record the input and output of anything the agent runs as root.
-
How do I keep an AI agent from deleting a production database?
Let the agent connect as a database role that has SELECT on the tables it needs and nothing else, so DROP, DELETE and TRUNCATE fail with a permission error. Keep the production password out of every file and variable the agent can read, give it a copy of the data to work on, and restore a backup once before the agent connects to production.
-
Which commands are safe to auto-approve for an AI agent on a server?
Only exact commands that read and cannot start another program. find, sed, awk, a redirection and ssh look like reads and are not, so a rule that allows them by name allows what they can do.
-
Can Claude Code SSH into a server?
Yes. Claude Code has a shell, so it can run ssh host 'command' from your machine. It can also run on the server itself, and the desktop app sets that up with its SSH sessions. What the agent can reach, and where your key ends up, depends on the setup you choose.
-
What do I do when Claude Code needs sudo?
Do not paste the password into the chat and do not write NOPASSWD ALL. Run the command with sudo -n so it fails at once, read what the account may run with sudo -l, and give the account a sudoers rule for that one command.
-
How does Codex connect to a server over SSH?
Codex, in the ChatGPT desktop app, finds hosts in your ~/.ssh/config, resolves them with OpenSSH and starts its remote app server over SSH. Its documentation shows a key and never mentions a password, so move a host that asks for one to a key first.
The app is free.
Get notified at launchmacOS is coming to the MacĀ AppĀ Store. iOS and Android later.