Software & AppsOperating SystemLinux

Making /etc/resolv.conf Changes Permanent in WSL 2

Ubuntu 18

In this article, we’ll be discussing how to make changes to the /etc/resolv.conf file permanent in Windows Subsystem for Linux 2 (WSL 2). This is a common requirement for system administrators and developers who often need to modify DNS settings in their WSL 2 environments.

Quick Answer

To make changes to the /etc/resolv.conf file permanent in WSL 2, you need to disable its automatic generation by creating or editing the /etc/wsl.conf file and adding the line [network] generateResolvConf = false. Then, remove the existing symlink and create a new /etc/resolv.conf file with the desired DNS server. Finally, make the /etc/resolv.conf file immutable using the chattr +i command.

Understanding /etc/resolv.conf

The /etc/resolv.conf file is a configuration file in Unix-like operating systems that contains information about DNS servers. It’s automatically generated at system startup or when a network connection is established.

In WSL 2, the /etc/resolv.conf file is also automatically generated and linked to another file. This means that any changes you make to this file will be lost after a system restart or WSL shutdown.

Checking the Current Status of /etc/resolv.conf

Before making any changes, let’s first check the current status of /etc/resolv.conf. Open a terminal in your WSL 2 instance and run the following command:

ls -al /etc/resolv.conf

This command lists files in a long format (-l) and includes hidden files (-a). The output should show a symlink (->) pointing to another file, such as ../run/resolvconf/resolv.conf.

Disabling Automatic Generation of /etc/resolv.conf

To make changes to /etc/resolv.conf permanent, we first need to disable its automatic generation. This can be done by creating or editing the /etc/wsl.conf file.

Run the following command to open the /etc/wsl.conf file in a text editor:

sudo nano /etc/wsl.conf

If the file doesn’t exist, this command will create it. The sudo command is used to run the command as the root user, and nano is a simple text editor.

Add the following lines to the /etc/wsl.conf file:

[network]
generateResolvConf = false

This configuration disables the automatic generation of /etc/resolv.conf by WSL. Save the changes and exit the editor.

Creating a New /etc/resolv.conf File

Next, we need to remove the existing symlink and create a regular file for /etc/resolv.conf. Run the following commands:

sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

The first command (rm) removes the symlink. The second command creates a new /etc/resolv.conf file with the desired DNS server. In this case, we’re using 8.8.8.8, which is a public DNS server provided by Google.

Making /etc/resolv.conf Immutable

Finally, we’ll make the /etc/resolv.conf file immutable to prevent it from being overwritten. Run the following command:

sudo chattr +i /etc/resolv.conf

The chattr command changes file attributes on a Linux file system. The +i option sets the immutable attribute, making the file read-only and preventing any modifications.

Conclusion

After following these steps, your changes to /etc/resolv.conf should persist even after system restarts or WSL shutdowns. This allows you to customize your DNS settings in WSL 2 as needed.

Remember to always be careful when modifying system files, as incorrect changes can lead to system instability or other issues. Always make sure to backup any important data before making changes.

For more information on WSL 2 and its features, you can refer to the official Microsoft documentation.

How can I check the current status of `/etc/resolv.conf` in WSL 2?

To check the current status of /etc/resolv.conf, open a terminal in your WSL 2 instance and run the command ls -al /etc/resolv.conf. This command lists files in a long format (-l) and includes hidden files (-a).

How do I disable the automatic generation of `/etc/resolv.conf` in WSL 2?

To disable the automatic generation of /etc/resolv.conf, you need to create or edit the /etc/wsl.conf file. Open the file using the command sudo nano /etc/wsl.conf and add the following lines to it:

[network]
generateResolvConf = false

Save the changes and exit the editor.

How can I create a new `/etc/resolv.conf` file in WSL 2?

To create a new /etc/resolv.conf file, you need to remove the existing symlink and create a regular file. Run the following commands:

sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

The first command (rm) removes the symlink, and the second command creates a new /etc/resolv.conf file with the desired DNS server. In this example, we’re using 8.8.8.8, which is a public DNS server provided by Google.

How can I make the `/etc/resolv.conf` file immutable in WSL 2?

To make the /etc/resolv.conf file immutable and prevent it from being overwritten, run the command sudo chattr +i /etc/resolv.conf. The chattr command changes file attributes on a Linux file system, and the +i option sets the immutable attribute, making the file read-only and preventing modifications.

Will my changes to `/etc/resolv.conf` persist after system restarts or WSL shutdowns?

Yes, after following the steps mentioned in this article, your changes to /etc/resolv.conf should persist even after system restarts or WSL shutdowns. The file will remain intact and the DNS settings you specified will be retained.

What should I do if I encounter any issues or system instability after modifying `/etc/resolv.conf`?

If you encounter any issues or system instability after modifying /etc/resolv.conf, it’s recommended to revert the changes and restore the original configuration. Always be cautious when modifying system files, as incorrect changes can cause problems. It’s a good practice to backup any important data before making changes.

Leave a Comment

Your email address will not be published. Required fields are marked *