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

相关推荐
开开心心_Every1 天前
安卓免费证件照制作软件,无广告弹窗
linux·运维·服务器·安全·elasticsearch·zookeeper·pdf
aichitang20241 天前
AI Agent 终端与系统级自动化深度指南
运维·人工智能·自动化
花间相见1 天前
【Ubuntu实用工具】—— 高颜值实用ls命令工具lsd
linux·运维·ubuntu
the sun341 天前
共享文件夹设置:让Windows和Ubuntu能方便的传输文件
linux·windows·ubuntu
虎头金猫1 天前
监控机不在被监控的机器上,Prometheus怎么跨网络把数据拉过来
运维·服务器·开发语言·网络·云原生·开源·prometheus
枳实-叶1 天前
【Linux驱动开发】第5天:字符设备驱动核心原理:主次设备号+cdev+数据拷贝全解
linux·运维·驱动开发
lbb 小魔仙1 天前
内网 Jenkins 如何实现公网访问?我解决了这个痛点!
运维·jenkins
流浪0011 天前
Linux基础篇 (一) 不费力掌握入门级命令
linux·运维·服务器
zzzyyy5381 天前
Ext系列文件系统
linux·运维
卧室小白1 天前
MYSQL-主从复制
运维