SSH 密钥登录linux

作者:Garany 发布于:2015-11-11

禁止wang用户使用密码登录,必须使用密钥登陆 

一、首先生成一对密钥,一把私有密钥和一把公钥
公钥放到远程登录的主机上(~/.ssh/authorized_keys),私钥放置到本地
# useradd wang    #新建用户wang
# passwd wang    #设置wang用户密码
# su wang    #切换到wang用户
# ssh-keygen -t rsa    #生成密钥
Enter file in which to save the key (/home/wang/.ssh/id_rsa):   #选择验证文件保存位置,默认/home/wang/.ssh/id_rsa
Enter passphrase (empty for no passphrase):   #输入秘钥验证密码,最少六位。可直接回车跳过
Enter same passphrase again:   #确认秘钥验证密码
# cd /home/wang/.ssh                #进入家目录
# mv id_rsa.pub authorized_keys        #生成验证文件

二、下载服务器/home/wang/.ssh/id_rsa文件到本地

三、修改ssh配置文件
在sshd_config末尾添加:
# vim /etc/ssh/sshd_config 
#禁止wang用户使用密码登录
Match User wang
PasswordAuthentication no

#禁止一个组使用密码登录
Match Group groupname
PasswordAuthentication no

#只有某用户可以使用密码登录,其他用户都不可以
Match User !username
PasswordAuthentication no

# service sshd restart

四、在ssh客户端登陆的时使用密码无法登陆,必须使用从服务器端下载的id_rsa文件+验证密码才行

我来说说