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

news2024/9/23 14:23:10
头像
🚀个人主页:奋斗的小羊
🚀所属专栏: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.c
     1	#include <stdio.h>

     2	int main()
     3	{
     4	    printf("hello world\n");
     5	    return 0;
     6	}
root@hcss-ecs-8f13:~/112# cat -n test.c
     1	#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
└── test3

0 directories, 3 files
root@hcss-ecs-8f13:~/112# zip -r dir.zip dir
  adding: 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.zip
   creating: dir/
 extracting: dir/test2               
 extracting: dir/test1               
 extracting: dir/test3               
root@hcss-ecs-8f13:~/112/mydir# tree 
.
├── dir
│   ├── test1
│   ├── test2
│   └── test3
└── dir.zip

1 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 dir
  adding: 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.zip
   creating: dir/
 extracting: dir/test2               
 extracting: dir/test1               
 extracting: dir/test3               
root@hcss-ecs-8f13:~/112/mydir# tree
.
├── dir
│   ├── test1
│   ├── test2
│   └── test3
└── dir.zip

1 directory, 4 files
root@hcss-ecs-8f13:~/112/mydir# unzip dir.zip -d ../otherdir
Archive:  dir.zip
   creating: ../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
    └── test3

1 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.coloradmin.cn/o/2157748.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

webLogic反序列化漏洞CVE-2017-3506

1.环境搭建 cd vulhub-master/weblogic/weak_password docker-compose up -d 2.判断wls-wsat组件是否存在 拼接/wls-wsat/CoordinatorPortType 查看页面是否有回显 有回显说明存在组件 3.在当前页面抓包 反弹shell 添加请求包内容 <soapenv:Envelope xmlns:soapenv&q…

hCaptcha 图像识别 API 对接说明

hCaptcha 图像识别 API 对接说明 本文将介绍一种 hCaptcha 图像识别 API 对接说明&#xff0c;它可以通过用户输入识别的内容和 hCaptcha验证码图像&#xff0c;最后返回需要点击的小图像的坐标&#xff0c;完成验证。 接下来介绍下 hCaptcha 图像识别 API 的对接说明。 注册…

线程的状态及join()插队方法

一、线程的状态 线程整个生命周期中有6种状态&#xff0c;分别为 NEW 新建状态 、RUNNABLE 可运行状态、TERMINATED 终止状态、TIMED_WAITING计时等待状态、WAITING 等待状态、BLOCKED 阻塞状态 线程各个状态之间的转换&#xff1a; 在 JAVA 程序中&#xff0c;一个线程对象通过…

一文搞懂offset、client、scroll系列及案例

目录 一、offset 1-1、offset系列属性 1-2、offset与style区别 1-3、案例 1-3-1、计算鼠标在盒子内的坐标 1-3-2、拖动模态框 二、client 2-1、client系列属性 三、scroll 3-1、scroll系列属性 3-2、案例 3-2-1、滚动页面一定距离后固定侧边栏 一、offset offset是…

pg入门3—详解tablespaces—下

pg默认的tablespace的location为空&#xff0c;那么如果表设置了默认的tablespace&#xff0c;数据实际上是存哪个目录的呢? 在 PostgreSQL 中&#xff0c;如果你创建了一个表并且没有显式指定表空间&#xff08;tablespace&#xff09;&#xff0c;或者表空间的 location 为…

数据库数据恢复—SQL Server附加数据库出现“错误823”怎么恢复数据?

SQL Server数据库故障&#xff1a; SQL Server附加数据库出现错误823&#xff0c;附加数据库失败。数据库没有备份&#xff0c;无法通过备份恢复数据库。 SQL Server数据库出现823错误的可能原因有&#xff1a;数据库物理页面损坏、数据库物理页面校验值损坏导致无法识别该页面…

【靶点Talk】免疫检查点争夺战:TIGIT能否超越PD-1?

曾经的TIGIT靶点顶着“下一个PD-1”的名号横空出世&#xff0c;三年的“征程”中TIGIT走过一次又一次的失败&#xff0c;然而面对质疑和压力仍有一批公司选择前行。今天给大家分享TIGIT靶点的相关内容&#xff0c;更多靶点科普视频请关注义翘神州B站和知乎官方账号。 TIGIT的“…

C#和数据库高级:虚方法

文章目录 一、抽象方法和抽象类中的思考1.1、回顾抽象方法的特点1.2、针对抽象方法问题的引出 二、虚方法的使用步骤2.1、虚方法重写方法的调用2.2、系统自带的虚方法2.3、重写Equals方法2.4、虚方法和抽象方法的比较 三、虚方法和抽象方法的联系3.1、ToString()方法的应用 一、…

2024/9/23 leetcode 25题 k个一组翻转链表

目录 25.k个一组翻转链表 题目描述 题目链接 解题思路与代码 25.k个一组翻转链表 题目描述 给你链表的头节点 head &#xff0c;每 k 个节点一组进行翻转&#xff0c;请你返回修改后的链表。 k 是一个正整数&#xff0c;它的值小于或等于链表的长度。如果节点总数不是 k 的…

Gartner:中国企业利用GenAI提高生产力的三大策略

作者&#xff1a;Gartner高级首席分析师 雷丝、Gartner 研究总监 闫斌、Gartner高级研究总监 张桐 随着生成式人工智能&#xff08;GenAI&#xff09;风靡全球&#xff0c;大多数企业都希望利用人工智能&#xff08;AI&#xff09;技术进行创新&#xff0c;以收获更多的业务成果…

JS 历史简介

目录 1. JS 历史简介 2. JS 技术特征 1. JS 历史简介 举例&#xff1a;在提交用户的注册信息的时候&#xff0c;为避免注册出现错误后重新填写信息&#xff0c;可以在写完一栏信息后进行校验&#xff0c;并提示是否出现错误&#xff0c;这样会大大提高用户提交的成功率&…

PCL 随机下采样

目录 一、概述 1.1原理 1.2实现步骤 1.3应用场景 二、代码实现 2.1关键函数 2.2完整代码 三、实现效果 PCL点云算法汇总及实战案例汇总的目录地址链接&#xff1a; PCL点云算法与项目实战案例汇总&#xff08;长期更新&#xff09; 一、概述 随机下采样 是一种常用的点…

大模型LLM对话模拟器Dialogue Simulator Visualization可视化工具

伴随着生成式人工智能技术发展&#xff0c;进2年涌现出大语言模型LLM/Agent系统/AI推理等众多方向的技术项目和论文。其中对话系统&#xff0c;智能体交互是用户通过UX界面和AI系统进行交互&#xff0c;这种交互有时候也是多模态&#xff08;用户输入文字/语音/图像&#xff09…

众数信科 AI智能体智慧办公解决方案——众数办公AI

智慧办公解决方案 众数办公AI 智能问答 拥有广域知识库 结合AI交互能力 通过智能人机、多轮对话即可完成 信息检索、知识提炼、内容衍生等问答操作 为用户提供多样化的问答服务 PPT创作 支持用户明确计划创作内容后进行需求提交 即刻智能生成符合需求的文章 支持上百种…

【软件工程】验证软件需求

一、验证需求正确性的四个方面 二、验证软件需求的方法 三、用于需求分析的软件工具 小结 例题 判断题

Java/Spring项目的包开头为什么是com?

Java/Spring项目的包开头为什么是com&#xff1f; 下面是一个使用Maven构建的项目初始结构 src/main/java/ --> Java 源代码com.example/ --->为什么这里是com开头resources/ --> 资源文件 (配置、静态文件等)test/java/ --> 测试代码resourc…

【机器学习】揭秘GBDT:梯度提升决策树

目录 &#x1f354; 提升树 &#x1f354; 梯度提升树 &#x1f354; 举例介绍 3.1 初始化弱学习器&#xff08;CART树&#xff09; 3.2 构建第一个弱学习器&#xff08;CART树&#xff09; 3.3 构建第二个弱学习器&#xff08;CART树&#xff09; 3.4 构建第三个弱学习…

Java ----常用类

包装类 包装类的分类 1) 针对八种基本数据类型相应的引用类型—包装类2) 有了类的特点&#xff0c;就可以调用类中的方法。3) 如图 包装类和基本数据的转换 jdk5 前的手动装箱和拆箱方式&#xff0c;装箱&#xff1a;基本类型转包装类型&#xff0c;拆箱&#xff1a;包装类型…

基于物联网的火灾报警器设计与实现(论文+源码)

1 总体方案设计 本次基于物联网的火灾报警器&#xff0c;其系统总体架构如图2.1所示&#xff0c;采用STM32f103单片机作为控制器&#xff0c;通过DS18B20传感器实现温度检测&#xff1b;通过MQ-2烟雾传感器实现烟雾检测&#xff1b;.通过火焰传感器实现火焰检测&#xff0c;当…

ps证件照蓝底换白底

ps证件照蓝底换白底 1、打开 Photoshop&#xff0c;导入需要处理的照片。 2、左侧工具栏中选择“魔棒工具”&#xff0c;点击证件照的背景区域进行选择。 3、使用快捷键 Shift F5 或者从顶部菜单选择“编辑” -> “填充”&#xff0c;在弹出的对话框中选择“填充内容”中…