How to fix useradd: cannot lock /etc/passwd; try again later linux error.
Sooner or later you'll get this error. This could be caused by several reasons.
- The currently logged in user OR the bash script is not running as root
- the /etc/passwd is indeed "locked" to prevent issues when creating users at the same time.
You can check if you're running as root by typing "whoami" command.
To see if the /etc/passwd is "locked" run this command.
find /etc | grep -iE '.lo?ck$'
If the command above returns a list of files and especially /etc/.pwd.lock that means that the /etc/passwd is "locked" indeed.
Ideally, you should check what process is causing this lock as it's not normal for this to stay for more than a fraction of a second.
After you've checked you can delete /etc/.pwd.lock file.