Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a specific use case where I would really like to be able to change a user's password with a single command with no interactivity. This is being done in a safe fashion (over SSH, and on a system with only one user able to be logged in), so it's fine to expose the new password (and even the old one, if necessary) on the command line. FWIW, it's a Ubuntu system.

I just want to avoid having to add something Expect-like to this system for just this one task.

share|improve this question
1  
For those who want to do this on FreeBSD: echo "newpassword" | pw usermod theusername -h 0 –  Paul Hoffman Dec 2 '11 at 23:21
add comment

4 Answers 4

up vote 6 down vote accepted

You could use chpasswd.

echo user:pass | /usr/sbin/chpasswd
share|improve this answer
add comment

You can use usermod with the -p option to provide a password hash (not the actual password). You can generate the password hash using something like mkpasswd -m sha-256 or mkpasswd -m md5

share|improve this answer
1  
...But my AIX system doesn't have usermod :'( -- I'll fix the tags on the question. –  voretaq7 Nov 30 '11 at 17:01
add comment

The passwd utility has a --stdin option that states:

This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.

Syntax:

echo "newpass" | passwd --stdin user1

Even though you mentioned you don't care, you could put the password in a text file and then do cat pass.txt instead of the echo command, that way it doesn't show up in the bash history.

share|improve this answer
    
That's the one. echo VerySecret | passwd --stdin username –  MadHatter Nov 30 '11 at 17:06
1  
My Ubuntu system doesn't have --stdin as an option for passwd. –  Jeff Ferland Nov 30 '11 at 17:19
    
You're right, it doesn't. I checked on both a CentOS system and on a Mac and they have the --stdin option, so the removal of it must be a Ubuntu thing. –  Safado Nov 30 '11 at 17:48
    
@RyanM. FreeBSD (and AFAIK NetBSD and OpenBSD, AIX, HP-UX and Solaris last I checke) don't support --stdin either - mainly because it's a big giant security hole waiting to happen :-) –  voretaq7 Nov 30 '11 at 20:12
1  
@quanta: Or start the echo "newpass" | passwd --stdin user1 line with a space; that way it won't go to bash history at all. –  Janne Pikkarainen Dec 1 '11 at 7:44
add comment

Sure.

  1. Hash the password on your local system.
  2. Connect to the remote machine (where you want to change the password)
  3. Feed the hashed password & the username to a creative sed script that updates your system's password file (/etc/shadow, /etc/master.passwd, whatever it happens to be).
share|improve this answer
add comment

Your Answer

 
discard

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.