在 Linux 下,可以使用以下单条命令创建具有 root 权限的用户(即将用户加入 sudo
组或 wheel
组,具体取决于发行版):
useradd -m -G sudo,wheel -s /bin/bash username && echo "username:password" | chpasswd
useradd -m
:创建用户并创建家目录-G sudo,wheel
:将用户同时加入 sudo 和 wheel 组(兼容不同发行版)-s /bin/bash
:设置默认 shell 为 bash&&
:前一个命令成功后执行下一个命令echo "username:password" | chpasswd
:设置用户密码(将 username 和 password 替换为你需要的值)adduser --gecos "" --disabled-password username && echo "username:password" | chpasswd && usermod -aG sudo username
生产环境中建议:
1. 使用 passwd
命令交互式设置更安全的密码
2. 考虑使用 SSH 密钥认证代替密码
3. 限制 sudo 权限(通过编辑 /etc/sudoers)