【问题标题】:How to create a new username and password in Bash script?如何在 Bash 脚本中创建新的用户名和密码?
【发布时间】:2018-01-15 20:03:28
【问题描述】:

我正在尝试创建一个脚本,它会在其中添加新用户和密码,并在以 Root 运行时检查该用户和密码是否已经存在。

所以,我的脚本运行良好。它只在根目录中运行,它会正确检查用户名是否已被使用。但是,我似乎无法添加新用户和密码。以下是我的整个脚本:

#!/bin/bash
#Creating a script that creates a new user

ROOT_UID=0      #Root has $UID 0
SUCCESS=0
E_USEREXISTS=70
E_NOTROOT=65        #Not root

#Run as root, and this checks to see if the creater is in root. If not, will not run
if [ "$UID" -ne "$ROOT_UID" ]; then
    echo "Sorry must be in root to run this script"
    exit $E_NOTROOT
fi

if [ $# -eq 2 ]; then
username=$1
pass=$2

grep -q "$username" /etc/passwd

if [ $? -eq $SUCCESS ]; then
    echo "User $username already exists"
    echo "Please choose another username"
    exit $E_USEREXISTS 
fi

echo $pass | passwd $username --stdin
echo "the account is setup"

else
    echo "this program needs 2 arguments and you have given $#"
    echo "you have to call the script $0 username and the pass"
fi

exit 0

而且我没有得到一个直接的错误,但这里是正在发生的事情的一个例子: 我尝试添加某人(用户名然后密码):

[root@localhost Documents]# ./Script dkong 123bif

响应是:

passwd: Unknown user name 'dkong'
the account is setup

有人可以帮助指导我吗?我希望它创建一个新的用户名和密码,但我不明白为什么不这样做。

我已经有一段时间没有使用脚本了,如果答案很明显,我很抱歉!只需要一点方向。提前谢谢!

【问题讨论】:

    标签: bash passwords


    【解决方案1】:

    没关系!我让它工作了! 这是我的解决方案:

    useradd $username -d /home/$username -m ;
    echo $passwd | passwd $username --stdin;
    echo "the account is setup"
    

    【讨论】:

      【解决方案2】:

      错误在于您需要在检查该行'grep -q“$username”/etc/passwd'不存在之后将用户名添加到/etc/passwd 所以你可以简单地添加那行'useradd $username' 在第二个 if 条件之后 正好在那行之前 'echo $pass |密码 $username --stdin' 发生错误是因为您告诉 passwd 为尚不存在的用户设置密码

      【讨论】:

        【解决方案3】:

        首先,永远不要对用户变量使用完整的大写标识符,因为它们是为 shell 环境保留的。

        所以

        ROOT_UID=0
        

        应该是

        root_uid=0 # and so on
        

        创建用户名需要useradd 命令。它会自动进行后台检查以查看用户是否已存在,如果存在则中止进程,您只需检查命令的退出状态以查看用户创建是否成功。

        您可以将用户名作为参数传递给脚本,但从安全角度来看,以纯文本形式传递密码并不好。 Linux 密码存储为散列。哈希算法可以是MD5、SHA-512等。您需要使用crypt 函数,提到here,来帮助useradd 处理存储散列密码。

        下面是我的脚本

        #!/bin/bash
        # Checking pre-requirements, the script should contain one argument
        [ -z "$1" ] && echo "Usage : script user_name" && exit 1
        username=$1
        # Read password key and has silently using -s ie keystrokes are invisible
        read -sp 'Enter password key - ' key
        # Typically ask for a confirmation, but omitted for the sake of brevity
        # I suppose you default password hash algorithm is MD5
        
        # You need some characters for a salt. Read the content in link mentioned above
        read -sp 'Read upto 8 characters for salt - ' salt
        # You can sanitize the user input for salt to trim its length to 8 and use it like below
        useradd -m -d /home/$username \
        -p $(perl -e 'print crypt("$ARGV[0]", "\$1\$$ARGV[1]")' "$username" "${salt:0:8}")
        # Check the exit status of the last command
        [ $? -eq 0 ] && echo "User $username successfully created" || \
        echo "Sorry ! User $username couldn't be created at the moment"
        

        【讨论】:

          猜你喜欢
          • 2019-11-02
          • 2015-05-11
          • 1970-01-01
          • 2020-01-26
          • 2018-03-27
          • 1970-01-01
          • 1970-01-01
          • 2020-04-03
          • 2015-09-27
          相关资源
          最近更新 更多