在Linux中使用PSSH(无密码)执行SSH公钥身份验证
SSH公钥认证。
sshauthorized_keys。
SSH密钥。
ssh密钥登录。
linux启用ssh公钥认证。
生成ssh密钥并将ssh密钥添加到服务器。
ssh使用authorized_keys使用私钥登录而不使用密码。
将公用密钥添加到服务器。
如何使用私钥进行ssh。
没有密码的Linux登录。
创建ssh密钥。
ssh添加密钥。
ssh keygen。
使用ssh键登录linux服务器。
ssh密钥存储其中。
ssh使用公钥centos。
rhel ssh公钥认证。
rhel ssh密钥交换。
登录到Linux服务器,无需密码centos。
PSSH是一种实用程序,用于从一台服务器并行执行到多个客户端节点的SSH并执行定义的某些任务。
默认情况下,PSSH具有-A参数,该工具将使用该参数提示输入密码,该密码将用于连接到所有目标主机。
但是我们也可以将PSSH配置为使用SSH公钥身份验证。
我们可以使用SSH密钥登录到多个Linux服务器,使用密码短语或者密码更少(无密码)。
-A --askpass Prompt for a password and pass it to ssh. The password Jan be used for either to unlock a key or for password authentication. The password is transferred in a fairly secure manner (e.g., it will not show up in argument lists). However, be aware that a root user on your system could potentially intercept the password.
不使用密码登录Linux服务器如何工作?
Secure Shell依赖于一种称为"公钥加密"的技术。
它的作用类似于银行的保管箱:我们需要两个钥匙才能打开该保管箱,或者至少要经过多层安全检查。
对于公共密钥密码学,我们需要两个数学密钥:"公共密钥"和"私有密钥"。
加密数据并将其从一个人发送到另一个人的实际过程需要几个步骤。
我们将使用流行的"Alice and Bob
"比喻,并一次一步地进行此过程,因为他们俩都试图以一种安全的方式相互交流。
"爱丽丝拿到鲍勃的公钥"
"爱丽丝使用鲍勃的公钥以及她的私钥分别对数据进行加密和签名。
"
"爱丽丝将加密的数据发送给鲍勃。
"
"鲍勃获取爱丽丝的公钥。
"
"鲍勃分别使用爱丽丝的公钥和私钥来验证和解密数据。
"
配置SSH公钥身份验证
在以下步骤中,我将为root用户在3个节点之间配置SSH公钥身份验证。
我有3个节点,使用它们我将配置SSH公钥身份验证以不使用密码登录Linux服务器
centos-master
centos-client-1
centos-client-2
其中我将在" centos-master"上生成SSH密钥对,我们将尝试使用该密钥对登录并连接到没有密码的" centos-client"节点(使用ssh公钥身份验证)
SSH通信使用公共密钥加密技术进行保护。
当用户首次使用SSH客户端连接到SSH服务器时,SSH程序会将SSH服务器公共密钥存储在用户的主目录中的文件named_hosts中,该文件位于名为~/ ssh /的隐藏文件夹中。
1.生成SSH密钥对(私有和公共)
第一步将是生成私有和公共ssh密钥。
这里的" centos-master"将是我的主服务器。
在此步骤中,客户端使用特殊实用程序ssh-keygen生成用于与服务器进行身份验证的SSH密钥对。
该实用程序与OpenSSH捆绑在一起,默认情况下会创建一个2048位RSA密钥对。
它支持具有不同密钥长度的RSA和DSA。
建议使用4096位的密钥长度在两台计算机之间建立安全连接。
下图显示了如何创建2048位的RSA(" Rivest-Shamir-Adleman")密钥对:
[root@centos-master ~]# 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: SHA256:WDIWm4i8/UhU/zjiKZrmGVcg5PZj5mzXT4aZd37Gnbs [email protected] The key's randomart image is: +---[RSA 2048]----+ | . o | | + . o = | | * + * o | | . * o = o | | . B + S . | | * * + = | | . B = = + .. ..| | .B o = o +..| | o= . .o Eo| +----[SHA256]-----+
2.将公钥复制到远程Linux机器(authorized_keys)
当我们连接到远程主机时,SSH会根据" authorized_keys"列表验证我们提供的密钥ID。
有一个实用程序ssh-copy-id,它也与OpenSSH捆绑在一起,可用于将密钥复制到远程系统。
默认情况下,它将自动将~/.ssh/id_rsa.pub
文件复制到远程系统中
我们使用ssh-copy-id
是因为它复制了公钥,并且还将公钥添加到authorized_keys
上。
使用-i参数提供公钥的路径。
这个公共密钥将被复制到我们的centos-client-1
节点,公共SSH密钥的内容将被添加到root用户的/root/.ssh/authorized_keys中。
[root@centos-master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@centos-client-1 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@centos-client-1's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@centos-client-1'" and check to make sure that only the key(s) you wanted were added.
同样,我将SSH公钥复制到root用户的" authorized_keys"的" centos-client-2"中。
[root@centos-master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@centos-client-2 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'centos-client-2'" and check to make sure that only the key(s) you wanted were added.
3.配置和保护登录名(sshd_config)
通过修改远程主机上的OpenSSH服务器配置文件/etc/ssh/sshd_config
可以启用其他安全措施。
以下是可以确保SSH登录名安全的一些步骤:
现在,由于我们已在上一节中配置了基于SSH密钥的身份验证,因此我们可以在SSH服务器配置文件中禁用密码身份验证以保护SSH登录名。
在运行SSH服务器的远程主机上编辑SSH守护程序配置文件,并将PasswordAuthentication
指令值设置为no,如下所示:
# vi /etc/ssh/sshd_config PasswordAuthentication no
由于root用户具有不受限制的特权,并且默认情况下在每个Linux系统上都存在,因此禁止通过SSH以root用户身份直接登录。
为了通过SSH保护根用户帐户,我们可以在配置文件中进行以下更改:使用伪指令PermitRootLogin注释该行,如下所示:
#PermitRootLogin yes
通过将伪指令PermitRootLogin的值设置为withwithout-password,在根帐户中仅允许基于密钥的ssh登录,如下所示:
PermitRootLogin without-password
在SSH服务器配置文件/etc/ssh/sshd_config
中进行更改后,重新启动sshd
服务以使应用的更改生效,如下所示:
# systemctl restart sshd
创建SSH身份验证代理(ssh-agent)
但是,我们仍然必须输入在SSH私钥上设置的密码。
如果我们每次要连接到远程主机时都必须执行此操作,则将无法实现设置基于密钥的身份验证的目的。
SSH agent,一个小的守护程序,用于将解锁的SSH私钥保存在内存中。
ssh-agent是一个程序,用于保存用于公钥身份验证的私钥(RSA,DSA,ECDSA和Ed25519)。
" ssh-agent"通常在X会话或者登录会话的开始时启动,而所有其他窗口或者程序都作为" ssh-agent"程序的客户端启动。
[root@centos-master ~]# eval `ssh-agent` ssh-add /root/.ssh/id_rsa Agent pid 4696 Enter passphrase for /root/.ssh/id_rsa: Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
验证过程状态
[root@centos-master ~]# ps -ef | grep ssh-agent root 4696 1 0 21:32 ? 00:00:00 ssh-agent root 4699 4004 0 21:32 pts/0 00:00:00 grep --color=auto ssh-agent
安装PSSH
我们可以从EPEL存储库中获取PSSH rpm
[root@centos-master ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm Preparing... ################################# [100%] Updating/installing... 1:epel-release-7-11 ################################# [100%]
接下来,我们可以使用yum
安装PSSH。
[root@centos-master ~]# yum install pssh
执行并行SSH(PSSH)
现在我们都设置了SSH公钥身份验证,并且无需输入任何密码即可执行PSSH。
另外,我在PSSH中使用了更多SSHD选项来禁用基于密码的登录并选择基于密码的身份验证。
[root@centos-master ~]# pssh -i -H "centos-client-1 centos-client-2" -l root -x "-o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o PreferredAuthentications=publickey -o PubkeyAuthentication=yes" hostname [1] 23:07:10 [SUCCESS] centos-client-2 centos-client-2.example.com [2] 23:07:10 [SUCCESS] centos-client-1 centos-client-1.example.com
如我们所见,我没有使用-A,但是PSSH工具能够在不提示输入任何密码的情况下连接到所有提供的主机。
说明:
完成后,我们必须手动终止由ssh-agent创建的PID。
我们可以使用kill -9 <ssh-agent的PID>