basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    Cheat sheetLinuxSSH

    Everyday SSH: logging in, copying, keys, debugging

    OpenSSH client and server commands on one screen. web1 is a name from ~/.ssh/config (~/.ssh/config); you can write [email protected] instead.

    Logging in and running commands

    Command What it does
    ssh web1 log in by the name from ~/.ssh/config
    ssh -p 48731 [email protected] log in on another port
    ssh web1 'df -h /' run one command and exit
    ssh -t web1 'sudo journalctl -f' a command that needs a terminal
    ssh -J web1 [email protected] through a jump host
    ssh -o ConnectTimeout=5 web1 give up after 5 seconds

    A frozen session ignores Ctrl+C — press Enter, then ~.: the client drops the connection itself. ~? lists these escapes. They work only at the start of a line.

    Copying

    Command What it does
    scp backup.tar.gz web1:/tmp/ file to the server
    scp web1:/opt/traefik/data/traefik.yml . file from the server
    scp -3 web1:/tmp/a.tar web2:/tmp/ server to server through your computer
    rsync -av --dry-run ./site/ web1:/srv/site/ show what would be sent, change nothing
    rsync -av --delete ./site/ web1:/srv/site/ make an exact copy: extra files on the server get deleted
    rsync -av --progress -e 'ssh -p 48731' big.iso web1: on another port, with progress
    sftp web1 interactive: ls, get, put, exit

    With rsync, a trailing slash on the source decides: ./site/ is the directory’s contents, ./site is the directory itself, put inside /srv/site/. Start with --dry-run. Since OpenSSH 9.0 scp runs over SFTP; the old flags still work.

    Keys

    Command What it does
    ssh-keygen -t ed25519 -C "laptop-2026" new key (SSH keys)
    ssh-keygen -p -f ~/.ssh/id_ed25519 change or set the passphrase
    ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub rebuild .pub from the private key
    ssh-keygen -lf ~/.ssh/id_ed25519.pub key fingerprint
    ssh-copy-id -i ~/.ssh/id_ed25519.pub web1 put the key on the server
    ssh-add -l keys in the agent (ssh-agent)
    ssh-add -D remove all keys from the agent

    Server keys, known_hosts

    Command What it does
    ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub on the server: fingerprint to compare (Server fingerprint)
    ssh-keyscan -t ed25519 203.0.113.10 | ssh-keygen -lf - the fingerprint the server shows right now
    ssh-keygen -F 203.0.113.10 is the server in known_hosts
    ssh-keygen -R 203.0.113.10 forget the server’s old key
    ssh -o UpdateHostKeys=yes web1 accept the server’s extra keys when it rotates them

    Client debugging

    Command What it does
    ssh -v web1 login trace: which keys, which one was accepted
    ssh -vvv web1 everything, down to packets
    ssh -G web1 effective settings for a name, no connection
    ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no web1 is password login closed
    ssh -Q kex key exchange algorithms the client knows
    nc -vz 203.0.113.10 22 is the port open at all

    Server

    Command What it does
    sudo sshd -t check the config before restarting
    sudo sshd -T | grep -i passwordauth effective value of a setting
    sudo systemctl restart ssh apply the config; sessions stay up
    systemctl status ssh ssh.socket who holds the port: the service or the socket
    ss -tlnp | grep -E ':22 ' which address and port are listening
    sudo journalctl -u ssh -f logins and refusals live
    sudo journalctl -u ssh --since today | grep Accepted who logged in today and with which key
    who who’s on the server now
    last -n 10 recent logins
    sudo lastb -n 10 recent failed attempts
    sudo pkill -KILL -t pts/1 kick the session on terminal pts/1

    The line Accepted publickey for example-user from … ssh2: ED25519 SHA256:u7WT… carries the key fingerprint, which tells you which device logged in (SSH keys).