如何在CentOS 6/7上为ssh-agent安装钥匙串管理器

时间:2020-01-09 10:41:02  来源:igfitidea点击:

OpenSSH无需提供密码即可向远程系统提供RSA和DSA身份验证。

keychain是一种特殊的bash脚本,旨在使基于密钥的身份验证变得异常便捷和灵活。
与无密码密钥相比,它具有各种安全优势。
如何在CentOS Linux 6.x或7.x上安装钥匙串?
钥匙串是ssh-agent的管理器,通常从CentOS Linux上的~/.bash_profile文件运行。
它允许您的shell和cron作业轻松共享一个ssh-agent进程。
默认情况下,即使您已经退出系统,由钥匙串启动的ssh-agent也会长时间运行,并且将继续运行。
如果要更改此行为,请通过如下所述的--clear和--timeout选项。

本教程将学习如何使用yum命令在CentOS Linux版本6.x或7.x上安装钥匙串管理器。

步骤1启用精神病Repo

执行以下rpm命令以导入gpg密钥:

$ sudo rpm --import http://wiki.psychotic.ninja/RPM-GPG-KEY-psychotic

通过执行以下命令来安装存储库配置:

$ sudo rpm -ivh http://packages.psychotic.ninja/6/base/i386/RPMS/psychotic-release-1.0.0-1.el6.psychotic.noarch.rpm

输出示例:

Retrieving http://packages.psychotic.ninja/6/base/i386/RPMS/psychotic-release-1.0.0-1.el6.psychotic.noarch.rpm
Preparing...                                                            (100%################################# [100%]
Updating / installing...
   1:psychotic-release-1.0.0-1.el6.psy                                  (  2%################################# [100%]

步骤2安装钥匙串

执行以下yum命令以安装钥匙串:

$ sudo yum --enablerepo=psychotic install keychain

输出示例:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos-hn.viettelidc.com.vn
* epel: ftp.cuhk.edu.hk
* extras: mirrors.vinahost.vn
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package keychain.noarch 0:2.8.0-3.el7.psychotic will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
======================================================================
Package     Arch      Version                     Repository    Size
======================================================================
Installing:
keychain    noarch    2.8.0-3.el7.psychotic       psychotic     44 k
 
Transaction Summary
======================================================================
Install  1 Package
 
Total download size: 44 k
Installed size: 97 k
Is this ok [y/d/N]: y
Downloading packages:
keychain-2.8.0-3.el7.psychotic.noarch.rpm        |  44 kB   00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : keychain-2.8.0-3.el7.psychotic.noarch              1/1 
  Verifying  : keychain-2.8.0-3.el7.psychotic.noarch              1/1 
 
Installed:
  keychain.noarch 0:2.8.0-3.el7.psychotic                             
 
Complete!

步骤3使用密码设置SSH密钥

您需要使用密码设置SSH密钥。
语法为:

$ ssh-keygen -t rsa

在提示时分配密码。

步骤4更新您的~/.bash_profile

一旦使用口令配置了OpenSSH密钥,请更新$HOME/.bash_profile文件(这是您的个人初始化文件),并使用文本编辑器(例如vi命令/nano命令/vim命令)对登录BASH shell执行以下操作:

$ vi $HOME/.bash_profile

追加以下内容:

/usr/bin/keychain $HOME/.ssh/id_dsa
source $HOME/.keychain/$HOSTNAME-sh

或在~/.bash_profile中使用eval命令,如下所示:

eval $(/usr/bin/keychain --eval --agents ssh id_rsa)

保存并关闭文件。

步骤4进行测试

使用ssh命令从台式机/笔记本电脑/其他服务器登录,如下所示:

$ ssh root@centos-7-server
$ ssh Hyman@centos-7-server-ip-here

步骤5接下来做什么?

keyhcain已启动并在CentOS 6.x/7.x机器上运行。
现在,您要做的就是将服务器密钥文件$HOME/.ssh/id_rsa.pub追加到其他UNIX/Linux/BSD/macOS服务器中

# ssh-copy-id -i ~/.ssh/id_rsa.pub Hyman@macbookpro
# ssh-copy-id -i ~/.ssh/id_rsa.pub Hyman@x230

输出示例:

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'x230 (192.168.1.21)' can't be established.
ECDSA key fingerprint is SHA256:gz7S2ecz/V4kMkVVH4LZOCyA2J17luVfEUS1uxiHVsg.
ECDSA key fingerprint is MD5:5d:78:32:df:ac:fe:4f:33:4e:02:93:da:99:d4:4b:b5.
Are you sure you want to continue connecting (yes/no)? yes
/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
Hyman@x230's password: 
 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh 'Hyman@x230'"
and check to make sure that only the key(s) you wanted were added.

如何删除所有ssh-agents密钥?

语法为:

keychain --clear

通常,它在.bash_profile中使用。
背后的理论是,除非另外证明,否则钥匙串应该假定您是入侵者。
但是,尽管此选项提高了安全性,但仍允许您的cron作业在您注销后使用ssh密钥。
例如,以下grep命令显示备份服务器条目:

$ grep 'keychain' $HOME/.bash_profile
/usr/bin/keychain --clear $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOSTNAME-sh

如何在几分钟内设置超时时间?

语法为:

keychain --timeout minutes
/usr/bin/keychain --timeout 30 $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOSTNAME-sh

这会传递给ssh-agent,由于密钥链不会连续运行,因此ssh-agent会对密钥进行实际计时。