欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在Windows 10上安装CentOS 7的WSL版本并启用SSH服务进行远程访问

最编程 2024-08-03 13:42:41
...

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进行远程代码开发,爽歪歪。

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

推荐阅读