汪道之

有人的地方就有江湖

0%

wsl的安装与配置

前言

之前上过一个Linux的选修课,了解了Linux系统的一些知识,越用越觉得一些需要命令行的软件用Linux来简直太方便了,这篇文章就介绍以下如何在windows系统上搭建Linux子系统(也就是wsl)(Windows Subsystem for Linux),这样就不用使用VMware来装虚拟机了

windows 10 开启wsl功能

  1. 按Windows+Q进入搜索功能,也可以右键点击开始,选择里面的搜索功能
  2. 在Windows 10的搜索框中输入启用或关闭Windows功能
  3. 打开后选中适用于Linux的Windows子系统
    image-20210503170452634

Linux的安装

  1. 打开微软商店(Microsoft Store)
  2. 搜索Linux
  3. 选择其中的一个发行版本进行安装,本文以debian为例

image-20210503171000458

image-20210503171035452

Linux初始化

  1. 打开刚下好的Linux,等一小会它的安装

  2. 输入新创建的用户名

  3. 输入该用户的密码,输入两次后就进入了Linux系统,新用户也创建完成

  4. 输入sudo passwd更改root的密码

  5. 一些知识

    输入su再输入密码进入root模式
    输入exit或者按ctrl+d退出root模式
    输入sudo 命令可以在普通用户下执行root权限的命令,密码是普通用户的密码

Terminal安装

Terminal是一款高颜值的管理命令行软件,如果你习惯于PowerShell或者cmd的话可以跳过此步骤

  1. 打开微软商店(MicroSoft Store)
  2. 搜索Terminal
  3. 点击安装

Terminal配置

简单配置一下Terminal,让你用起来更舒服,可跳过

  1. 打开Terminal
  2. 打开设置

image-20210503171852186

  1. 点击左下角的设置图标,进入Terminal的配置文件,如下,为了安全起见建议备份一下,不然出问题不好还原
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// This file was initially generated by Windows Terminal 1.4.3243.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",

"defaultProfile": "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
"initialRows": 25,
"initialCols": 90,

// You can add more global application settings here.
// To learn more about global settings, visit https://aka.ms/terminal-global-settings

// If enabled, selections are automatically copied to your clipboard.
"copyOnSelect": false,

// If enabled, formatted data is also copied to your clipboard
"copyFormatting": false,

// A profile specifies a command to execute paired with information about how it should look and feel.
// Each one of them will appear in the 'New Tab' dropdown,
// and can be invoked from the commandline with `wt.exe -p xxx`
// To learn more about profiles, visit https://aka.ms/terminal-profile-settings
"profiles": {
"defaults": {
// Put settings here that you want to apply to all profiles.
"hidden": false,
"acrylicOpacity": 0.75,
"useAcrylic": false,
"colorScheme": "Campbell",
"fontFace": "Consolas",
"fontSize": 11,
"cursorColor": "#00FF00",
"cursorHeight": 25,
"cursorShape": "vintage",
"snapOnInput": true,
"padding": "0, 0, 0, 0",
"closeOnExit": true
},
"list": [

{
"guid": "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
"hidden": false,
"name": "Debian",
"source": "Windows.Terminal.Wsl",
"startingDirectory": "//wsl$/Debian/home/tom/"
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "Command Prompt",
"commandline": "cmd.exe",
"hidden": true
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": true,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false
}

]
},

// Add custom color schemes to this array.
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
"name": "Campbell",
"foreground": "#A7B191",
"background": "#0C0C0C",
"colors": [
"#0C0C0C",
"#C50F1F",
"#13A10E",
"#C19C00",
"#0037DA",
"#881798",
"#3A96DD",
"#CCCCCC",
"#767676",
"#E74856",
"#16C60C",
"#F9F1A5",
"#3B78FF",
"#B4009E",
"#61D6D6",
"#F2F2F2"
]
},
{
"name": "Solarized Dark",
"foreground": "#FDF6E3",
"background": "#073642",
"colors": [
"#073642",
"#D30102",
"#859900",
"#B58900",
"#268BD2",
"#D33682",
"#2AA198",
"#EEE8D5",
"#002B36",
"#CB4B16",
"#586E75",
"#657B83",
"#839496",
"#6C71C4",
"#93A1A1",
"#FDF6E3"
]
},
{
"name": "Solarized Light",
"foreground": "#073642",
"background": "#FDF6E3",
"colors": [
"#073642",
"#D30102",
"#859900",
"#B58900",
"#268BD2",
"#D33682",
"#2AA198",
"#EEE8D5",
"#002B36",
"#CB4B16",
"#586E75",
"#657B83",
"#839496",
"#6C71C4",
"#93A1A1",
"#FDF6E3"
]
}
],

// Add custom actions and keybindings to this array.
// To unbind a key combination from your defaults.json, set the command to "unbound".
// To learn more about actions and keybindings, visit https://aka.ms/terminal-keybindings
"actions": [
// Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
// These two lines additionally bind them to Ctrl+C and Ctrl+V.
// To learn more about selection, visit https://aka.ms/terminal-selection
{
"command": {
"action": "copy",
"singleLine": false
},
"keys": "ctrl+c"
},
{
"command": "paste",
"keys": "ctrl+v"
},

// Press Ctrl+Shift+F to open the search box
{
"command": "find",
"keys": "ctrl+shift+f"
},

// Press Alt+Shift+D to open a new pane.
// - "split": "auto" makes this pane open in the direction that provides the most surface area.
// - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
// To learn more about panes, visit https://aka.ms/terminal-panes
{
"command": {
"action": "splitPane",
"split": "auto",
"splitMode": "duplicate"
},
"keys": "alt+shift+d"
}
]
}
  1. 更改配置的几个地方(不要直接复制粘贴,一些值不同)
    • 更改defaultProfile,后面的值就粘贴list里面的几个项的guid,这是默认启动打开哪个命令行,我设置的是Debian的
    • initialRowsinitialCols设置打开后初始宽高
    • profiles中的defaults是全局设置,可以对照更改。list里面就是Terminal管理的几个命令行软件,有刚装的debian,cmd,Powershell等
    • 我在listDebian的设置中加了"startingDirectory":"//wsl$/Debian/home/tom/",这是默认打开Debian后的目录,tom是我的用户名,大家可以改成自己的
    • schemes中的配置是主题的一些配置,大家可以直接复制粘贴过去添加上
    • actions中是快捷键的配置,大家自行参考

恭喜,至此Terminal配置完毕,我用的也是老师给的配置,觉得挺简约还挺好看的,一些其他更花里胡哨的配置请自行网上冲浪

配置wsl

言归正传,接下来我们可以使用terminal打开debian,可以在那个加号后面的向下的小箭头里面选择打开的命令行

换源

因为debian使用apt作为包管理器,而apt的默认服务器在国外,网速很慢,我们可以换到国内的镜像源来

  1. 使用su切换到root权限

  2. 输入cd /etc/apt切换到对应目录

  3. 输入cp /etc/apt/sources.list /etc/apt/sources.list_backup备份一下源文件,出错的话便于还原

  4. 输入nano sources.list利用nano打开sources.list文件

    如果你会使用其他文本编辑器也是一样的
    实在不会用的话也可以用记事本,wsl的根目录在windows的C:\Users\Admin\AppData\Local\Packages\TheDebianProject.DebianGNULinux_76v4gfsz19hv4\LocalState\rootfs下,其中用户名和装的Linux有出入,大家对照自己的就行
    用记事本打开并做下面更改,保存即可

  5. 删除所有内容,将以下内容复制粘贴进去(命令行中复制和粘贴都是右键)

    debian源:

    1
    2
    3
    4
    5
    6
    7
    8
    # 中科大源
    deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free
    # deb-src http://mirrors.ustc.edu.cn/debian stable main contrib non-free
    deb http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free
    # deb-src http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free

    # deb http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free
    # deb-src http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free

    Ubuntu源:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #  阿里源
    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
  6. ctrl+x退出,输入y再点enter确定修改

  7. 换源完成

更新包

  1. sudo apt update会将新的包下载下来
  2. sudo apt upgrade会升级为最新的包,两条命令顺序不要反了
  3. 以后安装软件就可以sudo apt install 软件名了,嘻嘻,快捷方便

完成

恭喜你,你的wsl已经搞定啦,享受Linux带来的快乐吧!