启用WSL后,使用ssh通道连接ubuntu

Enjoy WSL

目的

启用wsl后,使用windows自带的powershell、cmd操作linux还是不太好使。以下介绍开启ssh通道,并保证能在ssh通道下,也能正常使用wsl中的win命令行,以及正常打开gui应用。

离线更新WSL,请跳转链接:离线更新WSL

打开ssh通道

  1. 删除 ubuntu 自带的openssh,在wsl环境下,无法正常启动

    sudo apt remove openssh-server
    sudo apt install -y openssh-server

  2. 编辑sshd配置文件

    vim /etc/ssh/sshd_config

    ...
    Port 22
    AddressFamily any
    #ListenAddress 0.0.0.0
    #ListenAddress ::

    ...

    To disable tunneled clear text passwords, change to no here!

    PasswordAuthentication yes
    #PermitEmptyPasswords no

    ...

  3. 启动ssh

    systemctl start ssh.service
    systemctl enable ssh.service

设置wsl环境变量

当通过ssh方式连接时,无法打开gui应用(或者只能通过隧道方式打开gui应用);同时也无法使用windows下的命令。

解决方法:

  1. 在用户目录下的 /home/user/.bashrc 追加上一下内容

    读取windows下的环境变量

    当通过ssh方式连接时,也能共享 windows 的环境变量

    WSL_ENV=/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0//powershell.exe -Command '& {\$env:Path}'|sed 's#;#:#g;s#\\\\#/#g;s#D:#/mnt/d#g;s#C:#/mnt/c#g'
    export PATH=PATH:WSL_ENV

    设置总是在:0上显示,

    当通过ssh方式连接时,也能打开GUI应用

    export DISPLAY=":0"

相关推荐
likfishdn2 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean22 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=24 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
若云止水33 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
北京耐用通信1 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
崔lc4 小时前
linux中安装部署Jenkins,成功构建springboot项目详细教程
运维·jenkins
luojiaao4 小时前
【Jenkins】个人向-Jenkinsfile如何写
运维·jenkins
wanhengidc8 小时前
单线服务器和双线服务器分别是指什么?
运维·服务器