如何使用Bash脚本自动添加用户帐户和密码?

时间:2022-06-25 15:43:33

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).

我需要能够在我的Linux (Fedora 10)上创建用户帐户,并通过bash脚本自动分配密码(如果需要的话)。

It's easy to create the user via Bash e.g.:

很容易通过Bash创建用户。

[whoever@server ]#  /usr/sbin/useradd newuser

Is it possible to assign a password in Bash, something functionally similar to this, but automatically:

是否可能在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 ]#

19 个解决方案

#1


88  

You can run the passwd command and send it piped input. So, do something like:

您可以运行passwd命令并发送管道输入。所以,做一些类似:

echo thePassword | passwd theUsername --stdin

#2


159  

You could also use chpasswd:

你也可以使用chpasswd:

echo username:new_password | chpasswd

so, you change password for user username to new_password.

因此,您将用户名的密码更改为new_password。

#3


55  

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:

我问自己同样的问题,不想依赖Python脚本。这是在一个bash行中添加具有定义密码的用户的行:

/usr/sbin/useradd -p \`openssl passwd -1 $PASS\` $USER

#4


34  

The code below worked in Ubuntu 14.04. Try before you use it in other versions/linux variants.

下面的代码在Ubuntu 14.04中工作。在其他版本/linux版本中使用它之前,先尝试一下。

# quietly add a user without password
adduser --quiet --disabled-password --shell /bin/bash --home /home/newuser --gecos "User" newuser

# set password
echo "newuser:newpassword" | chpasswd

#5


29  

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.

我喜欢Tralemonkey的echo thePassword | passwd用户名——stdin,尽管它对我来说并不是很有效。这对我来说很有用。

echo -e "$password\n$password\n" | sudo passwd $user

-e is to recognize \n as new line.

-e是将\n视为新行。

sudo is root access for Ubuntu.

sudo是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 what passwd requires.

上面的命令将密码和一条新行传递给passwd,这是passwd需要的。

If not using variables, I think this probably works.

如果不使用变量,我认为这可能行得通。

echo -e 'password\npassword\n' | sudo passwd username

Single quotes should suffice here.

单引号就足够了。

#6


20  

The following works for me and tested on Ubuntu 14.04. It is a one liner that does not require any user input.

以下是我的工作,并在Ubuntu 14.04上进行了测试。它是一种不需要任何用户输入的内衬。

sudo useradd -p $(openssl passwd -1 $PASS) $USERNAME

Taken from @Tralemonkey

取自@Tralemonkey

#7


11  

You can use the -p option.

你可以使用-p选项。

useradd -p encrypted_password newuser

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.

不幸的是,这需要您自己来哈希密码(passwd为您做的)。不幸的是,似乎没有一个标准的实用程序来哈希一些数据,所以你必须自己写。

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:

这里有一个小的Python脚本,我为你做了加密。假设您将其命名为pcrypt,那么您可以将上面的命令行写入:

useradd -p $(pcrypt ${passwd}) newuser

A couple of warnings to be aware of.

需要注意的几个警告。

  1. While pcrypt is running, the plaintext will be visible to any user via the ps command.
  2. 当pcrypt正在运行时,明文将通过ps命令对任何用户可见。
  3. pcrypt uses the old style crypt function - if you are using something more moderns like an MD5 hash, you'll need to change pcrypt.
  4. pcrypt使用旧式的crypt函数——如果您使用的是像MD5散列那样的更现代的东西,您需要修改pcrypt。

and here's pcrypt:

这里是pcrypt:

#!/usr/bin/env python

import crypt
import sys
import random

saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def salt():
    return random.choice(saltchars) + random.choice(saltchars)

def hash(plain):
    return crypt.crypt(arg, salt())

if __name__ == "__main__":
    random.seed()
    for arg in sys.argv[1:]:
        sys.stdout.write("%s\n" % (hash(arg),))

#8


7  

--stdin doesn't work on Debian. It says:

——stdin对Debian没有作用。它说:

`passwd: unrecognized option '--stdin'`

This worked for me:

这工作对我来说:

#useradd $USER
#echo "$USER:$SENHA" | chpasswd

Here we can find some other good ways:

在这里我们可以找到其他的好方法:

#9


5  

You can use expect in your bash script.

您可以在bash脚本中使用expect。

From http://www.seanodonnell.com/code/?id=21

从http://www.seanodonnell.com/code/?id=21

#!/usr/bin/expect 
######################################### 
#$ file: htpasswd.sh 
#$ desc: Automated htpasswd shell script 
######################################### 
#$ 
#$ usage example: 
#$ 
#$ ./htpasswd.sh passwdpath username userpass 
#$ 
###################################### 

set htpasswdpath [lindex $argv 0] 
set username [lindex $argv 1] 
set userpass [lindex $argv 2] 

# spawn the htpasswd command process 
spawn htpasswd $htpasswdpath $username 

# Automate the 'New password' Procedure 
expect "New password:" 
send "$userpass\r" 

expect "Re-type new password:" 
send "$userpass\r"

#10


4  

I know I'm coming at this years later, but I can't believe no one suggested usermod.

我知道我是在这几年来的,但是我不能相信没有人推荐usermod。

usermod --password `perl -e "print crypt('password','sa');"` root

Hell, just in case someone wants to do this on an older HPUX you can use usermod.sam.

如果有人想在旧的HPUX上做这个,你可以使用usermod.sam。

/usr/sam/lbin/usermod.sam -F -p `perl -e "print crypt('password','sa');"` username

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.

如果执行脚本的人是当前用户,则只需要-F。当然,您不需要使用Perl来创建散列。您可以在其位置使用openssl或其他许多命令。

#11


4  

Single liner to create a sudo user with home directory and password.

单一的班轮创建一个sudo用户与主目录和密码。

useradd -m -p $(openssl passwd -1 ${PASSWORD}) -s /bin/bash -G sudo ${USERNAME}

#12


3  

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:

如:

chage -m 18 $user
chage -M 28 $user

to the script that will set the password age and so on.

对于设置密码年龄等的脚本。

=======

= = = = = = =

#!/bin/bash

# Checks if you have the right privileges
if [ "$USER" = "root" ]
then

# CHANGE THIS PARAMETERS FOR A PARTICULAR USE
PERS_HOME="/home/"
PERS_SH="/bin/bash"

   # Checks if there is an argument
   [ $# -eq 0 ] && { echo >&2 ERROR: You may enter as an argument a text file containing users, one per line. ; exit 1; }
   # checks if there a regular file
   [ -f "$1" ] || { echo >&2 ERROR: The input file does not exists. ; exit 1; }
   TMPIN=$(mktemp)
   # Remove blank lines and delete duplicates 
   sed '/^$/d' "$1"| sort -g | uniq > "$TMPIN"

   NOW=$(date +"%Y-%m-%d-%X")
   LOGFILE="AMU-log-$NOW.log"

   for user in $(more "$TMPIN"); do
      # Checks if the user already exists.
      cut -d: -f1 /etc/passwd | grep "$user" > /dev/null
      OUT=$?
      if [ $OUT -eq 0 ];then
         echo >&2 "ERROR: User account: \"$user\" already exists."
         echo >&2 "ERROR: User account: \"$user\" already exists." >> "$LOGFILE"
      else
         # Create a new user
         /usr/sbin/useradd -d "$PERS_HOME""$user" -s "$PERS_SH" -m "$user"
         # passwdgen must be installed
         pass=$(passwdgen -paq --length 8)
         echo $pass | passwd --stdin $user
         # save user and password in a file
         echo -e $user"\t"$pass >> "$LOGFILE"
         echo "The user \"$user\" has been created and has the password: $pass"
      fi
   done
   rm -f "$TMPIN"
   exit 0
else
   echo >&2 "ERROR: You must be a root user to execute this script."
   exit 1
fi

===========

= = = = = = = = = = =

Hope this helps.

希望这个有帮助。

Cheers, Carel

欢呼,卡路

#13


2  

I've tested in my own shell script.

我已经在自己的shell脚本中测试过了。

  • $new_username means newly created user
  • $new_username是新创建的用户。
  • $new_password means newly password
  • 美元new_password意味着新密码

For CentOS

echo "$new_password" | passwd --stdin "$new_username"

For Debian/Ubuntu

echo "$new_username:$new_password" | chpasswd

For OpenSUSE

echo -e "$new_password\n$new_password" | passwd "$new_username"

#14


1  

The solution that works on both Debian and Red Hat. Depends on perl, uses sha-512 hashes:

Debian和Red Hat的解决方案。依赖于perl,使用sha-512哈希:

cat userpassadd
    #!/usr/bin/env bash

    salt=$(cat /dev/urandom | tr -dc A-Za-z0-9/_- | head -c16)
    useradd -p $(perl -e "print crypt('$2', '\$6\$' . '$salt' . '\$')") $1

Usage:

用法:

userpassadd jim jimslongpassword

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:

它可以有效地作为一行程序使用,但是您必须在正确的地方指定密码、盐和用户名:

useradd -p $(perl -e "print crypt('pass', '\$6\$salt\$')") username

#15


1  

From IBM (https://www.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.cmds1/chpasswd.htm):

从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:

创建一个文本文件,比如文本。txt和用户:密码对如下:

user1:password1
user2:password2
...
usern:passwordn

Save the text.txt file, and run

保存文本。txt文件,并运行

cat text.txt | chpassword

That's it. The solution is (a) scalable and (b) does not involve printing passwords on the command line.

就是这样。解决方案是(a)可伸缩的,(b)不涉及在命令行上打印密码。

#16


0  

Tralemonkey's solution almost worked for me as well ... but not quite. I ended up doing it this way:

Tralemonkey的解决方案几乎对我也起作用了……但不完全是。我这样做:

echo -n '$#@password@#$' | passwd myusername --stdin

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.

他的解决方案没有包含的两个关键细节,-n可以避免在加密的密码中添加一个\n,而单引号可以保护内容不被shell (bash)解释。

BTW I ran this command as root on a CentOS 5.6 system in case anyone is wondering.

顺便说一下,我在CentOS 5.6系统上运行这个命令,以防有人想知道。

#17


0  

{ echo $password; echo $password; } | passwd $username 

#18


0  

For RedHat / CentOS here's the code that creates a user, adds the passwords and makes the user a sudoer:

对于RedHat / CentOS,这是创建用户的代码,添加密码并使用户成为sudoer:

#!/bin/sh
echo -n "Enter username: "
read uname

echo -n "Enter password: "
read -s passwd

adduser "$uname"
echo $uname:$passwd | sudo chpasswd

gpasswd wheel -a $uname

#19


0  

usage: ./my_add_user.sh USER PASSWD

用法:。/ my_add_user。上海用户密码

code:

代码:

#!/bin/bash
# my_add_user.sh

if [ "$#" -lt 2 ] 
 then
       echo "$0 username passwd"
       exit
fi

user=$1
passwd=$2

useradd $user -d /data/home/$user  -m  ;
echo $passwd | passwd $user --stdin;

#1


88  

You can run the passwd command and send it piped input. So, do something like:

您可以运行passwd命令并发送管道输入。所以,做一些类似:

echo thePassword | passwd theUsername --stdin

#2


159  

You could also use chpasswd:

你也可以使用chpasswd:

echo username:new_password | chpasswd

so, you change password for user username to new_password.

因此,您将用户名的密码更改为new_password。

#3


55  

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:

我问自己同样的问题,不想依赖Python脚本。这是在一个bash行中添加具有定义密码的用户的行:

/usr/sbin/useradd -p \`openssl passwd -1 $PASS\` $USER

#4


34  

The code below worked in Ubuntu 14.04. Try before you use it in other versions/linux variants.

下面的代码在Ubuntu 14.04中工作。在其他版本/linux版本中使用它之前,先尝试一下。

# quietly add a user without password
adduser --quiet --disabled-password --shell /bin/bash --home /home/newuser --gecos "User" newuser

# set password
echo "newuser:newpassword" | chpasswd

#5


29  

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.

我喜欢Tralemonkey的echo thePassword | passwd用户名——stdin,尽管它对我来说并不是很有效。这对我来说很有用。

echo -e "$password\n$password\n" | sudo passwd $user

-e is to recognize \n as new line.

-e是将\n视为新行。

sudo is root access for Ubuntu.

sudo是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 what passwd requires.

上面的命令将密码和一条新行传递给passwd,这是passwd需要的。

If not using variables, I think this probably works.

如果不使用变量,我认为这可能行得通。

echo -e 'password\npassword\n' | sudo passwd username

Single quotes should suffice here.

单引号就足够了。

#6


20  

The following works for me and tested on Ubuntu 14.04. It is a one liner that does not require any user input.

以下是我的工作,并在Ubuntu 14.04上进行了测试。它是一种不需要任何用户输入的内衬。

sudo useradd -p $(openssl passwd -1 $PASS) $USERNAME

Taken from @Tralemonkey

取自@Tralemonkey

#7


11  

You can use the -p option.

你可以使用-p选项。

useradd -p encrypted_password newuser

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.

不幸的是,这需要您自己来哈希密码(passwd为您做的)。不幸的是,似乎没有一个标准的实用程序来哈希一些数据,所以你必须自己写。

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:

这里有一个小的Python脚本,我为你做了加密。假设您将其命名为pcrypt,那么您可以将上面的命令行写入:

useradd -p $(pcrypt ${passwd}) newuser

A couple of warnings to be aware of.

需要注意的几个警告。

  1. While pcrypt is running, the plaintext will be visible to any user via the ps command.
  2. 当pcrypt正在运行时,明文将通过ps命令对任何用户可见。
  3. pcrypt uses the old style crypt function - if you are using something more moderns like an MD5 hash, you'll need to change pcrypt.
  4. pcrypt使用旧式的crypt函数——如果您使用的是像MD5散列那样的更现代的东西,您需要修改pcrypt。

and here's pcrypt:

这里是pcrypt:

#!/usr/bin/env python

import crypt
import sys
import random

saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def salt():
    return random.choice(saltchars) + random.choice(saltchars)

def hash(plain):
    return crypt.crypt(arg, salt())

if __name__ == "__main__":
    random.seed()
    for arg in sys.argv[1:]:
        sys.stdout.write("%s\n" % (hash(arg),))

#8


7  

--stdin doesn't work on Debian. It says:

——stdin对Debian没有作用。它说:

`passwd: unrecognized option '--stdin'`

This worked for me:

这工作对我来说:

#useradd $USER
#echo "$USER:$SENHA" | chpasswd

Here we can find some other good ways:

在这里我们可以找到其他的好方法:

#9


5  

You can use expect in your bash script.

您可以在bash脚本中使用expect。

From http://www.seanodonnell.com/code/?id=21

从http://www.seanodonnell.com/code/?id=21

#!/usr/bin/expect 
######################################### 
#$ file: htpasswd.sh 
#$ desc: Automated htpasswd shell script 
######################################### 
#$ 
#$ usage example: 
#$ 
#$ ./htpasswd.sh passwdpath username userpass 
#$ 
###################################### 

set htpasswdpath [lindex $argv 0] 
set username [lindex $argv 1] 
set userpass [lindex $argv 2] 

# spawn the htpasswd command process 
spawn htpasswd $htpasswdpath $username 

# Automate the 'New password' Procedure 
expect "New password:" 
send "$userpass\r" 

expect "Re-type new password:" 
send "$userpass\r"

#10


4  

I know I'm coming at this years later, but I can't believe no one suggested usermod.

我知道我是在这几年来的,但是我不能相信没有人推荐usermod。

usermod --password `perl -e "print crypt('password','sa');"` root

Hell, just in case someone wants to do this on an older HPUX you can use usermod.sam.

如果有人想在旧的HPUX上做这个,你可以使用usermod.sam。

/usr/sam/lbin/usermod.sam -F -p `perl -e "print crypt('password','sa');"` username

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.

如果执行脚本的人是当前用户,则只需要-F。当然,您不需要使用Perl来创建散列。您可以在其位置使用openssl或其他许多命令。

#11


4  

Single liner to create a sudo user with home directory and password.

单一的班轮创建一个sudo用户与主目录和密码。

useradd -m -p $(openssl passwd -1 ${PASSWORD}) -s /bin/bash -G sudo ${USERNAME}

#12


3  

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:

如:

chage -m 18 $user
chage -M 28 $user

to the script that will set the password age and so on.

对于设置密码年龄等的脚本。

=======

= = = = = = =

#!/bin/bash

# Checks if you have the right privileges
if [ "$USER" = "root" ]
then

# CHANGE THIS PARAMETERS FOR A PARTICULAR USE
PERS_HOME="/home/"
PERS_SH="/bin/bash"

   # Checks if there is an argument
   [ $# -eq 0 ] && { echo >&2 ERROR: You may enter as an argument a text file containing users, one per line. ; exit 1; }
   # checks if there a regular file
   [ -f "$1" ] || { echo >&2 ERROR: The input file does not exists. ; exit 1; }
   TMPIN=$(mktemp)
   # Remove blank lines and delete duplicates 
   sed '/^$/d' "$1"| sort -g | uniq > "$TMPIN"

   NOW=$(date +"%Y-%m-%d-%X")
   LOGFILE="AMU-log-$NOW.log"

   for user in $(more "$TMPIN"); do
      # Checks if the user already exists.
      cut -d: -f1 /etc/passwd | grep "$user" > /dev/null
      OUT=$?
      if [ $OUT -eq 0 ];then
         echo >&2 "ERROR: User account: \"$user\" already exists."
         echo >&2 "ERROR: User account: \"$user\" already exists." >> "$LOGFILE"
      else
         # Create a new user
         /usr/sbin/useradd -d "$PERS_HOME""$user" -s "$PERS_SH" -m "$user"
         # passwdgen must be installed
         pass=$(passwdgen -paq --length 8)
         echo $pass | passwd --stdin $user
         # save user and password in a file
         echo -e $user"\t"$pass >> "$LOGFILE"
         echo "The user \"$user\" has been created and has the password: $pass"
      fi
   done
   rm -f "$TMPIN"
   exit 0
else
   echo >&2 "ERROR: You must be a root user to execute this script."
   exit 1
fi

===========

= = = = = = = = = = =

Hope this helps.

希望这个有帮助。

Cheers, Carel

欢呼,卡路

#13


2  

I've tested in my own shell script.

我已经在自己的shell脚本中测试过了。

  • $new_username means newly created user
  • $new_username是新创建的用户。
  • $new_password means newly password
  • 美元new_password意味着新密码

For CentOS

echo "$new_password" | passwd --stdin "$new_username"

For Debian/Ubuntu

echo "$new_username:$new_password" | chpasswd

For OpenSUSE

echo -e "$new_password\n$new_password" | passwd "$new_username"

#14


1  

The solution that works on both Debian and Red Hat. Depends on perl, uses sha-512 hashes:

Debian和Red Hat的解决方案。依赖于perl,使用sha-512哈希:

cat userpassadd
    #!/usr/bin/env bash

    salt=$(cat /dev/urandom | tr -dc A-Za-z0-9/_- | head -c16)
    useradd -p $(perl -e "print crypt('$2', '\$6\$' . '$salt' . '\$')") $1

Usage:

用法:

userpassadd jim jimslongpassword

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:

它可以有效地作为一行程序使用,但是您必须在正确的地方指定密码、盐和用户名:

useradd -p $(perl -e "print crypt('pass', '\$6\$salt\$')") username

#15


1  

From IBM (https://www.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.cmds1/chpasswd.htm):

从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:

创建一个文本文件,比如文本。txt和用户:密码对如下:

user1:password1
user2:password2
...
usern:passwordn

Save the text.txt file, and run

保存文本。txt文件,并运行

cat text.txt | chpassword

That's it. The solution is (a) scalable and (b) does not involve printing passwords on the command line.

就是这样。解决方案是(a)可伸缩的,(b)不涉及在命令行上打印密码。

#16


0  

Tralemonkey's solution almost worked for me as well ... but not quite. I ended up doing it this way:

Tralemonkey的解决方案几乎对我也起作用了……但不完全是。我这样做:

echo -n '$#@password@#$' | passwd myusername --stdin

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.

他的解决方案没有包含的两个关键细节,-n可以避免在加密的密码中添加一个\n,而单引号可以保护内容不被shell (bash)解释。

BTW I ran this command as root on a CentOS 5.6 system in case anyone is wondering.

顺便说一下,我在CentOS 5.6系统上运行这个命令,以防有人想知道。

#17


0  

{ echo $password; echo $password; } | passwd $username 

#18


0  

For RedHat / CentOS here's the code that creates a user, adds the passwords and makes the user a sudoer:

对于RedHat / CentOS,这是创建用户的代码,添加密码并使用户成为sudoer:

#!/bin/sh
echo -n "Enter username: "
read uname

echo -n "Enter password: "
read -s passwd

adduser "$uname"
echo $uname:$passwd | sudo chpasswd

gpasswd wheel -a $uname

#19


0  

usage: ./my_add_user.sh USER PASSWD

用法:。/ my_add_user。上海用户密码

code:

代码:

#!/bin/bash
# my_add_user.sh

if [ "$#" -lt 2 ] 
 then
       echo "$0 username passwd"
       exit
fi

user=$1
passwd=$2

useradd $user -d /data/home/$user  -m  ;
echo $passwd | passwd $user --stdin;