6

I would like to change the password of a user in the /etc/shadow. I can't use password changing programs like passwd. It must be done by editing the /etc/shadow. The /etc/shadow is on a mounted hard drive and chroot is not working.

For example:

root:$asdf.1AH.MJ3sivd4$YFoFnasdf4cZW9H6G6cbT4.1VBmasdfmv.a0wCU/i3zEFasdfasdf4W.:15843:0:99999:7:::
otheruser:$asdf.1AH.MJ3sivd4$YFoFnasdf4cZW9H6G6cbT4.1VBmasdfmv.a0wCU/i3zEFasdfasdf4W.:15843:0:99999:7:::

Can anybody provide me a shell script to change the password of root with sed or awk or something like that?

  • 1
    Out of curiosity, why can't you use password changing tools? – Bratchley May 18 '13 at 23:51
9

You can use the mkpasswd tool to do this. There's a good primer on how to use it over on cyberciti.biz, titled: Linux / UNIX: Generating random password with mkpasswd.

Example

mkpasswd --char=10 --crypt-md5

The package is usually called makepasswd, but the tool is typically called mkpasswd.

See the man page for more details.

Generating contents of /etc/shadow directly

The following python command will generate the portion that goes into the /etc/shadow file:

Python

$ python -c "import crypt, getpass, pwd; \
         print crypt.crypt('password', '\$6\$SALTsalt\$')"

Perl

$ perl -e 'print crypt("password","\$6\$saltsalt\$") . "\n"'

Which generates the following output:

$6$SALTsalt$UiZikbV3VeeBPsg8./Q5DAfq9aj7CVZMDU6ffBiBLgUEpxv7LMXKbcZ9JSZnYDrZQftdG319XkbLVMvWcF/Vr/

Changing the /etc/shadow

Here's a command that will change the existing entry with the newly generated password field from the command above.

perl -pe 's|(root):(\$.*?:)|\1:\$6\$SALTsalt\$UiZikbV3VeeBPsg8./Q5DAfq9aj7CVZMDU6ffBiBLgUEpxv7LMXKbcZ9JSZnYDrZQftdG319XkbLVMvWcF/Vr/:|' /etc/shadow > /etc/shadow.new

NOTE: This is a rough example but works. You'll need to take the results from the command that generated the hashed password, and escape the dollar signs ($) with slashes (\$).

The results are stored in a new file /etc/shadow.new. Once you've confirmed the results you can replace /etc/shadow with the new file, /etc/shadow.new.

  • Thank You, but I need a command to edit the string in the shadow file. – John Smithv1 May 18 '13 at 21:09
  • See my updates, is that what you're after? – slm May 18 '13 at 23:48
  • Hello, can you also provide me a command to replace the existing password of an user in the /etc/shadow file with the generated password? – John Smithv1 May 19 '13 at 8:57
  • 1
    Why can't you use changing password programs? @JoelDavis asked the question as a comment on your question too. Just curious. – slm May 19 '13 at 10:52
  • @slm, in my case, I don't want to login in the user's machine and leave anything in their history or last logs. – Elgs Qian Chen Jul 18 '15 at 7:00
-1

Remove the password all together for the user in /etc/shadow, boot the system, login with no password, then use the passwd command. If possible, do not bring the system on the network until this is completed.

  • 1
    Removing the password hash in /etc/shadow will prevent the user from logging in with any password since nothing you enter can result in a hash that would match the empty string. – n.st Mar 21 '14 at 16:55
  • 2
    @n.st This advice seems to be valid to me. See this post: stackoverflow.com/a/11700775/870150 – Alex Mar 22 '17 at 22:48
  • 1
    Also seems to be the advice over at askubuntu.com/a/84697/178596 – Wilf Jun 25 '17 at 10:51

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.