windows ubuntu 子系统

701 阅读2分钟

WINDOWS UBUNTU

平常总有一些场景需要使用linux系统,使用virtualbox或者vmware这种虚机比较吃性能而且也会有费用问题,所以尝试使用windos提供的子系统。

版本信息

  1. WINDOWS 10
  2. UBUNTU 18.04LTS

安装

由于本地已经装好了,只是从WINDOS STORE上下载即可,没有做其他操作。

使用

需求

由于本地系统是windos,而通常服务都部署在linunx环境下,所以有一些场景希望可以在本地的linux调试一下,比如jar包的启动,JVM参数测试,中间件的

部署,gradle test等等。

  • 运行jar包,调试JVM。
  • 执行gradle test。
  • 部署中间件。
  • etc...

环境搭建

在正式使用之前,需要将后续需要的一些基础环境部署好

设置ROOT用户

由于非root用户会有一些权限问题,sudo的话开tab还要输入密码,所以就切换成ROOT用户了。

  • 管理员权限打开CMD,查找到ubunt.exe的位置输入

    ubuntu.exe config --default-user root
    
  • 重新启动WSL。

APT-GET

之前在使用ubuntu时候对于这个包管理工具感觉还是很方便的,所以第一步是先调整好这个。

  • 版本

    root@DLC000FQ64Y33L:/# apt-get -v
    apt 1.6.12ubuntu0.1 (amd64)
    Supported modules:
    *Ver: Standard .deb
    *Pkg:  Debian dpkg interface (Priority 30)
     Pkg:  Debian APT solver interface (Priority -1000)
     Pkg:  Debian APT planner interface (Priority -1000)
     S.L: 'deb' Debian binary tree
     S.L: 'deb-src' Debian source tree
     Idx: Debian Source Index
     Idx: Debian Package Index
     Idx: Debian Translation Index
     Idx: Debian dpkg status file
     Idx: Debian deb file
     Idx: Debian dsc file
     Idx: Debian control file
     Idx: EDSP scenario file
     Idx: EIPP scenario file
    
  • 默认情况使用agt-get update

    image-20211014175237692

    • ping: baidu.com: Temporary failure in name resolution。

    第一步就遇到问题了,使用apt-update时无法解析域名。

    根据查到的资料发现(我本地使用的WSL2,WSL1好像没问题),是因为WSL2在每次启动

    时都会重写/etc/resolv.conf,nameserver使用的是内网,所以没有办法访问外网

    susama@DLC000FQ64Y33L:~$ cat /etc/resolv.conf
    # This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
    # [network]
    # generateResolvConf = false
    nameserver 172.17.225.81
    
    • 解决方案是定义/etc/wsl.conf 设置自动生成为false,重写/etc/resolv.conf.
    sudo rm /etc/resolv.conf
    sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
    sudo bash -c 'echo "[network]" > /etc/wsl.conf'
    sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
    sudo chattr +i /etc/resolv.conf
    
    • 这样设置完仍然访问不通,是因为国外的DNS可能不适用本地,需要修改nameserver对应的DNS IP。
    # 在windos下执行
    ipconfig /all
    # 查询到本机的DNS后替换上面的8.8.8.8
    
  • 解决DNS的问题后,需要替换成国内的镜像源,要不然很卡,这里使用的是ALI的

    # 备份
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    # 修改配置
    sudo gedit /etc/apt/sources.list
    # 本地没按gedit,可以使用vi
    sudo vi /etc/apt/sources.list
    # 清楚文件内容,在vi命令模式下执行 
    gg # 跳转首行首字符
    :.,$d # 删除全部
    # 将下面源地址替换
    # ali
    deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse 
    
  • 执行apt-get update 验证结果

    图太长不贴了。

    反正是成功了。

APT-GET COMMAND

下面列举一些常用的APT-GET 指令,后续安装环境的时候会使用到。

# 使用之前先更新一下本地的包列表
apt-get update
# 搜索想要安装的内容
apt-cache search java
# 选择对应的版本下载
apt-get install openjdk-11-jdk

JDK

jdk目前用的多的就是1.8,跟11,就装这俩,用apt-get搞。

  • 安装jdk8 & 11

    apt-get install openjdk-11-jdk
    apt-get install openjdk-8-jdk
    
  • 切换jdk ...