首页 > Linux操作系统 > Linux操作系统 > Linux SSH主机互信配置
配置ssh互信:这里以 sndw_p 和 sndw_stb01 两台LINUX主机为例:
1.生成公钥私钥对
在两台主机上分别生成,提示输入信息时直接回车:
--sndw_p主机:
[oracle@sndw_p ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/oracle/.ssh/id_rsa):
Created directory '/home/oracle/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/oracle/.ssh/id_rsa.
Your public key has been saved in /home/oracle/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:BoNorHAxpuftxcJcfwhM4Cg6LsF7yoA+wsGVmqI3fzc oracle@sndw_p
The key's randomart image is:
+---[RSA 2048]----+
| + ... |
| + * + |
|+ B + * |
|+B * o = . |
|*.= = o S . |
|+B.. o . . |
|Bo... |
|Bo= . E |
|.=.o.. . . |
+----[SHA256]-----+
[oracle@sndw_p ~]$
[oracle@sndw_p ~]$ cd .ssh/
[oracle@sndw_p .ssh]$ ll
total 8
-rw------- 1 oracle oinstall 1679 May 30 14:51 id_rsa
-rw-r--r-- 1 oracle oinstall 395 May 30 14:51 id_rsa.pub
[oracle@sndw_p .ssh]$
--sndw_stb01主机:
[oracle@sndw_stb01 .ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/oracle/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/oracle/.ssh/id_rsa.
Your public key has been saved in /home/oracle/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:tDSVQ5j9ycf9SJ/wrGoxWUQwYnl1h+65/sVQOK9ckG0 oracle@sndw_stb01
The key's randomart image is:
+---[RSA 2048]----+
| B+++o o.|
| +o=...o+.|
| + .+.+=.E|
| o o +o=*.|
| S o+=+=|
| + .+Oo|
| o +.o|
| . .. .|
| .......|
+----[SHA256]-----+
[oracle@sndw_stb01 .ssh]$
[oracle@sndw_p ~]$ cd .ssh/
[oracle@sndw_p .ssh]$ ll
total 8
-rw------- 1 oracle oinstall 1679 May 30 14:51 id_rsa
-rw-r--r-- 1 oracle oinstall 395 May 30 14:51 id_rsa.pub
[oracle@sndw_p .ssh]$
这样,就创建了公钥和密钥,会生成**id_rsa和id_rsa.pub**两个文件。 生成ssh密钥后,密钥将默认存储在家目录下的**.ssh/目录**中。
私钥和公钥的权限分别为**600和644**。 .ssh目录权限必须是700
2.将自己的公钥文件发送给对方
# 命令格式:
ssh-copy-id [-i [identity_file]] [user@]machine
这个命令的作用是:将id_rsa.pub文件内容传输至对方的 .ssh目录中,生成文件名为authorized_keys 文件,并且会设置远程
主机用户目录的**.ssh和.ssh/authorized_keys**权限
--在sndw_p上执行以下操作:
[oracle@sndw_p .ssh]$ ssh-copy-id -p 10022 10.132.99.235
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/oracle/.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
oracle@10.132.99.235's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -p '10022' '10.132.99.235'"
and check to make sure that only the key(s) you wanted were added.
[oracle@sndw_p .ssh]$
--这里在sndw_stb01主机上查看:
[oracle@sndw_stb01 .ssh]$ ls -lrt
total 12
-rw-r--r-- 1 oracle oinstall 399 May 30 14:52 id_rsa.pub
-rw------- 1 oracle oinstall 1675 May 30 14:52 id_rsa
-rw-r--r-- 1 oracle oinstall 183 May 30 14:54 known_hosts
[oracle@sndw_stb01 .ssh]$
上面的操作只是单方面信任,主机sndw_p登录主机sndw_stb01不需输入密码,反过来不行,所以还需下面操作:
--在主机sndw_stb01上将其钥复制到主机sndw_p上:
[oracle@sndw_stb01 .ssh]$ ssh-copy-id -p 10022 10.132.99.231
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/oracle/.ssh/id_rsa.pub"
The authenticity of host '[10.132.99.231]:10022 ([10.132.99.231]:10022)' can't be established.
ECDSA key fingerprint is SHA256:NA4l6+rDWETpimp2KFRax+WKsSQLs9xSfLWjx4eRU9U.
ECDSA key fingerprint is MD5:96:11:a8:9e:4d:a8:22:9b:3f:13:b3:df:4c:12:ea:8b.
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
oracle@10.132.99.231's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -p '10022' '10.132.99.231'"
and check to make sure that only the key(s) you wanted were added.
[oracle@sndw_stb01 .ssh]$
3.验证互信
分别在sndw_p主机和sndw_stb01主机上通过SSH登录,看是否需要输入密码:
--在sndw_p主机上登录sndw_stb01主机:
[oracle@sndw_p .ssh]$ ssh -p 10022 10.132.99.235
Last failed login: Fri May 30 14:58:13 CST 2025 from 10.132.99.231 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Fri May 30 14:40:55 2025 from 58.210.76.18
**********************************************************************************
* Welcome to sndw_stb01
* em1 120.132.99.236
* em2 10.132.99.235
* UPTIME: 14:59:52 up 38 days, 22:50, 4 users, load average: 0.71, 0.37, 0.27
**********************************************************************************
**********************************************************************************
* Welcome to SuZhou Sndw Oracle Primary Database Server (With HugePage)
* WAN : em1: broadcast em2: broadcast em3: (Ethernet) em4: (Ethernet)
* LAN :
* DISK_USAGE WARNNING :
* UPTIME: 14:59:52 up 38 days, 22:50, 4 users, load average: 0.71, 0.37, 0.27
* Oracle Version: Release 11.2.0.4.0 Production
**********************************************************************************
[oracle@sndw_stb01 ~]$ exit
logout
Connection to 10.132.99.235 closed.
[oracle@sndw_p .ssh]$
无密登录成功。
--同样,在sndw_stb01主机上登录sndw_p主机:
[oracle@sndw_stb01 ~]$ ssh -p 10022 10.132.99.231
Last login: Thu May 29 11:20:24 2025 from 58.210.76.18
**********************************************************************************
* Welcome to sndw_p
* em1 120.132.99.231
* em2 10.132.99.231
* UPTIME: 14:56:56 up 17 days, 23:55, 3 users, load average: 0.00, 0.02, 0.05
**********************************************************************************
**********************************************************************************
* Welcome to SuZhou Sndw Oracle Primary Database Server (With HugePage)
* WAN : em1: broadcast em2: broadcast em3: (Ethernet) em4: (Ethernet)
* LAN :
* DISK_USAGE WARNNING :
* UPTIME: 14:56:56 up 17 days, 23:55, 3 users, load average: 0.00, 0.02, 0.05
* Oracle Version: Release 11.2.0.4.0 Production
**********************************************************************************
[oracle@sndw_p ~]$ exit
logout
Connection to 10.132.99.231 closed.
[oracle@sndw_stb01 ~]$
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31397003/viewspace-3086317/ ,如需转载,请注明出处,否则将追究法律责任。