Home > System Tutorial > Linux > body text

linux files are authorized to users. Linux security model: wide limit is tight, and time is enough to stagnate.

WBOY
Release: 2024-06-22 10:32:52
Original
492 people have browsed it

linux 文件授权给用户 Linux 安全性模型:宽为限紧用功,功夫到滞塞通

Overview of the Linux security model through wide limits and tight efforts to achieve stagnation

Linux uses User and Group to control users' access rights to files. Users created in the Linux system can log in to Linux using their account and password. The Linux system treats everything as a file. Each file has an owner, but the owner belongs to a certain Group.

这里写图片描述

User

Each user in the Linux system has a unique UserID. User information is stored in /etc/passwd. The format of this file is as follows:

root:x:0:0:root:/root:/bin/bash
Copy after login

The corresponding meaning of each digit is:

用户名:密码:用户ID:组ID:用户全名:主目录:登录shell
Copy after login

In the old system, the user's password is directly stored in the second digit. Because all users of the /etc/passwd file have read permissions, for security reasons, later systems took out the passwords separately and stored them in the /etc/shadow file, and the second digit of the /etc/passwd file was only used for Store whether the user needs a password. If the user has a password, this bit is marked with 'x'. If it is "!" or empty, it means that the user cannot log in with a password

Normally, only users with the HOME directory and the specified shell can log in to the system. Users are strictly prohibited from reading, writing or executing other User's files without authorization. The Linux root user has supreme authority and can unconditionally access, modify and delete all files in the system. Generally, do not use root to log in and operate the system.

There are two commands to add new users to the Linux operating system, namely useradd and adduser. Under root authority, useradd only creates a username, such as (useradd+username). It does not create a folder with the same name in the /home directory, nor does it create a password. Therefore, you cannot log in to the system with this user. In order to prevent this situation, you can use the (useradd-m+username) method to create it. It will create a folder with the same name in the /home directory, and then use (passwd+username) to set a password for the specified username. Its parameter options are as follows:

-c comment 指定一段注释性描述。
-d 目录 指定用户主目录,如果此目录不存在,则同时使用-m选项,可以创建主目录。
-g 用户组 指定用户所属的用户组。
-G 用户组,用户组 指定用户所属的附加组。
-s Shell文件 指定用户的登录Shell-u 用户号 指定用户的用户号,如果同时有-o选项,则可以重复使用其他用户的标识号。

-m, --create-home

如果用户不存在,则创建用户主目录。骨架目录中的文件和目录(可以使用 -k 选项指定),将会复制到主目录。默认上,如果没有指定此选项并且CREATE_HOME 没有启用,不会创建主目录。
Copy after login

You can directly use adduser to create a new user (adduser+username), so that a folder with the same name will be manually created in the /home directory without providing any parameters.

The userdel command is used to delete users. The commonly used option is -r, which is used to delete the user's home directory together.

The usermod command is used to change account information. Commonly used options include -c, -d, -m, -g, -G, -s, -u and -o and other common Linux systems. The meaning of this option is the same as in the useradd command. As with the options, you can specify new resource values ​​for the user.

Tools or commands for managing users:

useradd注:添加用户
adduser注:添加用户
passwd 注:为用户设置密码
usermod注:修改用户命令,可以通过usermod 来修改登录名、用户的家目录等等;
pwcov注:同步用户从/etc/passwd 到/etc/shadow
pwck 注:pwck是校验用户配置文件/etc/passwd 和/etc/shadow 文件内容是否合法或完整;
pwunconv 注:是pwcov 的立逆向操作,是从/etc/shadow和 /etc/passwd 创建/etc/passwd ,然后会删除 /etc/shadow 文件;
finger 注:查看用户信息工具
id 注:查看用户的UID、GID及所归属的用户组
chfn 注:更改用户信息工具
su 注:用户切换工具
sudo 注:sudo 是通过另一个用户来执行命令(execute a command as another user),su 是用来切换用户,然后通过切换到的用户来完成相应的任务,但sudo 能后面直接执行命令,比如sudo 不需要root 密码就可以执行root 赋与的执行只有root才能执行相应的命令;但得通过visudo 来编辑/etc/sudoers来实现;
visudo 注:visodo 是编辑 /etc/sudoers 的命令;也可以不用这个命令,直接用vi 来编辑 /etc/sudoers 的效果是一样的;
sudoedit 注:和sudo 功能差不多;
Copy after login

linux 文件授权给用户_授权linux文件执行权限_linux授予用户文件权限

User Group Overview

Linux系统的每位User都属于一个Group,具有惟一的标示符gid。Group信息储存于/etc/group中linux 文件授权给用户,且可以为group创建密码,密码储存在/etc/gshadow文件中,并且通常情况下不须要为用户组创建密码。系统会为每位User关联一个和User同名的Group,每位User起码存在于自己同名的Group中,同时User也可以加入其他的Group。在同一个Group中的成员可以共享其他成员的文件。降低一个新的用户组使用groupadd命令。其格式如下:

用法:groupadd [选项]
Copy after login

可以使用的选项有:

-f, --force 如果组已经存在则成功退出并且如果 GID 已经存在则取消 -g
-g, --gid GID 为新组使用 GID
-h, --help显示此帮助信息并推出
-K, --key KEY=VALUE 不使用 /etc/login.defs 中的默认值
-o, --non-unique允许创建有重复 GID 的组
-p, --password PASSWORD 为新组使用此加密过的密码
-r, --system创建一个系统账户
-R, --root CHROOT_DIR chroot 到的目录
Copy after login

假如要删掉一个已有的用户组,使用groupdel命令,其格式为:groupdel用户组。更改用户组的属性使用groupmod命令,其句型为:groupmod选项用户组

假如须要将一个用户添加到某个用户组中,可以采用如下命令:

usermod -a -G 用户组 用户名
Copy after login

参数-a代表append,也就是将用户添加到新用户组中而毋须离开原有的其他用户组,不过须要与-G选项配合使用,-G表示指定用户组。假如仅仅只改变一个用户原有的用户组,则采用如下命令:

useradd -g 用户组
Copy after login

管理用户组(group)的工具或命令:

groupadd注:添加用户组;
groupdel注:删除用户组;
groupmod注:修改用户组信息
groups注:显示用户所属的用户组
grpck
grpconv 注:通过/etc/group和/etc/gshadow 的文件内容来同步或创建/etc/gshadow ,如果/etc/gshadow 不存在则创建;
grpunconv 注:通过/etc/group 和/etc/gshadow 文件内容来同步或创建/etc/group ,然后删除gshadow文件;
Copy after login

Linux文件和目录权限剖析1、三种基本权限

(1)r(read)读

针对目录,有读(r)权限就代表能对此目录有列表功能,就是可以执行ls命令进行查看,另外还有cp的功能。针对文件,有读(r)权限就代表能对此文件有阅读功能,可以通过cat等命令查看文件内容。

linux授予用户文件权限_linux 文件授权给用户_授权linux文件执行权限

(2)w(write)写

针对目录,有写(w)权限就代表着在此目录下创建文件和目录,可以通过touch,mkdir等命令创建文件和目录,另外还可以删掉此目录下的文件。针对文件,有写(w)权限就代表着对此文件可以写入新的内容和更改文件内容。

(3)x(execute)执行

针对目录,有执行(x)权限就代表能步入此目录,借助cd等命令步入此目录针对文件,有执行(x)权限就代表可以执行此文件。

在Linux下每一文件或目录的访问权限都有三组,每组用三位表示linux 文件授权给用户,分别为文件属主的读、写和执行权限;和属主同组的用户的读、写和执行权限;系统中其他用户的读、写和执行权限。当用ls-l命令显示文件或目录的周详信息时,最左侧的一列为文件的访问权限。最右侧的一位为文件的类型,比如“-”表示普通文件,“d”表示目录等。ls-l所列举的信息各栏含意如下:

第一个栏位,表示文件的属性。
第二个栏位,表示文件个数,即硬链接数。
第三个栏位,表示该文件或目录的拥有者。
第四个栏位,表示所属的组(group)。
第五栏位,表示文件大小。
第六个栏位,表示创建日期。
第七个栏位,表示文件名。
Copy after login

2、特殊位引起权限变化通常有以下两种

(1)特权位(s)

setuid s位在前三位
Copy after login

setgid s位在中间三位
Copy after login

特权位只针对文件有效,而且只能添加在权限位的前三位和中间三位;一个可执行文件拥有s位而且在前三位时,当别的用户来执行此文件,使用的权限是此可执行文件属主权限;假如一个可执行文件拥有s位而且在中间三位时,当他人的用户来执行此文件,使用的权限是此可执行文件属组的权限

(2)粘帖位(t)

当一个目录共享给其他用户使用而且用户可以上传文件和删掉文件,而且只能删掉自己的文件,这么就必须用到粘帖位(t),非常用在/tmp目录。只针对目录有效。有t位的目录,任何用户在有权限的情况下是可以创建文件和目录,即使是有权限删掉他人的文件或目录也不能删掉,同时相互也不能强制保存更改,自己只能删掉自己创建的目录,用于一些共享上传的文件服务器场合

注:s位和t位都是占用x位,这么是否有x位,主要是看s或t的大小写来判断:小写,表示没有执行权限x位;大写,表示有执行权限x位

3、隐藏属性权限

linux不仅9个权限外,还有些隐藏属性,使用lsattr和chattr命令来查看和设置这种隐藏属性。

lsattr --listfile attributes on a Linux second extended file system
chattr --change file attributeson a Linux second extended file system
Copy after login

chattr命令句型格式:

chattr [-RVf] [-+=aAcCdDeijsStTu] [-v version] files
Copy after login

参数说明:

-R:递归处理所有的文件及子目录。
-V:详细显示修改内容,并打印输出。
-:失效属性。
+:激活属性。
= :指定属性。
Copy after login

属性:

 Ano atime update 不允许修改atime
 Dsynchoronous directory updates
 S synchronous updates 必须sync
 T top of directory hierarchy
 a append only只允许append
 c compressed自动压缩,读取时自动解压缩,哇!好高级!
 d no dumpdump时,具有d属性的文件不加入dump
 e extent format
 i immuttbale 可厉害了,让一个文件不能删除,改名,增加软硬链接,无法写入
 j data journalling ext3时会将写入记录 journal
 s secure deletion 可以安全删除
 t no tail-merging
 uundeletable 与s相反,删除时数据还会存在磁盘中
Copy after login

4、chmod命令

chmod命令是十分重要的,用于改动文件或目录的访问权限。用户用他控制文件或目录的访问权限。该命令有两种用法。一种是包含字母和操作符表达式的文字设定法;另一种是包含数字的数字设定法。

文字设定法

chmod [who] [+- =] [mode] file
Copy after login

命令中各选项的含意为:

操作对象who而且下列字母中的任一个或她们的组合:

u 表示“用户(user)”,即文件或目录的所有者。
g 表示“同组(group)用户”,即和文件属主有相同组ID的所有用户。
o 表示“其他(others)用户”。
a 表示“所有(all)用户”。他是系统默认值。
Copy after login

linux授予用户文件权限_linux 文件授权给用户_授权linux文件执行权限

操作符号可以是:

+ 添加某个权限。
- 取消某个权限。
= 赋予给定权限并取消其他所有权限(如果有的话)。
Copy after login

设置mode所表示的权限可用下列字母的任意组合:

r 可读。
w 可写。
x 可执行。
Copy after login

x只有目标文件对个别用户是可执行的或该目标文件是目录时才追加x属性。

s在文件执行时把进程的属主或组ID置为该文件的文件属主。形式“u+s”设置文件的用户ID位,“g+s”设置组ID位。

t 保存程式的文本到交换设备上。
u 和文件属主拥有相同的权限。
g 和和文件属主同组的用户拥有相同的权限。
o 和其他用户拥有相同的权限。
Copy after login

文件可以是以空格分开的要改动权限的文件列表,支持转义。在一个命令行中可给出多个权限形式,其间用冒号隔开。诸如:

chmod g+r,o+r example
Copy after login

这儿的意思是使同组和其他用户对文件example有读权限。

数字设定法

数字表示的属性的含意:0表示没有权限,1表示可执行权限,2表示可写权限,4表示可读权限。通过将三个值相减来得到要设置的权限。所以数字属性的格式应为3个从0到7的八补码数,其次序是(u)(g)(o)。诸如,假如想让某个文件的属主有“读/写”二种权限,须要把4(可读)+2(可写)=6(读/写)。数字设定法的通常方式为:

chmod [mode] file
Copy after login

比如,将一个文件的权限设置为属主可读可写,同组用户可读可写,其他用户可读,则命令为:

chmod 664 file s
Copy after login

linux 文件授权给用户_授权linux文件执行权限_linux授予用户文件权限

5、chgrp和chown命令

chgrp命令用于改动文件或目录所属的组。句型格式如下:

chgrp [-R] group file
Copy after login

该命令改动指定指定文件所属的用户组。其中group能是用户组IDred hat linux 下载,也能是/etc/group文件中用户组的组名。文件名是以空格分开的要改动属组的文件列表,支持转义。假如用户不是该文件的属主或终极用户,则不能改动该文件的组。-R参数递归式地改动指定目录及其下的所有子目录和文件的属组。诸如改动/opt/local/book/及其子目录下的所有文件的属组为book:

chgrp –R book /opt/local /book
Copy after login

chown命令用于修改某个文件或目录的属主和属组。这个命令也十分常用。诸如root用户把自己的一个文件拷贝给用户huoty,为了让用户huoty才能存取这个文件,root用户应当把这个文件的属主设为huoty,否则,用户huoty难以存取这个文件。句型格式如下:

chown [选项] 用户或组 文件
Copy after login

chown将指定文件的拥有者改为指定的用户或组。用户能是用户名或用户ID。组能是组名或组ID。文件是以空格分开的要改动权限的文件列表,支持转义。该命令的各选项涵义如下:

--c 显示更改的部分的信息
-f 忽略错误信息
-h 修复符号链接
-R 处理指定目录以及其子目录下的所有文件
-v 显示详细的处理信息
-deference 作用于符号链接的指向,而不是链接文件本身
Copy after login

使用例示:

改变文件的所有者:

chown www-data: test
Copy after login

改变文件的所属组:

chown :www-data test
Copy after login

同时改变文件的所有者和所属组:

chown huoty:huoty test
Copy after login

站在巨人的右臂上

The above is the detailed content of linux files are authorized to users. Linux security model: wide limit is tight, and time is enough to stagnate.. For more information, please follow other related articles on the PHP Chinese website!

source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!