启用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"

相关推荐
一只叫煤球的猫43 分钟前
[自荐] macOS 99% 原生 SSH + SFTP 工具 Remora ,现在开源了~
开源·ssh·github
小比特_蓝光1 小时前
Linux开发工具
linux·运维·服务器
岁岁种桃花儿1 小时前
AI超级智能开发系列从入门到上天第十篇:SpringAI+云知识库服务
linux·运维·数据库·人工智能·oracle·llm
小陈工1 小时前
2026年3月24日技术资讯洞察:边缘AI商业化,Java26正式发布与开源大模型成本革命
java·运维·开发语言·人工智能·python·容器·开源
AttaGain1 小时前
【Ubuntu配置VLAN网络】
linux·网络·ubuntu
ljh5746491192 小时前
Linux find命令
linux·运维·chrome
东方不败之鸭梨的测试笔记2 小时前
基于RF自动化重跑
运维·自动化
纪伊路上盛名在2 小时前
Zerotier-Tailscale 自动化监控
linux·运维·自动化·内网穿透
无忧智库2 小时前
大型能源集团的数字中枢:EA框架如何驱动ERP系统从“流程自动化”迈向“智能决策”(PPT)
运维·自动化·能源
培小新2 小时前
五、Dokcer网络
linux·运维·docker·容器