• linux
  • SSH
  • Remote
发布于

SSH 远程控制


SSH 远程控制

1. 本地生成密钥对

ssh-keygen -t ed25519 -C "注释" -f ~/.ssh/id_remote_ed25519 -N "密码"

公钥:~/.ssh/id_remote_ed25519.pub
私钥(不要复制或公开):~/.ssh/id_remote_ed25519

2. 将公钥上传到你的服务器(当你有已登陆的服务器时)

假设你的 linux 服务器 IP192.168.1.100,用户名是 user

ssh-copy-id -i ~/.ssh/id_remote_ed25519.pub user@192.168.1.100

3. 登陆

假设你的 linux 服务器 IP192.168.1.100,端口为 22,用户名是 user

ssh -i id_remote_ed25519 -p 22 user@192.168.1.100

4. 服务端端口配置

配置 /etc/ssh/sshd_configPort 字段:

sudo $EDITOR /etc/ssh/sshd_config # 替换 $EDITOR 为你的编辑器

5. 禁用密码登录(提高安全性)

登录到 linux 服务器,编辑 SSH 配置:

sudo $EDITOR /etc/ssh/sshd_config

修改以下项目:

PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin no  # 建议禁止 root 登录

重启 SSH 服务

sudo systemctl restart sshd

6. 配置文件简化登陆(可选)

在你的登陆设备编辑 ~/.ssh/config, 假设你的 linux 服务器 IP192.168.1.100,端口为 22,用户名是 user

Host myserver
    HostName 192.168.1.100
    User user
    Port 22
    IdentityFile ~/.ssh/id_remote_ed25519

于是就可以用以下命令登陆:

ssh myserver