mycpen

Mycpen

记录学习历程与受益知识
github
telegram
bilibili

16_Linux基础-文件&目录权限及归属

一。文件 / 目录的权限#

文件 / 目录的权限

访问权限

  • 可读(read):允许查看文件内容、显示目录列表 r
  • 可写(write):允许修改文件内容,允许在目录中新建、移动、删除文件或子目录 w
  • 可执行(execute):允许运行程序、切换目录 x

归属(所有权)

  • 文件拥有者(owner):拥有该文件或目录的用户帐号 # 注:属主
  • 属组(group):拥有该文件或目录的组帐号 # 注:属组
  • 其它人(others):除了属主和属组的其他人 # 注:其他

#注:区分是对于文件而言还是对于目录而言

ls -al 查看当前目录下面文件或者文件夹的详细信息,包括文件的权限
ls -ld 查看当前目录
示例1:查看文件权限 ls -al
# 注:第三列属主 第四列属组
---------------------------------------------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# ls -al				#注:查看文件权限
总用量 668
-rw-r--r--.  1 root root    211 9月  18 10:31 '!'
dr-xr-x---. 21 root root  12288 11月  6 15:16  .
[root@sanchuang-linux ~]# ls -al zuoye.sh 		# 注:查看单个文件权限
-rw-r--r--. 1 root root 1212 10月 29 20:20 zuoye.sh
[root@sanchuang-linux ~]# ls -al /home			# 注:查看/home目录下所有文件的权限
总用量 18348
drwxr-xr-x. 43 root         root            4096 11月  6 15:36 .
dr-xr-xr-x. 38 root         root            4096 11月  5 15:12 ..
[root@sanchuang-linux ~]# ls -al *.sh			# 注:查看文件权限
-rw-r--r--. 1 root root    0 9月  25 09:35 backup_log.sh
-rw-r--r--. 1 root root   41 10月 28 20:27 file_test.sh

示例2:查看目录权限 ls -ld /home
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# ls -ld /home			# 注:查看/home 目录的权限
drwxr-xr-x. 43 root root 4096 11月  6 15:36 /home

二。文件 / 目录的权限#

[root@localhost ~]# ls -ld first.py 
-rw-r--r--. 1 root root 1097 10月 13 10:20 first.py
文件类型 访问权限  所有者  属组

image-20220816204743513

示例:drwxr-xr-x
--------------------------------------------------------------------------------------------
第一列:表示文件类型与权限
	第一个字符
        -	普通文件 file
        d	表示目录
        s	表示socket文件			# 注:socket(套接字) 进程之间通信的一种方式。
        p	管道文件 pipe
        c	字符设备文件	tty
        b	块设备文件	 磁盘(disk)	# 注:跟硬件相关的块设备文件一般放在 /dev下面
        l	链接文件				# 注:/usr/bin/目录下 有大量 链接文件

  #权限 r 可读  w 可写  x 可执行
  ·第2-第4个字符
  	  表示属主的权限
  ·第5-第7个字符
  	  表示属组的权限
  ·后三个
	  表示其他人的权限
--------------------------------------------------------------------------------------------
敲的所有命令都是在PATH环境变量路径下查找

创建链接文件
# 注:ln -s 源文件 目标文件		symbolic adj. 象征的;符号的;使用符号的
[root@sanchuang-linux bin]# ln -s python3.6 python			# 注:-s创建软链接
[root@sanchuang-linux bin]# ln -s python3.6 /root/python	# 注:可以在绝对路径下创建链接文件
#注:链接文件/root/python 放在了/root目录下
which查看命令的绝对路径
[root@sanchuang-linux bin]# which mkdir
/usr/bin/mkdir
[root@sanchuang-linux bin]# mv /usr/bin/mkdir /usr/bin/mkdirbak	# 注:修改名字后 命令找不到了
[root@sanchuang-linux bin]# mkdir
-bash: mkdir: 未找到命令
[root@sanchuang-linux bin]# mkdirbak /tmp/mkdir
[root@sanchuang-linux bin]# mv /usr/bin/mkdirbak /usr/bin/mkdir	# 注:改回来

/usr/bin/下有大量链接文件
[root@sanchuang-linux tmp]# cd /usr/bin/						# 注:/usr/bin/下有大量链接文件
[root@sanchuang-linux bin]# ls -al python2						# 注:链接文件
lrwxrwxrwx. 1 root root 9 6月   5 11:38 python2 -> python2.7
[root@sanchuang-linux bin]# ls -al python3						# 注:链接文件
lrwxrwxrwx. 1 root root 25 9月  18 15:47 python3 -> /etc/alternatives/python3

三. socket(套接字)#

#注:socket(套接字) 进程之间通信的一种方式,通常用于不同主机不同进程之间的通信方式(网络编程)

ftp 有 vsftpd 的服务,vsftpd 服务端服务起来了开启 21 号端口。ftp 客户端要想连接这个服务,ftp 客户端需要开启一个随机端口去连接 21 号端口,进行网络传输。通过网络指定某种特定协议,进行网络连接。

开启一个 vsftpd,会产生一个 socket 文件

开放了端口基本上都会有一个 socket 文件

[root@localhost mail]# cd /data/mysql
[root@localhost mysql]# ls -al
srwxrwxrwx  1 mysql mysql        0 10月  7 12:04 mysql.sock		# 注:s socket文件 (粉色)

四. Linux 内核五大子系统#

Linux 内核五大子系统:

  • 1、文件系统
  • 2、内存管理
  • 3、进程通信
  • 4、进程调度
  • 5、网络接口

五。进程#

进程是系统进行资源分配的基本单位,不同进程之间资源是互相隔离的

进程之间通信(常见的 5 种):

  1. 管道
  2. 信号 (kill -9)
  3. 共享内存
  4. socket 套接字
  5. 消息队列
示例1:管道				#注:管道是进程之间通信的一种方式
示例
[root@sanchuang-linux ~]# cat /etc/passwd |grep sanchuang	# 注:通过管道把前进程的输出赋给后进程的输入
sanchuang:x:1005:1005::/home/sanchuang:/bin/bash
#注:适用于相同主机不同进程
--------------------------------------------------------------------------------------------
示例2:信号				
示例:kill -9 pid
[root@sanchuang-linux ~]# kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1………………
[root@sanchuang-linux ~]# ps -ef |grep nginx
root        5868    5691  0 16:29 pts/0    00:00:00 grep --color=auto nginx
[root@sanchuang-linux ~]# kill -9 2868					# 注:发送9这个信号给1209进程
--------------------------------------------------------------------------------------------
示例3:共享内存
#注:A、B之间创建了一片新的内存空间,可以一起去拿
#注:适用于相同主机不同进程
--------------------------------------------------------------------------------------------
示例4:socket套接字
#注:用于不同主机之间不同进程  网络通信
--------------------------------------------------------------------------------------------
示例5:消息队列
#注:相当于排队的 1,2,3,4,5,6
#注:AB共享的内存空间里,A产生的B去获取。A产生1,B取出来,1从A里出去

六. / 根目录下#

/etc 存放配置文件
/dev 存放设备文件 # 注:硬件相关的 磁盘、网卡、socket
/root # 注:root 用户家目录
/home # 注:存放普通用户的家目录文件

[root@sanchuang-linux ~]# cd /dev
[root@sanchuang-linux dev]# ls -al
crw-rw-rw-   1 root tty       5,   0 11月  6 11:32 tty		# 注:字符设备文件	tty
crw--w----   1 root tty       4,   0 11月  5 15:12 tty0
crw--w----   1 root tty       4,   1 11月  6 14:28 tty1
brw-rw----   1 root disk      8,   0 11月  5 15:12 sda		# 注:块设备文件	磁盘

七。可执行权限#

第一列第二到第十个字符
  #权限 r 可取  w 可写  x 可执行
  第2-第4个字符
  	  表示属主的权限
  第5-第7个字符
  	  表示属组的权限
  后三个
	  表示其他人的权限
示例
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# ls -al men_test.sh 
-rw-r--r--. 1 root root 0 10月 30 16:26 men_test.sh
[root@sanchuang-linux ~]# bash men_test.sh 	# 注:使用bash解释器去执行(生成一个新的bash)
[root@sanchuang-linux ~]# sh men_test.sh 	# 注:使用sh解释器去执行(生成一个新的sh)
[root@sanchuang-linux ~]# . men_test.sh		# 注:使用当前bash去执行,会继承当前环境变量
#注:相当于当前bash,当前环境
[root@sanchuang-linux ~]# ./men_test.sh 	# 注:权限不够,本身没有可执行权限
-bash: ./men_test.sh: 权限不够				 # 注:有可执行权限的文件 可以直接./men_test.sh执行

八. Shell 文件执行方式#

Shell 文件执行方式(3 种)
#注:shell 里面可执行文件后缀没有要求 不一定非要.sh 结尾

[root@sanchuang-linux ~]# vim test_aa.sh
echo "testaa..."
echo $a
echo $b
[root@sanchuang-linux ~]# a=1
[root@sanchuang-linux ~]# b=2
[root@sanchuang-linux ~]# . test_aa.sh 	# 注:使用当前bash去执行test_aa.sh,会继承当前 shell 的 环境变量
testaa...								# 注:用.会继承当前bash里的一切变量
1
2
[root@sanchuang-linux ~]# bash test_aa.sh 	# 注:创建一个新的bash环境去执行命令
testaa...									# 注:新bash环境中没有定义a、b变量
											# 注:没有输出父bash的a、b
											# 注:没有获取到a、b
[root@sanchuang-linux ~]# sh test_aa.sh 		# 注:创建一个新的sh环境去执行命令
testaa...
											# 注:没有输出

[root@sanchuang-linux ~]# ./test_aa.sh 		# 注:执行文件本身,看文件本身有没有执行权限
-bash: ./test_aa.sh: 权限不够				 # 注:直接执行当前目录下的这个文件 需要可执行权限
# 注:其他的方式不一定需要可执行权限

总结
. test_aa.sh			不需要可执行权限	当前bash进程执行
bash test_aa.sh			不需要可执行权限	新的bash进程运行
sh test_aa.sh			不需要可执行权限	新的bash进程运行
./test_aa.sh			需要可执行权限	新的bash进程运行

九. chmod 命令#

chmod 命令 # 注:修改文件的访问权限

格式 1chmod [ugoa] [+-=] [rwx] 文件或目录...

  • u、g、o、a 分别表示
    u 属主、g 属组、o 其他用户、a 所有用户

  • +、-、= 分别表示
    + 增加、- 去除、= 设置权限

  • rwx
    对应的权限字符

常用命令选项

  • -R:递归修改指定目录下所有文件、子目录的权限
示例
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# chmod u+x men_test.sh 		# 注:对属主+可执行权限
[root@sanchuang-linux ~]# ls -al men_test.sh 
-rwxr--r--. 1 root root 0 10月 30 16:26 men_test.sh		# 注:可执行文件 都是绿的
#注:可执行文件	都是绿的
#注:socket文件	都是粉的
[root@sanchuang-linux ~]# ./men_test.sh 				# 注:可以直接执行了
--------------------------------------------------------------------------------------------
[root@sanchuang-linux lianxi]# chmod a-x sc -R		# 注:-R递归,把该目录和该目录下的所有子文件都去除可执行权限
[root@sanchuang-linux lianxi]# chmod a+x sc -R		# 注:所有用户增加可执行权限
============================================================================================
#注:根目录只有root用户能用
[root@sanchuang-linux ~]# ls -ld /
dr-xr-xr-x. 38 root root 4096 11月  5 15:12 /
============================================================================================
[root@mysql-binary ~]# chmod u+x group_member.sh 
[root@mysql-binary ~]# ./group_member.sh sanchuang5		# 注:添加了可执行权限后可以 ./运行
sanchuang5,sanchuang11,sanchuang13,sanchuang10,sanchuang12

十. chmod 命令#

chmod 命令 # 注:修改文件的访问权限

格式 2chmod nnn 文件或目录... # 注:nnn 表示 3 位八进制数

权限项 读 写 执行 读 写 执行 读 写 执行

字符表示 r w x r w x r w x

数字表示 4 2 1 4 2 1 4 2 1

权限分配 文件所有者 文件所属组 其他用户

image-20220816210259355

常用命令选项

  • -R:递归修改指定目录下所有文件、子目录的权限

#注:root 用户基本上无论有没有权限,它都可以使用

示例
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# chmod 777 sc 				# 注:都有读写执行的权限
[root@sanchuang-linux ~]# chmod 700 sc 				# 注:属主有读写执行的权限
--------------------------------------------------------------------------------------------
[root@sanchuang-linux home]# ls -ld sanchuang		# 注:查看目录权限 ls -ld
drwx------. 4 sanchuang sanchuang 122 11月  6 17:10 sanchuang
[root@sanchuang-linux home]# chmod 777 /home/sanchuang		# 注:都有读写执行的权限
[root@sanchuang-linux home]# ls -ld sanchuang
drwxrwxrwx. 4 sanchuang sanchuang 122 11月  6 17:10 sanchuang
[root@sanchuang-linux home]# chmod 777 /home/sanchuang -R	# 注:-R:递归修改指定目录下所有文件、子目录的权限
[root@sanchuang-linux home]# ls -ld sanchuang
drwxrwxrwx. 4 sanchuang sanchuang 122 11月  6 17:10 sanchuang
#注:用户的家目录下(如/home/sanchuang)授予所有对象读写执行权限,会造成普通用户sanchuang无法进行远程登录

示例:root用户基本上无论有没有权限,它都可以使用
[root@sanchuang-linux ~]# ls -ld /etc/shadow
---------- 1 root root 4201 11月  7 12:04 /etc/shadow
[root@sanchuang-linux ~]# vim /etc/shadow

十一。一般情况下普通用户只能在家目录下或者 /tmp 目录下创建文件或者文件夹#

一般情况下普通用户只能在家目录下或者 /tmp 目录下创建文件或者文件夹

#注:/tmp 目录是一个临时目录,一般存放临时性的东西(如:程序运行中需要记录的一些东西,程序结束后把 /tmp 目录下的东西删掉)

[root@sanchuang-linux ~]# su - sanchuang
上一次登录:五 11月  6 17:09:41 CST 2020pts/2
[sanchuang@sanchuang-linux ~]$ ls -ld /
dr-xr-xr-x. 38 root root 4096 11月  5 15:12 /
[sanchuang@sanchuang-linux ~]$ ls -ld /etc
drwxr-xr-x. 90 root root 8192 11月  7 15:00 /etc
[sanchuang@sanchuang-linux ~]$ ls -ld ./				# 注:家目录下 创建文件或者文件夹
drwxrwxrwx. 4 sanchuang sanchuang 122 11月  6 17:10 ./
[sanchuang@sanchuang-linux ~]$ ls -ld /tmp				# 注:/tmp目录下 创建文件或者文件夹
drwxrwxrwt. 12 root root 264 11月  7 10:36 /tmp

#注:chmod  自己是属主的时候,才可以修改权限

十二. chmod#

chmod # 注:修改文件的归属权限

chmod 使用,必须是文件的属主,才能改动文件的读写执行权限(root 除外)

[root@sanchuang-linux chenpeng]# su - sanchuang11		# 注:加 - 会自动到家目录下
上一次登录:六 11月  7 15:10:51 CST 2020pts/0
[sanchuang11@sanchuang-linux ~]$ exit
注销
[root@sanchuang-linux chenpeng]# su sanchuang11			# 注:不加 - 会在当前路径下
[sanchuang11@sanchuang-linux chenpeng]$ 

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.