Linux基本指令(下)

文章目录

      • [2.22 tar 指令](#2.22 tar 指令)
      • [2.23 bc 指令](#2.23 bc 指令)
      • [2.24 uname 指令](#2.24 uname 指令)
      • [2.25 重要的几个热键](#2.25 重要的几个热键)
      • [2.26 关机](#2.26 关机)
    • [3. shell命令以及运行原理](#3. shell命令以及运行原理)
    • [4. Linux权限的概念](#4. Linux权限的概念)
    • [5. Linux权限管理](#5. Linux权限管理)

2.22 tar 指令

参数:

  • -c :建立一个压缩文件的参数指令(create 的意思)
  • -x :解开一个压缩文件的参数指令
  • -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
  • -v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
  • -f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!
  • -C : 解压到指定目录
c 复制代码
[root@VM-4-3-centos 112]# ll
total 16
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
drwxr-xr-x 3 root root 4096 Oct  6 16:55 lesson4
drwxr-xr-x 2 root root 4096 Oct  6 17:46 otherdir
[root@VM-4-3-centos 112]# tar czf lesson4.tgz lesson4
[root@VM-4-3-centos 112]# ll
total 20
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
drwxr-xr-x 3 root root 4096 Oct  6 16:55 lesson4
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz
drwxr-xr-x 2 root root 4096 Oct  6 17:46 otherdir
[root@VM-4-3-centos 112]# tar cvzf lesson4.tgz lesson4
lesson4/
lesson4/dir/
lesson4/dir/dir1/
lesson4/dir/dir1/dir2/
lesson4/dir/dir1/dir2/dir3/
lesson4/file.txt
lesson4/new.txt
lesson4/bigfile.txt
[root@VM-4-3-centos 112]# ll
total 20
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
drwxr-xr-x 3 root root 4096 Oct  6 16:55 lesson4
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz
drwxr-xr-x 2 root root 4096 Oct  6 17:46 otherdir
[root@VM-4-3-centos 112]# rm lesson4 -rf
[root@VM-4-3-centos 112]# ll
total 16
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz
drwxr-xr-x 2 root root 4096 Oct  6 17:46 otherdir
[root@VM-4-3-centos 112]# tar xvzf lesson4.tgz 
lesson4/
lesson4/dir/
lesson4/dir/dir1/
lesson4/dir/dir1/dir2/
lesson4/dir/dir1/dir2/dir3/
lesson4/file.txt
lesson4/new.txt
lesson4/bigfile.txt
[root@VM-4-3-centos 112]# ll
total 20
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
drwxr-xr-x 3 root root 4096 Oct  6 16:55 lesson4
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz
drwxr-xr-x 2 root root 4096 Oct  6 17:46 otherdir
[root@VM-4-3-centos 112]# tree lesson4
lesson4
|-- bigfile.txt
|-- dir
|   `-- dir1
|       `-- dir2
|           `-- dir3
|-- file.txt
`-- new.txt

4 directories, 3 files
[root@VM-4-3-centos 112]# cat lesson4/bigfile.txt 
aaaaaaaaa
bbbbb
cccccc
ddddddddd
eeeeeeeeeee
ffffffffff
[root@VM-4-3-centos 112]# sz lesson4.tgz 
[root@VM-4-3-centos 112]# 
rz -E
rz waiting to receive.
[root@VM-4-3-centos 112]# ll
total 24
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
drwxr-xr-x 3 root root 4096 Oct  6 16:55 lesson4
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz.0
drwxr-xr-x 2 root root 4096 Oct  6 17:46 otherdir
[root@VM-4-3-centos 112]# rm lesson4.tgz.0 
rm: remove regular file 'lesson4.tgz.0'? y
[root@VM-4-3-centos 112]# ll
total 20
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
drwxr-xr-x 3 root root 4096 Oct  6 16:55 lesson4
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz
drwxr-xr-x 2 root root 4096 Oct  6 17:46 otherdir
[root@VM-4-3-centos 112]# tar xvzf lesson4.tgz -C otherdir/
lesson4/
lesson4/dir/
lesson4/dir/dir1/
lesson4/dir/dir1/dir2/
lesson4/dir/dir1/dir2/dir3/
lesson4/file.txt
lesson4/new.txt
lesson4/bigfile.txt
[root@VM-4-3-centos 112]# ll
total 20
drwxr-xr-x 3 root root 4096 Jan 30  2025 lesson2
drwxr-xr-x 3 root root 4096 Oct  5 15:45 lesson3
drwxr-xr-x 3 root root 4096 Oct  6 16:55 lesson4
-rw-r--r-- 1 root root  284 Oct  7 13:04 lesson4.tgz
drwxr-xr-x 3 root root 4096 Oct  7 13:10 otherdir
[root@VM-4-3-centos 112]# tree otherdir/
otherdir/
`-- lesson4
    |-- bigfile.txt
    |-- dir
    |   `-- dir1
    |       `-- dir2
    |           `-- dir3
    |-- file.txt
    `-- new.txt

5 directories, 3 files
[root@VM-4-3-centos 112]# cat otherdir/lesson4/bigfile.txt 
aaaaaaaaa
bbbbb
cccccc
ddddddddd
eeeeeeeeeee
ffffffffff

2.23 bc 指令

bc命令可以很方便的进行浮点运算

c 复制代码
[root@VM-4-3-centos 112]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+1
2
13*100
1300
10/0
Runtime error (func=(main), adr=6): Divide by zero
quit
[root@VM-4-3-centos 112]# echo "1+2+3+4+5+6" | bc
21

2.24 uname 指令

功能: uname用来获取电脑和操作系统的相关信息。

补充说明:uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息。

常用选项:

  • -a 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
c 复制代码
[root@VM-4-3-centos 112]# uname
Linux
[root@VM-4-3-centos 112]# hostname
VM-4-3-centos
[root@VM-4-3-centos 112]# uname -a
Linux VM-4-3-centos 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
[root@VM-4-3-centos 112]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@VM-4-3-centos 112]# uname -r
3.10.0-1160.119.1.el7.x86_64

2.25 重要的几个热键

  • Tab\]按键---具有『命令补全』和『档案补齐』的功能

  • Ctrl\]-c按键---让当前的程序『停掉』

2.26 关机

语法:shutdown [选项]

常见选项:

  • -h:将系统的服务停掉后,立即关机
  • -r:在将系统的服务停掉之后就重新启动
  • -t sec:-t 后面加秒数,亦即『过几秒后关机』的意思

3. shell命令以及运行原理

Linux严格意义上说的是一个操作系统,我们称之为"核心(kernel)" ,但我们一般用户,不能直接使用kernel。而是通过kernel的"外壳"程序,也就是所谓的shell,来与kernel沟通。如何理解?为什么不能直接使用kernel?

4. Linux权限的概念

Linux下有两种用户:超级用户(root)、普通用户。

  • 超级用户:可以再linux系统下做任何事情,不受限制
  • 普通用户:在linux下做有限的事情
  • 超级用户的命令提示符是"#",普通用户的命令提示符是"$"

命令:su [用户名]

功能:切换用户

c 复制代码
[gsm@VM-4-3-centos ~]$ whoami
gsm
[gsm@VM-4-3-centos ~]$ su -
Password: 
Last login: Thu Oct  9 15:40:56 CST 2025 on pts/0
[root@VM-4-3-centos ~]# whoami
root
[root@VM-4-3-centos ~]# logout
[gsm@VM-4-3-centos ~]$ whoami
gsm
[gsm@VM-4-3-centos ~]$ ls /home
gsm  lighthouse
[gsm@VM-4-3-centos ~]$ su lighthouse
Password: 
[gsm@VM-4-3-centos ~]$ su -
Password: 
Last login: Thu Oct  9 15:42:48 CST 2025 on pts/0
[root@VM-4-3-centos ~]# whoami
root
[root@VM-4-3-centos ~]# su lighthouse
[lighthouse@VM-4-3-centos root]$ whoami
lighthouse
[lighthouse@VM-4-3-centos root]$ exit

5. Linux权限管理

c 复制代码
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod u+x file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwxrw-r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod u-w file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-r-xrw-r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod u-rx file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
----rw-r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod u+rwx file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwxrw-r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod g-w file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwxr--r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod g+x file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwxr-xr-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod g+w,g-rx file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwx-w-r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod o-rwx file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwx-w---- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod o+rwx file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwx-w-rwx 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod u-rwx,g-rwx,o-rwx file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
---------- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod a+rwx file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwxrwxrwx 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod a-x file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rw-rw-rw- 1 gsm gsm 8 Oct  9 16:48 file-gsm
c 复制代码
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rw-rw-rw- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod 000 file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
---------- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod 777 file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rwxrwxrwx 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod 444 file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-r--r--r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod 644 file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rw-r--r-- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod 640 file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rw-r----- 1 gsm gsm 8 Oct  9 16:48 file-gsm
c 复制代码
[gsm@VM-4-3-centos lesson4]$ ll
total 4
-rw-r----- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ chmod u-rwx,g+rw file-gsm 
[gsm@VM-4-3-centos lesson4]$ ll
total 4
----rw---- 1 gsm gsm 8 Oct  9 16:48 file-gsm
[gsm@VM-4-3-centos lesson4]$ echo "hello world" > file-gsm 
-bash: file-gsm: Permission denied
c 复制代码
[root@VM-4-3-centos lesson5]# ll
total 4
---------- 1 root root 9 Oct  9 18:12 testfile.txt
[root@VM-4-3-centos lesson5]# chmod 644 testfile.txt 
[root@VM-4-3-centos lesson5]# ll
total 4
-rw-r--r-- 1 root root 9 Oct  9 18:12 testfile.txt
[root@VM-4-3-centos lesson5]# su gsm
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-r--r-- 1 root root 9 Oct  9 18:12 testfile.txt
[gsm@VM-4-3-centos lesson5]$ whoami
gsm
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-r--r-- 1 root root 9 Oct  9 18:12 testfile.txt
[gsm@VM-4-3-centos lesson5]$ cat testfile.txt 
bbbbbbbb
[gsm@VM-4-3-centos lesson5]$ echo "aaaaa" >> testfile.txt 
bash: testfile.txt: Permission denied
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-r--r-- 1 root root 9 Oct  9 18:12 testfile.txt
[gsm@VM-4-3-centos lesson5]$ exit
[root@VM-4-3-centos lesson5]# ll
total 4
-rw-r--r-- 1 root root 9 Oct  9 18:12 testfile.txt
[root@VM-4-3-centos lesson5]# chown gsm testfile.txt 
[root@VM-4-3-centos lesson5]# ll
total 4
-rw-r--r-- 1 gsm root 9 Oct  9 18:12 testfile.txt
[root@VM-4-3-centos lesson5]# su gsm
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-r--r-- 1 gsm root 9 Oct  9 18:12 testfile.txt
[gsm@VM-4-3-centos lesson5]$ cat testfile.txt 
bbbbbbbb
[gsm@VM-4-3-centos lesson5]$ echo "aaaaaaa" >> testfile.txt 
[gsm@VM-4-3-centos lesson5]$ cat testfile.txt 
bbbbbbbb
aaaaaaa
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-r--r-- 1 gsm root 17 Oct  9 18:28 testfile.txt
[gsm@VM-4-3-centos lesson5]$ exit
[root@VM-4-3-centos lesson5]# ll
total 4
-rw-r--r-- 1 gsm root 17 Oct  9 18:28 testfile.txt
[root@VM-4-3-centos lesson5]# chown root testfile.txt 
[root@VM-4-3-centos lesson5]# ll
total 4
-rw-r--r-- 1 root root 17 Oct  9 18:28 testfile.txt
[root@VM-4-3-centos lesson5]# chmod g+w testfile.txt 
[root@VM-4-3-centos lesson5]# ll
total 4
-rw-rw-r-- 1 root root 17 Oct  9 18:28 testfile.txt
[root@VM-4-3-centos lesson5]# chmod u-w testfile.txt 
[root@VM-4-3-centos lesson5]# ll
total 4
-r--rw-r-- 1 root root 17 Oct  9 18:28 testfile.txt
[root@VM-4-3-centos lesson5]# chgrp gsm testfile.txt 
[root@VM-4-3-centos lesson5]# ll
total 4
-r--rw-r-- 1 root gsm 17 Oct  9 18:28 testfile.txt
[root@VM-4-3-centos lesson5]# su gsm
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-r--rw-r-- 1 root gsm 17 Oct  9 18:28 testfile.txt
[gsm@VM-4-3-centos lesson5]$ whoami
gsm
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-r--rw-r-- 1 root gsm 17 Oct  9 18:28 testfile.txt
[gsm@VM-4-3-centos lesson5]$ cat testfile.txt 
bbbbbbbb
aaaaaaa
[gsm@VM-4-3-centos lesson5]$ echo "cccccccccc" >> testfile.txt 
[gsm@VM-4-3-centos lesson5]$ cat testfile.txt 
bbbbbbbb
aaaaaaa
cccccccccc
c 复制代码
[gsm@VM-4-3-centos lesson5]$ pwd
/home/gsm/112/lesson5
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ cd dir
[gsm@VM-4-3-centos dir]$ pwd
/home/gsm/112/lesson5/dir
[gsm@VM-4-3-centos dir]$ cd ..
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ chmod u-r dir
[gsm@VM-4-3-centos lesson5]$ ll
total 8
d-wxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ cd dir
[gsm@VM-4-3-centos dir]$ pwd
/home/gsm/112/lesson5/dir
[gsm@VM-4-3-centos dir]$ ls
ls: cannot open directory .: Permission denied
[gsm@VM-4-3-centos dir]$ touch hello.txt
[gsm@VM-4-3-centos dir]$ ll
ls: cannot open directory .: Permission denied
[gsm@VM-4-3-centos dir]$ cd ..
[gsm@VM-4-3-centos lesson5]$ pwd
/home/gsm/112/lesson5
[gsm@VM-4-3-centos lesson5]$ ll
total 8
d-wxrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ chmod u+r dir
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ chmod u-w dir
[gsm@VM-4-3-centos lesson5]$ ll
total 8
dr-xrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ cd dir
[gsm@VM-4-3-centos dir]$ pwd
/home/gsm/112/lesson5/dir
[gsm@VM-4-3-centos dir]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct  9 20:04 hello.txt
[gsm@VM-4-3-centos dir]$ touch world.txt
touch: cannot touch 'world.txt': Permission denied
[gsm@VM-4-3-centos dir]$ rm hello.txt 
rm: cannot remove 'hello.txt': Permission denied
[gsm@VM-4-3-centos dir]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct  9 20:04 hello.txt
[gsm@VM-4-3-centos dir]$ mv hello.txt world.txt
mv: cannot move 'hello.txt' to 'world.txt': Permission denied
[gsm@VM-4-3-centos dir]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct  9 20:04 hello.txt
[gsm@VM-4-3-centos dir]$ cat hello.txt 
[gsm@VM-4-3-centos dir]$ echo "aaaaaaa" > hello.txt 
[gsm@VM-4-3-centos dir]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 8 Oct  9 20:10 hello.txt
[gsm@VM-4-3-centos dir]$ cat hello.txt 
aaaaaaa
[gsm@VM-4-3-centos dir]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 8 Oct  9 20:10 hello.txt
[gsm@VM-4-3-centos dir]$ pwd
/home/gsm/112/lesson5/dir
[gsm@VM-4-3-centos dir]$ cd ..
[gsm@VM-4-3-centos lesson5]$ ll
total 8
dr-xrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ chmod u+w dir
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ chmod u-x dir
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drw-rwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ cd dir
-bash: cd: dir: Permission denied
[gsm@VM-4-3-centos lesson5]$ chmod u+x dir
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ cd dir
[gsm@VM-4-3-centos dir]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 8 Oct  9 20:10 hello.txt
[gsm@VM-4-3-centos dir]$ cd ..
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file.txt
[gsm@VM-4-3-centos lesson5]$ pwd
/home/gsm/112/lesson5
[gsm@VM-4-3-centos lesson5]$ rm file.txt 
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:04 dir
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:01 dir1
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:00 file1.txt
[gsm@VM-4-3-centos lesson5]$ pwd
/home/gsm/112/lesson5
[gsm@VM-4-3-centos lesson5]$ ll -d /home/gsm/112/lesson5
drwxrwxr-x 4 gsm gsm 4096 Oct  9 20:15 /home/gsm/112/lesson5
c 复制代码
[gsm@VM-4-3-centos lesson5]$ mkdir dir
[gsm@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
[gsm@VM-4-3-centos lesson5]$ touch file.txt
[gsm@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:34 file.txt
[gsm@VM-4-3-centos lesson5]$ umask
0002
[gsm@VM-4-3-centos lesson5]$ umask 000
[gsm@VM-4-3-centos lesson5]$ umask
0000
[gsm@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:34 file.txt
[gsm@VM-4-3-centos lesson5]$ touch file1.txt
[gsm@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
-rw-rw-rw- 1 gsm gsm    0 Oct  9 20:56 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:34 file.txt
[gsm@VM-4-3-centos lesson5]$ umask
0000
[gsm@VM-4-3-centos lesson5]$ mkdir dir1
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
drwxrwxrwx 2 gsm gsm 4096 Oct  9 20:56 dir1
-rw-rw-rw- 1 gsm gsm    0 Oct  9 20:56 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:34 file.txt
[gsm@VM-4-3-centos lesson5]$ umask 777
[gsm@VM-4-3-centos lesson5]$ umask
0777
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
drwxrwxrwx 2 gsm gsm 4096 Oct  9 20:56 dir1
-rw-rw-rw- 1 gsm gsm    0 Oct  9 20:56 file1.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:34 file.txt
[gsm@VM-4-3-centos lesson5]$ touch file2.txt
[gsm@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
drwxrwxrwx 2 gsm gsm 4096 Oct  9 20:56 dir1
-rw-rw-rw- 1 gsm gsm    0 Oct  9 20:56 file1.txt
---------- 1 gsm gsm    0 Oct  9 20:58 file2.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:34 file.txt
[gsm@VM-4-3-centos lesson5]$ mkdir dir2
[gsm@VM-4-3-centos lesson5]$ ll
total 12
drwxrwxr-x 2 gsm gsm 4096 Oct  9 20:34 dir
drwxrwxrwx 2 gsm gsm 4096 Oct  9 20:56 dir1
d--------- 2 gsm gsm 4096 Oct  9 20:59 dir2
-rw-rw-rw- 1 gsm gsm    0 Oct  9 20:56 file1.txt
---------- 1 gsm gsm    0 Oct  9 20:58 file2.txt
-rw-rw-r-- 1 gsm gsm    0 Oct  9 20:34 file.txt
[gsm@VM-4-3-centos lesson5]$ umask 002
[gsm@VM-4-3-centos lesson5]$ umask
0002

注:

  1. root 不受权限约束
c 复制代码
[root@VM-4-3-centos lesson5]# ll
total 4
-rw-r--rw- 1 root root 13 Oct  9 17:42 testfile.txt
[root@VM-4-3-centos lesson5]# whoami
root
[root@VM-4-3-centos lesson5]# chmod 000 testfile.txt 
[root@VM-4-3-centos lesson5]# ll
total 4
---------- 1 root root 13 Oct  9 17:42 testfile.txt
[root@VM-4-3-centos lesson5]# cat testfile.txt 
aaaaaaaaaaaa
[root@VM-4-3-centos lesson5]# echo "bbbbbbbb" > testfile.txt 
[root@VM-4-3-centos lesson5]# cat testfile.txt 
bbbbbbbb
[root@VM-4-3-centos lesson5]# ll
total 4
---------- 1 root root 9 Oct  9 18:12 testfile.txt
[root@VM-4-3-centos lesson5]# cd /home/gsm/112/lesson5
[root@VM-4-3-centos lesson5]# ll
total 0
-rw-rw---- 1 gsm gsm 0 Oct  9 18:15 file.txt
[root@VM-4-3-centos lesson5]# echo "aaaaa" > file.txt
[root@VM-4-3-centos lesson5]# cat file.txt 
aaaaa
  1. 可执行权限有点特殊,不是说有可执行权限就能执行,一个程序真正要跑起来需要同时满足:它是一个可执行文件且你具备可执行权限
  2. 如果想更改文件的拥有者和所属组,只能是超级用户或者用sudo
c 复制代码
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-r--rw-r-- 1 root gsm 28 Oct  9 18:33 testfile.txt
[gsm@VM-4-3-centos lesson5]$ exit
[root@VM-4-3-centos lesson5]# chown gsm:gsm testfile.txt 
[root@VM-4-3-centos lesson5]# su gsm
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-r--rw-r-- 1 gsm gsm 28 Oct  9 18:33 testfile.txt
[gsm@VM-4-3-centos lesson5]$ pwd
/root/112/lesson5
[gsm@VM-4-3-centos lesson5]$ chmod 666 testfile.txt 
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-rw-rw- 1 gsm gsm 28 Oct  9 18:33 testfile.txt
[gsm@VM-4-3-centos lesson5]$ chown root testfile.txt 
chown: changing ownership of 'testfile.txt': Operation not permitted
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-rw-rw- 1 gsm gsm 28 Oct  9 18:33 testfile.txt
[gsm@VM-4-3-centos lesson5]$ chown lighthouse testfile.txt 
chown: changing ownership of 'testfile.txt': Operation not permitted
[gsm@VM-4-3-centos lesson5]$ ll
total 4
-rw-rw-rw- 1 gsm gsm 28 Oct  9 18:33 testfile.txt
[gsm@VM-4-3-centos lesson5]$ chgrp root testfile.txt 
chgrp: changing group of 'testfile.txt': Operation not permitted
[gsm@VM-4-3-centos lesson5]$ chgrp lighthouse testfile.txt 
chgrp: changing group of 'testfile.txt': Operation not permitted

粘滞位

如果很多人共享一个目录时,他们都具有rwx权限,但是不想让一个人删除该目录下其他人的文件和目录,就需要用到粘滞位。

c 复制代码
[root@VM-4-3-centos /]# chmod o+t /share_112
[root@VM-4-3-centos /]# ls /share_112/ -ld
drwxr-xrwt 2 root root 4096 Oct  9 22:05 /share_112/

当一个目录被设置为"粘滞位",则该目录下的文件只能由

  1. 超级管理员删除
  2. 该目录的所有者删除
  3. 该文件的所有者删除
相关推荐
顾安r2 分钟前
12.18 脚本网页 C标准库
linux·c语言·stm32·嵌入式硬件·html5
NineData2 分钟前
NineData 数据库 DevOps 正式支持谷歌云,全面接入 GCP 数据源
运维·数据库·devops·ninedata·gcp·玖章算术·数据智能管理平台
从零开始学习人工智能3 分钟前
《8076 能通 9003 却超时?一次 Docker 容器跨网段排障小记》
运维·docker·容器
A13247053123 分钟前
Linux文件查找:find和locate命令入门
linux·运维·服务器·网络·chrome
咕噜企业分发小米3 分钟前
阿里云与华为云服务器哪个更利于人工智能?
服务器·阿里云·华为云
物联网软硬件开发-轨物科技4 分钟前
【轨物新闻】轨物科技光伏清洁机器人智能控制和运维解决方案正式上线华为鲲鹏解决方案市场
运维·科技·机器人
一周困⁸天.4 分钟前
DevOps
运维·devops
飞机火车巴雷特5 分钟前
PyCharm通过跳板机连接隔离内网服务器
运维·服务器
峥嵘life7 分钟前
Android16 EDLA 认证测试BTS过程介绍
android·java·linux
yenggd9 分钟前
华为+sr-mpls BE简单配置案例
运维·网络·华为