Jots
  1. ssh server hardening #/etc/ssh/ss…Oct 29
  2. ssh unavailable until user login …Oct 29
  3. to secure an SSH server: root log…Oct 28
  4. Situation: ssh-copy-id succeeds, …Oct 25

ssh

ssh server hardening

#/etc/ssh/sshd_config.d/10-hardening.conf

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
AllowUsers elias ansible

ssh unavailable until user login

After rebooting a server, I couldn't SSH in until manually logging in on the device itself... everything changed when nmcli attacked:

sudo nmcli -f NAME,UUID,TYPE,DEVICE connection show   # list connections; grab UUID 
nmcli -g connection.permissions connection show <UUID>     # if `user\:<user>`, restricted
sudo nmcli connection modify <UUID> connection.permissions ""
sudo nmcli connection modify <UUID> connection.autoconnect yes

to secure an SSH server:

  • root login disabled
  • key based auth for others
  • fail2ban
  • block IP from some countries at firewall level (China, North Korea, etc)

from here

Situation: ssh-copy-id succeeds, but password still required to connect:

Triage:

To verify that the fingerprint of the local public key matches to a corresponding fingerprint in the remote ~/.ssh/authorized_keys file, run ssh-keygen -lf ~/.ssh/authorized_keys on the remote device, then ssh-keygen -lf ~/.ssh/id_<YOUR_KEY>.pub on the local device. There should be a match if ssh-copy-id succeeded.

Root Cause

The issue is not the key's content but the permissions of the key files. Those issues may exist on the remote or the local device.

Resolution

This script (written by Claude) will assert standard SSH configuration permissions for necessary files:

#!/usr/bin/env bash

set -euo pipefail

ssh_dir="$HOME/.ssh"

if [[ ! -d "$ssh_dir" ]]; then
    printf 'Error: %s does not exist\n' "$ssh_dir" >&2
    exit 1
fi

chmod go-w "$HOME"
chmod 700 "$ssh_dir"

# Normalize ownership of the SSH directory and its contents.
chown -R "$(id -un):$(id -gn)" "$ssh_dir"

# Normalize common SSH authentication files if present.
for file in authorized_keys; do
    if [[ -f "$ssh_dir/$file" ]]; then
        chmod 600 "$ssh_dir/$file"
    fi
done

printf 'SSH directory permissions normalized for %s\n' "$USER"

Annotation