Secure remote access using SSH keys

Last updated on 2021-03-08 Tagged under  #ssh   #network   #homeServer   #debian   #linux 

Create cryptographic keys and disable password logins to make remote machines more secure.

Let's go!

Server is running Debian configured for SSH logins from a Linux client.

1. On the server: Install

Install openssh-server ...

$ sudo apt install openssh-server                                         

Create an SSH configuration in the home directory of users who requires access to the system ...

$ mkdir ~/.ssh && chmod 700 ~/.ssh && touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys

2. On the client: Install

Install openssh-client and create the SSH folder in $HOME ...

$ sudo apt install openssh-client                                             
$ mkdir ~/.ssh && chmod 700 ~/.ssh                                                

Create ~/.ssh/config to hold aliases with the login options for a server.

Example ...

Host laptop-server.lan
Port 22                                                                      
User foo

Test the SSH password login to the server ...

$ ssh laptop-server.lan
foo@'s password: 

3. On the client: Generate keys

$ ssh-keygen -t ed25519 -C "$(whoami)@$(hostname)-$(date -I)" 

Upload the public key to the server and append to ~/.ssh/authorized_keys ...

$ ssh-copy-id -i ~/.ssh/ laptop-server.lan

Notify SSH that you have keys by running ssh-add ...

$ ssh-add
Enter passphrase for /home/foo/.ssh/id_ed25519:
Identity added: /home/foo/.ssh/id_ed25519 (/home/foo/.ssh/id_ed25519)

All SSH sessions launched from this console will access this user key stored in memory.

Make sure to test the connection before disabling password logins ...

$ ssh laptop-server.lan

No request for a passphrase indicates SSH key authentication is properly configured.

4. On the server: Disable password logins

Make the following modifications in /etc/ssh/sshd_config ...

PermitRootLogin no
PubkeyAuthentication yes                                                    
ChallengeResponseAuthentication no                                          
PasswordAuthentication no                                                   
UsePAM no                                                                   

Restart SSH ...

$ sudo systemctl restart ssh

5. On the client: Key management

Keychain is an OpenSSH key manager. From the package description ...

When keychain is run, it checks for a running ssh-agent, otherwise it starts one. It saves the ssh-agent environment variables to ~/.keychain/$HOSTNAME-sh, so that subsequent logins and non-interactive shells such as cron jobs can source the file and make passwordless ssh connections. In addition, when keychain runs, it verifies that the key files specified on the command-line are known to ssh-agent, otherwise it loads them, prompting you for a password if necessary.

Install ...

$ sudo apt install keychain                                             

Configure ~/.bashrc ...

# Use `keychain` for ssh-agent management
if [[ -x /usr/bin/keychain ]]; then
	keychain ~/.ssh/id_ed25519
	. "${HOME}/.keychain/${HOSTNAME}-sh"

Flush all cached keys from memory ...

$ keychain --clear                  

If using tmux, enable persistent SSH key management across sessions by editing ~/.tmux.conf ...

Thanks for reading! Read other posts?

» Later: Automatic upgrades in Debian

« Earlier: Virtualbox on Debian Buster