|
| 1 | +--- |
| 2 | +title: Import any Linux distribution to use with WSL |
| 3 | +description: Learn how to import any Linux distribution to use with the Windows Subsystem for Linux. |
| 4 | +keywords: BashOnWindows, bash, wsl, windows, windows subsystem, distro, custom |
| 5 | +ms.date: 02/17/2021 |
| 6 | +ms.topic: article |
| 7 | +--- |
| 8 | + |
| 9 | +# Import any Linux distribution to use with WSL |
| 10 | + |
| 11 | +You can use any Linux distribution inside of the Windows Subsystem for Linux (WSL), even if it is not available in the [Microsoft Store](https://www.microsoft.com/en-us/search/shop/apps?q=linux), by importing it with a tar file. |
| 12 | + |
| 13 | +This article shows how to import the Linux distribution, [CentOS](https://www.centos.org/), for use with WSL by obtaining its tar file using a Docker container. This process can be applied to import any Linux distribution. |
| 14 | + |
| 15 | +## Obtain a tar file for the distribution |
| 16 | + |
| 17 | +First you'll need to obtain a tar file that contains all the Linux binaries for the distribution. |
| 18 | + |
| 19 | +You can obtain a tar file in a variety of ways, two of which include: |
| 20 | + |
| 21 | +- Download a provided tar file. You can find an example for Alpine in the "Mini Root Filesystem" section of the [Alpine Linux downloads](https://alpinelinux.org/downloads/) site. |
| 22 | +- Find a Linux distribution container and export an instance as a tar file. The example below will show this process using the [CentOS container](https://hub.docker.com/_/centos). |
| 23 | + |
| 24 | +### Obtaining a tar file for CentOS example |
| 25 | + |
| 26 | +In this example, we'll use Docker inside of a WSL distribution to obtain the tar file for CentOS. |
| 27 | + |
| 28 | +#### Prerequisites |
| 29 | + |
| 30 | +- You must have [WSL enabled with a Linux distribution installed running WSL 2](./install-win10.md#manual-installation-steps). |
| 31 | +- You must have [Docker Desktop for Windows installed with the WSL 2 engine enabled and integration checked](./tutorials/wsl-containers.md#install-docker-desktop) for the distribution you will use in the next steps. |
| 32 | + |
| 33 | +#### Export the tar from a container |
| 34 | + |
| 35 | +1. Open the command line (Bash) for a Linux distribution that you've already installed from the Microsoft Store (Ubuntu in this example). |
| 36 | + |
| 37 | +2. Start the Docker service: |
| 38 | + |
| 39 | +```bash |
| 40 | +sudo service docker start |
| 41 | +``` |
| 42 | + |
| 43 | +3. Run the CentOS container inside Docker: |
| 44 | + |
| 45 | +```bash |
| 46 | +docker run -t centos bash ls / |
| 47 | +``` |
| 48 | + |
| 49 | +4. Grab the CentOS container ID using grep and awk: |
| 50 | + |
| 51 | +```bash |
| 52 | +dockerContainerID=$(docker container ls -a | grep -i centos | awk '{print $1}') |
| 53 | +``` |
| 54 | + |
| 55 | +5. Export the container ID to a tar file on your mounted c-drive: |
| 56 | + |
| 57 | +```bash |
| 58 | +docker export $dockerContainerID > /mnt/c/temp/centos.tar |
| 59 | +``` |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +This process exports the CentOS tar file from the Docker container so that we can now import it for use locally with WSL. |
| 64 | + |
| 65 | +## Import the tar file into WSL |
| 66 | + |
| 67 | +Once you have a tar file ready, you can import it using the command: `wsl --import <Distro> <InstallLocation> <FileName>`. |
| 68 | + |
| 69 | +### Importing CentOS example |
| 70 | + |
| 71 | +To import the CentOS distribution tar file into WSL: |
| 72 | + |
| 73 | +1. Open PowerShell and ensure that you have a folder created where you'd like the distribution to be stored. |
| 74 | + |
| 75 | +```PowerShell |
| 76 | +cd C:\temp |
| 77 | +mkdir E:\wslDistroStorage\CentOS |
| 78 | +``` |
| 79 | + |
| 80 | +2. Use the command `wsl --import <DistroName> <InstallLocation> <InstallTarFile>` to import the tar file. |
| 81 | + |
| 82 | +```PowerShell |
| 83 | +wsl --import CentOS E:\wslDistroStorage\CentOS .\centos.tar |
| 84 | +``` |
| 85 | + |
| 86 | +3. Use the command `wsl -l -v` to check which distributions you have installed. |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +4. Finally, use the command `wsl -d CentOS` to run your newly imported CentOS Linux distribution. |
| 91 | + |
| 92 | +## Add WSL specific components like a default user |
| 93 | + |
| 94 | +By default when using --import, you are always started as the root user. You can set up your own user account, but note that the set up process will vary slightly based on each different Linux distribution. |
| 95 | + |
| 96 | +To set up user account with the CentOS distribution we just imported, first open PowerShell and boot into CentOS, using the command: |
| 97 | + |
| 98 | +```PowerShell |
| 99 | +wsl -d CentOS |
| 100 | +``` |
| 101 | + |
| 102 | +Next, open your CentOS command line. Use this command to install sudo and password setting tools into CentOS, create a user account, and set it as the default user. In this example, the username will be 'caloewen'. |
| 103 | + |
| 104 | +```bash |
| 105 | +yum update -y && yum install passwd sudo -y |
| 106 | +myUsername=caloewen |
| 107 | +adduser -G wheel $myUsername |
| 108 | +echo -e "[user]\ndefault=$myUsername" >> /etc/wsl.conf |
| 109 | +passwd $myUsername |
| 110 | +``` |
| 111 | + |
| 112 | +You must now quit out of that instance and ensure that all WSL instances are terminated. Start your distribution again to see your new default user by running this command in PowerShell: |
| 113 | + |
| 114 | +```PowerShell |
| 115 | +wsl --shutdown |
| 116 | +wsl -d CentOS |
| 117 | +``` |
| 118 | + |
| 119 | +You will now see `[caloewen@loewen-dev]$` as the output based on this example. |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +To learn more about configuring WSL settings, see [Launch commands & configurations](./wsl-config.md#configure-per-distro-launch-settings-with-wslconf). |
| 124 | + |
| 125 | +## Use a custom Linux distribution |
| 126 | + |
| 127 | +You can create your own customized Linux distribution, packaged as a UWP app, that will behave exactly like the WSL distributions available in the Microsoft Store. To learn how, see [Creating a Custom Linux Distribution for WSL](./build-custom-distro.md). |
0 commit comments