如何使用 Bash 脚本自动添加用户帐户和密码?
我需要能够在 Linux (Fedora 10) 上创建用户帐户,并通过 bash 脚本(或其他方式,如果需要的话)自动分配密码。
通过 Bash 创建用户很容易,例如:
[whoever@server ]# /usr/sbin/useradd newuser
是否可以在 Bash 中分配密码,功能与此类似,但会自动执行:
[whoever@server ]# passwd newuser
Changing password for user testpass.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[whoever@server ]#
I need to have the ability to create user accounts on my Linux (Fedora 10) and automatically assign a password via a bash script(or otherwise, if need be).
It's easy to create the user via Bash e.g.:
[whoever@server ]# /usr/sbin/useradd newuser
Is it possible to assign a password in Bash, something functionally similar to this, but automatically:
[whoever@server ]# passwd newuser
Changing password for user testpass.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[whoever@server ]#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
您还可以使用chpasswd:
因此,您可以更改用户
用户名 到
new_password
。You could also use chpasswd:
so, you change password for user
username
tonew_password
.您可以运行 passwd 命令并向其发送管道输入。所以,做类似的事情:
You can run the passwd command and send it piped input. So, do something like:
我也问自己同样的问题,并且不想依赖 Python 脚本。
这是在一个 bash 行中添加具有已定义密码的用户的行:
$
。-6
标志指定的SHA-512
哈希值。请参阅openssl passwd --help
了解更多选项。编辑:自从 c87a7f31a3 选项
-crypt
被删除。因此在上面的示例中替换为-6
标志。I was asking myself the same thing, and didn't want to rely on a Python script.
This is the line to add a user with a defined password in one bash line:
$
in this case.SHA-512
hash, specified by the-6
flag. Seeopenssl passwd --help
for more options.Edit: Since c87a7f31a3 the option
-crypt
is removed. Therefore replaced with-6
flag in the example above.下面的代码在 Ubuntu 14.04 上运行。在其他版本/Linux 变体中使用它之前先尝试一下。
https://en.wikipedia.org/wiki/Gecos_field gecos 字段或 GECOS 字段是Unix 和类似操作系统上 /etc/passwd 文件中的条目。它通常用于记录有关帐户或其用户的一般信息,例如他们的真实姓名和电话号码。 GECOS的意思是通用电气综合操作系统,在GE的大型系统部门出售给霍尼韦尔后更名为GCOS。
指定这一点非常有用。设置此字符串,至少等于用户名,因为它使用户可以区分,例如,当他们列在显示管理器的登录屏幕上时。
The code below worked in Ubuntu 14.04. Try before you use it in other versions/linux variants.
https://en.wikipedia.org/wiki/Gecos_field The gecos field, or GECOS field is an entry in the /etc/passwd file on Unix, and similar operating systems. It is typically used to record general information about the account or its user(s) such as their real name and phone number. GECOS means General Electric Comprehensive Operating System, which has been renamed to GCOS when GE’s large systems division was sold to Honeywell.
This can be very useful to specify. Setting this string, at least equal to the username, as it makes the user distinguishable, e.g. when they are listed at the login screen of a display manager.
我喜欢 Tralemonkey 的 echo thePassword | 方法passwd theUsername --stdin 虽然它对我来说不太有效。然而这对我有用。
-e
是将\n
识别为换行符。sudo
是 Ubuntu 的 root 访问权限。双引号用于识别
$
并扩展变量。上面的命令将密码和换行符两次传递给
passwd
,这是passwd
所需要的。如果不使用变量,我认为这可能有效。
这里单引号就足够了。
I liked Tralemonkey's approach of
echo thePassword | passwd theUsername --stdin
though it didn't quite work for me as written. This however worked for me.-e
is to recognize\n
as new line.sudo
is root access for Ubuntu.The double quotes are to recognize
$
and expand the variables.The above command passes the password and a new line, two times, to
passwd
, which is whatpasswd
requires.If not using variables, I think this probably works.
Single quotes should suffice here.
以下内容适用于我并在 Ubuntu 14.04 上进行了测试。它是一个不需要任何用户输入的单行程序。
摘自@Tralemonkey
The following works for me and tested on Ubuntu 14.04. It is a one liner that does not require any user input.
Taken from @Tralemonkey
单行创建具有主目录和密码的 sudo 用户。
Single liner to create a sudo user with home directory and password.
您可以使用 -p 选项。
不幸的是,这确实需要您自己对密码进行哈希处理(其中 passwd 会为您做这件事)。不幸的是,似乎没有一个标准实用程序来散列某些数据,因此您必须自己编写。
这是我编写的一个 Python 脚本,用于为您进行加密。假设您将其命名为 pcrypt,则您可以将上述命令行写入:
需要注意的几个警告。
这是 pcrypt:
You can use the -p option.
Unfortunately, this does require you to hash the password yourself (where passwd does that for you). Unfortunately, there does not seem to be a standard utility to hash some data so you'll have to write that yourself.
Here's a little Python script I whipped up to do the encryption for you. Assuming you called it pcrypt, you would then write your above command line to:
A couple of warnings to be aware of.
and here's pcrypt:
--stdin
在 Debian 上不起作用。它说:这对我有用:
在这里我们可以找到其他一些好方法:
--stdin
doesn't work on Debian. It says:This worked for me:
Here we can find some other good ways:
您可以在 bash 脚本中使用 Expect。
来自 http://www.seanodonnell.com/code/?id=21
You can use expect in your bash script.
From http://www.seanodonnell.com/code/?id=21
我知道我会在几年后到来,但我不敢相信没有人建议使用 usermod。
天哪,万一有人想在旧版 HPUX 上执行此操作,您可以使用
usermod.sam
。仅当执行脚本的人是当前用户时才需要 -F。当然,您不需要使用 Perl 来创建哈希。您可以使用 openssl 或许多其他命令来代替它。
I know I'm coming at this years later, but I can't believe no one suggested usermod.
Hell, just in case someone wants to do this on an older HPUX you can use
usermod.sam
.The -F is only needed if the person executing the script is the current user. Of course you don't need to use Perl to create the hash. You could use openssl or many other commands in its place.
我已经在自己的 shell 脚本中进行了测试。
$new_username
表示新创建的用户$new_password
表示新密码对于 CentOS
对于 Debian/Ubuntu
对于 OpenSUSE
I've tested in my own shell script.
$new_username
means newly created user$new_password
means newly passwordFor CentOS
For Debian/Ubuntu
For OpenSUSE
这是一个可以为您完成此操作的脚本......
如果需要,您可以添加用户列表(或仅一个用户),一次完成所有操作,每个用户都有不同的密码。作为奖励,您会在脚本末尾看到每个用户密码的列表。 ....如果您愿意,您可以添加一些用户维护选项,
例如:
到将设置密码期限等的脚本。
=======
===========
希望这会有所帮助。
干杯,
卡雷尔
Here is a script that will do it for you .....
You can add a list of users (or just one user) if you want, all in one go and each will have a different password. As a bonus you are presented at the end of the script with a list of each users password. .... If you want you can add some user maintenance options
like:
to the script that will set the password age and so on.
=======
===========
Hope this helps.
Cheers,
Carel
该解决方案适用于 Debian 和 Red Hat。取决于 perl,使用 sha-512 哈希值:
. '$salt' . '\
用法:
它可以有效地用作单行代码,但您必须自己在正确的位置指定密码、盐和用户名:
)") $1
用法:
它可以有效地用作单行代码,但您必须自己在正确的位置指定密码、盐和用户名:
The solution that works on both Debian and Red Hat. Depends on perl, uses sha-512 hashes:
. '$salt' . '\
Usage:
It can effectively be used as a one-liner, but you'll have to specify the password, salt and username at the right places yourself:
)") $1
Usage:
It can effectively be used as a one-liner, but you'll have to specify the password, salt and username at the right places yourself:
来自 IBM (https://www.ibm. com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.cmds1/chpasswd.htm):
创建一个文本文件,例如 text.txt 并使用用户:密码对填充它,如下所示:
保存 text.txt文件,然后运行
就是这样。该解决方案 (a) 可扩展,并且 (b) 不涉及在命令行上打印密码。
From IBM (https://www.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.cmds1/chpasswd.htm):
Create a text file, say text.txt and populate it with user:password pairs as follows:
Save the text.txt file, and run
That's it. The solution is (a) scalable and (b) does not involve printing passwords on the command line.
Tralemonkey 的解决方案几乎对我也有效……但不完全有效。我最终这样做了:
他的解决方案没有包含 2 个关键细节,
-n
阻止回显将\n
添加到正在加密的密码中,在我的例子中,单引号可以保护内容不被 shell (bash) 解释。顺便说一句,我在 CentOS 5.6 系统上以 root 身份运行了这个命令,以防有人想知道。
Tralemonkey's solution almost worked for me as well ... but not quite. I ended up doing it this way:
2 key details his solution didn't include, the
-n
keeps echo from adding a\n
to the password that is getting encrypted, and the single quotes protect the contents from being interpreted by the shell (bash) in my case.BTW I ran this command as root on a CentOS 5.6 system in case anyone is wondering.
对于 RedHat / CentOS,以下是创建用户、添加密码并使用户成为 sudoer 的代码:
For RedHat / CentOS here's the code that creates a user, adds the passwords and makes the user a sudoer:
用法:
./my_add_user.sh 用户密码
代码:
usage:
./my_add_user.sh USER PASSWD
code:
请使用 sudo 权限运行以下脚本,以便通过脚本创建用户。
注意:此脚本支持所有 Linux 操作系统,如 Redhat、Centos、Ubuntu、suse、kali、Arch、Bitname、BSD...等
Kindly run below script with sudo permission for creating a user by script.
Note: This script supports all linux OSs like Redhat, Centos, Ubuntu, suse, kali, Arch, Bitname, BSD....etc