> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/user-and-group-managment.md).

# User And Group Managment

## User

A **user** is an account that represents an individual or a system entity.

1. Each user in a Linux system is assigned a unique **User ID (UID)**.
2. User information, such as **username, UID, home directory, and default shell**, is stored in the `/etc/passwd` file.
3. Linux categorizes users into three types:
   * **Root User**: UID = 0
   * **Regular User**: UID range: 1000 - 60000
   * **System/Service User**: UID range: 201 - 999

### **User Management Files**

#### **`/etc/passwd`**

This file stores essential user account information and consists of seven fields:

| Field | Description                |
| ----- | -------------------------- |
| 1     | Username                   |
| 2     | Password Placeholder (`x`) |
| 3     | UID                        |
| 4     | GID                        |
| 5     | Comment                    |
| 6     | Home Directory             |
| 7     | Default Shell              |

**Example Entry:**

```plaintext
user1:x:1001:1001:User One:/home/user1:/bin/bash
```

**`/etc/shadow`**

The `/etc/shadow` file stores password hashes and additional security information. It is only readable by root.

**Example Entry:**

```plaintext
user1:$6$hashedpassword::0:99999:7:::
```

| Field | Description          |
| ----- | -------------------- |
| 1     | Username             |
| 2     | Hashed Password      |
| 3     | Last Password Change |
| 4     | Minimum Password Age |
| 5     | Maximum Password Age |
| 6     | Warning Period       |
| 7     | Inactive Period      |
| 8     | Expiration Date      |

**Additional User Management Files**

* **`/etc/shells`** – Lists available shells.
* **`/etc/login.defs`** – Defines default user settings.
* **`/etc/default/useradd`** – Specifies default settings for new user creation.

***

### **User Management**

### **`useradd` - Add a User**

| Switch | Description                  |
| ------ | ---------------------------- |
| `-c`   | Add comment                  |
| `-d`   | Set home directory           |
| `-g`   | Set primary group ID         |
| `-G`   | Set secondary group ID       |
| `-m`   | Create home directory        |
| `-M`   | Do not create home directory |
| `-p`   | Set password                 |
| `-r`   | Create system user           |
| `-s`   | Set login shell              |
| `-u`   | Set UID                      |

**Example:**

```bash
useradd -c "John Doe" johndoe
```

```
useradd -d /custom/home johndoe
```

```
useradd -g 1002 johndoe
```

### **`usermod` - Modify a User**

| Switch | Description             |
| ------ | ----------------------- |
| `-c`   | Change comment          |
| `-d`   | Change home directory   |
| `-g`   | Change primary group    |
| `-G`   | Change secondary groups |
| `-L`   | Lock user               |
| `-U`   | Unlock user             |

**Example:**

```bash
usermod -c "Updated Comment" johndoe
```

```
usermod -L johndoe
```

```
usermod -U johndoe
```

### **`passwd` - Manage User Passwords**

| Switch | Description          |
| ------ | -------------------- |
| `-d`   | Delete password      |
| `-l`   | Lock password        |
| `-u`   | Unlock password      |
| `-x`   | Set max password age |

**Example:**

```bash
passwd johndoe
```

```
passwd -l johndoe
```

```
passwd -x 90 johndoe
```

### **`userdel` - Delete a User**

| Switch | Description           |
| ------ | --------------------- |
| `-r`   | Remove home directory |
| `-f`   | Force deletion        |

**Example:**

```bash
userdel -r johndoe
```

***

## **Group**

A **group** is a collection of users that share permissions and access rights.

### **Group Management Files**

**`/etc/group`**

This file stores **group-related information**.

| Field | Description                |
| ----- | -------------------------- |
| 1     | Group Name                 |
| 2     | Password Placeholder (`x`) |
| 3     | GID                        |
| 4     | Group Members              |

**Example Entry:**

```plaintext
admin:x:1001:user1,user2
```

**`/etc/gshadow`**

This file stores **secure group password information**.

| Field | Description         |
| ----- | ------------------- |
| 1     | Group Name          |
| 2     | Encrypted Password  |
| 3     | Group Administrator |
| 4     | Group Members       |

**Example Entry:**

```plaintext
admin:!!::user1,user2
```

***

### **Group Management**

### **`groupadd` - Add a Group**

| Switch | Description         |
| ------ | ------------------- |
| `-g`   | Set GID             |
| `-r`   | Create system group |

**Example:**

```bash
groupadd -g 1010 developers
```

```
groupadd -r systemgroup
```

#### **`groupmod` - Modify a Group**

| Switch | Description |
| ------ | ----------- |
| `-g`   | Change GID  |

**Example:**

```bash
groupmod -g 2000 developers
```

### **`gpasswd` - Manage Group Memberships**

| Switch | Description            |
| ------ | ---------------------- |
| `-a`   | Add user to group      |
| `-d`   | Remove user from group |

**Example:**

```bash
gpasswd -a johndoe developers
```

```
gpasswd -d johndoe developers
```

### **`groupdel` - Delete a Group**

**Example:**

```bash
groupdel developers
```
