|
1 | 1 | # User Account Management: |
2 | 2 | ## Table of Contents: |
| 3 | +* [Intro](#intro) |
| 4 | +* [Creating User Accounts](#creating-user-accounts) |
| 5 | + + [`useradd`](#useradd) |
| 6 | + + [User Defaults](#user-defaults) |
| 7 | + + [`usermod`](#usermod) |
| 8 | + + [`userdel`](#userdel) |
| 9 | +* [Group Accounts](#group-accounts) |
| 10 | + + [User Group Accounts](#user-group-accounts) |
| 11 | + + [Creating Group Accounts](#creating-group-accounts) |
| 12 | + + [Locked out of `sudoers` and Fixed](#locked-out-of-sudoers-and-fixed) |
| 13 | +* [Managing Users in Complex Systems](#managing-users-in-complex-systems) |
| 14 | + + [Setting Permissions with Access Control Lists (ACL)](#setting-permissions-with-access-control-lists-(acl)) |
| 15 | + + [Setting ACLs with `setfacl`](#setting-acls-with-setfacl) |
| 16 | + + [Setting Default ACLs](#setting-default-acls) |
| 17 | + + [Enabling ACLs](#enabling-acls) |
| 18 | + + [Adding Directories for Users to Collaborate](#adding-directories-for-users-to-collaborate) |
| 19 | + + [Creating Restricted Deletion Directories with Sticky Bits](#creating-restricted-deletion-directories-with-sticky-bits) |
| 20 | + |
3 | 21 | ## Intro: |
4 | | -- This chapter is |
| 22 | +- This chapter is about managing users in Linux. It does that in 3 main sections: |
| 23 | + - First, we tackle user accounts: how to add, delete and change information about single users. |
| 24 | + - Second, we treat user group accounts and see how we can create settings for a multiple users and what they can do by simply changing their group setting and by adding them or removing them from groups. |
| 25 | + - Third, we look at more advanced ways of managing users such as ACLs and setting collaborative directories with set GIT bit and sticky bit. |
5 | 26 |
|
6 | 27 | ## Creating User Accounts: |
7 | 28 | ### `useradd`: |
@@ -122,62 +143,88 @@ sudo groupadd -g 1111 sales |
122 | 143 | - The **`-g`** option indicates a specific GID. Without it, the next available GID is assigned to the group. |
123 | 144 | - The **`groupmod`** command allows you to modify several attributes from a given group. |
124 | 145 |
|
125 | | -## Managing User Accounts in a Complex Environment: |
126 | | -## Centralizing User Accounts: |
127 | | - |
128 | | - |
129 | | - |
130 | | - |
131 | | - |
132 | | - |
133 | | - |
134 | | - |
135 | | - |
136 | | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | | - |
142 | | - |
143 | | - |
144 | | - |
145 | | - |
146 | | - |
147 | | - |
148 | | - |
149 | | - |
150 | | - |
151 | | - |
152 | | - |
153 | | - |
154 | | - |
155 | | - |
156 | | - |
157 | | - |
158 | | - |
159 | | - |
160 | | - |
161 | | - |
162 | | - |
163 | | - |
164 | | - |
165 | | - |
166 | | - |
167 | | - |
168 | | - |
169 | | - |
170 | | - |
171 | | - |
172 | | - |
173 | | - |
174 | | - |
175 | | - |
176 | | - |
| 146 | +### Locked out of `sudoers` and Fixed: |
| 147 | +- ***I have stupidly managed to accidentally lock myself out of the sudoers files. I don't know what exactly caused this. Was it `usermod -G` or `usermod -g`.*** |
| 148 | +- To fix such a problem I had to boot the system in recovery mode by pressing the shift key during the booting process. Some screen showing an option for *recovery mode* appears. I click on root and, voilà, I'm logged in as root. I check the sudoers file and don't see my actual username, but see another one. I had deleted that user. Instead of modifying the sudoers file in this recovery mode I thought it'd be wiser to just add that user which is in the sudoers file. Before that I had to make the filesystem also writable because it's read-only in recovery mode. Making the filesystem writable is done with the following command: |
| 149 | +```sh |
| 150 | +mount -o rw,remount / |
| 151 | +``` |
| 152 | +- The question now is: how do I secure my system so that only an authorized user can actually log in in recovery mode and fix problems like this!!? |
| 153 | + |
| 154 | +## Managing Users in Complex Systems: |
| 155 | +- The user/group model is good in simple setups with few users. In more complex settings, this model becomes restrictive and suffers from such shortcomings as: |
| 156 | + - Only one user and one group can be assigned to a file/directory. |
| 157 | + - A regular user's ability to set permissions on different files/directories is limited. |
| 158 | +- We sometimes need more users to share a file and regular users to have more freedom to set permissions on files and directories. We will do this using **access control lists (ACLs)** and *shared directories* (**sticky bit** and **set GID bit** directories). |
| 159 | + |
| 160 | +### Setting Permissions with Access Control Lists (ACL): |
| 161 | +- Basically, ACLs allow regular users to share their files and directories with certain other users and groups. A regular user can allow select users/groups to read/write/execute files without such files having open permissions. |
| 162 | +- The following general rules apply to ACLs: |
| 163 | + - To use ACLs on a filesystem, you must enable them when that filesystem is mounted. |
| 164 | + - In Ubuntu, Fedora and others, ACLs are enabled by default on the filesystem created during installation of the system. |
| 165 | + - If you want ACLs enabled on a filesystem you create/add after installation, you must set the acl mount option when that filesystem is mounted. |
| 166 | + - ACLs are added to a file using the **`setfacl`** commands. Viewing ACLs on a file is done with **`getfacl`**. |
| 167 | + - Only the owner of a file can set ACLs on it. If a user is assigned user/group permissions using `setfacl`, that user cannot change ACLs on that file. |
| 168 | + - Because multiple groups/users can be assigned to a file, the permissions the user has on the file is the union of permissions assigned to the user and all the groups he belongs to. If I belong to sales which has read permission, and marketing which has write permission and development which has execute permission, the I have their permissions combined so I can read/write/execute the file. |
| 169 | + |
| 170 | +### Setting ACLs with `setfacl`: |
| 171 | +- You set ACL permissions on a file using `setfacl` along with the **`-m`** option as in: |
| 172 | +```sh |
| 173 | +setfacl -m u:username:rwx filename |
| 174 | +``` |
| 175 | +- You can remove these options using the **`-x`** option: |
| 176 | +```sh |
| 177 | +setfacl -x u:username filename |
| 178 | +``` |
| 179 | +- When you run `ls -l` and see a file has a plus in its permissions as in **`rw-rw-r--+`**, this means ACLs are set on it and you can use `getfacl` to see what ACLs it has. Let's look at an example of what `getfacl` prints out: |
| 180 | +``` |
| 181 | +# file: someFile |
| 182 | +# owner: am |
| 183 | +# group: am |
| 184 | +user::rw- |
| 185 | +user:fafa:rwx |
| 186 | +group::rw- |
| 187 | +mask::rwx |
| 188 | +other::r-- |
| 189 | +``` |
| 190 | +- **`mask`** in the example above is the maximum permissions on the file including ACL and regular users and groups. In Fedora this mask cannot exceed the regular group permissions. This *might be true for Ubuntu as well.* |
177 | 191 |
|
| 192 | +### Setting Default ACLs: |
| 193 | +- You can set default ACL permissions on a directory so all files and directories created in that directory inherit those ACL permissions. In the following example, we set default ACLs on directory `/business/john` using **`d:`** and **`g:`** is for group sales. Each file or directory added to this directory have the same ACLs for sales (`rwx`): |
| 194 | +```sh |
| 195 | +setfacl -m d:g:sales:rwx /business/john |
| 196 | +``` |
| 197 | +- Files/directories you create within this file might differently mainly due to the masking that dictates they cannot exceed the file's group permissions. Their **effective** permissions are union of the mask and the default ACL permissions. |
178 | 198 |
|
| 199 | +### Enabling ACLs: |
| 200 | +- I don't understand what's going on here, so I'll just skip this for now! |
179 | 201 |
|
| 202 | +### Adding Directories for Users to Collaborate: |
| 203 | +- A fourth set of bits is usually ignored when using `chmod`. These bits can be used by `chmod` to set special permissions. These are the so-called **set user ID bit**, **set group ID bit**, and the **sticky bit**. |
| 204 | +- The following table shows how these bits and their values work with `chmod` |
180 | 205 |
|
| 206 | +| Name | Numeric value | Letter value | |
| 207 | +| --- | --- | --- | |
| 208 | +| Set user ID bit | **`4`** | **`u+s`** | |
| 209 | +| Set group ID bit | **`2`** | **`g+s`** | |
| 210 | +| Sticky bit | **`1`** | **`o+t`** | |
181 | 211 |
|
| 212 | +- This section is about creating directories for collaboration, so we will just focus on the set group ID bit and sticky bit. |
| 213 | +- By creating a GID directory, any file created in that directory is automatically shared with/assigned to users of that directory's group. Let's say we want to make `mabi3at` directory a collaborative file among sales users. We start by using a new chmod that includes a 4th number or its equivalent letter representation: |
| 214 | +```sh |
| 215 | +chmod 2775 mabi3at |
| 216 | +``` |
| 217 | +- We assign the mabi3at directory to sales group, so users belonging to the sales group have permissions `775` on any file created in this directory. When a user belonging to the sales group creates a file in this directory, the group of the file is automatically sales, not the primary group of the user. I see how this can be more convenient than ACLs. |
| 218 | +- Directories with the set GID bit have an **`s`** in their group permissions bit, which we can see by running ls -l: |
| 219 | +```sh |
| 220 | +drwxrwsr-x 2 am sales 4096 Apr 7 20:43 mabi3at |
| 221 | +``` |
182 | 222 |
|
| 223 | +### Creating Restricted Deletion Directories with Sticky Bits: |
| 224 | +- By setting the sticky bit on a directory, you effectively prevent a user from deleting files that belong to other users. Imagine we have a collaborative directory where everyone can read everyone else's files, and can also add files to that directory. We cannot do this any other way. A user with the permission to write to a directory can create but also delete files in that directory. How can we get a user to only create files in a directory and delete her own but not delete other's files. This is the job of sticky bits which we can set using letter values as follows: |
| 225 | +```sh |
| 226 | +chmod o+t mabi3at |
| 227 | +``` |
| 228 | +- Directories with the set sticky bit have a **`t`** in their permissions as in **`drwxrwsr-t`**. |
| 229 | +- By the way, only the root user and the owner of the directory can delete files from this directory. |
183 | 230 |
|
0 commit comments