linux - 如何使用 python crypt 或 bcrypt 为 useradd 创建加密密码?

标签 linux bash python-2.7 crypt

我正在尝试为 debian/ubuntu useradd 创建一个散列密码。我使用 python 创建哈希并使用 bash 使用哈希密码创建用户。我认为我做错了什么,因为当我尝试使用用户密码登录时它不起作用。我想它想让我输入实际的哈希值。

在 python 中,我创建了这样的加密哈希密码。它在 python 类中,但我缩短了它以便您能看到图片。

import crypt
import random

def salt(self):
    saltchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    return random.choice(saltchars) + random.choice(saltchars)

def custom_user(self):
    user = self.textEditUser.text()
    password = self.textEditPassword.text()
    hashed_pw = crypt.crypt(str(password), "$6$"+self.salt()) # say password is "password"
    print hashed_pw #returns $6$5l$lrm4UvmiYZcducdBFs8NortA.zeWuXrnmoVEYtLxmmDIGLN.9gjs.X6Z/fR6wDkh06lnDN8LTjzwrImSCR72T/

我使用一个单独的 bash 脚本来创建这样的用户:

#!/bin/bash

PASSWORD='$6$5l$lrm4UvmiYZcducdBFs8NortA.zeWuXrnmoVEYtLxmmDIGLN.9gjs.X6Z/fR6wDkh06lnDN8LTjzwrImSCR72T/'
USERNAME="Jon"

if id -u $USERNAME >/dev/null 2>&1; then
    userdel -r -f $USERNAME
    useradd -m -p $PASSWORD -s /bin/bash $USERNAME
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd

else
    useradd -m -p $PASSWORD -s /bin/bash $USERNAME
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd
fi

我怎样才能让我以后使用我的密码而不是哈希来登录?

最佳答案

python -c "import crypt; print crypt.crypt(\"foo\", \"\$6\$$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)\$\")"

关于linux - 如何使用 python crypt 或 bcrypt 为 useradd 创建加密密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42462979/

相关文章:

java - 能够运行 shell 脚本,但不能运行来自 java 的 Runtime.getRuntime().exec() 的 curl 命令

bash - 为什么描述符重定向看起来不一致?

linux - 如何避免创建 nohup.out 以及在这种情况下将生成输出的位置?

python - 找出段落中出现的单词

python - 为什么我会收到 TypeError : 'module' object is not callable in python?

python - 如何在 python 2 程序创建蛇游戏的前一点清除屏幕?

linux - Apache、Linux 和远程命令,如 'Ping'

linux - 如何使用 SSH 下载目录/文件?

linux - 如何使用 Azure 为资源管理器 VM 提供的 DNS?

linux - 在 UNIX 时间戳 Shell/Bash 中使用时区转换日期