惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
爱范儿
爱范儿
Recent Announcements
Recent Announcements
AI
AI
V
Visual Studio Blog
H
Heimdal Security Blog
L
LINUX DO - 最新话题
Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
人人都是产品经理
人人都是产品经理
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
S
Secure Thoughts
S
Security Affairs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
Schneier on Security
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
The Cloudflare Blog
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
有赞技术团队
有赞技术团队
博客园 - 司徒正美
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
NISL@THU
NISL@THU
C
Cybersecurity and Infrastructure Security Agency CISA
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
罗磊的独立博客
V
Vulnerabilities – Threatpost
S
Securelist
N
News and Events Feed by Topic
Cloudbric
Cloudbric
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V2EX - 技术
V2EX - 技术
小众软件
小众软件

博客园 - HackerVirus

在window系统下搭建C/C++开发环境 C#中DataGridView处理大数据量的技巧分享 CSharp-MVVM框架 多线程&线程池 Prism的事件聚合器 Toggling Focus Assist mode in Win 10 Programmatically Windsurf:超越 Cursor 的下一代 AI 编辑器 编程辅助新选择:Devin与Cursor的性能对比评测 支持多语言、多商店的商城,.Net7 + EF7领域驱动设计架构 Mysql高可用架构方案 软件架构中的那些惯用手段 .Net轻量级的CMS开源项目 前后端分离的权限管理框架,前端采用 Vue 3 框架,后端采用 .NET 8、ORM 采用 EF 8 Roslyn的源生成器 IHostedService 和 BackgroundService 区别 Uno Platform是一个基于C#开源、功能强大、灵活的跨平台开发框架,用于快速构建单一代码库原生移动、Web、桌面和嵌入式应用程序 使用HslCommunication类库读取Siemens PLC DATA 功能齐全的 WPF 自定义控件 Sprint Planning
linux文件属性
HackerVirus · 2025-01-08 · via 博客园 - HackerVirus

文件属性

【1】、ls -ihl 每一列的含义

[root@kylin-xu ~]# ls -ilh 
总用量 105M
102604839 -rw-r--r-- 1 root root  57M  9月 21  2017 access.log
102685193 -rw------- 1 root root 2.7K 11月  3 12:29 anaconda-ks.cfg
102407797 -rw-r--r-- 1 root root 3.1K 11月  3 12:31 initial-setup-ks.cfg
102684881 -rw-r--r-- 1 root root  112 11月  4 23:52 nginxconf.bak.txt
102604829 -rw-r--r-- 1 root root  112 11月  4 23:50 nginxconf.txt
102684877 -rw-r--r-- 1 root root  327 11月  4 12:54 num.txt
102604833 -rw-r--r-- 1 root root  49M  2月 15  2017 secure-20161219

image-20241105191630522

【2】、inode和block

  • inode index node 索引节点,类似于我们的身份证

    • inode号码是身份证号

    • inode空间是类似于身份证存放个人信息

    • inode空间存放文件属性信息。

  • block 数据块, 存放数据(文件内容)

我们查看 abc.txt 文件的过程解析

我们在执行cat abc.txt 命令后,首先会进入inode区。因为inode中存在权限、用户/用户组等信息,如果我们的权限不匹配,会在这一步就被拒绝

如果可以进入indode区后,里面会存在着 block位置(指向数据实体指针),他会指引我们到block的位置,进而可以查看到数据信息

block区

inode区

数据

属性信息

文件大小

权限

用户/用户组

block位置

cat命令

  • 特点:

    • 每创建1个文件,需要1个inode,block(非空)
    • inode 256字节一般。
    • block 一般是4k.
    • 创建1个非空文件,占1个block, 文件小没有占满block,剩余的空间就空着。
    • inode大小,block大小,数量都是在格式化的时候诞生的。
  • 命令

    • 文件 inode 号码(不太重要),block(文件大小 重要)
    ls -lhi   filename
    
  • 目录 查看目录所占的空间

    [root@kylin-xu ~]# du -sh /etc/
    25M     /etc/
    # -h:以人类可读的方式查看大小
    # -s:查看汇总后的结果
    
    # 不要使用ls -ih去看
    

    对于目录来说,我们使用 ls -lh 命令去查看得到的大小并不是该目录在磁盘中占据的大小

    因为按照inode和block的关系来分析,目录的block中存放的是该目录下所有文件和目录的名字,因此我们不能使用ls -lh去查看目录的大小

  • 小结:

    • inode,block作用。

    • 命令ls,du

【3】、文件类型

一切皆文件

常见Linux文件类型 
-(文件)  
d(目录)  
l(软链接)  
c(字符设备) 后面使用 不断输出(白洞),不断吸收(黑洞)
/dev/null (黑洞)
/dev/urandom (白洞) /dev/zero(白洞)
b(块设备) 硬盘,光盘 (磁盘专题)
s(socket文件 套接字文件) 网络传输相关
......  
  • ls -l
  • file 查看文件类型
crw-rw-rw- 1 root root 1, 3 11月  4 14:40 /dev/null
[root@kylin-xu ~]# ll /dev/zero 
crw-rw-rw- 1 root root 1, 5 11月  4 14:40 /dev/zero
[root@kylin-xu ~]# ll /dev/urandom 
crw-rw-rw- 1 root root 1, 9 11月  4 14:40 /dev/urandom
[root@kylin-xu ~]# file /dev/urandom 
/dev/urandom: character special (1/9)

【4】、权限

1、rwx权限含义

r read # 读 可以使用这些命令 cat、less、more、vim
w  write   # 写 vim echo sed
x  excuter # 可执行,对于普通文件无意义,对可执行文件才有意义
-  没有权限


[root@kylin-xu day14]

文件的最高权限

目录的最高权限

2、rwx和数字的对应关系

r   # 4
w   # 2
x   # 1
-rw-r--r-- 1 xu xu 0 11月  6 04:04 1.txt
user   rw-;4+2+0 
group  r--:4+0+0
others r--:4+0+0
文件的权限用数字表示 644

通过数字得出文件使用字符表示权限
755
rwxr-xr-x

3、修改文件权限

(1)、chown

chown	 # 修改文件的属主属组
语法结构
		 chown oldboy file # 只修改属主
		 chown oldboy.oldboy file 
  • 案例1.修改test文件的属主为xu用户
[root@kylin-xu tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 11月  6 04:18 test
[root@kylin-xu tmp]# chown xu test 
[root@kylin-xu tmp]# ll
总用量 0
-rw-r--r-- 1 xu root 0 11月  6 04:18 test
  • 案例2.同时修改属主和属组为xu
[root@kylin-xu tmp]# ll
总用量 0
-rw-r--r-- 1 root root 0 11月  6 04:20 test
[root@kylin-xu tmp]# chown xu.xu test 
[root@kylin-xu tmp]# ll
总用量 0
-rw-r--r-- 1 xu xu 0 11月  6 04:20 test
  • 案例3.递归修改文件的属组和属组
[root@kylin-xu tmp]# chown xu.xu a -R 
[root@kylin-xu tmp]# ll a
总用量 0
drwxr-xr-x 3 xu xu 60 11月  6 04:21 b
[root@kylin-xu tmp]# ll a/b/
总用量 0
drwxr-xr-x 2 xu xu 40 11月  6 04:21 c
[root@kylin-xu tmp]# ll a/b/c -d
drwxr-xr-x 2 xu xu 40 11月  6 04:21 a/b/c
[root@kylin-xu tmp]

(2)、chmod

chmod    # 修改文件权限
语法结构
	     chmod +w file # 增加w权限
	     chmod -w file 
  • 案例1.给test 属主位增加x权限
[root@kylin-xu tmp]# chmod  u+x test 
[root@kylin-xu tmp]# ll
总用量 0
-rwxr--r-- 1 xu xu  0 11月  6 04:20 test
  • 案例2.给test 属组位减去w权限
chmod u-x test
  • 案例3.给test属主设置rwx权限
chmod u=rwx test
  • 给test other 设置rw权限
chmod o=rw test
  • 案例4.同时去掉陌生人的wx权限
chmod o-wx test
  • 案例5.使用等号来重新赋值权限位
chmod o=r test
  • 案例6.修改所有位置增加x权限
chmod +x test
  • 案例7.所有位置减去x权限
chmod -x test
  • 案例8.对所有位置增加w权限
chmod ugo+w test
  • 案例9.对所有的位置减少w权限使用a
chmod a-w test

小结: 使用ugo方式来对文件进行授权
chmod u+w # 授权属主位w权限
chmod u-w # 属主位减少w权限
chmod ug+x # 属主和属组增加x权限
chmod ugo+x # 所有位增加x权限
chmod +x 所有位增加x权限
chmod a+x 所有位增加x权限
chmod g=w 去掉原来的权限,重新增加w权限

使用数字的方式授权

r # 4
w # 2
x # 1
语法结构
		chmod 644 file  
  • 案例1.授权文件权限为rw-r-xr-- 权限
[xu@kylin-xu day14]$ chmod 654 1.txt 
[xu@kylin-xu day14]$ ll 1.txt 
-rw-r-xr-- 1 xu xu 0 11月  6 04:04 1.txt
  • 案例2.授权文件权限为rw------- 权限
[xu@kylin-xu day14]$ chmod 600 1.txt 
[xu@kylin-xu day14]$ ll 1.txt 
-rw------- 1 xu xu 0 11月  6 04:04 1.txt
  • 案例3.授权文件权限为 rw-r--r-- 权限
[xu@kylin-xu day14]$ chmod 644 1.txt 
[xu@kylin-xu day14]$ ll
总用量 0
-rw-r--r-- 1 xu xu 0 11月  6 04:04 1.txt
  • 案例4.授权文件权限为---------权限
chmod 000 1.txt
# 经常使用到的授权数字
644 rw-r--r--
755 rwxr-xr-x
600 rw-------
chmod # 递归授权文件的属主属组为600权限
参数选项:
	  -R  # 递归授权 比较危险 别用来修改目录

4、rwx对于文件的作用

r对于文件的作用:
1、可读
2、不可写  但是可以强制写入vim
3、不能执行
4、不能删除,删除是由目录的权限控制的
[xu@kylin-xu day14]$ echo pwd > 1.txt 
[xu@kylin-xu day14]$ chmod u=r 1.txt 
[xu@kylin-xu day14]$ ll 1.txt 
-r--r--r-- 1 xu xu 4 11月  6 04:59 1.txt
[xu@kylin-xu day14]$ cat 1.txt 
pwd
[xu@kylin-xu day14]$ echo aaa > 1.txt 
-bash: 1.txt: 权限不够
[xu@kylin-xu day14]$ ./1.txt
-bash: ./1.txt: 权限不够
[xu@kylin-xu day14]$ echo pwd > 1.txt 
-bash: 1.txt: 权限不够
w对于文件的作用:
[xu@kylin-xu day14]$ chmod u=w 2.txt 
[xu@kylin-xu day14]$ ll 2.txt 
--w-r--r-- 1 xu xu 0 11月  6 04:55 2.txt
[xu@kylin-xu day14]$ cat 2.txt 
cat: 2.txt: 权限不够
[xu@kylin-xu day14]$ echo pwd > 2.txt 
[xu@kylin-xu day14]$ 

1.不能查看文件内容
2.不能使用vim方式写入 只能使用echo 追加内容
3.不能执行
4、只有一个w的话,理论上是可以的,但是在实际生产中意义不大
x对于文件的作用:
[xu@kylin-xu day14]$ chmod u=x  1.txt 
[xu@kylin-xu day14]$ ll
总用量 8
---xr--r-- 1 xu xu 4 11月  6 04:59 1.txt


# 文件只有一个x啥都不能干
[xu@kylin-xu day14]$ ./1.txt 
bash: ./1.txt: 权限不够
# 想让文件可以执行必须有r权限
xu@kylin-xu day14]$ chmod u=rx 1.txt 
[xu@kylin-xu day14]$ ll 1.txt 
-r-xr--r-- 1 xu xu 4 11月  6 04:59 1.txt
[xu@kylin-xu day14]$ ./1.txt 
/home/xu/day14

总结:
1. 1个r对于文件有作用,只读
2. rw对于文件是最高权限可读写
3. r和x对于文件是有执行的权限
4. rwx脚本拥有最高的权限

对于文件来讲最高权限为666 所有位置都可读写

5、rwx对目录的作用

[xu@kylin-xu ~]$ ll -d day14/
drwxr-xr-x 2 xu xu 45 11月  6 04:55 day14/

1.目录只有r权限 啥都不能干。由于目录的block存放的是该目录下的文件名,因此使用 ll 查看也就只能看到文件名
[xu@kylin-xu ~]$ chmod u=r day14/
[xu@kylin-xu ~]$ ll day14/ -d
dr--r-xr-x 2 xu xu 45 11月  6 04:55 day14/
[xu@kylin-xu ~]$ ll day14/ 
ls: 无法访问 'day14/1.txt': 权限不够
ls: 无法访问 'day14/2.txt': 权限不够
ls: 无法访问 'day14/3.txt': 权限不够
总用量 0
-????????? ? ? ? ?             ? 1.txt
-????????? ? ? ? ?             ? 2.txt
-????????? ? ? ? ?             ? 3.txt


2.目录只有w权限 啥都不能干。

3.x对于目录的作用:
1.x控制是否可以cd到目录下
2.没有其他任何权限
[root@oldboyedu oldboy]

目录权限的常用组合方式:
1.r-x组合作用 可以进入到目录可以查看目录下所有的文件信息 能不能看文件内容具体看文件的权限
2.r-x组合不能在目录下删除 创建 改名等动作
3.rwx组合目录的最高权限可以进入可以增删改查

为什么会出现权限拒绝?

[xu@kylin-xu ~]$ cat /etc/shadow
cat: /etc/shadow: 
# 由于shadow文件本身对于other来说没有r权限

#passwd的其他位置没有w权限 必须rw
[xu@kylin-xu ~]$ echo 11111>> /etc/passwd
-bash: /etc/passwd: 权限不够
[xu@kylin-xu ~]$ ll /etc/passwd
-rw-r--r-- 1 root root 2031 11月  5 16:00 /etc/passwd

6、umask

umask作用决定默认创建文件和目录的权限 # 了解umask值 不作为重点
默认文件的权限: 644
默认目录的权限: 755

[root@kylin-xu ~]# umask 
0022

文件默认权限: 是由文件的最高权限666减去umask值得到的
 666
-022
=644
目录默认权限: 是由目录的最高权限777减去umask默认的值
 777
-022
=755
  • 案例1.umask值修改为044
[root@kylin-xu ~]# umask 044
[root@kylin-xu ~]# umask 
0044
[root@kylin-xu ~]# ll a.txt 
-rw--w--w- 1 root root 0 11月  6 06:41 a.txt
[root@kylin-xu ~]# mkdir aaaa
[root@kylin-xu ~]
  • 案例2.umask值修改为032
# 如果umask存在奇数位,文件相减后+1,目录不需要
[root@kylin-xu ~]# umask 032
[root@kylin-xu ~]# umask 
0032
[root@kylin-xu ~]# touch abc.txt
[root@kylin-xu ~]

7、隐藏权限位

# 查看隐藏权限位
[root@kylin-xu ~]# touch test.txt
[root@kylin-xu ~]# lsattr test.txt 
-------------------- test.txt

#增加a隐藏权限 作用只能追加内容到文件中
[root@kylin-xu ~]

我们可以理解为 隐藏权限是限制root用户的

8、特殊权限位

suid set uid   4# 作用 在用户执行命令的时候相当于属主的权限去执行。
需要我们给属主的位置增加s权限。任何人使用命令的时候相当于命令的属主的权限。
sgid         2  # 针对目录设置,对于设置了sgid的权限的目录,在该目录中创建的内容自动继承上一级目录的属组
sticky粘滞位 1   # 主要是针对共享目录设置的,设置了sticky的目录中,谁创建的内容,只能被谁删除。
suid

[root@kylin-xu ~]# ll /usr/bin/passwd 
-rwsr-xr-x 1 root root 30800  4月 20  2022 /usr/bin/passwd
sgid
[root@kylin-xu ~]# chown xu.xu test
[root@kylin-xu ~]# touch test/qqq
[root@kylin-xu ~]# ll test/qqq 
-rw-r--r-- 1 root xu 0 11月  6 07:14 test/qqq
sticky

[root@kylin-xu ~]# mkdir /nfs 
[root@kylin-xu ~]# chmod 777 /nfs
[root@kylin-xu ~]# chmod o+t /nfs
[root@kylin-xu ~]# ll -d /nfs
drwxrwxrwt 2 root root 35 11月  6 07:16 /nfs
[root@kylin-xu ~]

【5】、软硬连接

1、软链接

  • 软链接类似于windows快捷方式,存放源文件的位置.

    • 符号链接(symbolic link或symlink),软链接
  • 创建软链接 给/etc/sysconfig/network-scripts/ifcfg-ens33 创建软链接/opt/ifcfg-ens33

    [root@kylin-xu ~]# ln -s /etc/sysconfig/network-scripts/ifcfg-ens33  /opt/ifcfg-ens33
    [root@kylin-xu ~]# ll /opt/ifcfg-ens33 
    lrwxrwxrwx 1 root root 42 11月  5 08:02 /opt/ifcfg-ens33 -> /etc/sysconfig/network-scripts/ifcfg-ens33
    
    
    # ln -s 源文件  目标文件
    
  • 删除软连接

    [root@kylin-xu opt]# rm -f ifcfg-ens33 
    # 不会影响到源文件
    
  • 删除源文件

    [root@kylin-xu day06]# touch 123
    [root@kylin-xu day06]# ln -s 123 456
    [root@kylin-xu day06]# ll
    总用量 1636
    -rw-r--r-- 1 root root       0 11月  5 08:05 123
    lrwxrwxrwx 1 root root       3 11月  5 08:05 456 -> 123
    [root@kylin-xu day06]# rm -f 123
    

    image-20241105201459028

2、硬链接

  • 在同一个分区中,inode号码相同的互为硬链接。

    ln 源文件  目标文件
    

3、区别

  • 含义:
    • 软链接源文件位置
    • 硬链接在同一个分区中inode号码相同
  • 特点:
    • 最长用的是软链接,对文件,目录创建。
    • 硬链接只能对文件创建,不能对目录。
  • 删除

【6】、文件时间

时间 
修改时间 mtime 修改时间,最常用的。
访问时间 atime 看一次文件,时间就会改变。限制mtime有关。
属性改变时间 ctime 属性信息修改这个时间就变化。
创建时间 btime 文件创建时间。
[root@kylin-xu day06]# stat list.txt 
  文件:“list.txt”
  大小:20              块:8          IO 块:4096   普通文件
设备:fd00h/64768d      Inode:102684889   硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2024-11-05 00:06:10.939806497 -0300
最近更改:2024-11-05 00:06:08.442785880 -0300
最近改动:2024-11-05 00:06:08.442785880 -0300
创建时间:-

__EOF__