Linux 101: useradd or adduser

sandeep negi
2 min readNov 8, 2019

--

So why do we use the command called useradd we use this command to create a user when we run this command it performs three actions.

  1. It edits /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow files for the newly created User account.
  2. Creates and populates a home directory for the new user.
  3. Sets permissions and ownerships to the home directory.
  4. How to add a new user in Linux?
useradd sandy

the above command will create a user to set the password for the user

2. passwd sandy

once the user is created it will create a new entry in passwd file

sandy:x:1001:1001::/home/sandy:/bin/bash

this entry is like this

sandy = username

x = password in an encrypted format in the file /etc/passwd

1001 = userid

1001 = group id

:: = this space is empty it should be fulfilled by user info

/home/sandy = is user home directory

/bin/bash = is user location for bash

4. How to add a user to multiple groups

useradd -G group1,group2,group3 sandy

5. Add a user with a specific home directory, custom shell, and custom comment

useradd -m -d /var/www/sandy -s /bin/zsh -c “this is comment” -U sandy

6. To get all the id associated with the user

[ec2-user@ip-172–31–84–180 ~]$ id sandytestuid=1002(sandytest) gid=1002(sandytest) groups=1002(sandytest),1000(ec2-user)

gid 1002 and group name sandytest is the primary group whereas the groups are the secondary group

Usermod

usermod is used to change something associated with the existing user.

below command change the secondary group associated with ec2-user it will add the group sandy to it

usermod -G sandy ec2-user

so now you guys must be thinking what the hell is the primary and secondary group

So let's begin with primary group. when you create a new user and the group associated with it which has the same name as a user is the primary group and if we want to associate a user with multiple groups we will use secondary groups.

--

--

sandeep negi
sandeep negi

Written by sandeep negi

Software Engineer, who loves challenges and new technologies. :)

No responses yet