使用 Bash 创建用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29801896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create user using Bash
提问by soniccool
How can I create a user in linux using a bash script already knowing the username and password.
如何使用已经知道用户名和密码的 bash 脚本在 linux 中创建用户。
Lets say the username will be variable $username
and password will be $password
.
假设用户名是可变的$username
,密码是$password
.
I also dont want it to print anything. I simply want to run a .sh file and have it create the user with a static variable I defined.
我也不希望它打印任何东西。我只想运行一个 .sh 文件并让它使用我定义的静态变量创建用户。
I am having issues trying to accomplish this. Also I assume I need to encrypt the text version of my password in sha-512
我在尝试完成此操作时遇到问题。另外我假设我需要在 sha-512 中加密我的密码的文本版本
I tried this below no luck
我在下面试过这个没有运气
sudo useradd -m -s /bin/bash -p $password $username
Thanks!
谢谢!
回答by Etan Reisner
You haven't told us what exactly is going wrong but useradd
requires a crypted (or otherwise possibly) password. It might be better to just let passwd
do the work for you.
您还没有告诉我们究竟出了什么问题,但useradd
需要一个加密的(或可能的)密码。最好让passwd
您为您完成工作。
sudo useradd -m -s /bin/bash "$username"
echo "$password" | sudo passwd --stdin "$username"
回答by Jabba the Hutt
echo "$password" | sudo passwd --stdin "$username"
回答by f3rr
as root:
作为根:
adduser $username && echo "$username:$password" | chpasswd
回答by Francisco Tapia
#!/bin/bash
read -p "username: " username
useradd -m -s /bin/bash $username
passwd $username
try this v2.
试试这个 v2。