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

相关推荐
DeeplyMind8 小时前
第12章 Docker存储机制(重要)
运维·docker·容器
鸠摩智首席音效师8 小时前
如何在 Linux 中将文件复制到多个目录 ?
linux·运维·服务器
香蕉你个不拿拿^9 小时前
Linux进程地址空间解析
linux·运维·服务器
人间打气筒(Ada)9 小时前
Linux学习~日志文件参考
linux·运维·服务器·学习·日志·log·问题修复
OpsEye10 小时前
交换分区优化实战:从监控到调优,让系统告别卡顿
运维·it·监控·告警·swap·监控系统·交换分区
大熊程序猿10 小时前
metabase 报表使用
运维
feichang_notlike311 小时前
Windows (WSL2) 搭建 openclaw
运维
❀͜͡傀儡师12 小时前
Spring Boot Pf4j模块化能力设计思考
运维·spring boot·后端·pf4j
若谷老师12 小时前
21.WSL中部署gnina分子对接程序ds
linux·人工智能·ubuntu·卷积神经网络·gnina·smina
石油人单挑所有12 小时前
ProtoBuf编写网络版本通讯录时遇到问题及解决方案
运维·服务器