通过 GUI 应用程序更改用户密码

com*_*ler 4 linux gui passwd

我正在制作一个 GUI 应用程序来管理 Linux 中的用户和组!

我已经完成了创建新用户的部分,但仍然坚持为新创建的用户提供新密码的部分。我的应用程序所做的只是通过 GUI 获取所需的输入(用户名、组列表和密码)并运行一个脚本,将此信息作为参数传递。假设我们有一个用户帐户 xyz。如果我想更改此帐户的密码,那么我只需运行以下命令:

passwd xyz
Run Code Online (Sandbox Code Playgroud)

这将要求输入新密码。现在我可以使用脚本创建一个新帐户,因为所有必需的信息都在命令行中传递。

useradd -m -G users -g "groups" -s /bin/bash "UserName"
Run Code Online (Sandbox Code Playgroud)

我可以通过 Qt 应用程序运行脚本并创建用户,但在 中passwd cmd,在另一行中要求输入。怎么处理呢?

slm*_*slm 5

作为 root,您可以使用以下方法通过脚本系统地设置用户密码:

$ echo -n "$passwd" | passwd "$uname" --stdin
Run Code Online (Sandbox Code Playgroud)

生成密码

我喜欢使用命令行工具pwgen来生成密码。

$ passwd="`pwgen -1cny | sed 's/[\$\!]/%/g'`"

$ pwgen --help
Usage: pwgen [ OPTIONS ] [ pw_length ] [ num_pw ]

Options supported by pwgen:
  -c or --capitalize
    Include at least one capital letter in the password
  -A or --no-capitalize
    Don't include capital letters in the password
  -n or --numerals
    Include at least one number in the password
  -0 or --no-numerals
    Don't include numbers in the password
  -y or --symbols
    Include at least one special symbol in the password
  -s or --secure
    Generate completely random passwords
  -B or --ambiguous
    Don't include ambiguous characters in the password
  -h or --help
    Print a help message
  -H or --sha1=path/to/file[#seed]
    Use sha1 hash of given file as a (not so) random generator
  -C
    Print the generated passwords in columns
  -1
    Don't print the generated passwords in columns
  -v or --no-vowels
    Do not use any vowels so as to avoid accidental nasty words
Run Code Online (Sandbox Code Playgroud)

但这不是不安全吗?

不。密码是通过 STDIN 传递给的,passwd所以尽管有人可能通过 窥探进程ps,即使不应该允许用户看到 root 的进程,密码的传递passwd也被隔离了。

例子

假设我以 root 身份在一个终端中运行此命令:

$ ( sleep 10; echo "supersecret" | passwd "samtest" --stdin ) &
[1] 13989
Run Code Online (Sandbox Code Playgroud)

然后我ps在另一个终端运行:

$ ps -AOcmd | grep pass
14046 passwd samtest --stdin      R pts/11   00:00:00 passwd samtest --stdin
Run Code Online (Sandbox Code Playgroud)

在第一个终端更改密码后:

[root@greeneggs ~]# Changing password for user samtest.
passwd: all authentication tokens updated successfully.
Run Code Online (Sandbox Code Playgroud)

对 passwd 的回声怎么样?

即使这样也不会泄露密码。这是另一个证明这一点的测试。首先,我们在辅助终端中启动此命令。这将从ps.

$ while [ 1 ]; do ps -eaf2>&1 | grep -E "echo|pass";done | tee ps.txt
Run Code Online (Sandbox Code Playgroud)

然后我们运行我们的密码设置命令:

$ echo "supersecret" | passwd "samtest" --stdin &
[1] 20055
$ Changing password for user samtest.
passwd: all authentication tokens updated successfully.

[1]+  Done                    echo "supersecret" | passwd "samtest" --stdin
Run Code Online (Sandbox Code Playgroud)

检查内容ps.txt显示密码没有泄露:

$ grep supersecret ps.txt
$
Run Code Online (Sandbox Code Playgroud)

更改ps我们使用的命令ps -eaf也不会泄漏它。

  • 这不会在 `ps` 中显示用户的密码,即使只有一秒钟吗?似乎是一个安全问题;如果有人知道您这样做了,他们可以通过您的 GUI 监控密码更改。 (3认同)
  • @sim 错了。发出`ps -AOcmd` 给了我所有进程及其命令行的列表——不需要我是root。这当然会列出带有密码的 `echo` 作为参数。不过,你可以直接将密码从 `pwgen` 传送到 `passwd`,然后除了拥有调试器的所有者之外没有人可以读取它。 (2认同)

mat*_*tdm 5

我认为这里的正确答案是:不要使用命令行工具——使用库调用。这将使您更好地处理错误,并避免在命令行上传递密码的风险。

您可以使用的一个库是libuser,它相对简单并且具有 C 和 Python 绑定。