Désactiver l'authentification par mot de passe pour SSH


127

Je recherche un moyen de désactiver l'accès des clients SSH à l'invite de mot de passe, comme indiqué ici .

Je ne parviens pas à désactiver l' password:invite de connexion root . J'ai changé le sshd_configfichier pour lire:

ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

et ont également modifié les autorisations chmod 700 ~/.sshet chmod 600 ~/.ssh/authorized_keys. Qu'est-ce que je rate? Cela nécessite-t-il une phrase secrète?

Vidage détaillé:

debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/user/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/user/.ssh/id_dsa
debug1: Trying private key: /home/user/.ssh/id_ecdsa
debug1: Next authentication method: password

Fichier / etc / ssh / sshd_config:

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile    %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM no

ssh -v user @ server pour obtenir une sortie détaillée pour vous aider à résoudre les problèmes.
— Niall Byrne

Les deux ssh_configet sshd_configayez PasswordAuthentication- restez à l'affût!
— Brent Bradburn du

Réponses:


199

Dans le fichier /etc/ssh/sshd_config

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication no

Décommentez la deuxième ligne et, si nécessaire, remplacez oui par non.

Puis cours

service ssh restart

7
De nombreux guides suggèrent de changer ChallengeResponseAuthentication, PasswordAuthentication, UsePAM de oui à non sur le serveur. Testé avec succès sous Debian / Ubuntu.

9
Pour info, la façon systemd de redémarrer le service est la suivante: systemctl restart sshdIl s'agit de la valeur par défaut sur Ubuntu plus récent.
— therealjumbo

Comment ajouter de nouvelles clés au serveur alors que l'authentification par mot de passe est désactivée? Il existe sûrement un moyen de le faire, sans avoir à activer l'authentification par mot de passe, chaque fois que quelqu'un veut ajouter une clé ...
— Matthieu

1
@Matthew Peut-être en utilisantssh-copy-id
— iamyojimbo

@iamyojimbo mais vous avez toujours besoin d'une clé autorisée pour pouvoir vous connecter et en envoyer une avec cette commande.
— Matthieu

16

Courir

service ssh restart

au lieu de

/etc/init.d/ssh restart

Cela pourrait fonctionner.


Cela a fonctionné pour moi; des idées pourquoi utiliser init.d n'a pas?
— Seb

1
Cela dépend de votre saveur Linux ... Sur Gentoo, les deux fonctionnent pour moi.
— Gene Pavlovsky

15

Voici un script pour le faire automatiquement

# Only allow key based logins
sed -n 'H;${x;s/\#PasswordAuthentication yes/PasswordAuthentication no/;p;}' /etc/ssh/sshd_config > tmp_sshd_config
cat tmp_sshd_config > /etc/ssh/sshd_config
rm tmp_sshd_config

3

J'ai suivi ces étapes (pour Mac).

En /etc/ssh/sshd_configchangement

#ChallengeResponseAuthentication yes
#PasswordAuthentication yes

à

ChallengeResponseAuthentication no
PasswordAuthentication no

Générez maintenant la clé RSA:

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

(Pour moi, une clé RSA fonctionnait. Une clé DSA ne fonctionnait pas.)

Une clé privée sera générée ~/.ssh/id_rsaavec ~/.ssh/id_rsa.pub(clé publique).

Passez maintenant au dossier .ssh :cd ~/.ssh

Entrée rm -rf authorized_keys(parfois plusieurs touches entraînent une erreur).

Entrer vi authorized_keys

Entrez :wqpour enregistrer ce fichier vide

Entrer cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Redémarrez le SSH:

sudo launchctl stop com.openssh.sshd
sudo launchctl start com.openssh.sshd

Ne pouvez-vous pas utiliser touchpour créer le fichier vide authorized_keys?
— Peter Mortensen

-8

Ceci est d'un tutoriel que j'ai:

Sur votre machine en tant que root:

ssh-keygen -t rsa

Et laissez la phrase secrète vide.

Depuis votre machine locale (et vous devrez taper un mot de passe):

ssh root@remotemachine mkdir -p .ssh

Ensuite:

cat .ssh/id_rsa.pub | ssh root@remotemachine 'cat >> .ssh/authorized_keys'

Vous pouvez maintenant vous connecter sans invite de mot de passe

ssh root@remotemachine

Voir la connexion SSH sans mot de passe en cas de problème!


2
Je cherche à empêcher l'invite de mot de passe de s'afficher dans toutes les circonstances. Forcer la connexion via la clé ssh uniquement
— Matt Stokes

Cela devrait le faire. Il s'ouvrira directement sans vous demander de mot de passe mais à partir d'un client spécifique.
— Fabrizio Mazzoni

1
oui mais cela permettra également aux clients qui ne sont pas autorisés à tenter un mot de passe, n'est-ce pas?
— Matt Stokes

Modifiez ce qui suit: ChallengeResponseAuthentication no, PasswordAuthentication no, UsePAM no Cela autorisera uniquement l'authentification par clé. Je viens de tester mon système et il refusera avec: Autorisation refusée (clé publique)
— Fabrizio Mazzoni

1
Vous n'avez pas décommenté la section PasswordAuthentication no si le fichier que vous avez posté est la configuration réelle
— Fabrizio Mazzoni
En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.