48

With several commands in Linux, I get the error:

cannot lock /etc/passwd; try again later.

Does anybody know how to solve it? Also I don't get in my /etc/shadow directory.

Eric Leschinski
  • 6,828
  • 6
  • 45
  • 51

9 Answers9

26

If no .lock files are present but you still cannot create a user try the following

sudo mount -o remount,rw /

If logged in as root then use

mount -o remount,rw /
Jawa
  • 3,619
  • 13
  • 31
  • 36
16

The user you are running the commands as lack the required privileges. Change to root user by issuing the command

su -

or if you have/use sudo

sudo <command to run>
inquam
  • 323
  • 1
  • 8
14

If you have -R /some/chroot added to your useradd command, that might be the problem.

I thought it meant that the user would be jailed upon login, but that's not the case. By looking at strace output, I saw useradd chrooted into the specified directory, after which of course it cannot find /etc/passwd anymore. So I'm not sure what the option is for, but there's your (well, my) problem.

Luc
  • 2,783
  • 2
  • 26
  • 37
  • This was exactly what I was trying. After removing the `-R [dir]` option I was able to create the user. – Gustavo Straube Oct 11 '18 at 18:55
  • Nice guess! Thank you. I also understood that the chroot for `useradd` is not what you think it is :) It only uses the specified dir as a root for the user's *system*. It's useful for cloned isolated sub-OS, which is rarely what you want... – Ninj Sep 07 '19 at 13:45
10

That's because you don't have permissions for those operations

  • You can't read /etc/shadow
  • You can't directly modify /etc/passwd

You can change both files through specialized commands (e.g you can change your password).

9

I ran into this when a disk error occurred during a userdel operation and the system had to be rebooted. I needed to delete all four of the following files to proceed:

sudo rm /etc/passwd.lock
sudo rm /etc/shadow.lock
sudo rm /etc/group.lock
sudo rm /etc/gshadow.lock
Trevor Robinson
  • 221
  • 2
  • 5
7

This can also be caused by running out of space on the root filesystem. Use strace to be sure. strace is your friend.

Robin Green
  • 1,275
  • 1
  • 13
  • 28
  • I've been reading many threads with my problem and that was actually the solution. Should be one of the first things to check when having any filesystem problems, I guess. I'm using `df` instead of `strace` though. How would `strace` help me? Never used it – erikbstack Apr 14 '14 at 10:11
  • 3
    Well, if you already guessed that the problem might be running out of space, then df can help. But strace will tell you what the error code was when it tried to lock the file, so it should remove the guesswork. The way I usually call strace is `strace -f -e trace=file ` *command* since this usually gives the most useful results. – Robin Green Apr 14 '14 at 11:03
2

A demo of this error on Ubuntu 14.04:

user@mybox:/home$ sudo useradd eric
user@mybox:/home$ userdel eric
userdel: Permission denied.
userdel: cannot lock /etc/passwd; try again later.

sudo gives you the permission to lock it.

user@mybox:/home$ sudo userdel eric
user@mybox:/home$
Eric Leschinski
  • 6,828
  • 6
  • 45
  • 51
1

Look for /etc/group.lock, /etc/passwd.lock and /etc/shadow.lock files and remove them.

Be careful to only remove the files ending in 'lock' or else you might damage your system.

Reference: https://bugs.launchpad.net/ubuntu/+source/shadow/+bug/523896

1

Had same issue, since /etc was full. This is why /etc/passwd could not be written. Make sure that you have enough space on /etc, if not then enlarge it or clean unnecessary stuff.