Skip to content

ssh 笔记

ssh 免密码登陆

复制本机公钥到 linux,linux or macos 可直接使用此命令,windows 可以使用 git bash

bash
ssh-copy-id root@shaojie.fun
# 按照提示输入 linux 密码

此时可以免密码登陆到 linux

bash
ssh root@shaojie.fun # 无需密码直接登陆

ssh 简化登陆

无需输入用户名和域名或者端口

可实现 ssh ali 直接登陆

bash
# 编辑 ssh 配置文件
vim ~/.ssh/config
# 添加如下内容
Host ali
  HostName shaojie.fun
  User root
  # Port 22 # 默认端口 22 可以不写
  # JumpServer jump.hostname # 可配置跳板机,通过跳板机登陆到目标服务器

ssh 禁用 root 登录

bash
# 编辑 sshd_config
vim /etc/ssh/sshd_config
# 修改为如下
# PermitRootLogin no         # 禁用root
# PasswordAuthentication yes # 允许密码登录

# 重启 sshd 使修改生效
systemctl restart sshd

ssh 禁止 root 使用密码登陆

bash
vim /etc/ssh/sshd_config
# 修改配置如下
# PermitRootLogin prohibit-password # 禁止 root 使用密码登陆
# PasswordAuthentication yes        # 其他用户允许密码登录
# 重启 sshd 使修改生效
systemctl restart sshd

TIP

配置完成,此时 linux root 账户仅支持使用密钥登陆,若想配置其他设备使用 root 账户,可手动 copy 对应电脑的公钥追加到 linux 下的 /root/.ssh/authorized_keys 文件中