Linux: Groups and users
Linux users are important to any linux distribution, without a user you wont be able to administer your linux server at all. In this article I will go through the basics of linux users, groups and permissions.
To create a new user you can run the command
useradd this command will create the most simple user. If you also want the newly created user to have their own home directory you can create the user in two different ways.
useradd -d /home/ or by using the following command
useradd -m . Using the first one will specify the exact path to the new users home directory and the second one will set the default path.
Once the user is created you can set the password for the user with the following command:
Deleting a user
userdel can be used to delete a user from the system. If you wish to delete a specific user and its home directory (Including all files) you simply run this command:
userdel -r .
To create a new group you can run
If you want to check what groups a specific user are in you can run the
groups command. If you don’t add the username to the
groups command you will see what groups the currently logged in user (you) are in.
To add a existing user to a group you can use the
useradd command again but it will look like the following:
useradd -a -G . This command will add an existing user to the specified group, if the group does not exist it will be created. The
-a is there because we want to add a group to the user, without it we will replace the primary group of the user.
If you only want to create a new group, not adding it to a user you can run the following command:
group add .
Deleting a group is simple, run the following command:
Listing all users or groups
To list all users you must parse
/etc/passwd, this can be done using the
If you are looking for a specific user you can search for it using
grep combined with
cat. It will look like this:
cat /etc/passwd | grep “username”. Keep in mind that by default
grep is case sensitive, to turn this behaviour off you can use:
grep -i “username”.
If you want to list all groups you can use the same commands, you only have to replace the path to:
/etc/group to make it look like this:
cat /etc/group | grep -i “username”.