Always keep a beginner's min|

Undefined443

园龄:2年11个月粉丝:13关注:3

SSH 使用

常用命令

# 创建 SSH 密钥
ssh-keygen -t rsa

# 登录远程主机
ssh USER@HOST

# 上传密钥
ssh-copy-id USER@HOST

# 传输文件
scp src user@host:dst

SSH 配置

基本配置

SSH 的配置文件为 ~/.ssh/config

# 全局设置
Host *
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
    # 保活
    ServerAliveInterval 120
    # TCP 连接复用
    ControlMaster auto
    ControlPath ~/.ssh/multiplex/%r@%h:%p
    ControlPersist 1
    # 设置连接到任意主机使用的密钥(默认为 id_rsa)
    IdentityFile ~/.ssh/id_rsa

Host example
  HostName example.com
  User username
  IdentityFile ~/.ssh/private_key.pem
  Port 2222

# 设置通过 443 端口并通过代理连接到 github.com
Host github.com
    HostName ssh.github.com
    User git
    Port 443
    IdentityFile ~/.ssh/id_rsa
    ProxyCommand connect -H 127.0.0.1:6152 %h %p

此时登录时可以使用 ssh example 代替 ssh username@example.com -p 2222

说明

scp 只能识别以正斜杠分隔的路径,即便是在 Windows 上也是如此。因此 Shell 路径 /Users/username/.ssh/authorized_keys 相当于 CMD 上的:C:\Users\username\.ssh\authorized_keys

权限设置

  • .ssh 目录: 700
  • id_rsa: 600
  • id_rsa.pub: 644
  • authorized_keys: 600
  • known_hosts: 644
  • config: 600

跳板机

通过跳板机连接到目标服务器:

本地客户端
跳板机(bastion)
目标服务器(target)
# 跳板机配置
Host bastion
    HostName 跳板机的 IP 或域名
    User 跳板机的用户名
    IdentityFile ~/.ssh/id_rsa  # 本地私钥文件路径

# 目标服务器配置,通过跳板机
Host target
    HostName 目标服务器的 IP 或域名
    User 目标服务器的用户名
    ProxyJump bastion
    IdentityFile ~/.ssh/id_rsa  # 本地私钥文件路径

参见:

SSHD 配置

SSHD 的配置文件为 /etc/ssh/sshd_config

允许 root 登录

编辑 SSHD 配置文件:

sudo vim /etc/ssh/sshd_config

找到这两条配置:

#PermitRootLogin prohibit-password
#PermitEmptyPasswords no

改成这两条:

PermitRootLogin yes
PermitEmptyPasswords yes

最后重启 sshd 服务:

sudo systemctl restart sshd

Troubleshooting

REMOTE HOST IDENTIFICATION HAS CHANGED

在使用 SSH 连接远程主机时遇到如下警告:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:aFBqZqRp+ZlAldN8EXRkYOE7P2HdQwuI3yh++kW6dbQ.
Please contact your system administrator.
Add correct host key in /Users/username/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/username/.ssh/known_hosts:6
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
UpdateHostkeys is disabled because the host key is not trusted.
Error: forwarding disabled due to host key check failure
Connection closed by UNKNOWN port 65535
fatal: Could not read from remote repository.

这是一种安全警告,提示你远程主机的 SSH 密钥与本地存储的不匹配,这有可能是中间人攻击造成的。但是对于一般人来说,遇到中间人攻击的可能性不大,这更有可能是连接 SSH 时使用了代理,而每次使用的代理服务器不一样,所以 SSH 客户端看到了不同的代理服务器密钥,从而导致密钥验证失败。

在确保没有安全风险后,可以通过如下设置来绕过密钥验证机制:

Host *
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null

如果你使用 Windows,则使用下面的配置:

Host *
  StrictHostKeyChecking no
  UserKnownHostsFile nul

附:SSH 连接的验证机制

当你第一次连接到某个 SSH 服务器时,服务器会发送它的公钥给客户端。客户端会将这个公钥存储在 ~/.ssh/known_hosts 文件中。之后每次连接时,客户端都会比对服务器发来的公钥与存储的公钥是否一致。

本文作者:Undefined443

本文链接:https://www.cnblogs.com/Undefined443/p/-/ssh

版权声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

posted @   Undefined443  阅读(20)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起