ssh key登录验证的实现

一:简洁的配置文件
[root@cisco ~]# vi /etc/ssh/sshd_config

Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
KeyRegenerationInterval 3600
ServerKeyBits 768
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 600
PermitRootLogin yes
PubkeyAuthentication yes
AuthorizedKeysFile       .ssh/authorized_keys
RhostsAuthentication no
IgnoreRhosts yes
HostbasedAuthentication no
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
MaxStartups 20
Subsystem        sftp  /usr/libexec/openssh/sftp-server
~

二、使用公钥认证
1、原理:首先由用户生成一对密钥,然后将公钥保存在SSH服务器用户的目录下.ssh子目录中的authorized_key文件里(/root/.ssh/authorized_key).私钥保存在本地计算机.当用户登陆时,服务器检查authorized_key文件的公钥是否与用户的私钥对应,如果相符则允许登入,否则拒绝.由于私钥只有保存在用户的本地计算机中,因此入侵者就算得到用户口令,也不能登陆到服务器.
2、启用公钥认证
修改配置文件/etc/ssh/sshd_config  
将"PasswordAuthentication yes"修改为"PasswordAuthentication  no" ,并重启sshd: /etc/init.d/sshd reload
3、生成密钥
[root@rain ~]# ssh-keygen -t rsa
Generating public/private  rsa key pair.
Enter file in which to save the key  (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter  same passphrase again:
Your identification has been saved in  /root/.ssh/id_rsa.
Your public key has been saved in  /root/.ssh/id_rsa.pub.
The key fingerprint  is:
4d:dd:48:af:76:c2:ba:a8:bc:20:f3:28:1d:6a:28:53  root@rain.fish.com
4、在服务器端将/root/.ssh/id_rsa.pub改名为/root/.ssh/authorized_keys
mv  /root/.ssh/id_rsa.pub  /root/.ssh/authorized_keys
5、将私钥 /root/.ssh/id_rsa拷贝到客户机
linux下可以拷贝到/root/.ssh/就可以直接使用了。
6、到客户端登陆服务器看看

# ssh 192.168.1.107

直接enter就可以登录了。


三、SSH服务的配置
1、配置SSH服务的运行参数,是通过修改配置文件/etc/ssh/sshd_config实现的。
2、因为SSH服务使用默认的配置已经能够很好的工作,如果仅仅提供SSH服务不需要修改。这里只介绍一
些常用的选项。
#Port  22
定义了SSH服务监听的断口号,SSH服务默认使用的端口号是22
#Proctocol  2,1
定义了SSH服务器使用SSH协议的顺序。默认识先使用SSH2协议,如果不成功则使用SSH1协议,为了安全起
见,可以设置只使用SSH2协议。
#ListenAddress  0.0.0.0
定义SSH服务器帮定的IP地址,默认绑定服务器所有可用的IP地址.
#PermitRootLogin  yes
定义是否允许管理员登陆
#PermitEmptyPasswords  no
定义是否允许空密码登陆.

#PasswordAuthentication  no
定义是否使用口令认证方式,如果准备使用公钥认证可以设置为no

设置完成后要重启服务,不然大多数系统都不会直接生效。重启服务方式:

CentOS:/etc/init.d/sshd restart
其它系统类似。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值