我正在制作一个 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,在另一行中要求输入。怎么处理呢?
作为 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)
即使这样也不会泄露密码。这是另一个证明这一点的测试。首先,我们在辅助终端中启动此命令。这将从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也不会泄漏它。
| 归档时间: |
|
| 查看次数: |
2903 次 |
| 最近记录: |