安装ssh服务
yum -y install openssl openssh-server rpm -qa|grep openssh-server修改配置文件 /etc/ssh/sshd_config,确保以下几项打开:
PermitRootLogin yes RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys开机启动
systemctl enable sshd.service启动服务
systemctl start sshd.service停止服务
systemctl stop sshd.service重启服务
systemctl restart sshd.service生成rsa密钥
在客户端通过ssh-keygen 命令生成密钥,执行如下命令,询问操作时默认回车即可:
[root@localhost ~]# 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: SHA256:wOuQSFvu+Kb4hS4SQl+sGnemDqswWHvczxICwkubYdU [email protected] The keys randomart image is: +---[RSA 2048]----+ |.| | . E | |. o o o| |.B.= + o | |+.X.* . S| |+* X.*.| |=oB O.o. | |+=o=. .o | |=+=+..o| +----[SHA256]-----+ [root@localhost ~]#成功之后,会在~/.ssh目录下产生以下两个文件:
id_rsaid_rsa.pubid_rsa是私钥, id_rsa.pub是公钥。
目录
上传公钥
在客户端(A机)上传本地的公钥到服务端(B机)上,使用如下命令:
scp ~/.ssh/id_rsa.pub [email protected]:~/即将本地的公钥文件上传到了192.168.1.4机器上的root用户的home目录下。
向授权文件中添加公钥
在服务器(B机)上将客户端(A机)添加到服务器的授权文件中,具体命令如下:
[root@localhost ~]# cat id_rsa.pub >> ~/.ssh/authorized_keys添加成功后,可以在客户端(A机)连接服务器,则不需要密码的,效果如下:
Disen:~ apple$ ssh [email protected] Last login: Fri Mar 13 16:46:39 2020 from disen [root@localhost ~]#