Skip to main content

SSH

Server

# Is sshd running?
ps -aux | grep ssh

# Install if missing
sudo apt-get install openssh-server

# Configure (port, root login, key auth)
sudo vi /etc/ssh/sshd_config
# Port 11422
# PermitRootLogin yes
# PubkeyAuthentication yes
sudo ufw allow 11422
sudo systemctl restart ssh

ssh-agent

eval $(ssh-agent -s)
ssh-add path/to/key # default key is ~/.ssh/id_rsa

Key permissions

The private key must not be group- or world-readable, or the client refuses it (Permissions 0644 for '...id_rsa' are too open):

chmod 600 ~/.ssh/id_rsa

Authorizing a key on a server

cd ~ # the target user's home
mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
cat id_rsa.pub >> .ssh/authorized_keys
rm id_rsa.pub

The 700 on .ssh and 600 on authorized_keys matter: sshd ignores the keys if the permissions are looser, and the failure is silent from the client's side.

Trusting a host

The first connection to a new host prompts to accept its fingerprint. To add it non-interactively:

ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

Per-host configuration

~/.ssh/config maps a short alias to a host and a specific key — the way to use different keys for different accounts on the same provider:

Host github-rojarsmith
Hostname github.com
IdentityFile ~/.ssh/id_rsa_account_name

A Git remote then uses the alias in place of the hostname:

# Real: [email protected]:<account>/<repo>.git
# Alias: git@github-rojarsmith:<account>/<repo>.git

Converting PuTTY keys to OpenSSH

sudo apt install putty-tools
puttygen private_key.ppk -O private-openssh -o id_rsa
puttygen public_key.ppk -O public-openssh >> id_rsa.pub

# Straight into a server's authorized_keys
puttygen public.ppk -O public-openssh >> ~/.ssh/authorized_keys

Running a remote command from Windows

plink.exe -ssh user@host -pw password -m c:\path\command.txt > output.txt