Skip to content

Instantly share code, notes, and snippets.

@lucaswerkmeister
Created March 19, 2019 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswerkmeister/bf2a52453cc12ea2f86e597c6e77423f to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/bf2a52453cc12ea2f86e597c6e77423f to your computer and use it in GitHub Desktop.
systemd user units to wipe sensitive SSH keys from the SSH agent every night
[Unit]
Description=Wipe sensitive SSH keys from the SSH agent
[Service]
Type=oneshot
ExecStart=/usr/bin/ssh-add -d ${HOME}/.ssh/KEY-NAME-1 ${HOME}/.ssh/KEY-NAME-2 …
# don’t fail if the key wasn’t in the SSH agent in the first place
SuccessExitStatus=1
[Unit]
Description=Wipe sensitive SSH keys from the SSH agent every night
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
@lucaswerkmeister
Copy link
Author

Usage: adjust the ExecStart= line in wipe-ssh-agent.service to list the key(s) you want to wipe, then copy both files into ~/.config/systemd/user/ and run systemctl --user daemon-reload and systemctl --user enable --now wipe-ssh-agent.timer.

This assumes that your user manager has the SSH_AUTH_SOCK environment variable needed to talk to the SSH agent.

@lucaswerkmeister
Copy link
Author

Also, consider adding aliases like the following to your .bashrc:

alias npm='systemctl --user start wipe-ssh-agent && npm'
alias grunt='systemctl --user start wipe-ssh-agent && grunt'
alias composer='systemctl --user start wipe-ssh-agent && composer'
# ...

Or use a more sophisticated solution to protect your system from rogue npm packages and similar issues, such as fresh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment