配置 WSL
使用 ssh 远程 wsl
安装 ssh 服务
apt install openssh-server
修改 sshd 服务启动配置文件
vi /etc/ssh/sshd_config
# add
# 允许root用户登录
PermitRootLogin yes
# 服务端口,为了不和windows及其它wsl子系统冲突,手动指定一个
Port 12308
# 监听地址,如果需要远程机器连接
ListenAddress 0.0.0.0
启动
ubuntu
service ssh restart
wsl
wsl 是无法用 systemctl 启动 ssh 的,因此,需要自己写脚本 vi /etc/init.d/sshd
#!/bin/sh
# Start/stop/restart the secure shell server:
sshd_start() {
# Create host keys if needed.
if [ ! -r /etc/ssh/ssh_host_key ]; then
/usr/bin/ssh-keygen -t rsa1 -f /etc/ssh/ssh_host_key -N ''
fi
if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then
/usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
fi
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
/usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
fi
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
/usr/bin/ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key-N ''
fi
if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
/usr/bin/ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key-N ''
fi
/usr/sbin/sshd -f /etc/ssh/sshd_config
}
sshd_stop() {
killall sshd
}
sshd_restart() {
if [ -r /var/run/sshd.pid ]; then
echo "WARNING: killing listener process only. To kill every sshd process, you must"
echo " use 'rc.sshd stop'. 'rc.sshd restart' kills only the parent sshd to"
echo " allow an admin logged in through sshd to use 'rc.sshd restart' without"
echo " being cut off. If sshd has been upgraded, new connections will now"
echo " use the new version, which should be a safe enough approach."
kill `cat /var/run/sshd.pid`
else
killall sshd
fi
sleep 1
sshd_start
}
case "$1" in
'start')
sshd_start
;;
'stop')
sshd_stop
;;
'restart')
sshd_restart
;;
*)
echo "usage $0 start|stop|restart"
esac
启动
bash /etc/init.d/sshd start
查看
ps -ef|grep ssh
windows 下面 win+r 打开 cmd,输 入 netstat -ano|findstr "12308" 查看端口,发现已经启动
到这个阶段,已经可以用本地 win 主机来 ssh 连接 wsl 了
ssh <name>@<wsl ip> -p 12308
远程 ssh 连接
如果防火墙开启,那我们需要配置防火墙规则同时需要配置端口转发规则