1

I am trying to make a bash command change all user's passwords to x.

awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd

Can I pipe the users that are returned to passwd? I mean is that gonna require some kind of loop? If that is the case please do an example of how to do that?

2 Answers 2

3

passwd does not read passwords non-interactively e.g. via STDIN generated by pipe, as an alternative you can use chpasswd which reads password via STDIN but has a security issue.

The problem is chpasswd expects passwords to be given in clear text, following the format:

username:password

To make us somewhat less explicit we can save the passwords sequentially in separate lines in a file corresponding to the output given by the awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd command. Remember you need to maintain the sequence of the output of the command while saving passwords in the file.

Then we can use paste to generate the format chpasswd wants on its STDIN.

For example:

paste -d: <(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd)\
          /password/file | sudo chpasswd

Here /password/file contains the relevant passwords.

As a safety measure, we can remove the /password/file file afterwards.

4
  • 1
    passwd does not read passwords via STDIN ? So what is keyboard then if not STDIN ? Nov 22, 2015 at 18:05
  • @Serg Oops..I meant pipe..
    – heemayl
    Nov 22, 2015 at 18:13
  • No, you meant stdin. @Serg It just happens that passwd only reads from stdin if it is connected to a TTY.
    – muru
    Nov 22, 2015 at 20:35
  • @muru well, that's not "just happens" , it's by design, but for some users it well may be confusing Nov 22, 2015 at 21:05
0

It's possible with usermod -p PASSWD USERNAME and while loop. Problem is that PASSWD cannot be in plain text, it has to be encrypted password. For that purpose you can use mkpasswd. That way "newPassString" is encrypted , and remains the same for all users. Here's example of what can be done in just one line of code.

$ cat userlist |  while read USERNAME; do echo "$USERNAME";sudo usermod -p "$(mkpasswd "newPassString")" "$USERNAME"  ;done
tester
[sudo] password for xieerqi: 
testuser
[sudo] password for xieerqi: 

Notice that i have to enter sudo for each sudo call - that's my personal setting. In regular, unaltered /etc/sudoers file, the timeout is 15 minutes by default, so you'd need to enter it only once.

A better approach is to put the code into a script, and call with parameters $1 as filename and $2 as new password string :

 xieerqi:$ cat passScript.sh                                                                                        
#!/bin/bash
while read USERNAME; do
   echo "$USERNAME";
   usermod -p "$(mkpasswd "$2")" "$USERNAME"  
done < "$1"

xieerqi:$ chmod +x passScript.sh                                                                                   

xieerqi:$ sudo ./passScript.sh  userlist myNewPass                                                                 
[sudo] password for xieerqi: 
tester
testuser

xieerqi:$ su testuser
Password: 
testuser@eagle:/home/xieerqi$ 
4
  • Why specify a salt to mkpasswd? It can generate salts (and does, if you don't specify one).
    – muru
    Nov 22, 2015 at 20:36
  • @muru so that the user can go back and check it. But if 100% unnecessary, i can remove it Nov 22, 2015 at 21:06
  • Of course it is unnecessary! Adding a constant salt to everything negates the whole point of a salt.
    – muru
    Nov 22, 2015 at 21:07
  • 2
    In case anyone wonders, have a read through security.stackexchange.com/a/51983/54387 and security.stackexchange.com/a/31846/54387 to see why using the same salt for all passwords defeats the purpose of a salt. In this specific case, even though all the passwords are being set to x, a future attacker who does not know of this cannot take advantage of it - each hash will look different, and will have to be cracked individually. With the same salt, the hashes will all be the same - crack one, crack all.
    – muru
    Nov 22, 2015 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .