Linux配置SSH免密码登录
程序员文章站
2022-04-30 12:07:45
...
我们如果在Linux上通过ssh命令去登录另一台Linux机器,正常情况下,需要输入另一台机器的密码,但是如果我们需要频繁的登录的时候,每次都输入密码不免得很繁琐,所以这里对配置ssh免密码登录做一下记录。
1. 准备环境
- 系统环境: rhel-server-7.4-x86_64
这里演示的是一个Linux系统通过ssh命令登录到本机上(多台机器原理和步骤也是一样的),Linux系统已经在 /etc/hosts文件中配置了主机名 bigdata111
2. 配置步骤
- 刚开始,如果我们不做任何配置,那么我们通过ssh命令登录到Linux系统需要输入密码:
[[email protected] ~]# ssh bigdata111
[email protected]'s password:
Last login: Mon Jul 15 21:32:06 2019 from 192.168.2.1
[[email protected] ~]#
- 对需要免密码登录其他机器的Linux系统生成**对(谁需要免密码登录,就谁生成**对,假设服务器A需要登录服务器B的时候不需要输入密码,那么就是服务器A需要生成**对),这边通过ssh-****** -t rsa命令生成**对,一路回车即可
[[email protected] ~]# ssh-****** -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:Zywosk7DO6rD6AcV2weNS5AjF6mlxSudvOtQ07aGdYw [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| .o= o |
| . X + . |
| X O o |
| + B.ooo . |
| +ooEooS + |
| o.+=.o + |
|o.*..o |
|o+o=. |
|=+=o |
+----[SHA256]-----+
[[email protected] ~]#
上面命令执行成功后,生成的**对会在**/root/.ssh**目录下,
[[email protected] .ssh]# pwd
/root/.ssh
[[email protected] .ssh]# ll
total 12
-rw-------. 1 root root 1679 Jul 15 21:36 id_rsa
-rw-r--r--. 1 root root 397 Jul 15 21:36 id_rsa.pub
-rw-r--r--. 1 root root 526 Jul 14 06:34 known_hosts
[[email protected] .ssh]#
- 将生成的公钥发送给目标机器(需要免密码登录到哪台机器上),在这个过程,需要输入目标机器的密码
[[email protected] ~]# ssh-copy -i .ssh/id_rsa.pub [email protected]
bash: ssh-copy: command not found...
[[email protected] ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[[email protected] ~]#
发送成功之后,公钥会被保存在.ssh/authorized_keys文件中
[[email protected] ~]# cd .ssh/
[[email protected] .ssh]# ll
total 16
-rw-------. 1 root root 397 Jul 15 21:42 authorized_keys
-rw-------. 1 root root 1679 Jul 15 21:36 id_rsa
-rw-r--r--. 1 root root 397 Jul 15 21:36 id_rsa.pub
-rw-r--r--. 1 root root 526 Jul 14 06:34 known_hosts
[[email protected] .ssh]# pwd
/root/.ssh
[[email protected] .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHaiWaO/X4CQuSHX7brpRoiH9NZ9sTD3By+meSeCUjLeGYYLSNyB6mzbPxSlSYZLmktD7wwR33cEdyH75vca4qyhruqB6JeUerE7VFOUS+jT4mVNxctWeiCxnMNRYQyfgNlwoTbgSROKzeiaQduW0yHiWkWnjzqjq913OFh16jQKKvDyNpoI0ltW/i2qopGshBZwETaSzdVVdL+3FzX4DL4O9mb9NC3XHH5JrQG5xmsvnWdpWT/SPyOUBpJryTFGeQ46zeekQMJXfUyXDp/8tTD1dtsSorGV20wEJuoO/Z+IQYik36DdExH5mRS7nB6QEm4tUyDcJU5mXy/cfHehAN [email protected]
[[email protected] .ssh]#
- 经过上述配置,免密码登录配置完成,现在我们再执行ssh bigdata111执行远程登录时,发现不用再输入密码了
[[email protected] .ssh]# ssh bigdata111
Last login: Mon Jul 15 21:33:47 2019 from 192.168.2.111
[[email protected] ~]#