Users & groups

    Contents

  1. Users
  2. Groups
  3. User commands
  4. Group commands

Users #

Users and groups are a fundamental part of any Unix system. Every program is ran by a user. Every user and group can have permissions

The root user is the user with all permissions with UID and GUID 0.

Usernames are defined in file /etc/group and are internally just a user ID (UID). Most

UID 1000 is the user defined ID

Groups #

Groups are defined in file /etc/group and are internally just a group ID (GID). Groups can be used to set permissions on the group level instead for every user, see permissions. Often the group wheel is used for sudo privilages

GID 0 belongs to the root user GID 100 is the users group

User commands #

sudo #

Most distributions have sudo installed. It allows you to execute programs as the root or other users. sudo stands for super user do as.

99% of the time you can just start your command with sudo: sudo [your command]. You can also use sudo -i to open a root user shell.

Note that sudo is not required for Linux to work. But most distributions install it by default.

Switching users #

  • whoami shows the current user
  • su switch to root user
  • su - switch to root user and change home directories

Creating users #

Users can be created using the useradd command. Use the -m flag to create a home directory. You can specify a different home directory with -d. The -G flag allows you to specify groups.

TIP: If you already created the user and forgot to pass the -m flag you can use mkhomedir_helper.

Examples #

Typical user creation: creates a user called admin with a home directory in the group wheel with the bash shell:

NOTE: sudo is often configured to allow root access for all users in the wheel gropu.

useradd -m -G wheel -s /bin/bash admin
passwd admin

Create a user for a special program with a home directory on a special location:

useradd -m -d /opt/myopt operation

Setting password #

When creating a new user don't forget to set password with passwd.

Deleteting users #

userdel

userdel [username]

The groups command displays a list of the groups for a user. When run without arguments it will display the groups of the current user.

groups 
groups username

Group commands #

  • groupadd adds a new group
  • groupmod modify a group
  • groupdel delete a group
  • newgrp login to a new group, if you added a user to a group you can run this command instead of logging in again to update the permissions

Add user to group #

sudo usermod -aG groupname username