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

发布于 2024-08-19 21:54:57 字数 430 浏览 8 评论 0原文

我需要能够在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

千紇 2024-08-26 21:54:57

您还可以使用chpasswd

echo username:new_password | chpasswd

因此,您可以更改用户用户名 到 new_password

You could also use chpasswd:

echo username:new_password | chpasswd

so, you change password for user username to new_password.

甜`诱少女 2024-08-26 21:54:57

您可以运行 passwd 命令并向其发送管道输入。所以,做类似的事情:

echo thePassword | passwd theUsername --stdin

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

echo thePassword | passwd theUsername --stdin
℉絮湮 2024-08-26 21:54:57

我也问自己同样的问题,并且不想依赖 Python 脚本。

这是在一个 bash 行中添加具有已定义密码的用户的行:

useradd -p "$(openssl passwd -6 $PASS)" $USER
  • 请注意双引号以防止 shell 解释包含的特殊参数。在本例中即 $
  • 这使用由 -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:

useradd -p "$(openssl passwd -6 $PASS)" $USER
  • Note the double quote to prevent the shell interpreting included special parameters. Namely $ in this case.
  • This uses a SHA-512 hash, specified by the -6 flag. See openssl passwd --help for more options.

Edit: Since c87a7f31a3 the option -crypt is removed. Therefore replaced with -6 flag in the example above.

煮茶煮酒煮时光 2024-08-26 21:54:57

下面的代码在 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

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.

# 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

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.

小瓶盖 2024-08-26 21:54:57

我喜欢 Tralemonkey 的 echo thePassword | 方法passwd theUsername --stdin 虽然它对我来说不太有效。然而这对我有用。

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

-e 是将 \n 识别为换行符。

sudo 是 Ubuntu 的 root 访问权限。

双引号用于识别$并扩展变量。

上面的命令将密码和换行符两次传递给 passwd,这是 passwd 所需要的。

如果不使用变量,我认为这可能有效。

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

这里单引号就足够了。

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.

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

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

If not using variables, I think this probably works.

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

Single quotes should suffice here.

不弃不离 2024-08-26 21:54:57

以下内容适用于我并在 Ubuntu 14.04 上进行了测试。它是一个不需要任何用户输入的单行程序。

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

摘自@Tralemonkey

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

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

Taken from @Tralemonkey

囚你心 2024-08-26 21:54:57

单行创建具有主目录和密码的 sudo 用户。

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

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

useradd -m -p $(openssl passwd -1 ${PASSWORD}) -s /bin/bash -G sudo ${USERNAME}
秋意浓 2024-08-26 21:54:57

您可以使用 -p 选项。

useradd -p encrypted_password newuser

不幸的是,这确实需要您自己对密码进行哈希处理(其中 passwd 会为您做这件事)。不幸的是,似乎没有一个标准实用程序来散列某些数据,因此您必须自己编写。

这是我编写的一个 Python 脚本,用于为您进行加密。假设您将其命名为 pcrypt,则您可以将上述命令行写入:

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

需要注意的几个警告。

  1. 当 pcrypt 运行时,任何用户都可以通过 ps 命令看到明文。
  2. pcrypt 使用旧式的 crypt 函数 - 如果您使用的是更现代的东西,例如 MD5 哈希,则需要更改 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),))

You can use the -p option.

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.

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:

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 uses the old style crypt function - if you are using something more moderns like an MD5 hash, you'll need to change pcrypt.

and here's 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),))
揪着可爱 2024-08-26 21:54:57

--stdin 在 Debian 上不起作用。它说:

`passwd: unrecognized option '--stdin'`

这对我有用:

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

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

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

`passwd: unrecognized option '--stdin'`

This worked for me:

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

Here we can find some other good ways:

软的没边 2024-08-26 21:54:57

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

来自 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"

You can use expect in your bash script.

From 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"
记忆之渊 2024-08-26 21:54:57

我知道我会在几年后到来,但我不敢相信没有人建议使用 usermod。

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

天哪,万一有人想在旧版 HPUX 上执行此操作,您可以使用 usermod.sam

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

仅当执行脚本的人是当前用户时才需要 -F。当然,您不需要使用 Perl 来创建哈希。您可以使用 openssl 或许多其他命令来代替它。

I know I'm coming at this years later, but I can't believe no one suggested 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.

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

笑看君怀她人 2024-08-26 21:54:57

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

  • $new_username 表示新创建的用户
  • $new_password 表示新密码

对于 CentOS

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

对于 Debian/Ubuntu

echo "$new_username:$new_password" | chpasswd

对于 OpenSUSE

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

I've tested in my own shell script.

  • $new_username means newly created user
  • $new_password means newly 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"
活雷疯 2024-08-26 21:54:57

这是一个可以为您完成此操作的脚本......

如果需要,您可以添加用户列表(或仅一个用户),一次完成所有操作,每个用户都有不同的密码。作为奖励,您会在脚本末尾看到每个用户密码的列表。 ....如果您愿意,您可以添加一些用户维护选项,

例如:

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

到将设置密码期限等的脚本。

=======

#!/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

===========

希望这会有所帮助。

干杯,
卡雷尔

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

绝不放开 2024-08-26 21:54:57

该解决方案适用于 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\

用法:

userpassadd jim jimslongpassword

它可以有效地用作单行代码,但您必须自己在正确的位置指定密码、盐和用户名:

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

. '$salt' . '\

用法:


它可以有效地用作单行代码,但您必须自己在正确的位置指定密码、盐和用户名:



)") $1

用法:


它可以有效地用作单行代码,但您必须自己在正确的位置指定密码、盐和用户名:



)") username

. '$salt' . '\

用法:

它可以有效地用作单行代码,但您必须自己在正确的位置指定密码、盐和用户名:


)") $1

用法:

它可以有效地用作单行代码,但您必须自己在正确的位置指定密码、盐和用户名:


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

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\

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\

. '$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:



)") username

. '$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:


不忘初心 2024-08-26 21:54:57

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

创建一个文本文件,例如 text.txt 并使用用户:密码对填充它,如下所示:

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

保存 text.txt文件,然后运行

cat text.txt | chpassword

​​就是这样。该解决方案 (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:

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

Save the text.txt file, and run

cat text.txt | chpassword

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

陌伤ぢ 2024-08-26 21:54:57

Tralemonkey 的解决方案几乎对我也有效……但不完全有效。我最终这样做了:

echo -n '$#@password@#

他的解决方案没有包含 2 个关键细节, -n 阻止回显将 \n 添加到正在加密的密码中,在我的例子中,单引号可以保护内容不被 shell (bash) 解释。

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

| passwd myusername --stdin

他的解决方案没有包含 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:

echo -n '$#@password@#

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.

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

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

司马昭之心 2024-08-26 21:54:57
{ echo $password; echo $password; } | passwd $username 
{ echo $password; echo $password; } | passwd $username 
旧故 2024-08-26 21:54:57

对于 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

For RedHat / CentOS here's the code that creates a user, adds the passwords and makes the user a 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
自由如风 2024-08-26 21:54:57

用法: ./my_add_user.sh 用户密码

代码:

#!/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;

usage: ./my_add_user.sh USER PASSWD

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;
十六岁半 2024-08-26 21:54:57

请使用 sudo 权限运行以下脚本,以便通过脚本创建用户。

注意:此脚本支持所有 Linux 操作系统,如 Redhat、Centos、Ubuntu、suse、kali、Arch、Bitname、BSD...等

#!/bin/bash
#author: bablish jaiswal
#purpos: Linux user creation with a storng password
clear
#echo "Hi, I am a function to create sudo user with strong password. Kindly share following information"
echo -e "\n\n\n"
printf "\e[6;33mHi, I am a function to create sudo user with a strong password. Kindly share following information\e[0m";echo
read -p "user name:- " name #input name
read -p "complete path for $name home directory? example: /home/$name :- " home #user home path
( useradd  -m -d $home $name -s /bin/bash ) > /dev/null 2>&1
pass=$(cat /dev/urandom |tr -dc "[[:graph:]]" |head -c16)
(echo -e "$pass\n$pass" | passwd $name ) > /dev/null 2>&1
echo " "
printf "\e[6;33m-----------------------------Copy below credentials-------------------------\e[0m";echo
echo -e "User:- $name\nHomeDir:- $home\npassword:- $pass"
#svalue=$(cat /etc/sudoers |grep -i root |grep -i all|tail -n1 |awk '{$1=""}1')
svalue=$(cat /etc/sudoers |grep -i root |grep -i all|tail -n1 |awk '{print $2}')
echo "${name} ${svalue} NOPASSWD:ALL" >> /etc/sudoers && echo “Remark:- User $name is a sudo user”

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

#!/bin/bash
#author: bablish jaiswal
#purpos: Linux user creation with a storng password
clear
#echo "Hi, I am a function to create sudo user with strong password. Kindly share following information"
echo -e "\n\n\n"
printf "\e[6;33mHi, I am a function to create sudo user with a strong password. Kindly share following information\e[0m";echo
read -p "user name:- " name #input name
read -p "complete path for $name home directory? example: /home/$name :- " home #user home path
( useradd  -m -d $home $name -s /bin/bash ) > /dev/null 2>&1
pass=$(cat /dev/urandom |tr -dc "[[:graph:]]" |head -c16)
(echo -e "$pass\n$pass" | passwd $name ) > /dev/null 2>&1
echo " "
printf "\e[6;33m-----------------------------Copy below credentials-------------------------\e[0m";echo
echo -e "User:- $name\nHomeDir:- $home\npassword:- $pass"
#svalue=$(cat /etc/sudoers |grep -i root |grep -i all|tail -n1 |awk '{$1=""}1')
svalue=$(cat /etc/sudoers |grep -i root |grep -i all|tail -n1 |awk '{print $2}')
echo "${name} ${svalue} NOPASSWD:ALL" >> /etc/sudoers && echo “Remark:- User $name is a sudo user”
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文