当前位置: 首页 > news >正文

【Linux】指令和权限的这些细节,你确定都清楚吗?

头像
🚀个人主页:奋斗的小羊
🚀所属专栏:Linux
很荣幸您能阅读我的文章,诚请评论指点,欢迎欢迎 ~

动图描述

目录

  • 前言
  • 💥一、Linux基本指令
    • 💥1.1 mv 指令
    • 💥1.2 cat 指令
    • 💥1.3 echo 指令
    • 💥1.4 重定向
    • 💥1.5 more 指令
    • 💥1.6 less 指令
    • 💥1.7 head 指令
    • 💥1.8 tail 指令
    • 💥1.9 时间相关的指令
    • 💥1.10 find 指令
    • 💥1.11 grep 指令
    • 💥1.12 zip / unzip 指令
    • 💥1.13 tar 指令
    • 💥1.14 bc指令
    • 💥1.15 uname 指令
    • 💥1.16 几个重要的热键
  • 💥二、Linux权限管理
    • 💥2.1 Linux用户类型
    • 💥2.2 文件类型和访问权限
    • 💥2.3 文件访问权限的相关设置方法


前言

承接上文,本文将继续补充介绍一些Linux基本指令,以及探讨指令究竟是什么,又什么是权限?权限是 Linux 系统中非常重要的一部分,它决定了谁可以读取、写入或执行文件或目录。


💥一、Linux基本指令

💥1.1 mv 指令

mv指令是move的缩写,用来移动或重命名文件、目录,经常用来备份文件或目录。

  • mv old_name new_name: 重命名文件或目录
  • mv file /path/to/directory: 移动文件到指定目录
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 16 20:13 dir
-rw-r--r-- 1 root root   78 Sep 16 20:04 test.c
root@hcss-ecs-8f13:~/112# mv test.c name.c
root@hcss-ecs-8f13:~/112# mv dir mydir
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
-rw-r--r-- 1 root root   78 Sep 16 20:04 name.c
root@hcss-ecs-8f13:~/112# mv name.c ../
root@hcss-ecs-8f13:~/112# ls -l ../
total 12
drwxr-xr-x 3 root root 4096 Sep 16 20:16 112
-rw-r--r-- 1 root root   78 Sep 16 20:04 name.c
drwx------ 3 root root 4096 Sep 16 11:32 snap
root@hcss-ecs-8f13:~/112# ls -l
total 4
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
root@hcss-ecs-8f13:~/112# mv mydir ../
root@hcss-ecs-8f13:~/112# ls -l ../
total 16
drwxr-xr-x 2 root root 4096 Sep 16 20:16 112
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
-rw-r--r-- 1 root root   78 Sep 16 20:04 name.c
drwx------ 3 root root 4096 Sep 16 11:32 snap
root@hcss-ecs-8f13:~/112# ls -l
total 0
root@hcss-ecs-8f13:~/112# 

💥1.2 cat 指令

语法: cat [选项] [文件]
功能: 读取文件内容并将其输出到标准输出设备(通常是终端或屏幕)
常用选项:

  1. -b 对非空行输出行编号
  2. -n 对输出的所有行编号
  3. -s 不输出多行空行
root@hcss-ecs-8f13:~/112# pwd
/root/112
root@hcss-ecs-8f13:~/112# touch test.c
root@hcss-ecs-8f13:~/112# ls
test.c
root@hcss-ecs-8f13:~/112# nano test.c
root@hcss-ecs-8f13:~/112# cat test.c
#include <stdio.h>int main()
{printf("hello world\n");return 0;
}
root@hcss-ecs-8f13:~/112# cat -b test.c1	#include <stdio.h>2	int main()3	{4	    printf("hello world\n");5	    return 0;6	}
root@hcss-ecs-8f13:~/112# cat -n test.c1	#include <stdio.h>2	3	int main()4	{5	    printf("hello world\n");6	    return 0;7	}
root@hcss-ecs-8f13:~/112# cat -s test.c
#include <stdio.h>int main()
{printf("hello world\n");return 0;
}
root@hcss-ecs-8f13:~/112# 

cat会把文件中的所有内容显示出来,因此cat不适合看大文本,适合看小文本。

如果cat后什么都不跟,则通常情况下我们从键盘输入什么,屏幕上就输出什么。

root@hcss-ecs-8f13:~/112# cat
hello world
hello world
Are you ok?
Are you ok?

tac指令: 功能和cat相反,逆序打印文件内容。

root@hcss-ecs-8f13:~/112# ls 
mydir  name.c
root@hcss-ecs-8f13:~/112# tac name.c
}return 0;printf("hello world\n");
{
int main()#include <stdio.h>
root@hcss-ecs-8f13:~/112# 

💥1.3 echo 指令

功能:

  • echo 文本: 在终端(命令行界面)上输出文本
  • echo 文本>文件(如果文件不存在则新建): 将文本写入到文件中
root@hcss-ecs-8f13:~/112# ls -l
total 4
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
root@hcss-ecs-8f13:~/112# echo "hello world"
hello world
root@hcss-ecs-8f13:~/112# echo "hello world">test.txt
root@hcss-ecs-8f13:~/112# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 16 20:41 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir/
-rw-r--r-- 1 root root   12 Sep 16 20:41 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
root@hcss-ecs-8f13:~/112# 

💥1.4 重定向

Linux下一切皆文件,Linux下,显示器、键盘、网卡、普通文件等都可以看作文件,只不过显示器只有写方法,向显示器打印,其实就是向显示器文件写入,无法从显示器读取数据。键盘只有读方法。而普通文件具有读写两个功能。
echo指令默认把后面跟的文本写入显示器文件中。cat指令后面如果没有跟任何文件,则默认从键盘文件中读取数据,然后写入到显示器文件中。

上面我们用echo 文本>(输出重定向)文件将文本写入到文件中,如果文件不存在则新建

root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:47 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
root@hcss-ecs-8f13:~/112# echo "hello world">test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# echo "aa" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
aa
root@hcss-ecs-8f13:~/112# 

通过测试我们还可以得出,如果指定的文件里面有内容,则会先清空原内容,然后再写入(并不是覆盖)。

  • 当文件不存在时,>文件名可以新建空文件
  • 当文件存在时,>文件名可以清空文件内容
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
root@hcss-ecs-8f13:~/112# >test.txt
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root    0 Sep 16 22:58 test.txt
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root   12 Sep 16 22:59 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# >test.txt
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root    0 Sep 16 22:59 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
root@hcss-ecs-8f13:~/112# 

>指定文件写入内容会把原内容删除掉,如果我们就不想删除原始内容,将新内容追加到原始内容后,可以用>>(追加重定向)实现。

root@hcss-ecs-8f13:~/112# cat test.txt
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# echo "Hello" >> test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112# 

除了>>>进行输出,还有输入重定向符号<。前面我们说了如果cat后面什么都不跟,则默认从键盘上取数据。所以如果我们用输入重定向符号<指定文件,则会输出文件中的内容。

root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112# cat < test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112# 

💥1.5 more 指令

语法: more [选项] [文件]
功能: 分页显示文本文件内容,类似cat
常用选项:

  1. -n 对输出的所有行编号
  2. -s 将多个空行压缩成一个空行显示

快捷键:

  • 空格键: 显示下一页的内容
  • 回车键: 显示下一行的内容
  • b: 往回(向上)翻页,即显示上一页的内容
  • f: 往前(向下)翻页,功能与空格键相同
  • q: 退出 more

当我们想要查看一个文件的内容,但又不想一次性将整个文件内容全部加载到终端(或屏幕)上时,可以用more代替cat。

但是more指令我们也不太使用,因为我们更方便的指令来代替more。


💥1.6 less 指令

语法: less [参数] 文件
功能: less是一个功能强大的文本查看器,支持向前和向后滚动文本内容,以及搜索、跳转和复制等更多的功能。它特别适合用于查看大型文件或需要在文件的任意位置浏览内容的情况。
常用选项:

  1. -N 显示每行的行号
  2. / 字符串:向下搜索“字符串”的功能
  3. ?字符串:向上搜素字符串的功能
  4. n 重复前一个搜索
  5. N 反向重复前一个搜索

less指令也是按q退出,另外,less支持鼠标滑动来查看文件的上下内容, 相比之下less比more更好用。


💥1.7 head 指令

语法: head [参数]… 文件…
功能: 显示文本文件的头部内容,默认打印文件内容的前10行
常用选项:

  1. -n<行数> 显示的行数
root@hcss-ecs-8f13:~/112# head log.txt
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
hello 10
root@hcss-ecs-8f13:~/112# head -n5 log.txt
hello 1
hello 2
hello 3
hello 4
hello 5
root@hcss-ecs-8f13:~/112# 

💥1.8 tail 指令

语法: tail [参数] 文件
功能: 与 head 相反,它默认显示文件的最后10行内容,但同样可以通过命令行选项来定制。不指定文件时,作为输入信息进行处理,常用查看日志文件
常用选项:

  1. -f 循环读取
  2. -n<行数> 显示行数
root@hcss-ecs-8f13:~/112# tail log.txt
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
root@hcss-ecs-8f13:~/112# tail -n5 log.txt
hello 996
hello 997
hello 998
hello 999
hello 1000
root@hcss-ecs-8f13:~/112# 

| 管道举例:

管道符号| :它允许将一个命令的输出直接作为另一个命令的输入。

命令1 | 命令2 | 命令3 ...

问题: 文件log.txt有1000行内容,打印出第500行到510行之间的10行内容。

root@hcss-ecs-8f13:~/112# head -n510 log.txt | tail -n10 log.txt
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
root@hcss-ecs-8f13:~/112# 

💥1.9 时间相关的指令

| date命令

  • 功能: 用于显示和设置系统的日期和时间
  • 基本用法:
    • 查看当前时间: 直接在终端中输入date
    • 设置系统时间: 使用date -s后跟日期和时间字符串,格式为"YYYY-MM-DD HH:MM:SS",注意字符串需要用双引号括起来
    • 显示特定格式的日期和时间: 可以使用date +%F(显示年月日,等同于+%Y-%m-%d)等格式化选项
    • %H:小时
    • %M:分钟
    • %S:秒
    • %X:相当于%H:%M:%S
    • %d:日
    • %m:月
    • %Y:年
    • %F:相当于%Y-%m-%d
root@hcss-ecs-8f13:~/112# date
Wed Sep 18 11:19:19 PM CST 2024
root@hcss-ecs-8f13:~/112# date +%Y:%m:%d
2024:09:18
root@hcss-ecs-8f13:~/112# date +%Y:%m:%d:%H:%M:%S
2024:09:18:23:24:04
root@hcss-ecs-8f13:~/112# date +%F
2024-09-18
root@hcss-ecs-8f13:~/112# date +%X
11:27:49 PM
root@hcss-ecs-8f13:~/112# 

| 时间戳:

时间戳是指从某一特定时刻(通常是1970年1月1日 00:00:00 UTC,即协调世界时)起至现在的总秒数(或毫秒数、微秒数等,具体取决于系统和应用的精度需求)。这种时间表示方式是一种简单而有效的方法,用于在计算机系统和网络中记录时间信息。

  • 时间 -> 时间戳:date +%s
  • 时间戳 -> 时间:date -d@时间戳
root@hcss-ecs-8f13:~/112# date +%s
1726673405
root@hcss-ecs-8f13:~/112# date +%s
1726673436
root@hcss-ecs-8f13:~/112# date +%s
1726673452
root@hcss-ecs-8f13:~/112# date -d@1726673452
Wed Sep 18 11:30:52 PM CST 2024
root@hcss-ecs-8f13:~/112# 

💥1.10 find 指令

语法: find [路径] [选项] [操作]
功能: 在文件树中查找文件

  • 路径: 指定find命令开始搜索的目录路径。如果省略,find将从当前目录开始搜索
  • 选项: 用于指定搜索的条件或行为。选项可以是基于文件类型、名称、大小、时间戳等的过滤器
  • 操作: 对匹配的文件执行的操作。如果不指定操作,find将默认打印出所有匹配的文件路径
    常用选项:
  1. -name:按文件名搜索。支持使用通配符(如*、?等)
  2. -iname:类似于-name,但搜索时不区分大小写
  3. -type:按文件类型搜索。例如,f表示普通文件,d表示目录
root@hcss-ecs-8f13:~/112# find -name test*
./test.txt
root@hcss-ecs-8f13:~/112# find -type d
.
./dir
root@hcss-ecs-8f13:~/112# 

💥1.11 grep 指令

语法: grep [选项]… 模式 [文件]…
功能: 在文件中搜索字符串,将找到的行打印出来
常用选项:

  1. -i: 忽略大小写
  2. -v: 反向选择,只显示不匹配的行
  3. -c: 计算匹配的行数,而不是显示匹配的行内容
  4. -l: 只列出包含匹配文本的文件名,而不显示匹配的行
  5. -L: 只列出不包含匹配文本的文件名
  6. -n: 显示匹配的行号及内
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep hello test.txt
hello world
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep world test.txt
hello world
root@hcss-ecs-8f13:~/112# grep -i world test.txt
hello world
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep -v world test.txt
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep -iv world test.txt
root@hcss-ecs-8f13:~/112# grep -vn WORLD test.txt
1:hello world
2:hello World
3:hello WOrld
4:hello WORld
5:hello WORLd
root@hcss-ecs-8f13:~/112# 

实践中grep的使用率还是很高的,像查找进程等。


💥1.12 zip / unzip 指令

zip和unzip是两个非常常用的命令行工具,分别用于压缩文件和目录(打包成.zip格式)以及解压缩.zip文件。
语法: zip 压缩文件.zip 目录或文件
功能: 将目录或文件压缩成zip格式
常用选项:

  1. -r: 递归地将目录及子目录下的所有文件和目录都压缩到压缩文件中
root@hcss-ecs-8f13:~/112# ls -l
total 12
drwxr-xr-x 2 root root 4096 Sep 19 22:05 dir
drwxr-xr-x 2 root root 4096 Sep 19 22:09 mydir
-rw-r--r-- 1 root root   72 Sep 19 21:38 test.txt
root@hcss-ecs-8f13:~/112# tree dir
dir
├── test1
├── test2
└── test30 directories, 3 files
root@hcss-ecs-8f13:~/112# zip -r dir.zip diradding: dir/ (stored 0%)adding: dir/test2 (stored 0%)adding: dir/test1 (stored 0%)adding: dir/test3 (stored 0%)
root@hcss-ecs-8f13:~/112# ls -l;
total 16
drwxr-xr-x 2 root root 4096 Sep 19 22:05 dir
-rw-r--r-- 1 root root  596 Sep 19 22:11 dir.zip
drwxr-xr-x 2 root root 4096 Sep 19 22:09 mydir
-rw-r--r-- 1 root root   72 Sep 19 21:38 test.txt
root@hcss-ecs-8f13:~/112# mv dir.zip ./mydir
root@hcss-ecs-8f13:~/112# cd mydir
root@hcss-ecs-8f13:~/112/mydir# pwd
/root/112/mydir
root@hcss-ecs-8f13:~/112/mydir# ls -l
total 4
-rw-r--r-- 1 root root 596 Sep 19 22:11 dir.zip
root@hcss-ecs-8f13:~/112/mydir# unzip dir.zip
Archive:  dir.zipcreating: dir/extracting: dir/test2               extracting: dir/test1               extracting: dir/test3               
root@hcss-ecs-8f13:~/112/mydir# tree 
.
├── dir
│   ├── test1
│   ├── test2
│   └── test3
└── dir.zip1 directory, 4 files
root@hcss-ecs-8f13:~/112/mydir# 

unzip默认是解压到当前目录,当然我们也可以加上-d选项后解压到指定目录下。

root@hcss-ecs-8f13:~/112# ll
total 20
drwxr-xr-x 5 root root 4096 Sep 19 22:27 ./
drwx------ 7 root root 4096 Sep 19 21:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
drwxr-xr-x 2 root root 4096 Sep 19 22:27 mydir/
drwxr-xr-x 2 root root 4096 Sep 19 22:18 otherdir/
root@hcss-ecs-8f13:~/112# zip -r dir.zip diradding: dir/ (stored 0%)adding: dir/test2 (stored 0%)adding: dir/test1 (stored 0%)adding: dir/test3 (stored 0%)
root@hcss-ecs-8f13:~/112# ll
total 24
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ./
drwx------ 7 root root 4096 Sep 19 21:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
-rw-r--r-- 1 root root  596 Sep 19 22:28 dir.zip
drwxr-xr-x 2 root root 4096 Sep 19 22:27 mydir/
drwxr-xr-x 2 root root 4096 Sep 19 22:18 otherdir/
root@hcss-ecs-8f13:~/112# mv dir.zip ./mydir
root@hcss-ecs-8f13:~/112# ll
total 20
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ./
drwx------ 7 root root 4096 Sep 19 21:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
drwxr-xr-x 2 root root 4096 Sep 19 22:28 mydir/
drwxr-xr-x 2 root root 4096 Sep 19 22:18 otherdir/
root@hcss-ecs-8f13:~/112# cd mydir
root@hcss-ecs-8f13:~/112/mydir# ll
total 12
drwxr-xr-x 2 root root 4096 Sep 19 22:28 ./
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ../
-rw-r--r-- 1 root root  596 Sep 19 22:28 dir.zip
root@hcss-ecs-8f13:~/112/mydir# unzip dir.zip
Archive:  dir.zipcreating: dir/extracting: dir/test2               extracting: dir/test1               extracting: dir/test3               
root@hcss-ecs-8f13:~/112/mydir# tree
.
├── dir
│   ├── test1
│   ├── test2
│   └── test3
└── dir.zip1 directory, 4 files
root@hcss-ecs-8f13:~/112/mydir# unzip dir.zip -d ../otherdir
Archive:  dir.zipcreating: ../otherdir/dir/extracting: ../otherdir/dir/test2   extracting: ../otherdir/dir/test1   extracting: ../otherdir/dir/test3   
root@hcss-ecs-8f13:~/112/mydir# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 19 22:29 ./
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
-rw-r--r-- 1 root root  596 Sep 19 22:28 dir.zip
root@hcss-ecs-8f13:~/112/mydir# cd ..
root@hcss-ecs-8f13:~/112# cd otherdir
root@hcss-ecs-8f13:~/112/otherdir# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 19 22:29 ./
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
root@hcss-ecs-8f13:~/112/otherdir# tree
.
└── dir├── test1├── test2└── test31 directory, 3 files
root@hcss-ecs-8f13:~/112/otherdir# 

💥1.13 tar 指令

功能: 打包和解包文件
常用选项:

  1. -c :建立一个压缩文件的参数指令(create 的意思);
  2. -x :解开一个压缩文件的参数指令
  3. -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
  4. -v :显示详细过程
  5. -f :指定归档文件的名称,在 f 之后要立即接档名
  6. -C : 解压到指定目录

示例:

  • tar czf dir.tgz dir:将目录dir打包成dir.zip文件
  • tar cvzf dir.tgz dir:在打包过程中显示详细过程
  • tar xzf dir.tgz:将文件dir.zip解压到当前目录
  • tar xzf dir.tgz -C otherdir:将文件dir.zip解压到指定目录

💥1.14 bc指令

一个任意精度的计算器语言,通常用于执行数学运算。

  • echo 1+2+3+4+5 | bc:把计算结果输出到屏幕上

💥1.15 uname 指令

语法: uname [选项]
功能: uname用来获取电脑和操作系统的相关信息
常用选项:

  1. -a : 显示所有信息,这通常包括内核名称、主机名、内核发行版、处理器类型、硬件平台、操作系统名称等
  2. -s :显示内核名称
  3. -n :显示主机名
  4. -r :显示内核发行版
root@hcss-ecs-8f13:~/112# uname -a
Linux hcss-ecs-8f13 5.15.0-113-generic #123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
root@hcss-ecs-8f13:~/112# uname -r
5.15.0-113-generic
root@hcss-ecs-8f13:~/112# uname -n
hcss-ecs-8f13
root@hcss-ecs-8f13:~/112# uname -s
Linux
root@hcss-ecs-8f13:~/112# 

💥1.16 几个重要的热键

  • [Tab]:命令补全和档案补齐的功能
  • [Ctrl + c]:停掉当前程序
  • [Ctrl + d]:关闭整个终端会话
  • [Ctrl + r]:历史命令搜索

💥二、Linux权限管理

💥2.1 Linux用户类型

  • 超级用户(root):拥有系统中最高权限,可以执行系统级别的管理任务,访问和修改系统的所有文件和设置。
  • 普通用户:由管理员创建并分配给系统的普通用户账户,具有较低的权限,只能访问和修改自己的文件和一些共享的资源。

whoami:显示当前用户的用户名
su [用户名]:切换用户

root@hcss-ecs-8f13:~/112# whoami
root
root@hcss-ecs-8f13:~/112# su yjz
yjz@hcss-ecs-8f13:/root/112$ whoami
yjz
yjz@hcss-ecs-8f13:/root/112$ su
Password: 
root@hcss-ecs-8f13:~/112# whoami
root
root@hcss-ecs-8f13:~/112# 

su后面什么都不加默认切换到超级用户下,从普通用户切换到超级用户时需要输入超级用户的密码(输入的过程是不显示的),从普通用户切换到另一个普通用户时也要输密码,而从超级用户下切换到普通用户是不需要密码的。


💥2.2 文件类型和访问权限

文件的基本权限分为三组:

  1. 所有者(User):文件的创建者或拥有者,拥有对文件的完全控制权
  2. 所属组(Group):与文件相关联的用户组,该组的所有成员都具有一定的权限来访问文件
  3. 其他用户(Others):既不是文件的所有者也不是文件的所属组成员的用户,其权限受到文件的权限设置的限制

请添加图片描述

  • d:目录
  • -:普通文件
  • 最前面由10个字符组成,第一个字符表示文件类型,后面的九个字符按3个一组分别表示所有者、用户组和其他用户的权限

Linux权限是指对文件和目录所具有的操作权限,包括读(r)、写(w)和执行(x)权限。
权限 = 角色 + 文件的属性。 对某一个文件是否有读、写、执行权限,第一看我们的角色,第二看这个文件对于我们所扮演的这个角色是否有相应的权限。也就是说,即使我是这个文件的拥有者,如果这个文件没有读属性,我也不能读这个文件。

| 示例:

root@hcss-ecs-8f13:~/112# whoami
root
root@hcss-ecs-8f13:~/112# su yjz
yjz@hcss-ecs-8f13:/root/112$ whoami
yjz
yjz@hcss-ecs-8f13:/root/112$ ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rwxrwxr-x 1 root root   12 Sep 21 00:12 test.txt
yjz@hcss-ecs-8f13:/root/112$ echo "Are you ok?" > test.txt
bash: test.txt: Permission denied
yjz@hcss-ecs-8f13:/root/112$ su
Password: 
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# 

上面的示例中,文件test.txt的拥有者是root,我们从超级用户root切换到普通用户yjz下,对于文件test.txt,用户yjz是其他用户,而对于文件test.txt的其他用户是没有写权限的,所以当我们以yjz的身份往文件test.txt中写时系统提示错误,当我们重新切换到超级用户下就可以往文件test.txt中写入了。


💥2.3 文件访问权限的相关设置方法

更改文件权限,可以更改访问者的角色,也可以更改文件对于某类用户的权限。

  • chmod:设置文件的访问权限,只有文件的拥有者和root才能修改文件的访问权限。

用户符号:

  1. u:拥有者
  2. g:所属组
  3. o:其他用户
  4. a:所有用户

| 示例:

  • chmod u+x file:使文件file的拥有者具有执行权限
  • chmod g+wx file:使文件file的所属组具有写、执行权限
  • chmod o-rwx file:撤去文件file的其他用户的读、写、执行权限
  • chmod a+rwx file:使文件file的拥有者、所属组、其他用户都具有读、写、执行权限
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rw-r--r-- 1 root root   12 Sep 20 23:10 test.txt
root@hcss-ecs-8f13:~/112# chmod u+x test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rwxr--r-- 1 root root   12 Sep 20 23:10 test.txt
root@hcss-ecs-8f13:~/112# chmod g+wx test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rwxrwxr-- 1 root root   12 Sep 20 23:10 test.txt
root@hcss-ecs-8f13:~/112# chmod a-rwx test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
---------- 1 root root   12 Sep 20 23:10 test.txt
root@hcss-ecs-8f13:~/112# chmod a+rwx test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rwxrwxrwx 1 root root   12 Sep 20 23:10 test.txt
root@hcss-ecs-8f13:~/112# 

通过上面的示例我们不难看出,不管是拥有者、所属组、还是其他用户,r、w、x所对应的位置是不变的,所以我们也可以用8进制的数字来表示用户的权限。

在这里插入图片描述

例如: 对某一个文件来说,它的拥有者有读、写权限,没有执行权限,则对应的二进制为110,转换为8进制为6;它的所属组只有执行权限,没有读、写权限,则对应的二进制为001,转换为8进制为1;它的其他用户只有写权限,没有读、执行权限,则对应的二进制为100,转换为8进制为4。

root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-r--r--r-- 1 root root   12 Sep 21 00:24 test.txt
root@hcss-ecs-8f13:~/112# chmod 614 test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rw---xr-- 1 root root   12 Sep 21 00:24 test.txt
root@hcss-ecs-8f13:~/112# 

| 示例:

  • chmod 555 test.txt:文件的拥有者、所属组、其他用户都有读、执行权限,都没有写权限
  • chmod 777 test.txt:文件的拥有者、所属组、其他用户都有读、写、执行权限
  • chmod 444 test.txt:文件的拥有者、所属组、其他用户都只有读权限
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rwxrwxr-x 1 root root   12 Sep 21 00:24 test.txt
root@hcss-ecs-8f13:~/112# chmod 555 test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-r-xr-xr-x 1 root root   12 Sep 21 00:24 test.txt
root@hcss-ecs-8f13:~/112# chmod 777 test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-rwxrwxrwx 1 root root   12 Sep 21 00:24 test.txt
root@hcss-ecs-8f13:~/112# chmod 444 test.txt
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-r--r--r-- 1 root root   12 Sep 21 00:24 test.txt
root@hcss-ecs-8f13:~/112# 

| 注意:

1. 文件有可执行权限,和文件能否被执行,是两码事。

一个文件可执行,一要看这个文件对当前用户是否有可执行权限,二要这个文件就应该是个可执行文件。
就比如某个人明明有能力胜任某项工作,但领导就不给他做这个工作的机会,那他也没办法。而另一个人明明没有一点能力,但领导就点名让他去做,那领导就有点强人所难了。

2. 确定身份的过程,只能确定一次,一般顺序为拥有者、所属组、其他用户

如果某个文件的拥有者没有对这个文件的写权限,即使这个文件的所属组有写权限,而文件的拥有者也是所属组,那这个文件的拥有者依然不能对这个文件进行写入。

yjz@hcss-ecs-8f13:/root/112$ ls -l
total 8
-r--rw-r-- 1 yjz  yjz     0 Sep 21 11:39 file.txt
drwxr-xr-x 2 root root 4096 Sep 20 17:39 mydir
-r---wxr-- 1 root root   12 Sep 21 11:31 test.txt
yjz@hcss-ecs-8f13:/root/112$ echo "Are you ok?" > file.txt
bash: file.txt: Permission denied
yjz@hcss-ecs-8f13:/root/112$ 

本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~

头像

http://www.mrgr.cn/news/34120.html

相关文章:

  • Ubuntu的基本用法与指令(为后面学习ROS打基础)
  • 【BEV 视图变换】Ray-based(2): 代码复现+画图解释 基于深度估计、bev_pool(代码一键运行)
  • SpringBootWeb响应
  • 如何在产品上扩展大储存?外挂SPI Flash库轻松搞定!
  • 二维矩阵的行、列、斜线特征(二维数组)
  • 深入理解MySQL InnoDB中的B+索引机制
  • 【LLM论文日更】| GRIT如何统一文本生成与嵌入
  • HTTP、FTP 和 DICT,这三个协议,你真的了解吗?
  • JavaScript:数组遍历
  • 小程序开发设计-小程序的宿主环境:组件⑦
  • qt中QTatlewidget类常用操作表格的函数有哪些?
  • Cherry Studio:开启AI智能工作的新篇章
  • 倍增算法——AtCoder Beginner Contest 370 F
  • 【Linux系统编程】第二十二弹---操作系统核心概念:进程创建与终止机制详解
  • 众数信科 AI智能体政务服务解决方案——寻知智能审查系统
  • 跳蚤市场小程序|基于微信小程序的跳蚤市场(源码+数据库+文档)
  • 计算机前沿技术-人工智能算法-大语言模型-最新论文阅读-2024-09-19
  • UTON生态开发者签约大会圆满成功
  • 华为海思Hi3519DV500支持四路sensor 输入,支持 4K@30fps 内置双核 A55和 2.5Tops NN 算力
  • JS巧用.padStart()|.padEnd()方法用另一个字符串填充当前字符串