Windows 10 Pro以上的版本携有Hyper-V虚拟机功能,可以用来安装Linux。本文记录使用Hyper-V安装Ubuntu 18.04LTS的过程。
开启Windows 10的Hyper-V功能
Windows 10的高阶版本,比如Pro或者Enterprise都内置Hyper-V虚拟机功能,但是默认不开启,需要手动开启Install Hyper-V on Windows 10
配置NAT功能
我们要让Ubuntu使用网络地址转换(NAT)来访问外网,所以先配置NAT。
参考官方文档Set up a NAT network, 打开一个Admin权限的PowerShell,输入以下命令:
# 创建switch
New-VMSwitch -SwitchName "honolulu" -SwitchType Internal
# 获取Swith信息,查看honolulu的ifIndex,在下面的添加虚拟网段的命令中用得上
Get-NetAdapter
# 创建用于NAT的switch,地址为192.168.0.1,InterfaceIndex是从上条命令中获取的,子网掩码是24位
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex 24
# 创建一个NAT
New-NetNat -Name Hawaii -InternalIPInterfaceAddressPrefix 192.168.0.0/24
也可以不使用PowerShell。如果你安装了Hyper-V,在Hyper-V的Virtual Switch Manager里面也可以创建NAT
NAT相关参考
- Set Up NAT In Windows 10 Hyper-V
- USING POWERSHELL TO CREATE A NAT NETWORK FOR HYPER-V VMS
- Hyper-V Creating a NAT Network and vSwitch
- Windows 10: How to setup NAT network for Hyper-V guests?
- Native NAT in Windows 10 Hyper-V using a NAT virtual switch
- Configuring VM Networking on a Hyper-V NAT Switch
创建Ubuntu虚拟机
打开Hyper-V Manager,其Action菜单里面带有Quick Create功能。点击Quick Create,选择默认的 Ubuntu 18.04LTS,并且在高级选项中把switch设置为之前创建的NAT switch,之后一路默认操作就行了。
由于是内置的,所以Windows为Ubuntu 18.04做了好多设置,比如Enhaced Session Mode,基本上做到开箱即用,并且可以和主系统无缝对接,比如无缝复制粘贴等操作。
关于 Enhanced Session Mode,可以看官方的博客Sneak Peek: Taking a Spin with Enhanced Linux VMs。注意,需要Windows 10达到一定的版本才可以支持。
Hyper-V相关参考
- Microsoft Announce Enhanced Version of Ubuntu 18.04 for Hyper-V
- pUsing Enhanced Mode Ubuntu 18.04 for Hyper-V on Windows 10
- How to Turn On or Off Hyper-V Enhanced Session Mode in Windows 10
- xRDP – Microsoft adding Enhanced mode support for Ubuntu through xRDP
- Supported Ubuntu virtual machines on Hyper-V
配置Ubuntu的网络
由于Hyper-V的switch不带DHCP功能,所以Ubuntu无法从DHCP获取到IP地址,需要手动设置IP网络。
可以按以下设置Ubuntu的网络:
- IP地址:192.168.0.100
- 子网掩码:255.255.255.0
- DNS地址:4.2.2.2 and 8.8.8.8
上述DNS地址使用的是谷歌的DNS,也可以使用Windows 10的DNS地址。使用ipconfig /all
可以查看Windows 10的DNS地址。可以参考
How to Check DNS Address in Windows 10。
配置Ubuntu的apt源
把修改/etc/apt/sources.list
,把里面的地址替换成阿里云的地址https://mirrors.aliyun.com/ubuntu/
,然后可以使用相关的apt命令来操作软件源。
一些apt命令:
# 升级apt cache
$ sudo apt-get upgrade
# 搜索软件 vim
$ sudo apt-cache search vim
# 安装vim
$ sudo apt install vim
也可以使用图形化的Synaptic(Use Synaptic for more advanced software management)来管理软件包
(完)
2021-05-20更新
为了解决Hyper-V的DHCP服务器问题,以下有两种方案:
- DNS Proxy for Hyper-V with NAT Virtual Switch in Windows 10 v1511.作者编写了一个基于C#的服务,可以作为DHCP服务器用
- Enable easy NAT and DHCP Connectivity for your Hyper-V Virtual Machines的解决办法是按照VMWare Player,借用VMWare Player安装的虚拟网卡VMNet8的NAT和DHCP功能。VMNet8似乎要使用vmnetcfg.exe来配置。
可以为home目录开启fscryptfs
eCryptfs有点年老失修,于是选择fscryptfs,针对ext4分区。
主要参考https://tlbdk.github.io/ubuntu/2018/10/22/fscrypt.html
sudo apt-get install fscrypt libpam-fscrypt
确保getconf PAGE_SIZE
和sudo tune2fs -l $DEVICE | grep 'Block size'
的块大小一致。此处ext4文件系统不嫩建立在LVM逻辑卷上。
打开目标tune2fs -O encrypt /dev/sda1
,此处假设目标分区是/dev/sda1
。
使用PAM来自动解密,首先创建/usr/share/pam-configs/keyinit-fix
,内容如下:
Name: keyinit fix
Default: yes
Priority: 0
Session-Type: Additional
Session:
optional pam_keyinit.so force revoke
使用sudo pam-auth-update
重配PAM。
加密home的话,要先sudo fscrypt setup /home
。
加密需要一个空目录,对于已有用户,需要用其他用户登录操作:
sudo su -
export USERNAME=user1
mv /home/$USERNAME /home/$USERNAME.bak
mkdir /home/$USERNAME
chown $USERNAME:$USERNAME /home/$USERNAME
fscrypt encrypt /home/$USERNAME --user=$USERNAME
rsync -avH --info=progress2 --info=name0 /home/$USERNAME.bak/ /home/$USERNAME/
rm -rf /home/$USERNAME.bak
可以选择密钥和登录口令一致,避免重复输入密钥。
其他参考:
- EXT4 fscrypt vs. eCryptfs vs. LUKS dm-crypt Benchmarks
- https://wiki.archlinux.org/title/Fscrypt
- https://askubuntu.com/questions/1029249/how-to-encrypt-home-on-ubuntu-18-04
- https://www.guide2wsl.com/luks/
- https://bugs.launchpad.net/ubuntu/+source/ecryptfs-utils/+bug/1756840
- https://www.linuxuprising.com/2018/04/how-to-encrypt-home-folder-in-ubuntu.html
- https://askubuntu.com/questions/1335006/
- <what-is-the-recommended-method-to-encrypt-the-home-directory-in-ubuntu-21-04 https://kifarunix.com/encrypt-files-and-directories-with-ecryptfs-on-ubuntu-20-04/>
(更新完)