# SSH

Your private key should never leave a host. That should be a policy and you should write rules to detect when that policy is being violated (check for processes accessing the file).

If you need access from N computers you should be generating N keys. - HN

# tmux ⮺

A common way to keep long-running terminal tasks alive even if your SSH connection drops.

see also herdr 🐑 as a more friendly alternatives

$ ssh server
$ tmux new -s work
# run long job
Ctrl+b d
# disconnect safely

Later

$ ssh server

$ tmux ls     # list session
$ tmux attach -t work

see also

# Shortcuts

detach - Ctrl+b then d

see also

  • byobu - A user-friendly wrapper around tmux or screen that adds status bars, notifications, and easier keybindings.

# Truly convenient SSH

using kitty

$ kitty +kitten ssh user@host

# List identity

Find and take a note of your public key fingerprint, to recognize which key belong to who.

$ ssh-add -l -E sha256
> 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)

You might need to start ssh-agent before you run the ssh-add command (SO):

$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa # add keys to agent # if no keys are know

# Enroll public key ⮺

ssh-copy-id

$ ssh-copy-id -i .ssh/id_rsa.pub  root@10.11.99.1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.11.99.1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.11.99.1'"

direct Alternative for key copied locally

$ cat <your_public_key_file> >> ~/.ssh/authorized_keys

Or (eg if retrived by localsend)

$ echo "ssh-rsa AA..."  >> ~/.ssh/authorized_keys

# Changing SSH key passphrase ⮺

$ ssh-keygen -p

# SSH Quoting ⮺

$ ssh $server "$(cat script)"

# Test that it works ⮺

$ ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no example.com

=> yves@192.168.0.x: Permission denied (publickey).

# Keep SSH session ⮺

http://byobu.co/index.html

# Generating key ⮺ 🔑

Use one key per origin host, no need to change the default file.

$ ssh-keygen -t rsa -b 4096 -C <hostname>
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yves/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in github_rsa
Your public key has been saved in github_rsa.pub

# Github access ⮺

see Connect to GitHub with SSH

# SSH-Agent ⮺ 👮

see ssh-agent

# Enable SSH server ⮺ 🖥

sudo apt install openssh-server

# Disable password access ⮺

If you want to fully disable password-based authentication, set BOTH PasswordAuthentication and ChallengeResponseAuthentication to ‘no’… since SSH is a network-based protocol, the server has no way to guarantee that responses to ChallengeResponseAuthentication (a.k.a. ‘keyboard-interactive’) are actually being provided by a user sitting at a keyboard so long as the challenge(s) always and only consists of asking a user for her password.

# Edit /etc/ssh/sshd_config
PasswordAuthentication no
ChallengeResponseAuthentication no

$ sudo systemctl restart ssh

# MitM ⮺ ☠️

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:6baCZCPsl+AfQuujUS81fMEE/C7UeJ+AwjrImqmDB3E.
Please contact your system administrator.
  • this shows the strength of mutual authentication (ssh user key authentication, or HTTPS client certificates) - HN
      1. It can’t steal your password, since the private key never leaves your client;
      1. It can observe each command you type, but it can’t open a connection to the real server to know how it would respond to your commands;
      1. It can’t open any connection to the real server (it will fail authentication), so it can’t do anything to the real server.

# TroubleShooting 🛠️

# References

# Security

sshole / HN openssh on musl linux (alpine, openwrt, buildroot, yocto, and a lot of the docker images you’ve pulled) has had a little problem since 2016. Accounts that should never take a password at all (root, your key-only deploy user, ldap users, the default user on alpine’s aws and azure images) take any password longer than 256 characters.

see also

Written on April 29, 2019, Last update on July 26, 2026
network ssh shell cli security network-security linux-system tmux