Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create user in linux by providing uid and gid options? [closed]

Tags:

linux

I have 2 linux machines. On one machine these are the users:

sysadmin2:x:4201:4200::/home/sysadmin2:/bin/bash
appadmin1:x:4100:4100::/home/appadmin1:/bin/bash
appadmin2:x:4101:4100::/home/appadmin2:/bin/bash
dataadmin1:x:4300:4300::/home/dataadmin1:/bin/bash
dataadmin2:x:4301:4300::/home/dataadmin2:/bin/bash
sysadmin1:x:4200:4200::/home/sysadmin1:/bin/bash

I want to replicate these to another machine. How can I create these users with same uid and gid values? Is there a way I can copy them to another machine?

like image 938
sandip divekar Avatar asked May 12 '14 05:05

sandip divekar


People also ask

How do I add a user to a specific UID?

You can use the -u ( --uid ) option to create a user with a specific UID.

What is the command to create a user with a pre defined UID shell and home directory?

When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system. Depending on command line options, the useradd command updates system files and may also create the new user's home directory and copy initial files.


2 Answers

First, create the group if it doesn't exist:

$ groupadd -g 4200 sysadmin2

Next, create the user and add it to the group:

$ useradd sysadmin2 -u 4201 -g 4200 -m -s /bin/bash
$ useradd appadmin1 -u 4100 -g 4100 -m -s /bin/bash 

and don't forget to reset password for each user.

like image 128
Max Avatar answered Oct 15 '22 16:10

Max


In summary and in general, you can use the useradd command to add users to a linux system. The -u flag allows you to set a specific user id and the -g flag allows you to set a specific group id. Please see useradd's manpage for more details -- on a terminal, type man useradd to see it.

Now, specifically about your problem, see below.

Assumming you have three groups on your original machine:

$ cat /etc/group
...
appadmins:x:4100:
sysadmins:x:4200:
dataadmins:x:4300:
...

On your destination/new machine, you should first create the groups using:

groupadd appadmins -g4100
groupadd sysadmins -g4200
groupadd dataadmins -g4300

Then, you can proceed to create the actual users like so:

useradd appadmin1 -u4100 -g4100 -d/home/appadmin1 -s/bin/bash
useradd appadmin2 -u4101 -g4100 -d/home/appadmin1 -s/bin/bash
useradd sysadmin1 -u4200 -g4200 -d/home/sysadmin1 -s/bin/bash
useradd sysadmin2 -u4201 -g4200 -d/home/sysadmin2 -s/bin/bash
useradd dataadmin1 -u4300 -g4300 -d/home/dataadmin1 -s/bin/bash
useradd dataadmin2 -u4301 -g4300 -d/home/dataadmin2 -s/bin/bash

The -d option is used to set the home directory and the -s option is used to set the shell. Again, -u and -g are used to set a specific user and group id.

To check that everything went correctly, just use grep admin on your /etc/passwd file:

$ grep admin /etc/passwd
appadmin1:x:4100:4100::/home/appadmin1:/bin/bash
appadmin2:x:4101:4100::/home/appadmin1:/bin/bash
sysadmin1:x:4200:4200::/home/sysadmin1:/bin/bash
sysadmin2:x:4201:4200::/home/sysadmin2:/bin/bash
dataadmin1:x:4300:4300::/home/dataadmin1:/bin/bash
dataadmin2:x:4301:4300::/home/dataadmin2:/bin/bash

If something is wrong, you can use userdel or groupdel accordingly and start over.

like image 35
Rudy Matela Avatar answered Oct 15 '22 14:10

Rudy Matela