欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

WSL] ubuntu 无法通过 wsl 中的 useradd 添加用户 - 原因

最编程 2024-10-06 07:01:34
...

这是因为wsl2和传统的VMware类型虚拟机有一定区别,其不支持使用useradd直接添加用户,而是提供了adduser命令来处理。

准确来说是Ubuntu22.04不支持useradd,而是需要使用adduser。

sudo adduser <新的用户名>

使用这个命令则可以正常创建用户,如下所示我创建了一个用户名为git的用户。需要填写用户信息的部分直接回车跳过即可。

root:~# sudo adduser git
Adding user `git' ...
Adding new group `git' (1001) ...
Adding new user `git' (1001) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y
root:~# ls /home
git  wsl

使用id命令可以查看新用户的用户组以及uid等相关信息。

root:~# id git
uid=1001(git) gid=1001(git) groups=1001(git)

注意,该新用户不在/etc/sudoers文件中,无法使用sudo命令。需要修改该文件,在如下位置之后添加一行

root ALL=(ALL:ALL) ALL
# 新增此行
用户名 ALL=(ALL:ALL) ALL

修改了之后,子用户才能执行sudo命令。