useradd user with group with same uid and gid
I want to add a user named "nexus" with uid as 1234567 (example number) and gid 1234567.
I'm running the following command:
sudo useradd -r -U -u 1234567 -g 1234567 -m -c "nexus role account" -d /sonatype-work -s /bin/false nexus
But I get the error:
useradd: group '1234567' does not exist
And if I do:
sudo useradd -r -U -u 1234567 -m -c "nexus role account" -d /sonatype-work -s /bin/false nexus
Then when I check id -u nexus
it shows the correct uid
(1234567) but when I check id -g nexus
the gid
is set to 999
.
If I do sudo adduser --uid 1234567 nexus
then the user and group ids are set the same.
Can I do the same with useradd
or do I have to use adduser
to achieve my goal?
I've been following this tutorial btw:
http://www.tecmint.com/add-users-in-linux/
P.S.: If I have to use adduser
then can I do it without any human interaction; i.e.: automate the user creation via script?
EDIT:
This is the outcome of sudo adduser --uid 1234567 nexus
Adding user `nexus' ...
Adding new group `nexus' (1234567) ...
Adding new user `nexus' (1234567) with group `nexus' ...
The home directory `/home/nexus' already exists. Not copying from `/etc/skel'.
adduser: Warning: The home directory `/home/nexus' does not belong to the user you are currently creating.
Enter new UNIX password:
Retype new UNIX password:
No password supplied
Enter new UNIX password:
Retype new UNIX password:
No password supplied
Enter new UNIX password:
Retype new UNIX password:
No password supplied
passwd: Authentication token manipulation error
passwd: password unchanged
Try again? [y/N] n
Changing the user information for nexus
Enter the new value, or press ENTER for the default
Full Name []: Nexus
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
Solution 1:
Thanks to Tom Yan, I've ended up solving my issue by creating a group with the same name then added the user to that group. So I did the following:
sudo groupadd -r -g 1234567 nexus \
&& sudo useradd -r -u 1234567 -g 1234567 -m -c "nexus role account" -d /sonatype-work -s /bin/false nexus
Solution 2:
Frankly, I think it's a bug(feature). The documentation indicates that you can use the '-U' option to simultaneously create the user and group at the same time. Intuitively you would think that in such a case, it would take the value from the '-g' flag and create the group accordingly.
[root@centos4]# useradd -U -u 200 -g 200 -m -d /home/ansible -s /usr/bin/bash ansible
useradd: group '200' does not exist
The example above is similar to your useradd command. However, if I simply remove the '-g 200' argument it works immediately. Although unfortunately it seems that the gid is allocated with the standard method.
[root@centos4]# useradd -U -u 200 -m -d /home/ansible -s /usr/bin/bash ansible
[root@centos4]# egrep ansible /etc/passwd
ansible:x:200:1000::/home/ansible:/usr/bin/bash
[root@centos4]# egrep ansible /etc/group
ansible:x:1000:
The answer to your question is that if you want to add user and group at the same time you have to remove the -g option. It's not as elegant as both you and I hoped, but its better than nothing.
Hope that helps. Take Care.