Continuing with my WSL coverage, I would like to share how to add or remove a user to the sudo group in a WSL Linux distro in order to allow or deny it from running commands and apps as root. This is applicable to newly added user accounts, as they have no sudo privileges out of the box. Also, we will see how to find which user accounts are already members of the sudo group.
The ability to run Linux natively in Windows 10 is provided by the WSL feature. WSL stands for Windows Subsystem for Linux, which initially, was limited to Ubuntu only. Modern versions of WSL allow installing and running multiple Linux distros from Microsoft Store.
After enabling WSL, you can install various Linux versions from the Store. You can use the following links:
and more.
When you start a WSL distro for the first time, it opens a console window with a progress bar. After a moment of waiting, you will be prompted to type a new user account name, and its password. This account will be your default WSL user account that will be used to sign-in automatically every time you run the current distro. Also, it will be included in the 'sudo' group in order to allow it to run commands elevated (as root).
User Accounts in WSL Linux
Each Linux distribution running on the Windows Subsystem for Linux has its own Linux user accounts and passwords. You will have to configure a Linux user account any time you add a distribution, reinstall, or reset. Linux user accounts are not only independent per distribution, they are also independent from your Windows user account, so you can add or remove a Linux user account without changing your Windows credentials.
Sudo is a special user group in Linux. Members of that group are allowed to run commands and apps as a root user (i.e. elevated). The sudo group is available when the sudo package is installed. Besides the group, it provides the sudo command, that should be used to elevated a command or app, e.g. $ sudo vim /etc/default/keyboard
.
Find Users Included to Sudo
First of all, you might be interested in learning which user accounts in your WSL distro are permitted to use the sudo
command.
- Run your WSL Linux distro, e.g. Ubuntu.
- Type the following command and hit the Enter key:
grep sudo /etc/group| cut -d: -f4
.
The cut
command extracts values for column #4 from the /etc/group file, that contains everything related to groups in Linux. It is delimited by ":". The grep command prints only the line that contains the 'sudo' line.
It is worth noting that in certain distros, the sudo command might be configured to use any other group instead of the 'sudo' group. For example, in Arch Linux, members of the 'wheel' group are allowed to use sudo. There is no 'sudo' group in this distro. In such a case, take a look at the /etc/sudoers file, and read its man page (run $ man sudoers
).
Tip: You can list groups a user account included to with the groups <username> command. E.g.
$ groups winaero
To Add a User to Sudo in WSL Linux in Windows 10
- Run your WSL Linux distro, e.g. Ubuntu, under the root user or the user that is already allowed to use sudo.
- Type the command:
sudo usermod -a -G sudo <username>
. Substitute <username> in the command with the actual user account name you want to add to the sudo group. - Alternatively, you can use the following command:
gpasswd -a <username> sudo
. The result will be the same.
To Remove a User from Sudo in WSL Linux in Windows 10,
- Run your WSL Linux distro, e.g. Ubuntu, under the root user or the user that is already allowed to use sudo.
- Type the command:
sudo gpasswd -d <username> sudo
. Substitute <username> in the command with the actual user account name you want to remove from the sudo group.
That's it.
Related articles:
- Remove User From WSL Linux Distro in Windows 10
- Add User to WSL Linux Distro in Windows 10
- Update and Upgrade WSL Linux Distro in Windows 10
- Run WSL Linux Distro as Specific User in Windows 10
- Reset and Unregister WSL Linux Distro in Windows 10
- Reset Password for WSL Linux Distro in Windows 10
- All Ways to Run WSL Linux Distro in Windows 10
- Set Default WSL Linux Distro in Windows 10
- Find Running WSL Linux Distros in Windows 10
- Terminate Running WSL Linux Distro in Windows 10
- Remove Linux from Navigation Pane in Windows 10
- Export and Import WSL Linux Distro in Windows 10
- Access WSL Linux Files from Windows 10
- Enable WSL in Windows 10
- Set Default User for WSL in Windows 10
- Windows 10 Build 18836 Shows WSL/Linux File System in File Explorer
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options: