前言
之前在现场部署的时候, 某些用户没有root
用户的权限. 由此学习了一波Linux权限
相关的知识. 最近有机会, 将其总结如下:
- 用户与用户组
-
chrown
/chmod
/ 命令 -
-R
/755
/777
的含义 - 注意事项
正文
用户与用户组
用户与用户组是多对多
的关系. .一个用户可以在多个用户组内, 一个用户组也可以包含多个用户. 我们通常可以通过/etc/passwd
文件查看相关的信息.
localhost:~ Sean$ cat /etc/passwd
##
# User Database
#
# Note that this file is consulted directly only when the system is running
# in single-user mode. At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
用户组的相关信息一般存储在/etc/group
文件内:
localhost:~ Sean$ cat /etc/group
##
# Group Database
#
# Note that this file is consulted directly only when the system is running
# in single-user mode. At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
nobody:*:-2:
nogroup:*:-1:
wheel:*:0:root
daemon:*:1:root
其他相关操作: Linux 用户和用户组管理
chmod
/chown
我们对于文件的赋权通常包括三个部分<所有者权限><同组内用户权限><其他用户权限>
. 对于权限, 我们一般有两种表示方式: 字符形式 与 数字形式.
字符形式, 主要是方便读. 数字形式主要是方便授权.
我们在使用命令授权的时候经常使用chmod 777 /tmp/hello/
而不会写成 chmod u+x /tmp/hello/
. 主要是因为使用方便. 但是读取的时候, 还是使用字符的形式, 更加方便. 如:
localhost:Software Sean$ ls -lrt
total 238016
drwxr-xr-x@ 3 Sean staff 96 Feb 13 2012 Yummy
drwxr-xr-x 4 Sean staff 128 Oct 8 2016 Axure
其中:
- 第一位
d
表示为文件目录; - 后面的
rwx
中, 对于文件拥有者,r
表示可读,w
表示可写,x
表示可以执行(即./
执行脚本的方式). - 再后面的
r-x
表示,对于文件所在的组,r
表示可读,-
表示无写权限,x
表示可执行; - 再后面的
r-x
表示, 其他用户的权限, 同上. - 注:
r(读权限):4 / w(写权限):2 / x(执行权限):1
. 所以rwx
可就是常说的权限7
;r-x
是权限5.
我们在使用chmod
命令时候, 可以使用chmod 755 /xx/
赋予<所有者读写执行><同组读执行><其他用户读执行>
这样的权限.
linux如何修改文件或目录的权限(chmod)
linux drwxr-xr-x 什么意思
此外, 我们有时还会遇到chmod u+x /xx/
表示增加权限.
文件权限中 chmod、u+x、u、r、w、x分别代表什么
linux 权限 chmod u+x
其他注意实现
在创建文件时候, 有时系统默认设置权限的. 这时有一个umask
, 可以查看当前的默认权限.
localhost:Software Sean$ umask
0022
当前用户权限掩码为0022
. 所以, 用户创建文件夹的权限为777-022
, 也就是755
, 即rwx r-x r-x
. 我们可以创建一个文件夹查看下:
localhost:Software Sean$ mkdir tm
localhost:Software Sean$ ls -lrt | grep "tm"
drwxr-xr-x 3 Sean staff 96 Apr 8 16:13 tm
另外, 默认创建文件的权限为666
. 此时创建文件的默认权限为666-022
, 可就是644
, 即 rw- r-- r--
. 默认的文件是没有执行权限的. 我们可以同样创建一个文件来查看一下:
localhost:tm Sean$ touch
localhost:tm Sean$ ls -lrt
total 0
-rw-r--r-- 1 Sean staff 0 Apr 8 16:13
Linux里新建文件/目录的默认权限
Reference
[1]. linux如何修改文件或目录的权限(chmod)
[2]. linux drwxr-xr-x 什么意思
[3]. 文件权限中 chmod、u+x、u、r、w、x分别代表什么
[4]. linux 权限 chmod u+x
[5]. Linux里新建文件/目录的默认权限