I was making a GUI application to manage users and groups in Linux!

I'm done with the part where it creates a new user, but stuck with the part where it gives the newly created user a new password. What my app simply does is to take the required input (username, list of groups, and password) through a GUI and runs a script passing this info as argument. Let's say we have a user account, xyz. If I want to change the password for this account, then all I need to do is simply run the command:

passwd xyz

This will ask for new password. Now I can create a new account using scripts because all the required info is passed in the command line.

useradd -m -G users -g "groups" -s /bin/bash "UserName"

I can run a script through Qt app and create the user, but in passwd cmd, the input is asked in the other line. How does one deal with that?


I think the right answer here is: don't shell to a command-line tool — use a library call. This will let you handle errors better, and avoids risk-prone passing of the password on a command line.

One library you can use is libuser, which is relatively simple and has C and Python bindings.

  • @mattdm-Can you suggest some tutorials i mean some reference usage help! :)
    – compiler
    Jun 20 '14 at 5:31

As root you can systematically set a user's password via a script using this method:

$ echo -n "$passwd" | passwd "$uname" --stdin

Generating passwords

I like using the command line tool pwgen to generate passwords.

$ 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
    Print the generated passwords in columns
    Don't print the generated passwords in columns
  -v or --no-vowels
    Do not use any vowels so as to avoid accidental nasty words

But isn't this insecure?

No. The password is being passed via STDIN to passwd so though it might be possible for someone to snoop on the processes via ps, even that shouldn't allow a user to see root's processes, the handing of the password to passwd is walled off.


Say I run this command in one terminal as root:

$ ( sleep 10; echo "supersecret" | passwd "samtest" --stdin ) &
[1] 13989

I then run ps in another terminal:

$ ps -AOcmd | grep pass
14046 passwd samtest --stdin      R pts/11   00:00:00 passwd samtest --stdin

After the password is changed in the 1st terminal:

[root@greeneggs ~]# Changing password for user samtest.
passwd: all authentication tokens updated successfully.

What about the echo to passwd?

Even this does not leak the password. Here's another test demonstrating this. First we start this command in a secondary terminal. This will collect output from ps.

$ while [ 1 ]; do ps -eaf2>&1 | grep -E "echo|pass";done | tee ps.txt

We then run our password setting command:

$ 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

Checking the contents of ps.txt shows that the password is not leaked:

$ grep supersecret ps.txt

Changing the ps command we use to ps -eaf also doesn't leak it.

  • so its gonna put "$passwd" as password for user "$uname" ?
    – compiler
    Jun 20 '14 at 0:46
  • 3
    Wouldn't this show the user's password in ps, even if only for a second? Seems like a security issue; if someone was aware you did this, they could monitor password changes through your GUI.
    – sapi
    Jun 20 '14 at 2:50
  • 2
    @sim that's wrong. Issuing ps -AOcmd gives me the list of all the processes and their command lines — without requiring me to be root. This will of course list echo with password as its argument. Still, you could just pipe the password from pwgen to passwd directly, then no one could read it except owner equipped with debugger.
    – Ruslan
    Jun 20 '14 at 8:29
  • 1
    @slm no, I'm talking not about pipe. I mean the echo command. Clearly it has echo -n ThePassword123 command line, which will be visible via ps if you are lucky enough to catch it running. And on my system which echo tells /bin/echo, so it clearly may be not a builtin, for which the ps would be unable to see the command line.
    – Ruslan
    Jun 20 '14 at 11:26
  • 1
    The reason why echo is not appearing in ps is because it's a usually shell builtin. (Another reason could conceivably be a race condition, but that sounds unlikely.) I would not recommend using echo unless you know for sure it will be a shell builtin.
    – Oskar Skog
    Feb 13 '20 at 15:03

passwd --help on my box indicates that root can run it with --stdin to feed it the new password on stdin.


The chpasswd command reads a list of user name/password pairs from the command line to update a group of existing users. Each line is in the format


This command is intended to be used in large system environments where many accounts are created at a single time. To read about more options of this command, type

man chpasswd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.