[关闭]
@yangwenbo 2023-02-10T17:37:06.000000Z 字数 2293 阅读 209

SSH

ssh免密访问对端服务

姓名 IP 端口
ssh01 192.168.200.46 22
ssh02 192.168.200.47 22
ssh03 192.168.200.48 2200

1、用户之间互相免密访问

1.1 生成密匙

  1. #服务端生成密匙
  2. root@ssh01:~# ssh-keygen
  3. Generating public/private rsa key pair.
  4. Enter file in which to save the key (/root/.ssh/id_rsa):
  5. Created directory '/root/.ssh'.
  6. Enter passphrase (empty for no passphrase):
  7. Enter same passphrase again:
  8. Your identification has been saved in /root/.ssh/id_rsa
  9. Your public key has been saved in /root/.ssh/id_rsa.pub
  10. The key fingerprint is:
  11. SHA256:0S1OQouQJnrU/XluACpeHXT6RXWSpjlR2kUCDK33tBY root@ssh01
  12. The key's randomart image is:
  13. +---[RSA 3072]----+
  14. | ..+. ++o+=o+ |
  15. | o +.+= +++o= |
  16. | o o oo++o*=o |
  17. |. o o ..=**.E |
  18. | o o S=.+ o |
  19. | . o + |
  20. | . . |
  21. | |
  22. | |
  23. +----[SHA256]-----+
  1. #会在该用户下生成俩文件
  2. root@ssh01:~# ll .ssh/
  3. 总用量 16
  4. drwx------ 2 root root 4096 7 11 13:47 ./
  5. drwx------ 6 root root 4096 7 11 13:47 ../
  6. -rw------- 1 root root 2590 7 11 13:47 id_rsa # 私匙
  7. -rw-r--r-- 1 root root 564 7 11 13:47 id_rsa.pub # 公匙

1.2 分发密匙

  1. #将密匙分发到对端服务器
  2. root@ssh01:~# ssh-copy-id 192.168.200.47
  3. #会将服务端的公匙拷贝到客户端,在客户端下生成一个authorized_keys文件
  4. root@ssh02:~# ll .ssh/
  5. 总用量 12
  6. drwx------ 2 root root 4096 7 11 13:50 ./
  7. drwx------ 6 root root 4096 7 11 13:50 ../
  8. -rw------- 1 root root 564 7 11 13:50 authorized_keys
  9. #如果对端无法ssh访问的通,可以手动将拷贝过去的id_rsa.pub文件里的内容追加到~/.ssh/authorized_keys文件当中
  10. [root@ssh02 ~]# mkdir .ssh
  11. [root@ssh02 ~]# chmod +700 .ssh
  12. [root@ssh02 ~]# cd .ssh/
  13. [root@ssh02 .ssh]# cat id_rsa.pub > authorized_keys

1.3 实验测试

  1. #实验测试
  2. root@ssh01:~# hostname -I
  3. 192.168.200.46
  4. root@ssh01:~# ssh 192.168.200.47
  5. root@ssh02:~# hostname -I
  6. 192.168.200.47

2、非22端口如何不指定端口登录

2.1 分发密匙

  1. #分发密匙到非22端口的机器上面
  2. root@ssh01:~# ssh-copy-id -p 2200 192.168.200.48
  3. #会将服务端的公匙拷贝到客户端,在客户端下生成一个authorized_keys文件
  4. root@ssh03:~# ll .ssh/
  5. 总用量 12
  6. drwx------ 2 root root 4096 7 11 13:59 ./
  7. drwx------ 6 root root 4096 7 11 13:59 ../
  8. -rw------- 1 root root 564 7 11 13:59 authorized_keys

2.2 实验测试

方式一:

  1. #一般非22端口是需要指定端口登录的
  2. root@ssh01:~# hostname -I
  3. 192.168.200.46
  4. root@ssh01:~# ssh -p 2200 192.168.200.48
  5. root@ssh03:~# hostname -I
  6. 192.168.200.48

方式二:

  1. #但是有些场景下我们需要不指定端口登录
  2. #创建config文件
  3. root@ssh01:~# vim .ssh/config
  4. root@ssh01:~# cat .ssh/config
  5. Host ssh03
  6. User root
  7. Hostname 192.168.200.48
  8. Port 2200
  9. Identityfile ~/.ssh/id_rsa
  1. root@ssh01:~# hostname -I
  2. 192.168.200.46
  3. root@ssh01:~# ssh ssh03
  4. root@ssh03:~# hostname -I
  5. 192.168.200.48

2.3 config配置含义

  • Host
    服务器别名,只要是合法的变量名称且不重复即可,可任意指定,ssh命令通过该名称来连接到指定服务器,比如上面的 ssh ssh03 。
  • Hostname
    服务器地址,可以是域名,也可以是ip地址。
  • Port
    端口号,默认为22,只有修改了ssh连接的默认端口才需要配置此参数
  • User
    ssh的登陆用户名
  • IdentityFile
    ssh 私钥文件的地址(不带.pub后缀的文件)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注