useradd is a low-level utility to create new users to the system. When used without the -D option, useradd command will create a new user account using the command line plus the default values ??of the system. Depending on the command line option, useradd command will update system files and also can create new user’s home directory. In this article, I will explain to you how to create a user then add it to the particular group in CentOS 5 and CentOS 6. Prior to that , this article assumes that the server CentOS 5 or CentOS 6 has been fully installed.
1. Add a new user. In this example, the username will be ‘myroot‘.
[root@CentOS57 ~]# useradd myroot
[root@CentOS57 ~]# passwd myroot
Changing password for user myroot.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
2. Check current groups for ‘root‘ user.
[root@CentOS57 ~]# groups
root bin daemon sys adm disk wheel
3. Add ‘myroot‘ into root group as below:
useradd -G {group-name} username
[root@CentOS57 ~]# usermod -G root myroot
Above is example, we are adding a user ‘myroot‘ into group ‘root‘.
4. Switch to ‘myroot‘ user. Then Check current group for ‘myroot‘. root group will be the second group for ‘myroot’.
[root@CentOS57 ~]# su - myroot
[myroot@CentOS57 ~]$ groups
myroot root
5. Grep the ‘myroot’ user’s group.
[root@CentOS57 ~]# grep myroot /etc/group
root:x:0:root,myroot
myroot:x:502:
6. Check the file permission for /etc/passwd
[root@CentOS57 ~]# ls -al /etc/passwd
-rw-r--r-- 1 root root 1026 Jan 21 05:32 /etc/passwd
Explanation : Only the owner which is ‘root’ user can edit the /etc/passwd file, not in the root’s group.