【WSL】wsl中ubuntu无法通过useradd添加用户

本文首发于 ❄️慕雪的寒舍

wsl中ubuntu无法通过useradd添加用户,应该使用adduser命令。

情况说明

在wsl2中的ubuntu22.04无法使用useradd添加用户,表现为执行命令后,只出现该用户和用户组,但/home目录下没有出现该用户的家目录文件夹,且无法使用su - 用户名成功切换到该用户上。

如下所示,我先从wsl子用户切换到root用户,然后执行useradd命令,发现用户创建没有完全成功。

bash 复制代码
wsl:~$ sudo su - 

root:~# useradd git
root:~# ls /home
wsl
root:~# sudo adduser git
adduser: The user `git' already exists.
root:~# userdel -r git
userdel: git mail spool (/var/mail/git) not found
userdel: git home directory (/home/git) not found
root:~# userdel -r git
userdel: user 'git' does not exist

原因

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

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

bash 复制代码
sudo adduser <新的用户名>

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

bash 复制代码
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等相关信息。

bash 复制代码
root:~# id git
uid=1001(git) gid=1001(git) groups=1001(git)

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

bash 复制代码
root ALL=(ALL:ALL) ALL
# 新增此行
用户名 ALL=(ALL:ALL) ALL

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

相关推荐
hakesashou42 分钟前
python如何比较字符串
linux·开发语言·python
Ljubim.te1 小时前
Linux基于CentOS学习【进程状态】【进程优先级】【调度与切换】【进程挂起】【进程饥饿】
linux·学习·centos
cooldream20091 小时前
Linux性能调优技巧
linux
Elastic 中国社区官方博客1 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
QMCY_jason2 小时前
Ubuntu 安装RUST
linux·ubuntu·rust
苦逼IT运维2 小时前
YUM 源与 APT 源的详解及使用指南
linux·运维·ubuntu·centos·devops
第六五2 小时前
ubuntu命令行连接wifi
服务器·ubuntu
仍有未知等待探索2 小时前
Linux 传输层UDP
linux·运维·udp
没有余地 EliasJie2 小时前
Windows Ubuntu下搭建深度学习Pytorch训练框架与转换环境TensorRT
pytorch·windows·深度学习·ubuntu·pycharm·conda·tensorflow