Post

SSH配置远程免密登录

SSH配置远程免密登录

1 生成SSH密钥对

注:如果本地已有密钥对(C:/Users/用户/.ssh/下已有文件),则可跳过此步骤。

打开cmd,在Windows本地生成密钥对(默认ED25519):

1
ssh-keygen
  1. 提示选择存储地址,直接回车即可;
  2. 若提示覆盖已有密钥,则可输入n,跳过此步骤。
  3. 无需设置密钥口令(直接回车跳过),否则每次登录仍需输入口令。

2 将公钥上传至Linux服务器

方法1:(本地为WSL系统)

若已安装WSL(Windows Subsystem for Linux),在WSL终端中运行:

1
ssh-copy-id -i ~/.ssh/id_rsa.pub 用户名@Linux服务器IP

输入Linux用户密码后,公钥会自动追加到服务器的~/.ssh/authorized_keys文件中。

方法2:(本地为Windows系统)

手动复制公钥内容(C:\Users\用户名\.ssh\id_rsa.pub文件中的文本)。

通过SSH登录Linux服务器,执行以下命令:

1
2
3
4
mkdir -p ~/.ssh
echo "粘贴的公钥内容" >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

3 配置Linux服务器的SSH服务

确保Linux服务器的SSH配置允许公钥认证。编辑SSH配置文件:

1
sudo vim /etc/ssh/sshd_config

取消下列行的注释:

1
2
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

重启SSH服务即可免密登录:

1
sudo systemctl restart sshd

4 VSCode配置SSH登录时免选platform

在本地文件C:\Users\用户名\AppData\Roaming\Code\User\settings.json中,配置remote.SSH.remotePlatform:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
    "git.path": "D:\\Program Files\\Git\bin\\git.exe",
    "editor.rulers": [
    { "column": 80, "color": "#006400" }, // 暗绿
    { "column": 100, "color": "#CCCC00" }, // 暗黄
    { "column": 120, "color": "#8B0000" } // 暗红
    ],
    "git.terminalAuthentication": false,

    // 添加下面的部分
    "remote.SSH.remotePlatform": {
        // "IP" : "平台"
        "192.168.233.128": "linux"
    }
}
This post is licensed under CC BY 4.0 by the author.