Skip to content

Simplistic fix for problem where Yubikey GPG smartcard on linux fails to function after system restart.

License

Notifications You must be signed in to change notification settings

woodenphone/smartcard-reset

Repository files navigation

smartcard-reset

Simple utility to fix an issue where after a reboot a USB smartcard is not recognized until removed and reinserted.

It just restarts pcscd and then tells GPG to re-learn the smartcard association.

I wrote this becase it was annoying to have to reinsert my smartcard after a reboot.

The udev rule should trigger GPG to relearn the smartcard after plugging it in.

Manual usage

Running the script just once.

Invoke the script via sudo:

$ chmod -v +x ./smartcard-reset.sh
$ sudo ./smartcard-reset.sh USERNAME

Install

Making the script autorun.

Via ansible

  1. Change the username in the playbook to your linux username.
  2. Run the playbook:
$ ansible-playbook smartcard-reset.playbook.yml

Manually

  1. Put files in correct locations:
$ sudo cp -v 'files/smartcard-reset.sh' '/usr/local/bin/smartcard-reset.sh'

$ sudo cp -v 'files/80-yubikey-actions.rules' '/etc/udev/rules.d/80-yubikey-actions.rules'

$ sudo cp -v 'files/smartcard-reset.timer' 'smartcard-reset.timer'
$ sudo cp -v 'files/smartcard-reset.service' '/etc/systemd/system/smartcard-reset.service'

$ sudo chown -v 'o=rwx,g=rwx,o=r' smartcard-reset.timer '/etc/systemd/system/smartcard-reset.service' '/usr/local/bin/smartcard-reset.sh'
  1. Set user to act on in the service file:

$ sudo nano /etc/systemd/system/smartcard-reset.service

ExecStart=/usr/local/bin/smartcard-reset.sh "some-user"
  1. Reload udev rules (optional):
$ sudo udevadm control --reload-rules

Optionally force rules to be run right now:

$ sudo udevadm trigger --attr-match=vendor='Yubico'
  • udev should normally not need either of these and just detect that a file in its config dirs was changed on its own.
  1. Schedule to run:
$ sudo systemctl daemon-reload # Make systemd notice changed config files.
$ sudo systemctl enable smartcard-reset.timer smartcard-reset.service
  1. Manually run (if desired):
$ sudo systemctl start smartcard-reset.service

or

$ sudo /usr/local/bin/smartcard-reset.sh "some-user"

Testing

Check playbook:

$ ansible-playbook smartcard-reset.playbook.yml --syntax-check

$ ansible-playbook smartcard-reset.playbook.yml --check
  • I dont have a viable automated testing methodology for this beyond the playbook running.

Notes

It may be required to use sudo to run as root to install locally (localhost).

This does not currently bother to work for non-yubikey smartcards, even though it should be trivial to detect and handle them too.

Debugging and troubleshooting

# journalctl -b --unit=smartcard-reset.service

Links

About

Simplistic fix for problem where Yubikey GPG smartcard on linux fails to function after system restart.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published