USB SSH System Backup
This file contains the full working USB SSH system configuration.
USB SSH Script
File: /home/student/.local/bin/usb-ssh.sh
#!/bin/bash
USB_KEY=$(for d in /media/$USER/*; do
[ -f "$d/id_rsa" ] && echo "$d/id_rsa" && break
done)
LOCAL_KEY="$HOME/.ssh/id_rsa_usb"
mkdir -p "$HOME/.ssh"
# IMPORTANT: use existing session agent only
export SSH_AUTH_SOCK=/run/user/$(id -u)/keyring/ssh
if [ -n "$USB_KEY" ]; then
cp "$USB_KEY" "$LOCAL_KEY"
chmod 600 "$LOCAL_KEY"
ssh-add "$LOCAL_KEY" 2>/dev/null || true
else
ssh-add -d "$LOCAL_KEY" 2>/dev/null || true
fi
exit 0
Systemd Service
File: /home/student/.config/systemd/user/usb-ssh.service
[Unit]
Description=USB SSH Key Handler
[Service]
Type=oneshot
ExecStart=/home/student/.local/bin/usb-ssh.sh
Systemd Path Unit
File: /home/student/.config/systemd/user/usb-ssh.path
[Unit]
Description=Watch USB mount for SSH key
[Path]
PathChanged=/media/%u
[Install]
WantedBy=default.target
Notes
- Uses systemd user services
- Works with USB mounted under /media/$USER
- Requires SSH agent session
- Event-driven (no polling loops)