SSH无密码认证(密钥)配置

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

服务端SSH相关配置文件的修改

修改SSH的配置文件:

[root@localhost ~]# vi /etc/ssh/sshd_config

#Protocol 2,1SSH协议版本:将“#”删除,再将行末的“,1”删除,只允许SSH2方式的连接。

#ServerKeyBits 768密钥强度:将“#”删除,并将768改为1024,将ServerKey强度改为1024比特。

#PermitRootLogin yes 允许以root登录开关:将“#”删除,并将yes改为no。修改为不允许用root进行登录。

#PasswordAuthentication yes密码方式的登录开关:将“#”删除,将yes改为no,修改后不允许密码方式的登录

#PermitEmptyPasswords no空密码登录开关:将“#”删除,不允许空密码登录。

保存并退出。

因为我们只想让SSH服务为管理系统提供方便,所以在不通过外网远程管理系统的情况下,只允许内网客户端通过SSH登录到服务器,以最大限度减少不安全因素。设置方法如下:[root@localhost ~]# vi /etc/hosts.deny←修改屏蔽规则,在文尾添加相应行

#

# hosts.deny This file describes the names of the hosts which are

# *not* allowed to use the local INET services, as decided

# by the '/usr/sbin/tcpd' server.

#

# The portmap line is redundant, but it is left to remind you that

# the new secure portmap uses hosts.deny and hosts.allow. In particular

# you should know that NFS uses portmap!

sshd: ALL←添加这一行,屏蔽来自所有的SSH连接请求[root@localhost ~]# vi

/etc/hosts.allow←修改允许规则,在文尾添加相应行

#

# hosts.allow This file describes the names of the hosts which are

# allowed to use the local INET services, as decided

# by the '/usr/sbin/tcpd' server.

#

sshd: 192.168.0.←添加这一行,只允许来自内网的SSH连接请求

重新启动SSH服务

在修改完SSH的配置文件后,需要重新启动SSH服务才能使新的设置生效。

# /etc/rc.d/init.d/sshd restart

这时,以正常的密码的方式是无法登录服务器的。

SSH客户端公钥与私钥的建立

在客户端(linux)登录为需要建立密钥的用户,基于这个用户建立公钥与私钥。(这里以oracle用户为例)

[root@localhost ~]# su - oracle//登录为一般用户oracle

[oracle@localhost ~]$ ssh-keygen -t rsa //建立公钥与私钥对

Enter file in which to save the key (/home/kaz/.ssh/id_rsa): //私钥的路径,这里保持默认直接回车

Created directory '/home/kaz/.ssh'

Enter passphrase (empty for no passphrase): //私钥的密码(自动登录就设为空)Enter same passphrase again: //再次输入口令

Your identification has been saved in /home/kaz/.ssh/id_rsa. //私钥

Your public key has been saved in /home/kaz/.ssh/id_rsa.pub. //公钥

The key fingerprint is:

tf:rs:e3:7s:28:59:5s:93:fe:33:84:01:cj:65:3b:8e oracle@

将客户端用户公钥导成服务端authorized_keys

copy客户端用户公钥到远程服务器的远程用户的home/.ssh目录下:

[oracle@localhost ~]$ cd ~/.ssh//进入用户SSH配置文件的目录

[oracle@localhost .ssh]$ ls -l

[oracle@localhost .ssh]$ cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys//将用户公钥导成uthorized_keys

[oracle@localhost .ssh]$ rm -f ~/.ssh/id_rsa.pub//删除原来的公钥文件

[oracle@localhost .ssh]$ chmod 400 ~/.ssh/authorized_keys//将新建立的公钥文件属性设置为400

相关文档
最新文档