服务器配置免密登录(系统免密登录)
110
2022-10-19
本文目录一览:
1.Linux下生成密钥
ssh-keygen的命令手册,通过”man ssh-keygen“命令:
通过命令”ssh-keygen -t rsa“
生成之后会在用户的根目录生成一个 “.ssh”的文件夹
进入“.ssh”会生成以下几个文件
authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥
id_rsa : 生成的私钥文件
id_rsa.pub :生成的公钥文件
know_hosts : 已知的主机公钥清单
如果希望ssh公钥生效需满足至少下面两个条件:
1) .ssh目录的权限必须是700
2) .ssh/authorized_keys文件权限必须是600
2.远程免密登录
原理图:
常用以下几种方法:
2.1 通过ssh-copy-id的方式
命令: ssh-copy-id -i ~/.ssh/id_rsa.put romte_ip
举例:
[root@test .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135
root@192.168.91.135's password:
Now try logging into the machine, with "ssh '192.168.91.135'", andcheck in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@test .ssh]# ssh root@192.168.91.135
Last login: Mon Oct 10 01:25:49 2016 from 192.168.91.133
[root@localhost ~]#
常见错误:
[root@test ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.91.135
-bash: ssh-copy-id: command not found //提示命令不存在
解决办法:yum -y installopenssh-clients
2.2 通过scp将内容写到对方的文件中
命令:scp -p~/.ssh/id_rsa.pub root@remote_ip:/root/.ssh/authorized_keys
举例:
[root@test .ssh]# scp -p ~/.ssh/id_rsa.pubroot@192.168.91.135:/root/.ssh/authorized_keys
root@192.168.91.135's password:
id_rsa.pub 100% 408 0.4KB/s 00:00
[root@test .ssh]#
[root@test .ssh]#
[root@test .ssh]#
[root@test .ssh]# ssh root@192.168.91.135
Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133
[root@localhost ~]#
也可以分为两步操作:
$ scp ~/.ssh/id_rsa.pubroot@remote_ip:pub_key //将文件拷贝至远程服务器
$ cat ~/pub_key~/.ssh/authorized_keys //将内容追加到authorized_keys文件中,不过要登录远程服务器来执行这条命令
2.3 通过Ansible实现批量免密
2.3.1 将需要做免密操作的机器hosts添加到/etc/ansible/hosts下:
[Avoid close]
192.168.91.132
192.168.91.133
192.168.91.134
2.3.2 执行命令进行免密操作
ansiblegroupname -m authorized_key -a "user=root key='{{lookup('file','/root/.ssh/id_rsa.pub') }}'" -k
示例:
[root@test sshpass-1.05]# ansible test -m authorized_key -a"user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k
SSH password: -----输入密码
192.168.91.135 | success {
"changed": true,
"key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVP PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug/hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SMWQmpKUltj2nltuv3w== root@localhost.localdomain",
"key_options": null,
"keyfile": "/root/.ssh/authorized_keys",
"manage_dir": true,
"path": null,
"state": "present",
"unique": false,
"user": "root"
}
[root@test sshpass-1.05]#
2.4 手工复制粘贴的方式
将本地id_rsa.pub文件的内容拷贝至远程服务器的~/.ssh/authorized_keys文件中
1
[root@localhost .ssh]# /usr/bin/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:
30:f6:d7:2a:ac:56:eb:3f:fa:40:25:8d:90:96:68:cb
root@localhost.localdomain
在主机/.ssh目录下生成密匙
使用ssh的rsa密匙
其中
id_rsa 私匙
id_rsa.pub 公匙
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1
2
[root@localhost .ssh]# scp /root/.ssh/id_rsa.pubroot@172.16.142.5:/root/.ssh/authorized_keys
The authenticity of host '172.16.142.5 (172.16.142.5)' can't be established.
RSA key fingerprint is 4b:a5:74:fb:2e:08:60:af:fa:76:d4:b0:26:4c:13:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.142.5' (RSA) to the list of known hosts.
root@172.16.142.5's password:
id_rsa.pub 100% 236 0.2KB/s 00:00
说明:
将公钥拷贝到远端主机,并写入授权列表文件
你也可以把公钥文件拷贝过去后,在远端主机下直接执行
touch /root/.ssh/authorized_keys
cat /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
3
操作完毕,登陆检查。
[root@localhost .ssh]# ssh 172.16.142.5
root@172.16.142.5's password:
请注意此时如果仍提示输入密码,请检查如下文件夹和文件的操作权限,这是非常重要的, 否则ssh公钥认证体制不能正常工作:
172.16.142.4(客户端)
/home/root文件夹的权限是600
/home/root/.ssh文件夹的权限是600 (好像这个权限关系不是很大)
/home/root/.ssh/id_dsa私钥的权限600
172.16.142.5(远端主机)
/home/root文件夹的权限是644
/home/root/.ssh文件夹的权限是644 (好像这个权限关系不是很大)
/root/.ssh/authorized_keys公钥的权限644
4
[root@localhost ~]# ssh 172.16.142.5
Last login: Sat Dec 15 21:10:17 2007 from 172.16.142.4
[root@localhost ~]#
无密码SSH登陆成功!
作为系统管理员,你计划在 Linux 上使用 OpenSSH,完成日常工作的自动化,比如文件传输、备份数据库转储文件到另一台服务器等。为实现该目标,你需要从主机 A 能自动登录到主机 B。自动登录也就是说,要在 shell 脚本中使用ssh,而无需要输入任何密码。
本文会告诉你怎样在 CentOS/RHEL 上设置 SSH 免密码登录。自动登录配置好以后,你可以通过它使用 SSH (Secure Shell)和安全复制 (SCP)来移动文件。
SSH 是开源的,是用于远程登录的最为可靠的网络协议。系统管理员用它来执行命令,以及通过 SCP 协议在网络上向另一台电脑传输文件。
通过配置 SSH 免密码登录,你可以享受到如下的便利:
用脚本实现日常工作的自动化。
增强 Linux 服务器的安全性。这是防范虚拟专用服务器(VPS)遭受暴力破解攻击的一个推荐的方法,SSH 密钥单凭暴力破解是几乎不可攻破的。
什么是 ssh-keygen
ssh-keygen 是一个用来生成、创建和管理 SSH 认证用的公私钥的工具。通过 ssh-keygen 命令,用户可以创建支持SSH1 和 SSH2 两个协议的密钥。ssh-keygen 为 SSH1 协议创建 RSA 密钥,SSH2 则可以是 RSA 或 DSA。
什么是 ssh-copy-id
ssh-copy-id 是用来将本地公钥拷贝到远程的 authorized_keys 文件的脚本命令,它还会将身份标识文件追加到远程机器的 ~/.ssh/authorized_keys 文件中,并给远程主机的用户主目录适当的的权限。
SSH 密钥
SSH 密钥为登录 Linux 服务器提供了更好且安全的机制。运行 ssh-keygen 后,将会生成公私密钥对。你可以将公钥放置到任意服务器,从持有私钥的客户端连接到服务器的时,会用它来解锁。两者匹配时,系统无需密码就能解除锁定。
在 CentOS 和 RHEL 上设置免密码登录 SSH
以下步骤在 CentOS 5/6/7、RHEL 5/6/7 和 Oracle Linux 6/7 上测试通过。
节点1 : 192.168.0.9 节点2 : 192.168.l.10
步骤1 :
测试节点1到节点2的连接和访问:
[root@node1 ~]# ssh root@192.168.0.10The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.RSA key fingerprint is 6d:8f:63:9b:3b:63:e1:72:b3:06:a4:e4:f4:37:21:42.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.0.10' (RSA) to the list of known hosts.root@192.168.0.10's password:Last login: Thu Dec 10 22:04:55 2015 from 192.168.0.1[root@node2 ~]#
步骤二:
使用 ssh-key-gen 命令生成公钥和私钥,这里要注意的是可以对私钥进行加密保护以增强安全性。
[root@node1 ~]# ssh-keygenGenerating 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:b4:51:7e:1e:52:61:cd:fb:b2:98:4b:ad:a1:8b:31:6d root@node1.ehowstuff.localThe key's randomart image is:+--[ RSA 2048]----+| . ++ || o o o || o o o . || . o + .. || S . . || . .. .|| o E oo.o || = ooo. || . o.o. |+-----------------+
步骤三:
用 ssh-copy-id 命令将公钥复制或上传到远程主机,并将身份标识文件追加到节点2的 ~/.ssh/authorized_keys 中:
[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.10root@192.168.0.10's password:Now try logging into the machine, with "ssh '192.168.0.10'", and check in:.ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.
步骤四:
验证免密码 SSH 登录节点2:
[root@node1 ~]# ssh root@192.168.0.10Last login: Sun Dec 13 14:03:20 2015 from
我希望这篇文章能帮助到你,为你提供 SSH 免密码登录 CentOS / RHEL 的基本认知和快速指南。
在CentOS/RHEL上设置SSH免密码登录
导读 作为系统管理员,你计划在 Linux 上使用 OpenSSH,完成日常工作的自动化,比如文件传输、备份数据库转储文件到另一台服务器等。为实现该目标,你需要从主机 A 能自动登录到主机 B。自动登录也就是说,要在 shell 脚本中使用ssh,而无需要输入任何密码。
本文会告诉你怎样在 CentOS/RHEL 上设置 SSH 免密码登录。自动登录配置好以后,你可以通过它使用 SSH (Secure Shell)和安全复制 (SCP)来移动文件。
SSH 是开源的,是用于远程登录的最为可靠的网络协议。系统管理员用它来执行命令,以及通过 SCP 协议在网络上向另一台电脑传输文件。
通过配置 SSH 免密码登录,你可以享受到如下的便利:
◆用脚本实现日常工作的自动化。
◆增强 Linux 服务器的安全性。这是防范虚拟专用服务器(VPS)遭受暴力破解攻击的一个推荐的方法,SSH 密钥单凭暴力破解 是几乎不可攻破的。
什么是 Ssh-Keygen
ssh-keygen 是一个用来生成、创建和管理 SSH 认证用的公私钥的工具。通过 ssh-keygen 命令,用户可以创建支持SSH1 和 SSH2 两个协议的密钥。ssh-keygen 为 SSH1 协议创建 RSA 密钥,SSH2 则可以是 RSA 或 DSA。
什么是 Ssh-Copy-Id
ssh-copy-id 是用来将本地公钥拷贝到远程的 authorized_keys 文件的脚本命令,它还会将身份标识文件追加到远程机器的 ~/.ssh/authorized_keys 文件中,并给远程主机的用户主目录适当的的权限。
SSH 密钥
SSH 密钥为登录 Linux 服务器提供了更好且安全的机制。运行 ssh-keygen 后,将会生成公私密钥对。你可以将公钥放置到任意服务器,从持有私钥的客户端连接到服务器的时,会用它来解锁。两者匹配时,系统无需密码就能解除锁定。
在 CentOS 和 RHEL 上设置免密码登录 SSH
以下步骤在 CentOS 5/6/7、RHEL 5/6/7 和 Oracle Linux 6/7 上测试通过。
节点1:192.168.0.9 节点2 : 192.168.0.10
本章目录结构 [收起]
步骤一 :
步骤二:
步骤三:
步骤四:
步骤一 :
测试节点1到节点2的连接和访问:
[root@node1 ~]# ssh root@192.168.0.10
The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.
RSA key fingerprint is 6d:8f:63:9b:3b:63:e1:72:b3:06:a4:e4:f4:37:21:42.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.10' (RSA) to the list of known hosts.
root@192.168.0.10's password:
Last login: Thu Dec 10 22:04:55 2015 from 192.168.0.1
[root@node2 ~]#
步骤二:
使用 ssh-key-gen 命令生成公钥和私钥,这里要注意的是可以对私钥进行加密保护以增强安全性。
[root@node1 ~]# ssh-keygen
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:
b4:51:7e:1e:52:61:cd:fb:b2:98:4b:ad:a1:8b:31:6d root@node1.ehowstuff.local
The key's randomart image is:
+--[ RSA 2048]----+
| . ++ |
| o o o |
| o o o . |
| . o + .. |
| S . . |
| . .. .|
| o E oo.o |
| = ooo. |
| . o.o. |
+-----------------+
步骤三:
用 ssh-copy-id 命令将公钥复制或上传到远程主机,并将身份标识文件追加到节点2的 ~/.ssh/authorized_keys 中:
[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.10
root@192.168.0.10's password:
Now try logging into the machine, with "ssh '192.168.0.10'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
步骤四:
验证免密码 SSH 登录节点2:
[root@node1 ~]# ssh root@192.168.0.10
Last login: Sun Dec 13 14:03:20 2015 from
百度搜索《linux就该这么学》,技术干货里面有,也可以看下这本书!
首先需要在服务器端设置/etc/ssh/sshd_config
# vim /etc/ssh/sshd_config
修改如下两行为yes。其实大多数情况下不用修改,默认就是yes。
RSAAuthentication yes
PubkeyAuthentication yes
(1) 如果客户机和服务器都是Linux机器,那么我们使用下面的方法:(后面第2节会提到怎么在Windows下使用Putty生成密钥对)
我们需要在客户端生成RSA密钥对。使用ssh-keygen命令:
# ssh-keygen -t rsa
参数t的意思是type,后面跟着加密类型,这里我们是rsa。
然后会提示你输入密钥保存完成文件名,这里我们需要使用默认的id_rsa,之后才能正常才能登录。如果你生成的密钥作为其他用处,那么可以命名为其他名称:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/cake/.ssh/id_rsa):
之后会提示你输入一个passphrase,我们这里可以留空,这样我们登录的时候就不许输入密码。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
然后会提示你密钥生成成功。这是你的私钥保存为~/.ssh/id_rsa,你的公钥是~/.ssh/id_rsa.pub
我们现在需要做的是,把id_rsa.pub的内容,添加的服务器端的~/.ssh/autherized_keys文件最后。
你可以把这个文件上传到服务器端,然后使用命令:
# cat id_rsa.pub ~/.ssh/autherized_keys
到这里就完成了。
(2) 在Windows下使用Putty生成密钥对:
Putty的安装目录下有个puttygen.exe程序,我们运行这个程序。
之后点击Generate,开始生成密钥对。我们需要根据提示,在指定方框内随机滑动鼠标。这是为了根据鼠标轨迹,产生一些随机数据。
之后生成结束,我们点击Save Private Key将私钥存放在某个目录中。然后赋值最上面文本框中的全部内容,粘贴到Linux服务器端的autherized_key的最后。
我们现在可以关闭这个小程序。
现在打开Putty,在左边的选项中,选择Conneciton–SSH–Auth,在Private key file for authentication中,选择刚才保存的私钥路径就可以了。
到此位置,Putty也可以不用密码登录了。
这要看你通过什么方式登录了,有些就不需要密码就能登录例如黑客程序等,一般都需要密码认证比较安全一点,自己程序也可以,自己开端口,就不需要密码登录了
服务器配置免密登录的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于服务器配置免密登录什么意思、服务器配置免密登录的信息别忘了在维启网络进行查找喔。
发表评论
暂时没有评论,来抢沙发吧~