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:
Then let's open the Intellij IDE and choose the java project in the WSL2 following steps as in the image below.
Choose Project --> Open --> Select your Distribution mount in the Linux section in your File Explorer --> Select the java project in your WSL2 --> Click Select Folder button.
After open the project in the intellij then let's open the File --> Project Structure and set the project SDK as in the image below.
Open the SDK dropdown and select the SDK from the WSL2 of your distribution then click OK.
Then you will see the terminal of WSL2 is open at the location of your project.
Then from this terminal you can build the java project without any performance issue.
- Then you can also use the build tool of Intellij to build the project.
Then if you the build got the issue like [FATAL] Non-readable POM then let's grant the permission for the pom.xml by using the command.
1
chmod755pom.xml
Then if you still got the issue like the classes in the import doesn't exists then let's remove the folder JetBrains in the ~/.cache in WSL2
12
cd~/.cache
rm-rfJetBrains
Then on Windows restart your Intellij and then click build button of the Intellij again you should build the java project successfully.
Now, you can run your java project from Intellij without any issue.
Next, If you want to run the debug mode there you may get the issue as below.
Then you can try to follow the Intellij Instruction to fix the issue and hope it works. However, there is more easy way to run debugging is using remote debug.
So firstly, let's create a debug remote as in the image below.
Then in the Intellij Terminal run the command below to start the remote server.