20191209 Linux in respect of such a study (5)

The user identity and file permissions

5.1 user's identity and ability

The reason why Linux system administrator is root, not because its name is root, but because that is the user's identification number UID(User IDentification) value is 0. In the Linux system, the equivalent of our UID as unique ID number, so the user identity can be determined by the value of the user's UID. In RHEL 7 system, users have the following identity.

  • Administrator UID is 0: the system administrator user.
  • UID for the users of the system 1~999: Linux system in order to avoid a loophole in a service program is to provide the right to hackers entire server, the default service program will be responsible for running the stand-alone system users, thus effectively control the extent of damage.
  • Average user UID from 1000the beginning: is created by the administrator for the user's daily work.

UID is not conflicting, and the administrator to create a normal user's UID is the default 1000(even in front of idle number) began.

In order to facilitate the management of users belonging to the same group, Linux system also introduces the concept of user groups. By using the user group number (GID, Group IDentification), we can put more users join the same group, making it easy for the user group of unified planning permission or a specified task.

When you create each user in a Linux system will automatically create a basic user group with the same name, and this is only the basic user groups a user person. If the user later be grouped into other user groups, the user groups that other users extended group call. A user has only a basic user group, but can have multiple user groups expand to meet the daily operational needs.

5.1.1 useradd command

useradd command creates a new user in the format of "useradd [option] username."

When you create a user account to use this command, the default user's home directory is stored in the / home directory as the default Shell interpreter /bin/bash, and by default creates a basic user group with the same name as the user.

image

/sbin/nologinIt is the end of an interpreter, and the interpreter Bash starker. Once the user is provided to interpreter nologin, it represents that the user can not log into the system.

[root@hwjsLinux hwj]# useradd -d /home/linux -u 8888 -s /sbin/nologin linuxprobe
[root@hwjsLinux hwj]# id linuxprobe
uid=8888(linuxprobe) gid=8888(linuxprobe) groups=8888(linuxprobe)

5.1.2 groupadd command

groupadd command is used to create groups of users in the format of "groupadd [options] group name."

[root@hwjsLinux hwj]# groupadd ronny

5.1.3 usermod command

usermod command modifies the user's attributes, the format of "usermod [options] user name."

User information is stored in /etc/passwda file, you can use a text editor to directly modify the user parameters of the project, you can also use the command usermod modify user information has been created.

image

[root@hwjsLinux hwj]# id linuxprobe
uid=8888(linuxprobe) gid=8888(linuxprobe) groups=8888(linuxprobe)
[root@hwjsLinux hwj]# usermod -G root linuxprobe 
[root@hwjsLinux hwj]# id linuxprobe
uid=8888(linuxprobe) gid=8888(linuxprobe) groups=8888(linuxprobe),0(root)
[root@hwjsLinux hwj]# usermod -u 8889 linuxprobe 
[root@hwjsLinux hwj]# id linuxprobe
uid=8889(linuxprobe) gid=8888(linuxprobe) groups=8888(linuxprobe),0(root)

5.1.4 passwd command

passwd command to change user passwords, expiration date, and authentication information in the format of "passwd [options] [username]."

Ordinary users can only use the passwd command to change their system password, and root administrators have permission to change the password of everyone else. Do not need to verify the old password root administrator to change the password yourself or others in the Linux system.

image

[root@hwjsLinux hwj]# passwd -l linuxprobe 
Locking password for user linuxprobe.
passwd: Success
[root@hwjsLinux hwj]# passwd -S linuxprobe 
linuxprobe LK 2019-12-05 0 99999 7 -1 (Password locked.)
[root@hwjsLinux hwj]# passwd -u linuxprobe 
Unlocking password for user linuxprobe.
passwd: Success
[root@hwjsLinux hwj]# passwd -S linuxprobe 
linuxprobe PS 2019-12-05 0 99999 7 -1 (Password set, MD5 crypt.)

5.1.5 userdel command

userdel command to delete a user in the format of "userdel [options] user name."

When a delete operation, the default user's home directory is preserved, then you can use the -rparameters to remove it.

image

5.2 file permissions and ownership

Linux systems use different characters to distinguish, as shown in the following common characters.

  • -: Ordinary files.
  • d: Catalog file.
  • l: Link file.
  • b: Block device file.
  • c: Character device file.
  • p: Pipe file.

For general files, permissions easier to understand: "readable" means the ability to read the actual contents of the file; "write" means the ability to edit, add, modify, delete the actual contents of the file; "executable" means capable of run a script.

对目录文件来说,“可读”表示能够读取目录内的文件列表;“可写”表示能够在目录内新增、删除、重命名文件;而“可执行”则表示能够进入该目录。

文件的读、写、执行权限可以简写为 rwx,亦可分别用数字 421 来表示,文件所有者,所属组及其他用户权限之间无关联

image

文件权限的数字法表示基于字符表示(rwx)的权限计算而来,其目的是简化权限的表示。例如,若某个文件的权限为 7 则代表可读、可写、可执行(4+2+1);若权限为 6 则代表可读、可写(4+2)。

image

通过分析可知,该文件的类型为普通文件,所有者权限为可读、可写(rw-),所属组权限为可读(r--),除此以外的其他人也只有可读权限(r--),文件的磁盘占用大小是 34298 字节,最近一次的修改时间为 4 月 2 日的凌晨 23 分,文件的名称为 install.log。

5.3 文件的特殊权限

在复杂多变的生产环境中,单纯设置文件的 rwx 权限无法满足我们对安全和灵活性的需求,因此便有了 SUID、 SGID 与 SBIT 的特殊权限位。 这是一种对文件权限进行设置的特殊功能,可以与一般权限同时使用,以弥补一般权限不能实现的功能。

5.3.1 SUID

SUID 是一种对二进制程序进行设置的特殊权限,可以让二进制程序的执行者临时拥有属主的权限(仅对拥有执行权限的二进制程序有效)。

[root@hwjsLinux hwj]# ls -l /etc/shadow
----------. 1 root root 1172 Dec  6 01:13 /etc/shadow
[root@hwjsLinux hwj]# ls -l /bin/passwd 
-rwsr-xr-x. 1 root root 27832 Jun  9  2014 /bin/passwd

查看 passwd 命令属性时发现所有者的权限由 rwx 变成了 rws,其中 x 改变成 s 就意味着该文件被赋予了 SUID 权限。如果原先权限位上没有 x 执行权限,那么被赋予特殊权限后将变成大写的 S。

SUID的显示:

image

5.3.2 SGID

SGID 主要实现如下两种功能:

  • 让执行者临时拥有属组的权限(对拥有执行权限的二进制程序进行设置);
  • 在某个目录中创建的文件自动继承该目录的用户组(只可以对目录进行设置)。

chmod 命令是一个非常实用的命令,能够用来设置文件或目录的权限,格式为“chmod [参数] 权限 文件或目录名称”。除了设置文件或目录的权限外,还可以设置文件或目录的所有者和所属组,这里使用的命令为chown,其格式为“chown [参数] 所有者:所属组 文件或目录名称”。

chmod 和 chown 命令是用于修改文件属性和权限的最常用命令,它们还有一个特别的共性,就是针对目录进行操作时需要加上大写参数-R 来表示递归操作,即对目录内所有的文件进行整体操作。

[root@hwjsLinux hwj]# chmod 777 xxx 
[root@hwjsLinux hwj]# chown root:root xxx 
[root@hwjsLinux hwj]# chmod -R 777 www.linuxprobe.com/

SGUID的显示:

image

5.3.3 SBIT

SBIT 特殊权限位可确保用户只能删除自己的文件,而不能删除其他用户的文件。

与前面所讲的 SUID 和 SGID 权限显示方法不同,当目录被设置 SBIT 特殊权限位后,文件的其他人权限部分的 x 执行权限就会被替换成 t 或者 T,原本有 x 执行权限则会写成 t,原本没有 x 执行权限则会被写成 T。

SBIT的显示:

image

5.4 文件的隐藏属性

Linux 系统中的文件除了具备一般权限和特殊权限之外,还有一种隐藏权限,即被隐藏起来的权限,默认情况下不能直接被用户发觉。

5.4.1 chattr 命令

chattr 命令用于设置文件的隐藏权限,格式为“chattr [参数] 文件”。如果想要把某个隐藏功能添加到文件上,则需要在命令后面追加“+参数”,如果想要把某个隐藏功能移出文件,则需要追加“-参数”。

5.4.2 lsattr 命令

lsattr 命令用于显示文件的隐藏权限,格式为“lsattr [参数] 文件”。

5.5 文件访问控制列表

前文讲解的一般权限、特殊权限、隐藏权限其实有一个共性—权限是针对某一类用户设置的。如果希望对某个指定的用户进行单独的权限控制,就需要用到文件的访问控制列表(ACL)了。通俗来讲,基于普通文件或目录设置 ACL 其实就是针对指定的用户或用户组设置文件或目录的操作权限。另外,如果针对某个目录设置了 ACL,则目录中的文件会继承其 ACL;若针对文件设置了 ACL,则文件不再继承其所在目录的 ACL。

5.5.1 setfacl 命令

setfacl 命令用于管理文件的 ACL 规则,格式为“setfacl [参数] 文件名称”。

常用的 ls 命令是看不到 ACL 表信息的,但是却可以看到文件的权限最后一个点(.)变成了加号(+) ,这就意味着该文件已经设置了 ACL 了。

image

5.5.2 getfacl 命令

getfacl 命令用于显示文件上设置的 ACL 信息,格式为“getfacl 文件名称”。

5.6 su 命令与 sudo 服务

su 命令可以解决切换用户身份的需求,使得当前用户在不退出登录的情况下,顺畅地切换到其他用户

su 命令与用户名之间可以有一个减号(-),这意味着完全切换到新的用户,即把环境变量信息也变更为新用户的相应信息,而不是保留原始的信息。强烈建议在切换用户身份时添加这个减号(-)。

使用 sudo 命令把特定命令的执行权限赋予给指定用户

sudo 命令用于给普通用户提供额外的权限来完成原本 root 管理员才能完成的任务,格式为“sudo [参数] 命令名称”。

image

sudo 命令具有如下功能:

  • 限制用户执行指定的命令:
  • 记录用户执行的每一条命令;
  • 配置文件( /etc/sudoers)提供集中的用户管理、权限与主机等参数;
  • 验证密码的后 5 分钟内(默认值)无须再让用户再次验证密码。

When you configure the profile to use the sudo command visudo command, the same method used in the method and Vim editor of its operation, so after their completion remember to save and exit line mode. Sudo command in the configuration file, the following format:

image

Guess you like

Origin www.cnblogs.com/huangwenjie/p/12013102.html