我想使用adduser
命令通过一个Shell脚本添加一个用户(禁用密码)。
默认情况下,adduser
会提示你输入各种值(例如全名)。是否有办法通过命令行提交这些值?或者我需要使用useradd
命令代替?
我想使用adduser
命令通过一个Shell脚本添加一个用户(禁用密码)。
默认情况下,adduser
会提示你输入各种值(例如全名)。是否有办法通过命令行提交这些值?或者我需要使用useradd
命令代替?
--gecos
选项跳过chfn
的交互部分。adduser --disabled-password --gecos "" username
--gecos GECOS
Set the gecos field for the new entry generated. adduser will
not ask for finger information if this option is given.
全名,房间号码,工作电话,家庭电话
,尽管man页提到了指纹信息
详情-维基百科。adduser
,--gecos
选项被--comment
取代。--comment comment
Set the comment field for the new entry generated. adduser will
not ask for the information if this option is given. This field
is also known under the name GECOS field and contains informa‐
tion that is used by the finger command. This used to be the
--gecos option, which is deprecated and will be removed after
Debian bookworm. Valid Modes: adduser, adduser --system.
useradd
也可以添加用户,而且似乎没有任何提示的形式。
useradd -m -p <encryptedPassword> -s /bin/bash <user>
-m
,--create-home
:创建用户的主目录-p
,--password
:指定用户密码;跳过以禁用密码-s
,--shell
:用户登录时的默认Shell
留空将使用/etc/default/useradd
文件中SHELL
变量指定的默认登录Shell
<user>
替换为登录名<encryptedPassword>
替换为加密的密码有很多实现可以生成哈希密码。整个过程就是生成你的哈希密码。
输出格式:哈希机制($6
代表sha-512),随机盐(第二个美元符号后的八个字节$ASDF1234
),剩余部分是有效载荷。
mkpasswd mkpasswd -m sha-512
(mkpasswd
由whois
软件包提供)
输出格式:前两个字节是盐,剩余部分是有效载荷。整个过程就是生成你的哈希密码。
mkpasswd
(由whois
软件包提供)openssl passwd -crypt
perl -e "print crypt('password');"
python3 -c 'import crypt; print(crypt.crypt("password"))'
adduser
和useradd
是不同的命令,这确实有些令人困惑。 - ThorSummoner-v
或-vv
或-vvv
来查看问题所在,并在Stack Overflow或Google上搜索你的错误信息。 - ThorSummonersudo sed -i"" -e "s/PasswordAuthentication no/PasswordAuthentication yes/" /etc/ssh/sshd_config
和 sudo service ssh restart
- JSBachubuntu
Docker容器中),然后从/etc/shadow
中提取散列值。 - schnattereruseradd
命令会忽略用户默认设置,比如默认的shell。 - Kenny Evittusername=jovyan
password=jovyan
adduser --gecos "" --disabled-password $username
chpasswd <<<"$username:$password"
chpasswd
。比起手动使用mkpasswd
生成哈希值来说,这种方法简单得多,而且更适合脚本操作。 - J.D. Mallenecho "${username}:${password}" | chpasswd
来替换chpassword行。 - boly38
adduser
具有幂等性吗?(如果用户已经存在,保持静默而不是提示错误) - Ragtimeadduser
/useradd
之前使用类似getent passwd <username>
的方法来测试用户是否存在。 - Zoke