Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer
@mattdm-Can you suggest some tutorials i mean some reference usage help! :) – Chronix01 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.

share|improve this answer
so its gonna put "$passwd" as password for user "$uname" ? – Chronix01 Jun 20 '14 at 0:46
@Chronix01 - those are variables, but yes the password for $uname would be $passwd. – slm Jun 20 '14 at 0:47
wow if that works trust me buddy u saved the day! :D – Chronix01 Jun 20 '14 at 0:49
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
@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

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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