There are 2 types of WSL2 configuration files. The WSL2 global config file which is located in the Windows host system and the WSL2 local config file which is contained in the WSL2 machine.
The WSL2 global config file will have the name as .wslconfig and it is located at C:\Users\<your-user>. By default it is not created automatically by the Windows system. So if you want to configure something, you need to create the .wslconfig file manually following the path above.
The global WSL2 config file will allow you to configures global settings that affect all distributions (all WSL2 machines), such as memory limits, CPU count, swap size, etc.
After configuring the .wslconfig then you should stop the WSL2 machines and wait for 8 seconds to make sure all instances have been stopped completely (8 seconds rule). Then start your WSL2 machines again then you can see all distributions has been applied with your configurations.
The WSL2 local config file will have the name as wsl.conf and it is located at /etc/wsl.conf. By default this is created automatically inside the WSL2 machine and it will be read everytime at WSL2 startup. This WSL2 local config file is used for controlling per-distro behavior such as mount options, user login defauts or windows drives are mounted.
For example:
123
[network]hostname=duc-server
generateHosts=false
After configuring the wsl.conf then you should stop the WSL2 machine and wait for 8 seconds to make sure the instance have been stopped completely (8 seconds rule). Then start your WSL2 machine again then you can see your specific distribution has been applied with your configurations.
By default the hostname of the WSL machine will be the same with the hostname of the windows host machine. So if you use VPN such as tailscale for your windows host machine and WSL also then you may get issues with DNS which can make you get errors when you use browsers on Windows host machine.
For WSL machine you can't use normal way to set the hostname such as:
sudohostnamectlset-hostname<your-host-name>
Because your hostname will be reset to default after you restart your WSL machine. So you need following this way to set your hostname permanently.
Firstly, let's open the wsl.conf file for editing.
1
sudogedit/etc/wsl.conf
Then add these lines below to the end of the file.
123
[network]hostname=duc-server
generateHosts=false
Next let's open the /etc/hosts file.
1
sudogedit/etc/hosts
Then replace texts which contains your old hostname to your new hostname and save. For example: