linuxLinux学习与应用技巧windows运维

Win10 子系统使用笔记

2019-12-07  本文已影响0人  Godan

最近体验了下Win10子系统 Ubuntu 18.04 LST ,先说说优缺点,

优点:安装简便,启动快速。
缺点:没发直接使用桌面环境,没法直接安装docker服务。

关于缺点:
1. 可以使用VNC工具获得桌面环境能力,但是桌面中的应用联网会问题,暂时不知如何解决。
2. docker服务可以使用win10桌面版的,这就意味着需要开启 hyper-v 服务,安卓模拟器将无法使用。

不做Linux桌面下的联网程序开发,或者既要用Docker,又要用安卓模拟器,使用Win10子系统 WSL,能提供不错的Linux开发环境。另外,需要注意的WSL的系统和原生版本还是有不少区别的,想使用完整系统功能,请用vbox或vmware虚拟机。

安装

  1. 开启Linux子系统,以管理员权限打开 PowerShell
   # 启用虚拟机平台
   Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
   # 开启Linux子系统
   Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
  1. 开启开发者模式,WIN + S 搜索 开发者设置,打开后选择 开发者模式
  2. WIN + S 搜索 store, 打开 Microsoft Store(微软应用商店),搜索 Ubuntu ,选择 Ubuntu 18.04 LST 进行安装
  3. 待安装完成后,从应用中打开 Ubuntu 18.04 LST
Installing, this may take a few minutes...
  Please create a default UNIX user account. The username does not need to match your Windows username.
  For more information visit: https://aka.ms/wslusers
  # 输入管理员用户名,这里我取名为g
  Enter new UNIX username: g
  # 输入管理员密码
  Enter new UNIX password:
  # 再次输入管理员密码
  Retype new UNIX password:
  passwd: password updated successfully
  Installation successful!
  # 至此所有安装全部完成

整个安装过程还算简单,比vbox和vmware方便不少,并且由于WSL是服务,所以连接到shell管理也特别快,在我的笔记本上,打开耗时大概3~5秒

安装桌面环境

桌面环境不是必需品,除非你要用来做Linux桌面开发,否则不推荐安装,毕竟没有设置网络的方法,桌面环境下的联网程序无法使用。

  1. 更换apt源,使用阿里云提供源
   sudo cp sources.list sources.list.bak && sudo vim sources.list
   # 输入 100dd 清空文件,按 i 插入,粘贴如下内容
   
   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
   
   # 输入:wq或:x,保存并退出
   
   # 更新并升级系统
   sudo apt update
   sudo apt upgrade
  1. 安装桌面环境,以安装gnome为例
    # 安装桌面环境
    sudo apt install ubuntu-desktop
    # 安装 gnome
    sudo apt install gnome
    # 安装VNC服务和gnome组件
    sudo apt install vnc4server gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal
    
    # 文泉驿-微米黑
    # sudo apt-get install ttf-wqy-microhei  
    # 文泉驿-正黑
    # sudo apt-get install ttf-wqy-zenhei  
    # 文泉驿-点阵宋体
    # sudo apt-get install xfonts-wqy 
  1. 配置vnc服务启动脚本
vim ~/.vnc/xstartup

#!/bin/sh

export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &

gnome-panel &
gnome-settings-daemon &
gnome-session-fallback &

metacity &
nautilus &
gnome-terminal &

保存并退出,chmod u+x ~/.vnc/xstartup

  1. 启动vnc服务
# 启动vnc服务
vncserver :1
# 停止vnc服务
# vncserver -kill :1
  1. 使用vnc viewer 客户端连接,成功后就可以使用Ubuntu桌面环境。当然,不是vnc服务,你也可以直接打开某个桌面应用,首先进行如下配置
# 添加到 ~/.bashrc 末尾
echo "export DISPLAY=:0" >> ~/.bashrc
# 让新配置生效
. ~/.bashrc

然后打开MobaXterm,确认右上角的 X server 按钮已经开启,彩色表示开,灰色表示关,输入命令gnome-terminalfirefox试试。

MobaXterm

常规操作

使用管理员模式,打开PowerShell

  1. 启动停止 wsl 服务
# 停止子系统服务
net stop LxssManager
# 启动子系统服务
net start LxssManager
  1. 备份恢复,导出、导入子系统
# d:\Ubuntu-18.04.tar 导出文件路径
wsl --export Ubuntu-18.04 d:\Ubuntu-18.04.tar
# d:\wsl\u18.04 子系统导入后的安装路径
wsl --import Ubuntu-18.04 d:\wsl\u18.04 d:\Ubuntu-18.04.tar

安装Docker服务

实际上是在Win10中安装Docker桌面服务,Linux子系统中安装客户端,连接Win10上的Docker服务,进行操作

  1. 在Ubuntu子系统中安装Docker
# 更新apt包管理列表
sudo apt-get update -y

# 安装依赖包
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

# 加入Docker官方PGP公钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 确认指纹
sudo apt-key fingerprint 0EBFCD88

# 将stable(稳定版)Docker加入apt源中
#
# If you want to live on the edge, you can change "stable" below to "test" or
# "nightly". I highly recommend sticking with stable!
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# 更新apt包管理列表
sudo apt-get update -y

# 安装Docker CE最新版本
sudo apt-get install -y docker-ce

# 允许当前用户访问Docker CLI,不必使用root
sudo usermod -aG docker $USER

# 此时执行 docker version 会提示如下错误:
# Cannot connect to the Docker daemon at unix:///var/run/docker.sock.

# 配置Windows Docker服务地址
echo "export DOCKER_HOST=localhost:2375" >> ~/.bashrc
# 使配置生效
. ~/.bashrc
  1. 安装 Windows桌面Docker
  2. 勾选 Settings => General => Expose daemon on tcp://localhost:2375 without TLS
  3. 再次在Ubuntu中执行 docker version

安装OpenSSH-Server

默认WSL Ubuntu是没使用ssh服务的,当然你也可以装一个,方便远程管理

# 安装服务
sudo apt install openssh-server

# 生成rsa、ecdsa、ed25519密钥
sudo ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
sudo ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
sudo ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

# 修改配置
sudo vim /etc/ssh/sshd_config
# 监听端口
Port 22
# 禁止密码登录
PasswordAuthentication no
# 开启公钥登录
PubkeyAuthentication yes
# 指定登录公钥文件
AuthorizedKeysFile  .ssh/authorized_keys .ssh/authorized_keys2
# 退出并保存

# 创建登录公钥文件
touch ~/.ssh/authorized_keys
chmod 0700 ~/.ssh/authorized_keys
# 放入你的公钥

# 启动服务
sudo service ssh start
# 随系统启动
sudo systemctl enable ssh
上一篇下一篇

猜你喜欢

热点阅读