windows10安装wsl的centos7版本,并启动ssh服务远程访问

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下面随便给几个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

如果启动出现报错error: Could not load host key: /etc/ssh/ssh_host_ed25519_key,参考下面这篇文章:
[https://www.jianshu.com/p/f2b1370d87ac](解决" Could not load host key: /etc/ssh/ssh_host_ed25519_key"问题)

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

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

posted @   yang417  阅读(2182)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示