14

Here is what I have done:

  1. Change a username in the last line: tiny to abc

    • tiny@tty7:~$ sudo vim /etc/passwd
    • Change this:
      tiny:x:1000:1000:tiny,,,:/home/tiny:/bin/bash
      to this:
      abc:x:1000:1000:tiny,,,:/home/tiny:/bin/bash
  2. tiny@tty7:~$ sudo vim /etc/shadow

    • it shows: [sudo] passwork for abc:

    • I didn't change my password but it can't login root!

    • I type Ctrl+Alt+F1 to tty1 but neither tiny or abc can't login with my password

and I try to look at ls ~ -la, here it shows like this:

drwxr-xr-x 28 abc tiny 4096 Apr 6 03:04 .

How can I login root? and why the user name in prompt tiny@tty7 still be tiny while in sudo it show the abc's password?

18

The reason you got stuck after your edit is that the /etc/shadow file contained an entry for the password of tiny but no entry for abc, whereas the /etc/passwd file contained an entry for abc and not for tiny. When sudo looked, it identified you correctly (according to the password file) as abc based on the UID of the process you were running, but when it looked to compare what you entered as your password with the encrypted (hashed) password stored in /etc/shadow, it couldn't find an entry for abc, so it had to report the failure.

As shaddy said in his answer, don't edit the password or shadow files by hand. If you must break the rule, then edit both in a single invocation of vim, and don't exit from vim until you've proved that the changes are sane enough to work by using another terminal window, and do save backup copies of the files before you start hacking them, and do reconsider why you're breaking the rules in the first place, because it probably isn't a good idea.

22

You must NEVER edit your passwd file manually because you will mess things up.

The proper way to change your username is:

usermod -l newUsername oldUsername

Then change home folder:

usermod -d /home/newHomeDir -m newUsername

To change password you should use:

sudo passwd yourusername

I suggest you to revert your passwd file back, or if you can't edit it because you can not login with root - then boot with live cd and revert the file back. Then use the proper way.

  • 1
    thank you~ I thought the most difficult is to login with root then I reboot , choose revovery mode , root and then get my passwd file back~ it work~ – tinyork Apr 6 '15 at 8:45
0

Use: vipw

I have always used this on FreeBSD and just confirmed you can use it for Ubuntu too. Please read this page: Ubuntu Man Page for vipw

-3

procedure for changing your username (but not uid) by hand:

  1. have your head examined, when that fails proceed to step two.
  2. edit shadow file duplicating the user line with new username.
  3. edit groups, sudoers, allowing new username permissions, keep old
  4. symbolically link home directory to new name
  5. edit passwd file changing username and home directory
  6. edit shadow, sudoers, groups to remove old entries we kept in step three
  7. edit aliases and other references to old username.
  8. logout, switch to a text console (the next step will not work if any programs are running in users home directory, including window managers)
  9. cd to a directory above your home directory.
  10. remove symbolic link, and rename home directory.
  11. logout and login as normal.

Things are much simpler to change others username, although still not simple.

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.