简介
使用WSL连接远程服务器
配置
- 编辑/home/你的用户/.ssh/config文件(若无则新建)
$ vim ~/.ssh/config
- 添加如下内容
#配置跳板机
#跳板机别称
Host jumpServer001
#跳板机IP地址
HostName 192.168.101.6
#使用的ssh私钥文件
IdentityFile ~/ssh-key/id_rsa
#登录用户名
User yourAccount
#端口号
Port 22
#配置服务器
#服务器别称
Host master001
#服务器IP地址
HostName 192.168.10.250
#端口号
Port 2215
#登录用户名
User yourAccount
#使用的ssh私钥文件
IdentityFile ~/ssh-key/id_rsa
#使用代理命令
ProxyCommand ssh yourAccount@jumpServer001 -W %h:%p
- 使用ssh yourServer测试是否配置成功
$ ssh master001
Enter passphrase for key '/home/yourAccount/ssh-key/id_rsa':
Enter passphrase for key '/home/yourAccount/ssh-key/id_rsa':
Last login: Fri May 15 11:14:07 from 192.168.101.10
[yourAccount@master001 ~]$
至此,wsl透过跳板机连接服务器配置完成。
简化连接方式
- 此种方式每次连接服务器都要输入两次ssh私钥密码,很麻烦。可以使用ssh-agent 简化操作:
$ ssh-agent bash
$ ssh-add ~/ssh-key/id_rsa
Enter passphrase for key '/home/yourAccount/ssh-key/id_rsa':
Identity added: /home/yourAccount/ssh-key/id_rsa (/home/yourAccount/ssh-key/id_rsa)
- 上述方式配置,当WSL窗口关闭再打开时,仍然需要重复1步骤。为简化操作,使用以下脚本(转载自其他作者[因未找到原作者,未与批注],如有侵权,立即删除),并将该脚本加入.bashrc
#!/bin/sh
# auto start ssh-agent and add key
if [ -f ~/.agent.env ];then
. ~/.agent.env >/dev/null
if ! kill -0 $SSH_AGENT_PID >/dev/null 2>&1; then
echo " stale agent file found. Spawning new agent...."
eval `ssh-agent | tee ~/.agent.env `
if [[ $? -eq 0 ]] ;then
echo "agent ok!"
fi
ssh-add '/home/yourAccount/ssh-key/id_rsa' # your private key
if [[ $? -eq 0 ]] ; then
echo "key add ok!"
fi
fi
else
eval ` ssh-agent |tee ~/.agent.env` >/dev/null 2>&1
if [[ $? -eq 0 ]] ;then
echo "agent ok!"
fi
ssh-add '/home/yourAccount/ssh-key/id_rsa' # your private key
if [[ $? -eq 0 ]] ; then
echo "key add ok!"
fi
fi
echo "ali IP : jumpServer001"
echo "source /home/yourAccount/ssh-key/ssh-agent-auto-start.sh" >>~/.bashrc
至此配置完成,欢迎大家指教!