Authentification SSH multi-facteur avec OATH (OTP / HOTP) et clés publiques SSH

De Linux Server Wiki
Aller à la navigation Aller à la recherche


apt install libpam-oath oathtool

Générez un secret hexadécimal

$ head -10 /dev/urandom | sha512sum | cut -b 1-16
234197a7d5ef5c16

/etc/users.oath :

# Option User Prefix Seed
HOTP/T30/6 user - 234197a7d5ef5c16

Sécurisez l'accès aux clés secrètes :

chmod 600 /etc/users.oath
chown root:root /etc/users.oath

To enable oath for a specific service only, like ssh, you can edit the file /etc/pam.d/sshd and add at the beginning of the file the following line: Pour activer l'authentification oath pour ssh, ajoutez cette ligne au début du fichier /etc/pam.d/sshd :

auth required pam_oath.so usersfile=/etc/users.oath window=30 digits=6

Puis désactivez l'authentification par password unix en commentant la ligne @include common-auth :

#@include common-auth

Note : si vous ne commentez pas cette ligne, vous devrez entrer, en plus de l'OTP, le password unix pour vous loguer

Modifiez la configuration du serveur openssh :

ChallengeResponseAuthentication yes
UsePAM yes
PermitRootLogin yes
AuthenticationMethods publickey,keyboard-interactive
PasswordAuthentication no

Notez qu'on passe PermitRootLogin yes (par défaut c'est without-password). Si vous ne vous loguerez pas directement en root, passez PermitRootLogin no.

Pour utiliser une application TOTP, il faut convertire notre secret hexadecimal en base32 :

$ oathtool -v --totp -d6 234197a7d5ef5c16
Hex secret: 234197a7d5ef5c16
Base32 secret: ENAZPJ6V55OBM===
Digits: 6
Window size: 0
Step size (seconds): 30
Start time: 1970-01-01 00:00:00 UTC (0)
Current time: 2021-06-05 13:59:52 UTC (1622901592)
Counter: 0x339734F (54096719)

384979

Vous pouvez ensuite générer un QRcode avec qrencode, en veillant bien à passer le secret Base32 secret récupéré avec la commande précédente :

qrencode -t UTF8 'otpauth://totp/user@machine?secret=ENAZPJ6V55OBM==='