Skip to content

Commit b76744b

Browse files
Create Users_Management.md (ronreiter#699)
I add Basic tutorial to learn user management
1 parent f6df0fc commit b76744b

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

Diff for: tutorials/learnshell.org/en/Users_Management.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Tutorial
2+
-----------------
3+
4+
GNU/Linux operating systems allow the management of multiple users and groups, so it is an interesting aspect to take into account.
5+
6+
Before starting with the examples, it is necessary to have super user permission to be able to carry out such actions. To obtain these permissions, one of the ways to achieve it is to prepend the word “sudo” to the command that will be addressed in each section. Now we can continue with the tutorial:
7+
8+
Example
9+
----------------
10+
11+
In order to create a new user, the following syntax must be taken into account:
12+
* `sudo useradd [options] username`
13+
14+
Among the options we find the following:
15+
16+
| Options | Meaning |
17+
| ------ | ------ |
18+
| -u uid | Number that identifies the user |
19+
| -g primary_group |group identifier|
20+
| -G group2, group3, group4 ... | List of secondary group names |
21+
| -d directory | Absolute path to the connection directory |
22+
| -s Shell | Absolute path to shell |
23+
| -e date | Expiration date of the new account |
24+
| -p password | Password |
25+
26+
27+
In order to modify the attributes of a user (which has already been created) the following syntax must be taken into account:
28+
* `sudo usermod [options] username`
29+
30+
| Options | Meaning |
31+
| ------ | ------ |
32+
| -c (–comment) | Sets the user's full name and other relevant account data |
33+
| -d (–home) |Assigns a new HOME directory to the user|
34+
| -m | Moves the contents of the user's HOME to a new location |
35+
| -g | Changes the user's primary group. Group name or number must be used. The group must already exist |
36+
| -G | Set the groups to which the user will belong. If the user already belongs to any group in the system and this group is not named when using this option, it will disappear from that group |
37+
| -L | Locks the user's account. Simply set a '!' in front of the encrypted key |
38+
| -U | Expiration date of the new account |
39+
| -e (–expiredate) | Unlock the user's account. Remove the '!' in front of the encrypted key |
40+
| -s | Selects the new Shell to be used by the user. If left blank it will use the default one set in the system |
41+
42+
In order to delete a user (which has already been created) the following syntax must be taken into account:
43+
44+
* `sudo userdel [options] username`
45+
46+
47+
| Options | Meaning |
48+
| ------ | ------ |
49+
| -r | Also deletes the user's login directory, and the user's mail spool |
50+
51+
Example 1: Create user1, specifying its connection directory --> /home/user1.
52+
```sh
53+
sudo mkdir /home/user1
54+
sudo useradd –d /home/user1 user1
55+
```
56+
Example 2: Create user2 which must have "bash" as shell type, must belong to the group "collaborator" and its id number must be 1500.
57+
```sh
58+
sudo groupadd collaborator
59+
sudo useradd -s /bin/bash -g collaborator -u 1500 user2
60+
```
61+
Please note:
62+
The user that has been created can be verified in the /etc/passwd file:
63+
```sh
64+
cat /etc/passwd | grep ^user2
65+
```
66+
The group to which user2 belongs can be verified with the following command:
67+
```sh
68+
groups user2
69+
```
70+
Example 3: Change the shell type of user2 to "/bin/sh" and the primary group it will now belong to is "manager".
71+
```sh
72+
sudo groupadd manager
73+
sudo usermod -s /bin/sh -g manager user2
74+
```
75+
Example 4: Delete user2 where it is necessary to also delete its associated connection directory
76+
```sh
77+
sudo userdel -r user2
78+
```
79+
Exercise
80+
--------
81+
In a technological university it is necessary to add 2 new students called "Tomas" and "Lu" to a group called "Investigation" where the shell they will use is "bash". Their connection directories should be investigation_tomas and investigation_lu (HOME).
82+
83+
Solution
84+
--------
85+
```sh
86+
sudo groupadd Investigation
87+
sudo mkdir /home/investigation_tomas
88+
sudo useradd -g Investigation -s /bin/bash -d /home/investigation_tomas tomas
89+
sudo mkdir /home/investigation_lu
90+
sudo useradd -g Investigation -s /bin/bash -d /home/investigation_lu lu
91+
cat /etc/passwd | grep ^tomas
92+
groups tomas
93+
cat /etc/passwd | grep ^lu
94+
groups lu
95+
```
96+
97+
98+

0 commit comments

Comments
 (0)