18 年的时候由于好奇 + 装X + 活动优惠,买了个 3 年的阿里云服务器,想着没事练练手,可直到服务器过期,也没练过几次,不知道有多少小伙伴跟我一样...
最近想在本地(Windows 系统)装个 Redis,发现得先装 Linux 环境,才发现当初年少无知,花了冤枉钱..
话不多说,直接看整个安装过程。重点是 使用 Xshell 连接 WSL 有点坑。
先确认 Windows 系统版本
必须高于 WIN10 (19041) ,或者 WIN11
安装 WSL
进入命令行,执行 wsl.exe
C:\Users\daojia>wsl.exe
Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Fri Sep 2 15:31:22 CST 2022
System load: 0.52 Processes: 7
Usage of /home: unknown Users logged in: 0
Memory usage: 84% IPv4 address for wifi2: 10.253.110.192
Swap usage: 14%
0 updates can be installed immediately.
0 of these updates are security updates.
The list of available updates is more than a week old.
To check for new updates run: sudo apt update
This message is shown once once a day. To disable it please create the
/home/unix/.hushlogin file.
以 root 用户身份启动 Linux(很多东西都需要 root 权限),执行 wsl.exe --user root
C:\Users\daojia>wsl.exe --user root
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia#
安装 ssh
由于我们都习惯用 Xshell 等终端去操作,所以我们把本机当做远程服务器,通过 Xshell 连接登录,像真正使用 Linux 服务器一样。
首先要安装 ssh,执行 apt-get install openssh-server
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# apt-get install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
openssh-client openssh-sftp-server
Suggested packages:
keychain libpam-ssh monkeysphere ssh-askpass molly-guard
The following packages will be upgraded:
openssh-client openssh-server openssh-sftp-server
3 upgraded, 0 newly installed, 0 to remove and 275 not upgraded.
Need to get 1099 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n]
输入 Y,继续
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 openssh-server amd64 1:8.2p1-4 [377 kB]
Fetched 377 kB in 2s (169 kB/s)
Preconfiguring packages ...
Selecting previously unselected package openssh-server.
(Reading database ... 31818 files and directories currently installed.)
Preparing to unpack .../openssh-server_1%3a8.2p1-4_amd64.deb ...
Unpacking openssh-server (1:8.2p1-4) ...
Setting up openssh-server (1:8.2p1-4) ...
Creating SSH2 RSA key; this may take some time ...
3072 SHA256:Dpk45enJQgIQoz16NpGxc8R6d1ZnWx8/iyE5kNxpy9Q root@DESKTOP-LRCQE95 (RSA)
Creating SSH2 ECDSA key; this may take some time ...
256 SHA256:CM78lkrFr4PHvCKsOcmmGSU8N3a0XVNWEEtbQt9GNVw root@DESKTOP-LRCQE95 (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:U1QoEqgH0oGBmreW0EUrRM/wBJsTzY/AXu8VKlc1RBA root@DESKTOP-LRCQE95 (ED25519)
invoke-rc.d: could not determine current runlevel
Processing triggers for systemd (245.4-4ubuntu3) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for ufw (0.36-6) ...
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia#
修改配置文件
执行 vim /etc/ssh/sshd_config
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# vim /etc/ssh/sshd_config
修改端口,系统默认的 ssh 端口是 22,为避免冲突,可以修改为 2222
允许 root 用户登录
打开身份验证 PasswordAuthentication,
(本想不开启身份验证,但试了好多种方案,都没连接成功,还是采用身份验证的方式吧)
保存退出后重启 ssh,执行 service ssh --full-restart
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# service ssh --full-restart [ OK ]
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia#
启动服务的命令是 service ssh start
第一次启动时可能会有网络安全提示,选择“专用网络”即可。
重置系统密码,后面连接要用
root@DESKTOP-LRCQE95:/mnt/c/Users/daojia# passwd
New password:
Retype new password:
passwd: password updated successfully
使用 Xshell 连接
配置 协议、host、端口
配置用户名、密码,就是刚才前面设置的密码
点击连接
Connecting to 127.0.0.1:2222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Fri Sep 2 21:18:32 CST 2022
System load: 0.52 Processes: 14
Usage of /home: unknown Users logged in: 0
Memory usage: 80% IPv4 address for wifi2: 10.253.110.192
Swap usage: 15%
308 updates can be installed immediately.
203 of these updates are security updates.
To see these additional updates run: apt list --upgradable
New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Fri Sep 2 20:35:58 2022 from 127.0.0.1
root@DESKTOP-LRCQE95:~#
如果连接失败,可以从以下几点排查
是否启动了 ssh 服务?
host、port 是否正确,单独 telnet 是否通?
是否开着防火墙?
用户名密码是否正确?
安装 Redis
按照官网的说明,依次执行下面的命令
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis
启动服务端
root@DESKTOP-LRCQE95:~# sudo service redis-server start
Starting redis-server: redis-server.
启动客户端,并测试
root@DESKTOP-LRCQE95:~# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
3) "key"
127.0.0.1:6379>
有网友说,启动 WSL 后,命令行窗口不能关闭。我这个版本亲测关闭命令行窗口后,Xshell 还可以随意连接,只要电脑不重启,就可以一直使用。
接下来就可以为所欲为了。