WSL (Windows Subsystem for Linux) is a technology that allows you to use Linux on Windows without creating a virtual machine by leveraging Windows’ virtualization feature, Hyper-V(WSL operates based on Hyper-V, but it does not use Hyper-V.!).
WSL lets developers run a GNU/Linux environment – including most command-line tools, utilities, and applications – directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual-boot setup.
This document is based on Microsoft’s WSL documentation (https://learn.microsoft.com/en-us/windows/wsl/).
WSL Intro(Source: WikiDocs)
Key Features of WSL
- You can download and install various distributions such as Ubuntu, Debian, and Fedora.
- You can use Linux commands like
bash
, grep
, and awk
. - It is possible to share the file system between Linux and Windows.
- Developers can install essential tools and applications (e.g., Docker, Python) in a Linux environment while simultaneously taking advantage of Windows’ convenience.
Install WSL
Install WSL(default OS : Ubuntu)
To use the command below, you must be running Windows 10 version 2004 or later (build 19041 or higher) or Windows 11(Installation page). If you’re running an older build, or just prefer not to use the install command and would like step-by-step directions(in my case, I also had to install it manually due to company security policies, and additionally, proxy settings needed to be configured), see WSL manual installation steps for older versions.
wsl --install(To provide more details, wsl --install -d Ubuntu
wsl --set-default-version 2(if the version is not 2)
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
| # Check list of installable items
PS C:\Users\ycjang> wsl -l -o
다음은 설치할 수 있는 유효한 배포판 목록입니다.
'wsl.exe --install <Distro>'를 사용하여 설치합니다.
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
Ubuntu-24.04 Ubuntu 24.04 LTS
OracleLinux_7_9 Oracle Linux 7.9
OracleLinux_8_7 Oracle Linux 8.7
OracleLinux_9_1 Oracle Linux 9.1
openSUSE-Leap-15.6 openSUSE Leap 15.6
SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5
SUSE-Linux-Enterprise-15-SP6 SUSE Linux Enterprise 15 SP6
openSUSE-Tumbleweed openSUSE Tumbleweed
# Install WSL, default linux(Ubuntu)
PS C:\Users\ycjang> wsl --install
# Check the running WSL version
PS C:\Users\ycjang> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2
# Set WSL version 2 if the version is not 2.
PS C:\Users\ycjang> wsl --set-default-version 2
|
WSL Installation
- Once Ubuntu installation on WSL is complete, running
uname -a
usually displays the Linux kernel version. To check the exact Ubuntu version, use the following command:
lsb_release -a
cat /etc/os-release
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| jaoneol@DESKTOP-B7GM3C5:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
jaoneol@DESKTOP-B7GM3C5:~$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
|
Change the default path of WSL
The default path for the virtual environment that constitutes WSL is the C drive. If the C drive has limited space, it can restrict Docker image storage. Therefore, I want to change the default path to a specific directory on the D drive.
- The default path for WSL is as follows. Let’s assume that Ubuntu 24.04 is being used in WSL.
1
| C:\Users\ycjang\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx
|
- Ideally, it would be best to simply move the VHDX file to the D drive, but since Ubuntu OS was installed while enabling WSL, it was installed as a Windows App and may have other dependencies. Therefore, after moving the files to a different path, additional configurations need to be updated. Since manually checking and handling all these tasks can be complex, there is a project on GitHub that provides a script for this process.
wsl --shutdown
git clone https://github.com/pxlrbt/move-wsl.git
Set-ExecutionPolicy Unrestricted
.\move-wsl.ps1
ubuntu config --default-user jaoneol
1
2
3
4
5
6
7
8
9
10
11
| PS C:\Users\ycjang> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2
# You can use the command below,
# or if you close all WSL Ubuntu windows and wait for a while,
# it will change to "Stopped."
PS C:\Users\ycjang> wsl --shutdown
PS C:\Users\ycjang> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2
PS C:\Users\ycjang>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| PS C:\Users\ycjang> cd d:
PS D:\> cd .\01.WSL\
PS D:\01.WSL> git clone https://github.com/pxlrbt/move-wsl.git
Cloning into 'move-wsl'...
remote: Enumerating objects: 216, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 216 (delta 24), reused 40 (delta 17), pack-reused 165 (from 1)
Receiving objects: 100% (216/216), 116.42 KiB | 8.96 MiB/s, done.
Resolving deltas: 100% (108/108), done.
PS D:\01.WSL> cd .\move-wsl\
PS D:\01.WSL\move-wsl> .\move-wsl.ps1
.\move-wsl.ps1 : 이 시스템에서 스크립트를 실행할 수 없으므로 D:\01.WSL\move-wsl\move-wsl.ps1 파일을 로드할 수 없습니다
. 자세한 내용은 about_Execution_Policies(https://go.microsoft.com/fwlink/?LinkID=135170)를 참조하십시오.
위치 줄:1 문자:1
+ .\move-wsl.ps1
+ ~~~~~~~~~~~~~~
+ CategoryInfo : 보안 오류: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
# Since PowerShell's Execution Policy is set to Restricted,
# changing it to Unrestricted allows scripts and configuration files to be executed.
PS D:\01.WSL\move-wsl> Set-ExecutionPolicy Unrestricted
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| # Running it again will proceed normally.
PS D:\01.WSL\move-wsl> .\move-wsl.ps1
Getting distros...
Select distro to move:
1: Ubuntu
1
Enter WSL target directory:
d:\01.WSL
Move Ubuntu to "d:\01.WSL"? (Y|n): Y
Exporting VHDX to "d:\01.WSL\Ubuntu.tar" ...
내보내기가 진행 중입니다. 이 작업은 몇 분 정도 걸릴 수 있습니다.
작업을 완료했습니다.
Unregistering WSL ...
Importing Ubuntu from d:\01.WSL...
가져오기가 진행 중입니다. 이 작업은 몇 분 정도 걸릴 수 있습니다.
작업을 완료했습니다.
Cleaning up ...
Done!
PS D:\01.WSL\move-wsl>
|
Change the default path of WSL
1
2
3
4
5
6
7
8
9
10
| PS C:\Users\ycjang> wsl -d Ubuntu
root@DESKTOP-B7GM3C5:/mnt/c/Users/ycjang# exit
logout
PS C:\Users\ycjang> ubuntu
root@DESKTOP-B7GM3C5:~# exit
logout
# If you run this command, you will log in with the `jaoneol` account instead of the root account when accessing Ubuntu.
PS C:\Users\ycjang> ubuntu config --default-user jaoneol
PS C:\Users\ycjang> ubuntu
jaoneol@DESKTOP-B7GM3C5:~$
|
Ubuntu Settings
- Let’s change the root account password in Ubuntu.
wsl -u root
1
2
3
4
5
6
| PS C:\Users\ycjang> wsl -u root
root@DESKTOP-B7GM3C5:/mnt/c/Users/ycjang# passwd
New password:
Retype new password:
passwd: password updated successfully
root@DESKTOP-B7GM3C5:/mnt/c/Users/ycjang#
|
- Let’s omit the
sudo
command for your account (jaoneol
), proceed as follows.
Add jaoneol ALL=(ALL:ALL) ALL
to /etc/sudoers
.
1
2
3
4
5
6
7
| jaoneol@DESKTOP-B7GM3C5:~$ su -
Password:
root@DESKTOP-B7GM3C5:~# chmod +w /etc/sudoers
root@DESKTOP-B7GM3C5:~# vi /etc/sudoers
root@DESKTOP-B7GM3C5:~# vi /etc/sudoers
root@DESKTOP-B7GM3C5:~# chmod -w /etc/sudoers
root@DESKTOP-B7GM3C5:~#
|
1
2
3
4
5
6
7
8
9
10
11
12
| ##################
# vi /etc/sudoers
##################
# User privilege specification
root ALL=(ALL:ALL) ALL
jaoneol ALL=(ALL:ALL) ALL # Add this part.
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
|
[Option]Let’s Change the APT repository (ubuntu.sources
) to a domestic server.
Modify the file(etc/apt/sources.list.d/ubuntu.sources
)’s contents using the following command.
:%s/archive.ubuntu.com/mirror.kakao.com/g
:%s/security.ubuntu.com/mirror.kakao.com/g
Below is a list of servers operating in our country.
1
2
3
4
5
6
7
8
9
| # Connect to WSL - Ubuntu using an Ubuntu command.
PS C:\Users\ycjang> ubuntu
# Grant root privileges.
jaoneol@DESKTOP-B7GM3C5:~$ su -
Password:
# Open ubuntu.sources using the vi editor.
root@DESKTOP-B7GM3C5:~# vi /etc/apt/sources.list.d/ubuntu.sources
# Update apt repository
root@DESKTOP-B7GM3C5:~# apt update
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| ############################################
# vi etc/apt/sources.list.d/ubuntu.sources
# Change url in ubuntu.sources using this command
# :%s/archive.ubuntu.com/mirror.kakao.com/g
# :%s/security.ubuntu.com/mirror.kakao.com/g
############################################
Types: deb
URIs: http://mirror.kakao.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: http://mirror.kakao.com/ubuntu/
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
|
- Let’s check the wsl’s configure(
/etc/wsl.conf
) to use ‘systemd’ systemd(system daemon)
is a daemon that replaces the init process by being the first to be created after a Unix system boots and then executing other processes.
In Ubuntu 24.04, systemd is enabled by default, but it is not in older versions(Version 22.04 and older). This was included for verification purposes. If ps -p 1 -o comm=
does not return systemd
, the systemctl
command will not work. Keep this in mind. Add [boot] systemd=true
to /etc/wsl.conf
.
1
2
3
4
5
6
| ##################
# vi /etc/wsl.conf
##################
# systemd is enable
[boot]
systemd=true
|
1
2
3
| jaoneol@DESKTOP-B7GM3C5:~$ ps -p 1 -o comm=
systemd
jaoneol@DESKTOP-B7GM3C5:~$
|
1
2
3
4
5
6
7
8
9
| jaoneol@DESKTOP-B7GM3C5:~$ ps -ef | grep systemd
root 50 1 0 20:37 ? 00:00:00 /usr/lib/systemd/systemd-journald
root 80 1 0 20:37 ? 00:00:00 /usr/lib/systemd/systemd-udevd
systemd+ 102 1 0 20:37 ? 00:00:00 /usr/lib/systemd/systemd-resolved
systemd+ 103 1 0 20:37 ? 00:00:00 /usr/lib/systemd/systemd-timesyncd
message+ 146 1 0 20:37 ? 00:00:00 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root 153 1 0 20:37 ? 00:00:00 /usr/lib/systemd/systemd-logind
jaoneol 322 1 0 20:37 ? 00:00:00 /usr/lib/systemd/systemd --user
jaoneol@DESKTOP-B7GM3C5:~$
|
Install Windows Terminal
Microsoft recommends installing Windows Terminal. The installation manual provides various methods, but we will proceed using the CLI(Installing and running Windows Terminal).
1
| winget install --id Microsoft.WindowsTerminal -e
|
Windows Terminal Installation
Use MobaXterm(Recommended)
Microsoft recommended installing Windows Terminal, but I plan to use MobaXterm, a highly useful tool for working in a Linux environment. The connection is simple.
Connection method
Use VSCode(Recommended)
Most developers are probably using VSCode for development. I’m no exception. So, I will share how to connect to WSL using VSCode for development.
- Install the
WSL
extension in VSCode. - In PowerShell, navigate to the desired directory and run
code .
. This will automatically launch VSCode.
Connection method
Remove the Ubuntu Installed on WSL
To remove Ubuntu installed on WSL, simply enter the following command, and it will be easily removed. Additionally, you can search for “Ubuntu” in Windows > Settings > Apps, where the app will be listed—make sure to uninstall it from there as well.
A very important point to note is that if you remove the Ubuntu app, recovery will be impossible. The virtual disk file, ext4.vhdx, will be completely deleted, so be sure to back it up before proceeding.(In the next post, I will explain Import & Export.)
wsl --unregister Ubuntu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| # Check the list of installed distributions.
PS C:\Windows\system32> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2
PS C:\Windows\system32> wsl --shutdown
PS C:\Windows\system32> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2
# Very Important, Run the following command to remove the distribution.
# Be extremely careful!, If you run this command, the `ext4.vhdx` file will also be deleted!!!
PS C:\Windows\system32> wsl --unregister Ubuntu
등록 취소 중입니다.
작업을 완료했습니다.
PS C:\Windows\system32> wsl --shutdown
PS C:\Windows\system32> wsl -l -v
Linux용 Windows 하위 시스템에는 설치된 배포판이 없습니다.
'wsl.exe --list --online'을 사용하여 사용 가능한 배포판을 나열하고
'wsl.exe --install <Distro>'를 사용하여 설치하세요.
배포판은 Microsoft Store
(https://aka.ms/wslstore)를 방문하여 설치할 수도 있습니다.
오류 코드: Wsl/WSL_E_DEFAULT_DISTRO_NOT_FOUND
PS C:\Windows\system32>
|