groupadd fails with "cannot lock /etc/group; try again later"
I am connected to an Ubuntu 12.04.3 LTS server via SSH; when I try to add a group, I get the following message:
groupadd: cannot lock /etc/group; try again later.
In researching this problem, I found some suggestions to look for and remove a .pwd.lock file in the same directory. I found such a file, created over a year ago; I removed it but this did not change the behavior of groupadd.
I've read Cannot lock '/etc/group' in recovery mode but I am not booted into recovery mode and all partitions are mounted read-write. I am not logged in as the root user.
2 Answers
The error message is technically correct but the "try again later" message is misleading. The real issue here is hidden in an error message from adduser:
nonroot@host:/$ adduser existinguser newgroup
adduser: Only root may add a user or group to the system.To add a new group when logged in as a non-root user, you must use sudo:
nonroot@host:/$ sudo groupadd newgroup
[sudo] password for host:
nonroot@host:/$ grep 'newgroup' /etc/group
newgroup:x:1013:
nonroot@host:/$ I just found myself getting this message during a Docker build.
I solved it very easily by adding this directive before the privileged commands in the Dockerfile:
USER root