启用虚拟 虚拟机平台功能

1
2
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

wsl 命令

1
2
3
4
5
6
7
8
9
wsl -l -v #检查正在运行的Linux发行版
wsl --setdefault Ubuntu-18.04 #将 Ubuntu-18.04 设置为wsl默认使用的Linux发行版
wsl --update # 以防万一更新wsl
wsl --set-version Ubuntu-22.04 2 #设置更新到 Ubuntu-18.04为WSL2
wsl #进入到Ubuntu-18.04 命令行
wsl.exe --shutdown
wsl.exe
net stop LxssManager
net start LxssManager

更新Linux内核

1
wsl --update

设置WSL2为默认版本

1
wsl --set-default-version 2

迁移位置

1
2
# wsl --export <系统名> <导出文件位置>
wsl --export Ubuntu-20.04 D:\10_WSL2\Ubuntu-20.04.tar

导入ubuntu

1
2
# wsl --import <系统名> <安装位置> <导入文件位置> WSL版本号
wsl --import Ubuntu_new D:\10_WSL2 D:\10_WSL2\Ubuntu-20.04.tar --version 2

删除迁移前的系统

1
2
wsl -l -v
wsl --unregister Ubuntu-20.04

wsl win路径

1
\\wsl$

关闭 Win10 中的相关端口

Linux 中的 Samba 服务主要用到 139 和 445 这两个端口,因此首先要取消在 Win10 中的这两个端口

  • 139 端口:网络和 Internet - 更改适配器选项 - 右键当前网络对象属性 - 选中 TCP/IPv4 属性 - 高级 - WINS - 禁用 TCP/IP 上的 NetBIOS
  • 445 端口:服务 - 找到 Server 服务 - 启动类型禁用 - 重启

win 防火墙

1
wf.msc

win 查看端口占用

1
netstat -a|findstr LISTENING

wsl Ubuntu 替换阿里源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cp /etc/apt/sources.list /etc/apt/sources.list.bak

sudo vi /etc/apt/sources.list

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

# echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null

sudo apt-get update && sudo apt-get upgrade

Ubuntu 防火墙

1
2
ufw disable
ufw enable
配置wslconfig
1
2
3
4
5
6
7
8
9
10
11
12
cd ~
New-Item .wslconfig
.\.wslconfig

写入
[wsl2]
networkingMode=bridged
vmSwitch=WSL_external
dhcp=false
ipv6=true
memory=4GB
processors=2
配置network
1
2
3
4
5
6
7
8
9
10
11
12
sudo touch /lib/systemd/network/wsl_external.network # 创建网络配置文件
sudo vim /lib/systemd/network/wsl_external.network # 编辑该文件并添加以下内容

[Match]
Name=eth0
[Network]
Description=bridge
DHCP=false
Address=192.168.1.110/24
Gateway=192.168.1.1

#按ESC+:wq 保存并退出
配置wsl.conf
1
2
3
4
5
6
7
8
9
10
11
12
sudo touch /etc/wsl.conf #创建发行版wsl配置文件
sudo vim /etc/wsl.conf # 编辑该文件并添加以下内容

[boot]
systemd=true
[user]
default=root
[network]
generateResolvConf=false
hostname=WSL2

#按ESC+:wq 保存并退出
配置resolv.conf
1
2
3
4
5
6
7
8
9
10
sudo rm /etc/resolv.conf
sudo touch /etc/resolv.conf #创建并DNS服务器IP地址配置文件
sudo vim /etc/resolv.conf # 编辑该文件并添加以下内容

nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 223.5.5.5

#按ESC+:wq 保存并退出
执行以下命令重起网络服务
1
2
3
4
5
6
sudo apt-get update #更新包管理库,注意不要换阿里源/清华源什么的
sudo apt-get install systemd # 安装systemd管理工具
systemctl restart systemd-networkd
systemctl enable systemd-networkd
systemctl restart systemd-resolved.service
systemctl enable systemd-resolved.service

WSL samba 配置

安装
1
sudo apt-get install samba
重启 samba
1
2
sudo systemctl restart smbd.service
sudo service smbd restart
reload samba
1
sudo systemctl reload smbd
samba testparm
1
testparm -s /etc/samba/smb.conf 
配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
sudo vi /etc/samba/smb.conf

[global]
server string = Samba Server Version %v
workgroup = WORKGROUP
security = user
passdb backend = smbpasswd
guest ok = no
max log size = 1000
syslog = 0
log level = 3 vfs:10
log file = /var/log/samba/log.%m
max connections = 0
guest account = nobody
create mask = 0775
force create mode = 0775
directory mask = 0775
force directory mode = 0775
map to guest = bad user
smb passwd file = /etc/samba/smbpasswd
# username map = /etc/samba/smbusers
vfs objects = full_audit;recycle
full_audit:success = mkdir rmdir read pread write pwrite rename unlink
full_audit:prefix = %u|%I|%m|%S
full_audit:failure = none
# full_audit:failure = connect
full_audit:facility = local5
full_audit:priority = notice
recycle:exclude_dir= /mnt/d/recycle
recycle:repository = /mnt/d/recycle
recycle:keeptree = yes
recycle:versions = yes
recycle:touch = yes
recycle:directory_mode = 0777
recycle:subdir_mode = 0700
recycle:exclude = *.tmp, *.bak


[share]
path = /mnt/c/share
comment = "share"
public = no
available = yes
browseable = yes
read only = no
writable = no
valid users = caochen cg
read list = caochen cg
write list = cg

配置 smbpasswd
1
2
3
4
5
6
7
8
9
10
touch /etc/samba/smbpasswd

sudo useradd -s /sbin/nologin test
sudo smbpasswd -a test

smbpasswd -a 增加用户(要增加的用户必须以是系统用户)
smbpasswd -d 冻结用户,就是这个用户不能在登录了
smbpasswd -e 恢复用户,解冻用户,让冻结的用户可以在使用
smbpasswd -n 把用户的密码设置成空.要在global中写入 null passwords -true
smbpasswd -x 删除用户
full_audit 配置
1
2
3
4
5
6
7
vi /etc/rsyslog.d/50-default.conf
添加
*.*;local5,auth,authpriv.none -/var/log/syslog
local5.* /var/log/samba/full_audit.log

sudo systemctl restart smbd
sudo systemctl restart rsyslog
端口转发
1
2
3
4
5
6
7
8
9
netsh interface portproxy show all

# netsh interface portproxy add v4tov4 listenport=[win10端口] listenaddress=0.0.0.0 connectport=[虚拟机的端口] connectaddress=[虚拟机的ip]

netsh interface portproxy add v4tov4 listenport=139 listenaddress=0.0.0.0 connectport=139 connectaddress=[虚拟机的ip]
netsh interface portproxy add v4tov4 listenport=445 listenaddress=0.0.0.0 connectport=445 connectaddress=[虚拟机的ip]

netsh interface portproxy delete v4tov4 listenport=445 listenaddress=0.0.0.0
netsh interface portproxy delete v4tov4 listenport=139 listenaddress=0.0.0.0

wsl Ubuntu 安装宝塔

1
wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh