30

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
  • 5
    Out of curiosity, why can't you use password changing tools?
    – Bratchley
    May 18, 2013 at 23:51

3 Answers 3

26

The easiest solution, if having an empty password is not a problem for you, is to change the /etc/passwd file and not the /etc/shadow.

An example line of the /etc/passwd file is:

root:x:0:0:root:/root:/bin/bash

The 'x' in this line means that the password is actually stored hashed in the shadow file. One thing that you can do is to just remove it like this:

root::0:0:root:/root:/bin/bash

Then you can actually login with the username root and with an empty password.

If you don’t want to leave the root user’s password blank, once you’ve removed the password from root, change to root user:

user@machine$ su root
password: [blank]

Then run the command to change the root password:

root@machine# passwd
New password: [desired password]
Retype new password: [desired password]

Now you have a password for root that you know.

19

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.

8
  • Thank You, but I need a command to edit the string in the shadow file. May 18, 2013 at 21:09
  • See my updates, is that what you're after?
    – slm
    May 18, 2013 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? May 19, 2013 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, 2013 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.
    – Qian Chen
    Jul 18, 2015 at 7:00
2

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.

3
  • 4
    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, 2014 at 16:55
  • 3
    @n.st This advice seems to be valid to me. See this post: stackoverflow.com/a/11700775/870150
    – Alex
    Mar 22, 2017 at 22:48
  • 1
    Also seems to be the advice over at askubuntu.com/a/84697/178596
    – Wilf
    Jun 25, 2017 at 10:51

You must log in to answer this question.

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