文件权限

一、文档隐藏属性(lsattr, chattr)

1.chatter设置文件隐藏属性

+:增加某一个特殊参数。

-:移除某个特殊参数。

=:设定后面所接的参数

常用属性值

i:增加该属性,文件将不能删除,重命名,设定链接也无法写入或增加内容。

a:当设定a属性之后,该文件只能增加数据,不能删除。只有root可以设定该属性。

 //+i属性后无法增加内容,改名,新建硬链接,删除
 [root@localhost demo]# cd /tmp/
 [root@localhost tmp]# touch attr_demo
 [root@localhost tmp]# chattr +i attr_demo 
  
 [root@localhost tmp]# echo 1 > attr_demo 
 -bash: attr_demo: Permission denied
  
 [root@localhost tmp]# mv attr_demo attr_demo_01
 mv: cannot move ‘attr_demo’ to ‘attr_demo_01’: Operation not permitted
  
 [root@localhost tmp]# ln attr_demo attr_demo_hard
 ln: failed to create hard link ‘attr_demo_hard’ => ‘attr_demo’: Operation not permitted
  
 [root@localhost tmp]# rm attr_demo 
 rm: remove regular empty file ‘attr_demo’? y
 rm: cannot remove ‘attr_demo’: Operation not permitted
 [root@localhost tmp]# chattr -i attr_demo 
 [root@localhost tmp]# rm -f attr_demo 
  
 //+a属性后只能增加,不能删除数据
 [root@localhost tmp]# touch his.log
 [root@localhost tmp]# chattr +a his.log 
  
 [root@localhost tmp]# rm -f his.log 
 rm: cannot remove ‘his.log’: Operation not permitted
  
 [root@localhost tmp]# echo 111 > his.log 
 -bash: his.log: Operation not permitted
 
 [root@localhost tmp]# echo 111 >> his.log 
 [root@localhost tmp]# cat his.log 
 111

2.lsattr 显示文件隐藏属性

常用选项

-a:显示隐藏文件的隐藏属性

-R:显示子目录的信息也显示出来

 //显示文件的隐藏属性
 [root@localhost tmp]# lsattr his.log attr_demo
 -----a---------- his.log
 ----i----------- attr_demo
  
 [root@localhost tmp]# lsattr -a dir1/
 ----i----------- dir1/.
 ---------------- dir1/..
 ---------------- dir1/f1
 ---------------- dir1/f2
  
 [root@localhost tmp]# ls -R dir1
 dir1:
 dir2/  f1  f2
  
 dir1/dir2:
 f3
  
 [root@localhost tmp]# lsattr -Rd dir1/
 ----i----------- dir1/

二、特殊权限SETUID、SETGID、Stick_Bit

1.setuid

对可执行文件设置 setuid 权限时,将对运行该文件的进程授予基于文件属主的访问权限。该访问权限不是基于正在运行可执行文件的用户。使用此特殊权限,用户可以访问通常只有属主才可访问的文件和目录。普通用户能够更改密码就是基于/usr/bin/passwd具有setuid权限实现。

setuid权限只针对二进制可执行程序。如程序不具有执行权限,setuid权限标志位将变为S(大写的s)

 [root@localhost tmp]# ls -l /usr/bin/passwd 
 -rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd*
  
 [hjm@localhost ~]$ ls /root/
 ls: cannot open directory /root/: Permission denied
 [root@localhost ~]# chmod u+s /bin/ls
 [root@localhost ~]# ls -l /bin/ls
 -rwsr-xr-x. 1 root root 117656 Nov  5  2016 /bin/ls*
 [hjm@localhost ~]$ /bin/ls /root
 1.txt  anaconda-ks.cfg	demo
  
 [root@localhost ~]# chmod u-s /bin/ls
 [root@localhost ~]# ls -l /bin/ls
 -rwxr-xr-x. 1 root root 117656 Nov  5  2016 /bin/ls*
 [hjm@localhost ~]$ /bin/ls /root
 /bin/ls: cannot open directory /root: Permission denied

2.setgid
Setgid 权限与 setuid 权限类似。可将进程的有效组 ID (group ID, GID) 更改为拥有该文件的组,并基于授予该组的权限对用户授权访问权限。/usr/bin/mail 命令拥有 setgid 权限
将 setgid 权限应用于目录时,该目录中已创建的文件将属于该目录所属于的组。这些文件不属于创建进程所属于的组。在目录中拥有写和执行权限的任何用户都可以在其中创建文件。但是,文件将属于拥有该目录的组,而不是用户所属于的组。

 [hjm@localhost ~]$ ls -ld  music
 drwxrwxr-x. 2 hjm pub 6 May 15 05:35 music
  
 [hjm@localhost ~]$ groups hjm
 hjm : hjm pub
  
 [root@localhost demo]# chmod g+s /home/hjm/music
 [root@localhost demo]# ls -ld /home/hjm/music
 drwxrwsr-x. 2 hjm pub 6 May 15 05:35 /home/hjm/music/
  
 [hjm@localhost ~]$ cd music/
 [hjm@localhost music]$ touch f1 f2
 [hjm@localhost music]$ ls -l
 total 0
 -rw-rw-r--. 1 hjm pub 0 May 15 05:38 f1
 -rw-rw-r--. 1 hjm pub 0 May 15 05:38 f2

3.sticky
粘滞位是保护目录中文件的权限位。如果对目录设置了 sticky 位,则只有文件属主、目录属主或特权用户才可以删除文件。root 用户和主管理员角色即是特权用户。sticky 位禁止用户从公共目录(如 /tmp)中删除其他用户的文件

 [hjm@localhost ~]$ ls -ld /tmp/
 drwxrwxrwt. 11 root root 4096 May 15 05:24 /tmp/
  
 [hjm@localhost tmp]$ ls -l f*
 -rw-rw-r--. 1 kennminn kennminn 0 May 15 05:41 f1
 -rw-rw-r--. 1 kennminn kennminn 0 May 15 05:41 f2
 -rw-rw-r--. 1 hjm      hjm      0 May 15 05:41 f3
 -rw-rw-r--. 1 hjm      hjm      0 May 15 05:41 f4
 [hjm@localhost tmp]$ rm -f f1
 rm: cannot remove ‘f1’: Operation not permitted

三、硬链接和软链接

1.硬链接

文件都有文件名和数据,在linux上分成两个部分:用户数据(user data)与元数据(metadata)。用户数据即文件数据块,数据块是记录文件真实内容的地方,而元数据则是文件的附加属性、如文件大小,创建时间、所有者等信息。在Linux中,元数据中的inode号(inode是元数据的一部分但其并不包含文件名,inode号即索引节点号)才是文件的唯一标识而非文件名。程序通过文件名获取文件内容的过程如下:

xVcVS.jpg

若一个inode对应多个文件名,则称这些文件为硬链接。。换言之,硬链接就是同一个文件使用了多个别名 。

由于硬链接是有着相同 inode 号仅文件名不同的文件,因此硬链接存在以下几点特性:

  • 文件有相同的 inode 及 data block;
  • 只能对已存在的文件进行创建;
  • 不能交叉文件系统进行硬链接的创建(即不能跨分区);
  • 不能对目录进行创建,只可对文件创建;
  • 删除一个硬链接文件并不影响其他有相同 inode 号的文件。
  • 在linux系统中,删除静态文件(当前没有进程调用)(目录也认为是文件)的条件是与之相关的所有硬件文件均被删除。

硬链接创建方式:ln 源文件 目标文件

[root@bluequark demo]# ls
2.txt  3.txt  fd1  fd2  fd3
//只能对已存在的文件创建硬连接
[root@bluequark demo]# ln 1.txt 1.txt.hard
ln: failed to access ‘1.txt’: No such file or directory
//硬链接具有相同的inode
[root@bluequark demo]# head -n 5 /var/log/messages > 1.txt 
[root@bluequark demo]# ln 1.txt 1.txt.hard
[root@bluequark demo]# stat 1.txt 1.txt.hard
  File: ‘1.txt’
  Size: 421       	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 776364      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-05-15 22:49:09.125026602 +0800
Modify: 2018-05-15 22:49:09.125026602 +0800
Change: 2018-05-15 22:49:22.973986832 +0800
 Birth: -
  File: ‘1.txt.hard’
  Size: 421       	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 776364      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-05-15 22:49:09.125026602 +0800
Modify: 2018-05-15 22:49:09.125026602 +0800
Change: 2018-05-15 22:49:22.973986832 +0800
 Birth: -
//所有硬链接移除才会删除
[root@bluequark demo]# rm -f 1.txt
[root@bluequark demo]# ls -li 1.txt.hard 
776364 -rw-r--r-- 1 root root 421 May 15 22:20 1.txt.hard
[root@bluequark demo]# rm -f 1.txt.hard 
[root@bluequark demo]# ls
2.txt  3.txt  fd1  fd2  fd3
//不能对目录创建硬链接
[root@bluequark demo]# ln /etc dir_link
ln: ‘/etc’: hard link not allowed for directory
//不能跨分区
[root@bluequark demo]# ln 2.txt /boot/2.txt.hard
ln: failed to create hard link ‘/boot/2.txt.hard’ => ‘2.txt’: Invalid cross-device link

2.软链接

也称为符号链接,即“symbolic links/soft links”,是一个通过名称指向另一个不同文件的特殊的文件类型,软链接就是一个普通文件,只是数据块内容有点特殊。软链接有着自己的inode号以及用户数据块,因此软链接的创建与使用没有类似硬链接的诸多限制。

软链接的访问过程如下

xVkyR.jpg

软链接的特性

  • 软链接有自己的文件属性及权限等;

  • 可对不存在的文件或目录创建软链接;

  • 软链接可交叉文件系统;

  • 软链接可对文件或目录创建;

  • 创建软链接时,链接计数 i_nlink 不会增加;

  • 删除软链接并不影响被指向的文件,但若被指向的原文件被删除,则相关软连接被称为死链接(即 dangling link,若被指向路径文件被重新创建,死链接可恢复为正常的软链接)。

[root@bluequark demo]# ls -li
total 0
//对不存的文件创建软链接
[root@bluequark demo]# ln -s old.file soft.link
[root@bluequark demo]# ls -liF
total 0
776364 lrwxrwxrwx 1 root root 8 May 15 23:02 soft.link -> old.file
//由于被指向的文件不存,此时的软链接soft.link就是死链接
[root@bluequark demo]# cat soft.link 
cat: soft.link: No such file or directory
//创建被指向的文件old.file,soft.link恢复成正常的链接
[root@bluequark demo]# echo "This is an description" >> old.file
[root@bluequark demo]# cat soft.link 
This is an description
//对不存在的目录创建软链接
[root@bluequark demo]# ln -s old.dir soft.link.dir
[root@bluequark demo]# mkdir -p old.dir/test
[root@bluequark demo]# tree . -F --inodes
.
├── [ 776374]  old.dir/
│   └── [8805948]  test/
├── [ 776372]  old.file
├── [ 776372]  soft.link -> old.file
└── [ 776374]  soft.link.dir -> old.dir/

3 directories, 2 files

注意:软链接创建时原文件的路径指向使用绝对路径较好。使用相对路径创建的软链接被移动后该软链接文件将成为一个死链接

四、Find命令用法

通过搜索目录树查找文件

为了运行find,需要指定3件事情(按下序顺序):目录路径、测试和动作。,一般语法

find path … test… action

PATH((路径):find所做的第一件事就是查看每个路径,检查这些路径所表示的整个目录树,包括所有文件的列表。路径可以是一个,也可以是多个。可以使用相对路径,也可以使用绝对路径。

find /usr/bin

find /

find .

find ~

find ~kennminn

test(测试)

find程序常见测试

文件名 解释
-name pattern 包含pattern的文件名
-iname pattern 包含pattern的文件名(不区分大小写)
文件特征
-type [df.....] 文件类型:d=目录,f=普通文件
-perm mode 设置为mode的文件权限
-user userid 属主为userid
-group groupid 属组为groupid
-size [-+]n[cbkMG] 大小为n[字符(字节)、块、千字节、兆字节、吉字节]
-empty 空文件(大小=0)
访问时间、修改时间
-amin [-+]n n分钟前的访问
-anewer file file文件之后访问
-atime [-+]n n天前访问
-cmin [-+]n n分钟前状态改变
-cnewer file file文件之后状态改变
-ctime [-+]n n天前状态改变
-mmin [-+]n n分钟之前修改
-mtime [-+]n n天之前修改
-newer file file文件之后修改
[root@bluequark ~]# find /etc -type f -print | head -n 10
/etc/fstab
/etc/crypttab
/etc/resolv.conf
/etc/grub.d/00_header
/etc/grub.d/01_users
/etc/grub.d/10_linux
/etc/grub.d/20_linux_xen
/etc/grub.d/20_ppc_terminfo
/etc/grub.d/30_os-prober
/etc/grub.d/40_custom

[root@bluequark ~]# find /etc -type d -print | head -n 10
/etc
/etc/grub.d
/etc/pki
/etc/pki/rpm-gpg
/etc/pki/ca-trust
/etc/pki/ca-trust/extracted
/etc/pki/ca-trust/extracted/java
/etc/pki/ca-trust/extracted/openssl
/etc/pki/ca-trust/extracted/pem
/etc/pki/ca-trust/source

//通配需要引用
[root@bluequark ~]# find /usr -type f -name '*.c' | head -n 5
/usr/lib/firmware/isci/create_fw.c
/usr/share/doc/mpfr-3.1.1/mpfr/examples/divworst.c
/usr/share/doc/mpfr-3.1.1/mpfr/examples/rndo-add.c
/usr/share/doc/mpfr-3.1.1/mpfr/examples/sample.c
/usr/share/doc/mpfr-3.1.1/mpfr/examples/version.c

[root@bluequark ~]# find ~ -type d -perm 700 
/root/.ssh

[root@bluequark ~]# find ~ -type f -user hjm 
/root/dir2/passwd

[root@bluequark ~]# find ~ -type f -size 1b -print
/root/.bash_logout
/root/.bash_profile
/root/.cshrc
/root/.tcshrc
/root/.bashrc
/root/.lesshst
/root/demo/old.file

[root@bluequark ~]# find ~ -type f -size 100c -print
/root/.cshrc

[root@bluequark ~]# find ~ -type f -size -10k -print
/root/.bash_logout
/root/.bash_profile
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.bash_history
/root/.ssh/authorized_keys
/root/.bashrc
/root/.lesshst
/root/.viminfo
/root/pxe_config
/root/dir2/passwd
/root/bbbb
/root/demo/old.file

[root@bluequark ~]# find ~ -atime +180  -print
/root/.bash_logout
/root/.cshrc
/root/.tcshrc

使用!对运行符求反,必须在!号的两侧各留一个空格。必须引用!号

[root@bluequark ~]# find /usr -type f -name '*.jpg'
/usr/share/backgrounds/morning.jpg
/usr/share/backgrounds/night.jpg
/usr/share/backgrounds/day.jpg
/usr/share/backgrounds/default.jpg
//!号前后留空格,且用\引用,单引号引用也可以
[root@bluequark ~]# find /usr -type f \! -name '*.jpg' | head -n 5
/usr/bin/sdiff
/usr/bin/catchsegv
/usr/bin/diff3
/usr/bin/gencat
/usr/bin/which

用错误输出重定向处理文件权限错误信息。

[hjm@bluequark ~]$ find / -type d -name bin -print | head -n 5
find: ‘/boot/grub2’: Permission denied
find: ‘/proc/tty/driver’: Permission denied
find: ‘/proc/1/task/1/fd’: Permission denied
find: ‘/proc/1/task/1/fdinfo’: Permission denied
find: ‘/proc/1/task/1/ns’: Permission denied

[hjm@bluequark ~]$ find / -type d -name bin -print 2> /dev/null
/usr/bin
/usr/lib/debug/usr/bin
/usr/share/doc/perl-Test-Harness-3.28/examples/bin
/usr/share/locale/bin
/usr/local/bin

动作

动作 说明
-print 将路径写入到标准输出
-fprint file 同-print,将输出写到file
-ls 显示长上目录列表
-fls file 同-ls,将输出写到file中
-delete 删除文件
-exec command {} ; 执行command,{}指示匹配的文件名
-ok command {} ; 同-exec, 但是在运行command之前进行确认
[hjm@bluequark ~]$ find . -name important -print
./important

[hjm@bluequark ~]$ find . -name important 
./important

[hjm@bluequark ~]$ find / -type f -name '*.jpg' -print 2> /dev/null
/usr/share/backgrounds/morning.jpg
/usr/share/backgrounds/night.jpg
/usr/share/backgrounds/day.jpg
/usr/share/backgrounds/default.jpg

[hjm@bluequark ~]$ find / -type f -name '*.jpg' -fprint photolist 2> /dev/null
[hjm@bluequark ~]$ cat photolist 
/usr/share/backgrounds/morning.jpg
/usr/share/backgrounds/night.jpg
/usr/share/backgrounds/day.jpg
/usr/share/backgrounds/default.jpg

[hjm@bluequark ~]$ ls
f1  f2  f3  f4  fd1  fd2  fd3  fd4  important  photolist
[hjm@bluequark ~]$ find . -type f -name f2 -delete
[hjm@bluequark ~]$ ls
f1  f3  f4  fd1  fd2  fd3  fd4  important  photolist

[hjm@bluequark ~]$ find ~ -type d -exec echo {} \;
/home/hjm
/home/hjm/fd1
/home/hjm/fd2
/home/hjm/fd3
/home/hjm/fd4

[root@bluequark ~]# find / -type f -name '*.jpg' -exec cp {} ./ \;
[root@bluequark ~]# ls
anaconda-ks.cfg  day.jpg      demo  dir2  morning.jpg  night.jpg  pxe_config
bbbb             default.jpg  dir1  fd1   named.pipe   path_demo

五、 Linux下文件扩展名

一个Linux文件能否被执行,与文件名扩展名没有关系。而是与它的权限有关,这点跟windows上是不一样的,
win下的可执行文件扩展名通常是.com、.exe、.bat等,而在Linux下,只要文件具有x就代表这个文件具有可执行权限。
在Linux中虽然文件后缀扩展名并没有什么实际的意义,但一个约定俗成文件扩展名有利于我们了解文件的类型。
几种常用的扩展名:
.sh: 脚本或批处理文件,因为批处理文件使用shell写的,所以扩展名为sh
.Z、.tar、.tar.gz、.zip、.tgz: 经过打包的压缩文件。这是因为压缩的文件分别是gunzip、tar等,由于不同的压缩软件,
而取其相关的扩展名。
.c c源程序文件
.py python程序文件
.html、.php: 网页相关文件

六、与Windows文件共享

推荐winscp,绿色软件。

xYZTr.png

xYhJY.png

原文地址:https://www.cnblogs.com/minn/p/9043880.html