SSH
Configure secure SSH access with key-based authentication, server hardening and convenient client aliases.
-
SSH key management (client)
Section titled “SSH key management (client)”Generate unique keys for each device to improve security and auditability.
-
Generate named key
Section titled “Generate named key”Instead of the default name, label the key after the hostname (e.g.,
id_hp-laptoporid_macbook-pro).Terminal window ssh-keygen -t ed25519 -f ~/.ssh/id_{{HOSTNAME_VAR}} -
Copy key to server
Section titled “Copy key to server”Use
ssh-copy-idto automatically handle directory creation and permissions on the remote server.Terminal window ssh-copy-id -i ~/.ssh/id_{{HOSTNAME_VAR}} {{USERNAME_VAR}}@{{SERVER_IP_VAR}}
-
-
Server security (host)
Section titled “Server security (host)”Once your key is verified, disable password logins to prevent brute-force attacks.
-
Install SSH server
Section titled “Install SSH server”Ensure the OpenSSH server is installed and active.
Terminal window sudo apt install openssh-server -y && sudo systemctl enable --now ssh -
Configure SSH daemon
Section titled “Configure SSH daemon”Edit the server configuration to enforce key-based authentication.
Terminal window sudo nano /etc/ssh/sshd_config/etc/ssh/sshd_config # Disable password authPasswordAuthentication no# Allow using SSH public/private keysPubkeyAuthentication yes# Prevent root login entirelyPermitRootLogin no# Custom SSH port (optional)Port 2222# Only allow specific users (optional)AllowUsers {{USERNAME_VAR}} $USERNAME2 $USERNAME3 -
Verify and restart
Section titled “Verify and restart”Always check the status before exiting to ensure you aren’t locked out.
Terminal window sudo systemctl restart ssh && systemctl status ssh
-
-
SSH client (local)
Section titled “SSH client (local)”Simplify your workflow by creating aliases. This allows you to type
ssh $ALIAS_NAMEinstead of remembering IPs and key paths.-
Edit config
Section titled “Edit config”Open (or create) the SSH config file on your local machine.
Terminal window nano ~/.ssh/config -
Add host alias
Section titled “Add host alias”Define your server details. Now you can connect using just the alias name.
~/.ssh/config # Default settings for all hostsHost *ServerAliveInterval 60ServerAliveCountMax 5# Example hostHost {{HOSTNAME_VAR}}HostName {{SERVER_IP_VAR}}User {{USERNAME_VAR}}Port 22IdentityFile ~/.ssh/id_{{HOSTNAME_VAR}} -
Connect via alias
Section titled “Connect via alias”Test the new configuration.
Terminal window ssh {{HOSTNAME_VAR}}
-
-
SSH authorized_keys (optional)
Section titled “SSH authorized_keys (optional)”Use a system-wide authorized keys directory to manage SSH keys for multiple users from a single location.
-
SSH serivce
Section titled “SSH serivce”Ensure SSH is enabled as a system service.
Terminal window sudo systemctl enable ssh --now -
Global keys directory
Section titled “Global keys directory”Create a global keys directory.
Terminal window sudo mkdir -p /etc/ssh/authorized_keys -
Copy authorized keys
Section titled “Copy authorized keys”Copy your authorized keys file and set strict permissions.
Terminal window sudo cp ~/.ssh/authorized_keys /etc/ssh/authorized_keys/$USERTerminal window sudo chown root:root /etc/ssh/authorized_keys/$USERTerminal window sudo chmod 644 /etc/ssh/authorized_keys/$USER -
Add AuthorizedKeysFile
Section titled “Add AuthorizedKeysFile”Open (or create) the global SSH config file on your local machine.
Terminal window sudo nano /etc/ssh/sshd_configAdd or update this line in config.
/etc/ssh/sshd_config AuthorizedKeysFile /etc/ssh/authorized_keys/%u .ssh/authorized_keys -
Restart SSH
Section titled “Restart SSH”Test configuration and restart the SSH service.
Terminal window sudo sshd -t && sudo systemctl restart ssh
-