SSH客户端配置
Linux、MAC都自带SSH客户端。当我们需要登录远程服务器时,使用SSH一种很方便的方式。

Linux、MAC都自带SSH客户端。当我们需要登录远程服务器时,使用SSH一种很方便的方式。

➜  ~ ssh -V
OpenSSH_8.6p1, LibreSSL 3.3.6

登录方式

ssh登录支持两种方式,密码或者Public key。使用Public 可以不用记住密码,安全性也更高,所以推荐使用Public的登录方式。

使用密码登录

➜  ~ ssh root@192.168.1.100
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
ED25519 key fingerprint is SHA256:1UQENqSLT/o2X+3RBxArmU/5g6Hw7DMW/ag9z1NqqE2.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.100' (ED25519) to the list of known hosts.
root@192.168.1.100's password:

只有输入正确账号密码即可。

使用Public Key登录

使用public key的方式登录,需要首先在本地生成私钥和公钥。然后把公钥写入服务器上对应账号根目录的.ssh目录下authorized_keys 文件中。

➜  ~ ls -al ~/.ssh/authorized_keys
-rw-rw-r-- 1 yearnfar yearnfar 572 Jun 12 13:43 /home/yearnfar/.ssh/authorized_keys
➜  ~ ssh yearnfar@192.168.1.100

Welcome to Ubuntu 22.04 LTS Linux 5.4.125
Last login: Sun Nov  6 19:21:13 2022 from 192.168.1.100
➜  ~

配置文件

ssh还支持是用config文件的方式进行配置,默认读取当前账号下.ssh/config文件。

Host dev
     HostName 192.168.1.100
     Port 22
     User yearnfar
     IdentitiesOnly yes
     ServerAliveInterval 60
  • Host:后面dev是ssh配置名称
  • HostName: 服务器地址,支持IP和域名
  • Port: 服务端开放的ssh端口
  • User: 需要登录的账号
  • IdentitiesOnly:使用Public key登录,不使用去掉即可
  • ServerAliveInterval:向服务器发送心跳的时间,单位秒

通过配置上面的配置文件,登录服务器192.168.1.100 就可以简化成:

➜  ~ ssh dev

跳板机登录

有些公司为了安全,登录服务器都需要通过一台跳板机。这时候登录就变得比较繁琐,而且向使用VSCode Remote这种,就无法登录了。于是我又找了下如何通过Config配置,一步实现登录需要跳板机才能登录的服务器。具体配置如下:

Host jump_server
      HostName 192.168.1.101
      Port 22
      User yearnfar
      PubkeyAuthentication yes
      ServerAliveInterval 60
      
Host dst_server
      HostName 192.168.1.102
      Port 22
      User yearnfar
      PubkeyAuthentication yes
      ServerAliveInterval 60
      IdentityFile ~/.ssh/跳板机公钥.pub
      ProxyCommand ssh -W %h:%p jump_server

通过以上配置,就可以一步登录到目标机器了。

ssh dst_server 

最后修改于 2022-11-06

此篇文章的评论功能已经停用。