If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Users Administration in Linux
Add New User in Linux
useradd - Create a new user or update default new user information
Syntax
useradd [-c comment] [-d home_dir] [-e expire_date] [-f inactive_days] [-g initial_group] [-G group[,…]] [-m [-k skeleton_dir]] [-o] [-p passwd] [-s shell] [-u uid] login
useradd -D [-g default_group] [-b default_home] [-e default_expire_date]
[-f default_inactive] [-s default_shell]
If you want to know more available options you need to check the useradd man page
Examples
Adding New User
First you need to create three (test1,test2,admin1) groups for our examples using groupadd
Options
-d home directory
-s starting program (shell)
-p password
-g (primary group assigned to the users)
-G (Other groups the user belongs to)
-m (Create the user’s home directory )
To add a new user with
a primary group of test1
a second group test2
starting shell /bin/bash
password of xxxx
home directory of admin
create home directory
a login name of admin
#useradd -g test1 -G test2 -s /bin/bash -p xxxx -d/home/admin -m admin
This will create a new user admin.
One additional switch worth mentioning is “-D”, which controls the defaults for useradd.
Specifying the “-D” switch on its own will simply display the default settings, while specifying -D in conjunction with other switches will change the defaults to those values.
# useradd -D
GROUP=100
INACTIVE=-1HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
# useradd -D -s /bin/sh
# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
As you can see, this changes the default shell for created users from “bash” to “sh”.
adduser - User Friendly Frontend for useradd command
Syntax
adduser [options] user group
If you want to know available option refer add user man page
Example
#adduser admin
Adding user `admin’ …
Adding new group `admin’ (1001) …
Adding new user `admin’ (1001) with group `admin’ …
Creating home directory `/home/admin’ …
Copying files from `/etc/skel’ …
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for admin
Enter the new value, or press ENTER for the default
Full Name []: Admin
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [y/N] y
You’ll notice that, by default, the adduser command creates a group with the same name as the username, and makes this group the primary group for that user. This is called a user private group (UPG)
Modify User in Linux
usermod - Modify a user account
Syntax
usermod [-c comment] [-d home_dir [-m]] [-e expire_date] [-f inactive_days]
[-g initial_group] [-G group [,…]] [-l login_name] [-p passwd] [-s shell] [-u uid [-o]] [-L|-U] login
If you want to know available option check usermod man page
Example
Options:
-d home directory
-s starting program (shell)
-p password
-g (primary group assigned to the users)
-G (Other groups the user belongs to)
To add the group ‘others’ to the user admin
#usermod -G others admin
Delete User in Linux
userdel - Delete a user account and related files
Syntax
userdel [-r] login
If you want to know available options check userdel man page
Example
Options
-r (remove home directory)
To remove the user ‘admin’ and his home directory
#userdel -r admin
deluser - remove a user from the system
Syntax
deluser [options] user group
If you want more options check deluser man page
Example
By default, deluser will remove the user without removing the home directory, the mail spool or any other files on the system owned by the user. Removing the home directory and mail spool can be achieved using the –remove-home option. If the –home option is given, deluser will only remove the user if the directory given to the –home option matches the user’s real home directory.
#deluser –remove-home admin
Groups Administration in Linux
Add New Group in Linux
groupadd - Create a new group
Syntax
groupadd [-g gid [-o]] group
For more options check groupadd man page
Example
#groupadd test1
This will create a test1 group
addgroup - add a group to the system
Syntax
addgroup [options] [–gid ID] group
If you want to know available options check addgroup man page
#addgroup
Enter a groupname to add: admin1
Adding group `admin1′ (1001)…
Done.
Modify Group in Linux
groupmod - Modify a group
Syntax
groupmod [-g gid [-o]] [-n group_name ] group
For more options check groupmod man page
Example
#groupmod test1 test2
This will modify group name test1 to test2
Delete group in Linux
groupdel - Delete a group
Syntax
groupdel groupname
For more options check groupdel man page
Example
#groupdel test2
this will delete the test2 group
delgroup - remove a group from the system
Syntax
delgroup [options] [–only-if-empty] group
For more details about options check delgroup man page
Example
#delgroup –only-if-empty test2
Removing group `test2′…
done.
groups Command
print the groups a user is in
Syntax
groups [username]
This simple command displays what groups a user is a member of. It takes the username of user as a parameter. If no username is given, it defaults to the current user.
# groups
root
# groups admin
test1 : test2
You may also be interested in...