1.安装wsl的centos版本

wsl是什么就不多说了,不清楚的自己去谷歌吧。也可以参考下面几篇文章:

[https://learn.microsoft.com/zh-cn/windows/wsl/install-manual](microsoft官方文档)
[https://zhuanlan.zhihu.com/p/146545159]([安利] WSL Linux 子系统,真香!完整实操)
[https://www.jianshu.com/p/0a102c4bb3a2](WSL win10子系统linux-ubuntu 安装 开机启动ssh服务 secureCRT连接 换源 与windows系统文件交互)
而windows store商店的linux版本,能用的只有ubuntu版本。而生产环境服务器大部分都是centos版本,以前用虚拟机也基本都是centos版本,ubuntu确实是用不习惯。安装centos需要手动下载安装包。
[https://github.com/mishamosher/CentOS-WSL](CentOS-WSL)下面随便给几个cdn加速的github地址,访问不了github的自取。
wget https://gh.gh2233.ml/https://github.com/mishamosher/CentOS-WSL/releases/download/7.9-2111/CentOS7.zip
wget https://github.91chi.fun/https://github.com/mishamosher/CentOS-WSL/releases/download/7.9-2111/CentOS7.zip
wget https://download.fastgit.org/mishamosher/CentOS-WSL/releases/download/7.9-2111/CentOS7.zip


下载完成之后,直接解压,双击CentOS7.exe自动安装完成。

2.开启ssh服务

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

然后,修改sshd服务启动配置文件
vi /etc/ssh/sshd_config

#允许root用户登录
PermitRootLogin yes
#服务端口,为了不和windows及其它wsl子系统冲突,手动指定一个
Port 12308
#监听地址,如果需要远程机器连接
ListenAddress 0.0.0.0

启动sshd,查看服务已经启动

/etc/init.d/sshd start
ps -ef|grep ssh

windows下面win+r打开cmd,输入netstat -ano|findstr "12308",查看端口,发现已经启动

之后,就可以配合vscode,远程连接到windows下面的centos进行远程代码开发,爽歪歪。

原文地址:http://www.cnblogs.com/yang417/p/16865082.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性