1.先配置package.json
在scripts上加上要自动上传到服务器的配置代码:
"scripts": {
"build:devTo222": "vite build --mode development && scp -v -r D://xxx//xxx//dist root@192.168.1.xxx:/xxx/web/dev",
},
2. 生成 SSH 密钥对
首先,为你的用户生成一个 SSH 密钥对。如果你已经有了密钥对,可以跳过这一步。
在你的本地机器上,打开终端并运行以下命令
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
3. 添加公钥到远程服务器
将生成的公钥添加到远程服务器的 authorized_keys
文件中。
使用 ssh-copy-id
命令可以方便地将公钥复制到远程服务器:
ssh-copy-id user@remote_host
其中,user
是远程服务器上的用户名,remote_host
是远程服务器的 IP 地址或域名。
你也可以手动复制公钥。首先,显示公钥的内容:
cat ~/.ssh/id_rsa.pub
然后在远程服务器上登录并将其添加到 ~/.ssh/authorized_keys
文件中:
ssh user@remote_host
mkdir -p ~/.ssh echo "your_public_key" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
每个节点修改配置文件 /etc/ssh/sshd_config,修改内容如下:
vi /etc/ssh/sshd_config
sshd_config中插入一下代码
RSAAuthentication yes # 启用 RSA 认证
PubkeyAuthentication yes # 启用公钥私钥配对认证方式
AuthorizedKeysFile .ssh/authorized_keys # 公钥文件路径
4.配置多个用户的免密登录
重复上述步骤,为每个用户生成各自的 SSH 密钥对并将公钥添加到目标服务器的 authorized_keys
文件中。
你可以将多个公钥添加到同一个 authorized_keys
文件中,每行一个公钥。例如:
cat ~/.ssh/id_rsa_user1.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa_user2.pub >> ~/.ssh/authorized_keys
5.验证免密登录
现在,你可以从本地机器以免密方式登录到远程服务器:
ssh user@remote_host
6.配置 SSH 配置文件(可选)
Host remote_alias
HostName remote_host
User user IdentityFile ~/.ssh/id_rsa
这样,你就可以通过简短的命令登录:
ssh remote_alias