我需要能够在我的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 ]#

当前回答

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

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

useradd -p $(openssl passwd -crypt $PASS) $USER

其他回答

在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

用法:

userpassadd jim jimslongpassword

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

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

Tralemonkey的解决方案几乎也适用于我……但事实并非如此。我最后是这样做的:

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

他的解决方案没有包括2个关键细节,-n防止echo在加密的密码中添加\n,在我的例子中,单引号保护内容不被shell (bash)解释。

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

请使用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”

这里有一个脚本,将为您.....

如果你愿意,你可以添加一个用户列表(或者只是一个用户),一次完成,每个用户都有不同的密码。作为奖励,脚本的末尾会显示每个用户的密码. ....如果您愿意,您可以添加一些用户维护选项

如:

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

===========

希望这能有所帮助。

欢呼, 卡路

你也可以使用chpasswd:

echo username:new_password | chpasswd

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