在 Linux 中处理用户

添加用户:

要将用户添加到 Linux 系统,请使用 adduser 命令,后跟用户名。 adduser 将提示您还为新用户创建密码,并且可能还会为该用户创建一个新组。

新用户还将在 /home/newuser 中获得一个主目录,并将在 /etc/passwd 文件中列出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo adduser newuser
[sudo] password for trshpuppy:
Adding user `newuser' ...
Adding new group `newuser' (1001) ...
Adding new user `newuser' (1001) with group `newuser' ...
Creating home directory `/home/newuser' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for newuser
Enter the new value, or press ENTER for the default
Full Name []: new user
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
1
2
3
4
5
6
# the 'su' command lets us switch to the new user:
su newuser
Password:
newuser@trshheap:~$ # <-- new user shell context
cat /etc/passwd | grep "newuser"
newuser:x:1001:1001:new user,,,:/home/newuser:/bin/bash

/etc/passwd

该文件实际上并未存储密码,但曾经存储过。现在,可以将其连接起来以查看有关计算机上用户的信息。

1
2
3
4
cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...

第二个字段中的 x 是用户密码的占位符。接下来的字段是“用户ID”。在 root 用户行中,用户 ID 为 0 ,这意味着 root 是 OG 用户。

/etc/passwd 文件中的每一行还列出了每个用户的主文件夹和 shell 类型。例如,root 用户的主文件夹是 /root,shell 类型是 bash (/bin/bash)。

计算机上运行的不同服务可能会在 /etc/passwd 中列为用户,这意味着连接它可以告诉您存在哪些服务和程序。

/etc/影子

要查看 /etc/shadow 文件夹,您需要超级用户权限。该文件包含系统上用户的散列密码

1
2
3
4
5
6
sudo cat /etc/shadow
[sudo] password for trshpuppy:
root:*:18375:0:99999:7:::
daemon:*:18375:0:99999:7:::
...
newuser:$6$oi/lTewmOz8/mkiA$sysDHsD(...).:19496:0:99999:7:::

为每个用户细分列出的字段如下:

1
2
3
4
5
6
7
8
9
10
11
mark:$6$.n.:17736:0:99999:7:::
[--] [----] [---] - [---] ----
| | | | | |||+-----------> 9. Unused (reservered)
| | | | | ||+------------> 8. Expiration date
| | | | | |+-------------> 7. Inactivity period
| | | | | +--------------> 6. Warning period
| | | | +------------------> 5. Maximum password age
| | | +----------------------> 4. Minimum password age
| | +--------------------------> 3. Last password change
| +---------------------------------> 2. Encrypted Password
+----------------------------------------> 1. Username

-Linuxize:了解 /etc/shadow 文件

加密密码:

加密密码字段的格式为:$type$salt$hash,其中$type 是使用的加密算法。可以使用以下算法:

  • $1$:MD5
  • $2a$:河豚
  • $2y$:Eksblowfish
  • $5$:SHA-256
  • $6$:SHA-512

注意: 在 Linux 中,由于密码都经过哈希处理和加盐处理,因此相同的密码将产生两个不同的哈希值。 对于 Windows 来说情况并非如此,其中两个不同用户的相同密码将产生完全相同的哈希值。

如果密码字段设置为 *! ,则意味着用户将无法使用密码身份验证登录(仍然允许使用 su 和基于密钥的身份验证等其他方法)。

对于最佳实践,像 root 这样的用户不应该能够使用密码登录。相反,可以通过允许某些用户暂时将其权限提升为 root 并记录以记录发生情况的时间来控制 root 访问。

制作加密密码:

要在 bash 中创建加密密码,您可以使用 whois 包附带的 mkpasswd 命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mkpasswd --help
Usage mkpasswd [OPTIONS]... [SALT]]
Crypts the PASSWORD using crupt(3).

-m, --method=TYPE select method TYPE
-5 like --method=md5crypt
-S, --salt=SALT use the specified SALT
-R, --rounds=NUMBER use the specified NUMBER of rounds
-P, --password-fd=NUM read the password from the file descriptor NUM
instead of /dev/tty
-s, --stdin like --password-fd=0
-h, --help
-V, --version output version information and exit

If PASSWORD is missing then it is asked interactively.
if no SALT specified, a random one is generated.
If TYPE is 'help', available methods are printed.

更改用户密码:

在 kali/Linux 中(至少)有两种方法可以更改用户密码。第一种是使用 usermod -p 命令 但是 该命令要求您将密码以纯文本形式粘贴到命令行中 ** 这将在 shell 历史记录中可见,并在 /etc/shadow 中以纯文本形式查看!**

更改用户密码的更好方法是使用 sudo passwd <user>。该命令将提示您输入密码,但明文不会在命令行中显示或保存。另外,密码将在 etc/shadow 中更新为散列值。

删除用户:

您可以使用 deluser (或有时 userdel)命令

团体

将用户添加到组中:

要查看用户所在的组,您可以切换到该用户并使用 groups 命令,该命令将列出他们所在的所有组。要将用户添加到组中,请使用:

1
sudo usermod -a -G sudo exampleUser

-a 表示 append ,并将将此组追加到用户的当前组列表中,而不是覆盖其当前组列表。 G 代表 groups 并指定要将它们添加到的组。

/etc/组

/etc/group 文件列出了计算机上的所有组以及其中的用户。文件中的每一行都有 4 个字段:

1
2
3
4
5
6
7
sudo:x:27:trshpuppy,newuser
[--][-][-][----------------]
| | | |+ ---------> Group List: users in the group (separated w/ ',')
| | |+--------------------> Group ID: ea user has a group ID (listed in
| | /etc/passwd)
| |+-----------------------> Password: Not generally used, 'x' placeholder
|+--------------------------> Group Name: name of the group

-CyberCiti: Understanding /etc/group File

/etc/sudoers

The /etc/sudoers file contains information on the sudoers group including which users are part of it and who can use sudo to escalate their privileges.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sudo cat sudoers
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

当调查一个盒子/系统时,最好知道 sudo 权限是什么。您可以使用 sudo -l 来做到这一点:

1
2
3
4
5
6
sudo -l
Matching Defaults entries for trshpuppy on trshheap:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User trshpuppy may run the following commands on trshheap:
(ALL : ALL) ALL

在此示例中,sudo 命令和 sudoers 组中的用户具有 ALL 权限。

sudo -l 的男子条目:

1
2
3
4
-l, --list:
If no command is specified, list the allowed (and forbidden) commands for the invoking user (or the user specified by the -U option) on the current host. A longer list format is used if this option is specified multiple times and the security policy supports a verbose output format.

If a command is specified and is permitted by the security policy, the fully-qualified path to the command is displayed along with any command line arguments. If a command is specified but not allowed by the policy, sudo will exit with a status value of 1.

编辑 sudoers 文件:

编辑 sudoers 文件(添加或删除用户)时,您应该使用 visudovisudo 命令不仅检查语法和解析,还可以防止文件被多人同时编辑。

使用 visudo 将启动像 nano 这样的命令行编辑器。您可以通过 sudo EDITOR=nano visudo 设置它使用的编辑器。 visudo 将不允许您的更改被保存如果它们没有通过测试

[!资源:]