用 useradd 创建新用户
一条命令新建带主目录和默认 Shell 的本地账号,再设密码即可使用。
运行 sudo useradd -m -s /bin/bash alice 创建账号和主目录,再用 sudo passwd alice 设置密码。
01命令
命令
$ sudo useradd -m -s /bin/bash alice && sudo passwd alice02作用说明
-m 会创建 /home/alice 并从 /etc/skel 复制初始配置,不加的话账号没有主目录,很多程序会出问题;-s 指定登录 Shell。useradd 刻意不设置密码——新账号默认是锁定状态(只能 sudo su 过去用),必须跑一次 passwd 才能登录。桌面发行版上 adduser 是更友好的交互式包装,但 useradd 才是各发行版通用、可写进脚本的核心工具。
03风险
!注意: 建完账号不设密码或 SSH 密钥,就会留下一个锁死又容易被遗忘的账号;注意各发行版 /etc/login.defs 里 HOME_MODE 的默认值。
04撤销
撤销
$ sudo userdel -r alice05来源
- useradd(8) — create a new user依据: Documents -m/--create-home and -s/--shell options plus -c comment; confirmed the strings -m, --create-home and -s in the man page text.
- useradd(8) — Debian man page (passwd)依据: Man page documents -m creating the user home directory and -s setting the login shell; text contains -m and create home descriptions.
最后验证: 2026-09-19 · 分类: 系统与设置