目录

Linux SSH 免密码登录

在维护多台机器时,传统的用户名密码登录存在一些弊端,所以就分出一台当跳板机,从跳板机上登录其它机器。

SSH 服务

安装 SSH 服务

1
2
3
4
5
6
7
8
// CentOS
$ yum install openssh-server

// Ubuntu
$ apt install openssh-server

// sshd.service 启动|重启|停止|状态
$ systemctl start|restart|stop|status sshd.service

配置 SSH 服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ vim /etc/ssh/sshd_config
RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

UsePAM yes

ClientAliveInterval 300
ClientAliveCountMax 3

UseDNS no

AddressFamily inet
SyslogFacility AUTHPRIV
PermitRootLogin without-password
Protocol 2
PasswordAuthentication no
$ systemctl restart sshd.service

生成密钥对

本地机器,跳板机,跳板机登录的其它机器都需要生成密钥对。

1
2
3
4
5
6
7
8
9
$ ssh-keygen -t rsa -C "email|instance-id|hostname|xxx 均可"

$ chmod 0700 .ssh
$ chmod 0600 .ssh/authorized_keys

// 复制跳板机的公钥粘贴到跳板登录的其它机器的 authorized_keys 文件中
$ cat ~/.ssh/id_rsa.pub
$ ssh-copy-id -i .ssh/id_rsa.pub username@ip

登录测试

1
$ ssh root@ip

快捷配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ vim ~/.ssh/config
Host a-instance
  User root
  HostName 10.10.10.11
  PubkeyAuthentication yes
  IdentityFile ~/.ssh/id_rsa
Host b-instance
  User root
  HostName 10.10.10.12
  PubkeyAuthentication yes
  IdentityFile ~/.ssh/id_rsa

$ ssh a-instance