ssh config设置,快速登录服务器

.ssh 文件里的 config 可以提前预设好 ssh 的参数,然后就可以方便快速的 ssh 登录服务器了

最终配置

1
2
3
4
5
6
7
8
9
10
11
Host haojide
HostName 127.0.0.1
User root
Port 22
IdentityFile ~/.ssh/xxx.pem

Host *
ServerAliveInterval 30
TCPKeepAlive yes
ServerAliveCountMax 6
Compression yes

基础配置

1
2
3
4
5
# config 配置
Host haojide
HostName 127.0.0.1 # IP或者域名
User root # 用户名
Port 22 # 端口号

使用密钥登录

如果提示密钥权限不足或者权限过高,需要修改下密钥文件

1
sudo chmod 600 ~/.ssh/xxx.pem

配置

1
2
3
4
5
Host haojide
HostName 127.0.0.1 # IP或者域名
User root # 用户名
Port 22 # 端口号
IdentityFile ~/.ssh/xxx.pem

保持连接

1
2
3
4
5
Host *
ServerAliveInterval 30 # 每30秒向服务端发送一次保活信息
TCPKeepAlive yes
ServerAliveCountMax 6 # 发送6次未收到服务端响应断开链接
Compression yes

禁用密码登录

为了保障服务器的安全性,可以直接禁止使用密码登录。
修改服务器的 sshd 配置

1
/etc/ssh/sshd_config

配置

1
2
Host *
PasswordAuthentication no

使用方式

配置好后,只需要在终端 ssh 连接服务器就很方便了

1
ssh haojide

ssh config设置,快速登录服务器

https://blog.feijidui.com/linux-ssh-config/

作者

Wiley

发布于

2022-06-12

更新于

2024-05-26

许可协议