Linux文件隐藏权限chattr、lsattr+特殊权限set_uid、set_gid、stick_bit+硬、软链接文件+find+文件后缀+Linux与windows文件互传

  • 隐藏权限lsattr_chattr

设置隐藏权限(或附加权限)命令chattr

[root@zyshanlinux-01 ~]# ls -l
总用量 4
-rw-r--r--. 1 root root    0 5月  12 13:49 001.txt
-rw-------. 1 root root 1418 5月   7 04:27 anaconda-ks.cfg
[root@zyshanlinux-01 ~]# chattr +i 001.txt
[root@zyshanlinux-01 ~]# ls -l
总用量 4
-rw-r--r--. 1 root root    0 5月  12 13:49 001.txt
-rw-------. 1 root root 1418 5月   7 04:27 anaconda-ks.cfg

ls命令看不出来什么不同。

但是编辑模式里面无法保存更改。

:qw!强制保存也不行,只能:q!强制退出了;

重定向,把/etc/passwd的前2行内容写到001.txt.

[root@zyshanlinux-01 ~]# head -n2 /etc/passwd > 001.txt

root用户是能够写的,但写不了,应该想到有隐藏权限;

[root@zyshanlinux-01 ~]# lsattr 001.txt
----i----------- 001.txt
[root@zyshanlinux-01 ~]# touch 002.txt
[root@zyshanlinux-01 ~]# lsattr 002.txt
---------------- 002.txt

改名也不行;

[root@zyshanlinux-01 ~]# mv 001.txt 003.txt
mv: 无法将"001.txt" 移动至"003.txt": 不允许的操作

删除也不行;

[root@zyshanlinux-01 ~]# rm -f 001.txt
rm: 无法删除"001.txt": 不允许的操作

修改时间都不行;

[root@zyshanlinux-01 ~]# touch 001.txt
touch: 无法创建"001.txt": 权限不够


当你需要某个文件谁也不能改不能动不能追加时就需要chattr +i 1.txt取消这个i权限就-i就可以了。

[root@zyshanlinux-01 ~]# chattr -i 001.txt
[root@zyshanlinux-01 ~]# lsattr 001.txt
---------------- 001.txt
[root@zyshanlinux-01 ~]# mv 001.txt 003.txt
[root@zyshanlinux-01 ~]# ls
002.txt  003.txt  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# 

a权限仅能追加更改时间,-a可以把这权限去掉,以上是文件的。

[root@zyshanlinux-01 ~]# chattr +a 003.txt
[root@zyshanlinux-01 ~]# lsattr 003.txt
-----a---------- 003.txt
[root@zyshanlinux-01 ~]# rm -f 003.txt
rm: 无法删除"003.txt": 不允许的操作
[root@zyshanlinux-01 ~]# head -n2 /etc/passwd > 003.txt
-bash: 003.txt: 不允许的操作
[root@zyshanlinux-01 ~]# head -n2 /etc/passwd >> 003.txt  #可以追加
[root@zyshanlinux-01 ~]# cat 003.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@zyshanlinux-01 ~]# mv 003.txt 001.txt  #改名不行
mv: 无法将"003.txt" 移动至"001.txt": 不允许的操作
[root@zyshanlinux-01 ~]# touch 003.txt  #修改时间可以
[root@zyshanlinux-01 ~]# chattr -a 003.txt
[root@zyshanlinux-01 ~]# lsattr 003.txt
---------------- 003.txt
[root@zyshanlinux-01 ~]# 

当是目录的时候权限也相同,不同的是不论是i还是a,目录里面的文件内容都是可以追加和修改的,但不能新建新文件。目录的权限和文件关系不大。

lsattr查看隐藏权限,lsattr -R 111可以查看该目录下所有的书目录附加权限。

[root@zyshanlinux-01 ~]# lsattr -R 009
---------------- 009/008

009/008:

---------------- 009/008/005.txt
[root@zyshanlinux-01 ~]# 

lsattr -a 111可以查看隐藏的目录,-d只查看目录本身。

Tip:隐藏权限用ls-l命令是看不到的。

 vi的时候,会保存一个临时文件以~号为后缀,编辑保存退出,这个临时文件覆盖原文件,然后这个临时文件才会被删除;加了特殊权限保存不了,就只能以临时文件的形式保存。




  • 特殊权限set_uid

让普通用户临时拥有root权限能更改密码,特殊权限set_uid即普通用户临时拥有该命令所有者(root)的权限,rwxr-x---=>rwsr-x---新的权限s

给一个文件设置set_uid前提这个文件是可执行的二进制文件,eg:lspasswd都是二进制文件,而1.txt则不是。怎么用呢,

chmod u+s /usr/bin/ls,去除这个命令就u-s就可以了;

 

chmod u+s /usr/bin/ls;类似的命令chmod 4755 /usr/bin/ls

 

另一种用法chomd u=rws /usr/bin/ls这样赋予的权限是S(没有x)权限,如何获得s(包括x)权限呢,chomd u+x /usr/bin/ls就可以了等同于u+s

目录也可以用这个命令但没什么意义。

 

set_uid更改了s权限后,切换到普通用户,虽然没有x权限,但也可以用ls打开/root/目录。

 


可执行的二进制文件的判断;

用命令file;

文本文件;(包含text等)

[root@zyshanlinux-01 ~]# file /usr/bin/yum
/usr/bin/yum: Python script, ASCII text executable

可执行的二进制文件;(包含了ELF,x86-64等)

[root@zyshanlinux-01 ~]# file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=3d705971a4c4544545cb78fd890d27bf792af6d4, stripped
[root@zyshanlinux-01 ~]# 



  • 特殊权限set_gid

set_gid 特殊权限,与set_uid类似,命令:chmod g+s 234chmod g-s 234,但是它是作用在组权限位上面的,

set_uid的文件是红色标底,set_gid的文件是黄色标底。当set_gid作用在文件上的时候,普通用户临时拥有所属组的身份。

给所属组添加s权限:chmod g+s /usr/bin/ls


去除所属组的s权限:chmod g-s /usr/bin/ls


chmod g+s /usr/bin/ls;类似的命令chmod 2755 /usr/bin/ls;多了w


更改用户组命令:chown :user1 234;在不动所有者所属组的权限下创建的子文件子目录都是属于root所有者的和root所属

组的,但是,当set_gid作用在目录上的时候,更改了所属组,那么你之后所创建的子目录子文件都会归属的新的所属组下。

[root@zyshanlinux-01 ~]# chmod g+s 009  #更改009目录所属组的权限
[root@zyshanlinux-01 ~]# ls -lid /root/009
50717384 drwxr-sr-x. 3 root root 32 May 15 11:22 /root/009
[root@zyshanlinux-01 ~]#
[root@zyshanlinux-01 ~]# chown :test 009  #更改所属组为test
[root@zyshanlinux-01 ~]#
[root@zyshanlinux-01 ~]# touch 009/test01  #目录009下创建test01文件
[root@zyshanlinux-01 ~]# ls -li 009/
total 0
50717377 -rw-r--r--. 2 root root  0 May 15 09:53 004.txt
16784307 drwxr-xr-x. 2 root root 21 May 15 10:10 008
50717390 -rw-r--r--. 1 root test  0 May 15 14:12 test01  #该文件的组变了
[root@zyshanlinux-01 ~]#
[root@zyshanlinux-01 ~]# mkdir 009/010  #在目录009下创建010目录
[root@zyshanlinux-01 ~]# ls -li 009/
total 0
50717377 -rw-r--r--. 2 root root  0 May 15 09:53 004.txt
16784307 drwxr-xr-x. 2 root root 21 May 15 10:10 008
16784323 drwxr-sr-x. 2 root test 6 May 15 14:15 010  #目录010的组也变
50717390 -rw-r--r--. 1 root test  0 May 15 14:12 test01

拓展:

[root@zyshanlinux-01 ~]# groupadd test  #创建test用户组
[root@zyshanlinux-01 ~]# chown :text 009  #更改目录的所属组
[root@zyshanlinux-01 ~]#
[root@zyshanlinux-01 ~]# ls -lid /root/009  #查看,更改成功
50717384 drwxr-sr-x. 3 root test 32 May 15 11:22 /root/009
[root@zyshanlinux-01 ~]#
[root@zyshanlinux-01 ~]# useradd user1  #创建user1普通用户
[root@zyshanlinux-01 ~]# passwd user1  #设置user1密码
Changing password for user user1.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.  #设置成功
[root@zyshanlinux-01 ~]# 
[root@zyshanlinux-01 ~]# gpasswd -a user1 test  #把user1添加到test组
Adding user user1 to group test
[root@zyshanlinux-01 ~]# gpasswd -d user1 test  #把user1从test组移除
Removing user user1 from group test
[root@zyshanlinux-01 ~]# 



  • 特殊权限stick_bit

/tmp/目录权限drwxrwxrwt,这个t权限就是特殊权限stick_bit(也叫防删除位),防止在该目录下创建文件者之外的用户删除该文件(root用户除外)。


Other(其他用户)增加t防删除位。

chmod o+t /root/003.txt

chmod o+x /root/003.txt

去除t:

chmod o-t /root/003.txt

chmod o-x /root/003.txt

chmod 1755 /root/003.txt

文件(目录)能否删除是看它所在的目录的权限而不是文件本身的权限,如果文件没有写的权限而文件所在的目录有写的权限,那么该文件就能够被删除,因为创建的文件是由包含它的目录决定的。

改变当前用户命令:su - user1

  • 软链接文件

软连接命令:ln -s /tmp/yum.log  /root/111/yum.log,左边是源文件(体积大),右边是软连接文件(体积小)右边是一个软链接文件不可以用“/”;终端显示则是左边是软链接,右边是源文件。除了软连接文件也可以软链接目录。软链接可以跨区做软链接。

[root@zyshanlinux-01 ~]# 
[root@zyshanlinux-01 ~]# ls -l /bin
lrwxrwxrwx. 1 root root 7 May  7 04:19 /bin -> usr/bin
[root@zyshanlinux-01 ~]# ls -l /usr/bin/^C
[root@zyshanlinux-01 ~]# /bin/ls --> /usr/bin/ls^C
[root@zyshanlinux-01 ~]# /bin/ls
002.txt  003.txt  009  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# /usr/bin/ls
002.txt  003.txt  009  anaconda-ks.cfg
[root@zyshanlinux-01 ~]# 

软链接就是在文件中存了另外一个文件的路径或者目录的路径,这个软链接文件的大小与路径的长度有关,越长越大,再大也不过KB,与windows的快捷方式类似。

[root@zyshanlinux-01 009]# ln -s /root/009/004.txt /tmp/007/004.txt
[root@zyshanlinux-01 009]# cd /tmp/007
[root@zyshanlinux-01 007]# ls -l
total 0
lrwxrwxrwx. 1 root root 17 May 15 09:55 004.txt -> /root/009/004.txt
[root@zyshanlinux-01 007]# 
Tip:

相对路径的软连接,即是在当前目录用相对路径命令ln做的软链接,弊端是把包含该软链接的目录拷贝到另外一台主机时,这个相对路径的软链接会失效;绝对路径的软链接则不会发生这种情况,所以推荐用绝对路径的软链接。


 删除软链接文件,应该rm /bin才对,而rm /bin/则是删除真正的目录,因为软链接文件是文件而不是目录。

 

查看当前操作系统版本。

[root@zyshanlinux-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@zyshanlinux-01 ~]# 

查看文件系统的磁盘空间占有情况。

[root@zyshanlinux-01 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        28G  963M   27G   4% /
devtmpfs        907M     0  907M   0% /dev
tmpfs           916M     0  916M   0% /dev/shm
tmpfs           916M  8.6M  908M   1% /run
tmpfs           916M     0  916M   0% /sys/fs/cgroup
/dev/sda1       197M  113M   85M  58% /boot
tmpfs           184M     0  184M   0% /run/user/0
[root@zyshanlinux-01 ~]# 





  • 硬链接文件

硬链接不支持对目录进行硬链接,只支持对文件做。硬链接ln 1.txt 1_heard.txt软链接ln -s 1.txt 1_sorft.txt;硬链接的inode号和原文件一样,软链接的inode号不一样。硬链接和原文件同样大小(它们互为硬链接),软链接相当于快捷方式非常小。硬链接文件白色,软链接是粉蓝色。


硬链接删除原文件无所谓,软链接则不行。硬链接存储的信息是在inode号里的,硬链接互为硬链接(可以n多个硬链接),硬链接不会占用双份的空间,因为都是同一inode号。硬链接不能垮分区做硬链接,分区之间都有独立的inode号,跨区就会导致有相同的inode号。硬链接有多个,但只有一个inode号,所以不能全部删掉,必须留一个。


inode号也叫:节点号




  • find命令

搜索命令

Findwhichwhereis可以搜但不全,有个库需要每天更新才能找到、locate没安装,安装需要yum install -y mlocate,安装后还不能用,库每天凌晨4点自动更新,手动更新updatedb,就可以用了。

Ctrl+lCtrl+d(exit;logout;)Ctrl+cCtrl+u让光标把前面的东西都删掉、有个往后删Ctrl+dCtrl+e把光标挪到后面去、Ctrl+a把光标挪到开始

 

Find

指定文件find /etc/ -name “sshd_config”

查找目录find /etc/ -type d -name “sshd_config”

查找普通文件find /etc/ -type f -name “sshd_config”可以目录可以文件

根据文件类型进行搜索d \f(-)\l\s\c\b\p

[root@zyshanlinux-01 ~]# find /etc/ -name "sshd_config"
/etc/ssh/sshd_config
[root@zyshanlinux-01 ~]# find /root/ -type d -name "009"
/root/009
[root@zyshanlinux-01 ~]# find /etc/ -type f -name "sshd_config"
/etc/ssh/sshd_config
[root@zyshanlinux-01 ~]# 

stat可以查看文件的具体信息。

[root@zyshanlinux-02 ~]# stat anaconda-ks.cfg
  文件:"anaconda-ks.cfg"
  大小:1418      	块:8          IO 块:4096   普通文件
设备:803h/2051d	Inode:33582978    硬链接:1
权限:(0600/-rw-------)  Uid:(    0/    root)   Gid:(    0/    root)
环境:system_u:object_r:admin_home_t:s0
最近访问:2018-05-07 04:27:10.731983089 +0800
最近更改:2018-05-07 04:27:10.733983089 +0800
最近改动:2018-05-07 04:27:10.733983089 +0800
创建时间:-
[root@zyshanlinux-02 ~]# 

-atime访问时间,查看文件Access                                    ==>               Cat 2.txt

-mtime创建改文件内容Modify                                           ==>                Chmod 700 2.txt

-ctime 改权限Change-mtime-ctime一定变)    ==>               Echo “12344” >> 2.txt在文件中追加内容    


find / -type f -mtime -1

查找1天以内的普通文件(-2两天以内,+2两天以前,没有等于两天的)

[root@zyshanlinux-01 ~]# find /root/009 -type f -mtime -1
/root/009/008/005.txt
/root/009/004.txt
/root/009/test01
[root@zyshanlinux-01 ~]# 

find / -type f -mtime -1 -name “*.conf”   

并且,1天内,后缀名

[root@zyshanlinux-01 ~]# find /root/009 -type f -mtime -1 -name "*.txt"
/root/009/008/005.txt
/root/009/004.txt
[root@zyshanlinux-01 ~]#

find / -type f -o -mtime -1 -o -name “*.conf”

或者-o

[root@zyshanlinux-01 ~]# find /root/009 -type f -o -mtime -1 -o -name "*.txt"
/root/009
/root/009/008
/root/009/008/005.txt
/root/009/004.txt
/root/009/test01
/root/009/010
[root@zyshanlinux-01 ~]# 

find / -inum inode

指定inode号的文件

[root@zyshanlinux-01 ~]# find /root -inum 50717384
/root/009
[root@zyshanlinux-01 ~]# 

find /root/ -type f -mmin -120 两小时以内的文件

[root@zyshanlinux-01 ~]# find /root/009 -type f -mmin -120
/root/009/011.txt
[root@zyshanlinux-01 ~]# 

-exec这个是find的一个选项,找出文件并列出详细信息。

find /root/ -type f -mmin -60 -exec ls -l {} \;

{}列出的所有文件列表,\;脱译

[root@zyshanlinux-01 ~]# find /root/009 -type f -mmin -120 -exec ls -l {} \;
-rw-r--r--. 1 root test 0 5月  15 16:24 /root/009/011.txt
[root@zyshanlinux-01 ~]# 

find /root/ -type f -mmin -60 -exec mv {} {}.bak \;

把所有列出的{}改名为 {}.bak结尾的文件。

[root@zyshanlinux-01 ~]# find /root/009 -type f -mmin -120 -exec mv {} {}.bak \;
[root@zyshanlinux-01 ~]# find /root/009 -type f -mmin -120 -exec ls -l {} \;
-rw-r--r--. 1 root test 0 5月  15 16:24 /root/009/011.txt.bak
[root@zyshanlinux-01 ~]# 

find /root/ -size +10k查找大于10k的文件

[root@zyshanlinux-01 ~]# find /root/ -size +10k
/root/.ssh/.authorized_keys.swp
[root@zyshanlinux-01 ~]#

find /root/  -type f -size +10k -exec ls -lh {} \;列出详细信息。

[root@zyshanlinux-01 ~]# find /root/ -type f -size +10k -exec ls -lh {} \;
-rw-r--r--. 1 root root 12K 5月  12 15:35 /root/.ssh/.authorized_keys.swp
[root@zyshanlinux-01 ~]# 

find /root/  -type f -size +10M -esec ls -lh {} \;大于10M,必须大写,k不用

[root@zyshanlinux-01 ~]# find / -type f -size +10M -exec ls -lh {} \;
-rw-------. 1 root root 46M 5月   7 04:25 /boot/initramfs-0-rescue-0a69959c094245fe9bfe066b911152ce.img
-rw-------. 1 root root 18M 5月   7 04:27 /boot/initramfs-3.10.0-693.el7.x86_64.img
-rw-------. 1 root root 16M 5月   7 04:36 /boot/initramfs-3.10.0-693.el7.x86_64kdump.img
-r--------. 1 root root 128T 5月  15 16:40 /proc/kcore
find: ‘/proc/22797/task/22797/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/22797/fdinfo/6’: 没有那个文件或目录
-rw-------. 1 root root 128M 5月  15 09:24 /sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
-rw-------. 1 root root 128M 5月  15 09:24 /sys/devices/pci0000:00/0000:00:0f.0/resource1
-rw-r--r--. 1 root root 76M 5月  15 15:11 /etc/.motd.swp
-rw-r--r--. 1 root root 36M 5月  15 15:36 /var/lib/rpm/Packages
-rw-r--r--. 1 root root 30M 5月   4 04:17 /var/cache/yum/x86_64/7/base/gen/primary_db.sqlite
-rw-r--r--. 1 root root 102M 5月   7 04:20 /usr/lib/locale/locale-archive
-rwxr-xr-x. 1 root root 20M 11月 20 2015 /usr/lib64/libicudata.so.50.1.2
[root@zyshanlinux-01 ~]# 


Tip:Linux系统中,叫目录不叫文件夹。


/etc/motdmessage of today(布告栏信息),每次用户登录时,/etc/motd文件的内容会显示在用户的终端。

系统管理员可以在文件中编辑系统活动消息,

例如:管理员通知用户系统何时进行软件或硬件的升级、何时进行系统维护等。

如果shell支持中文,还可以使用中文,这样看起来更易于了解。

/etc/motd缺点是,用户登录系统如果是图形界面,这些信息就不会显示。

[root@zyshanlinux-01 ~]# cat /etc/motd
                    /^\/^\  
                  _|__|  O|  
         \/     /~     \_/ \  
          \____|__________/  \  
                 \_______      \  
                         `\     \                 \  
                           |     |                  \  
                          /      /                    \  
                         /     /                       \\  
                       /      /                         \ \  
                      /     /                            \  \  
                    /     /             _----_            \   \  
                   /     /           _-~      ~-_         |   |  
                  (      (        _-~    _--_    ~-_     _/   |  
                   \      ~-____-~    _-~    ~-_    ~-_-~    /  
                     ~-_           _-~          ~-_       _-~    
                        ~--______-~                ~-___-~
[root@zyshanlinux-01 ~]# 





  • 文件名后缀

文件名的后缀不太严谨,这些后缀并不代表什么。管理员习惯把相同的文件以相同的后缀标识出来,方便查看,约定成俗。

关于时间语言,命令: date -> echo $LANG ->stat 2.txt.bak 。就比较容易看出文件的修改文件内容时间。

[root@zyshanlinux-01 ~]# date
2018年 05月 15日 星期二 09:32:56 CST
[root@zyshanlinux-01 ~]# echo $LANG
zh_CN.UTF-8
[root@zyshanlinux-01 ~]# stat 003.txt
  文件:"003.txt"
  大小:65        	块:8          IO 块:4096   普通文件
设备:803h/2051d	Inode:33583275    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2018-05-15 09:11:15.931501934 +0800
最近更改:2018-05-15 09:11:15.931501934 +0800
最近改动:2018-05-15 09:12:07.170487846 +0800
创建时间:-
[root@zyshanlinux-01 ~]# LANG=en  #更改语言显示
[root@zyshanlinux-01 ~]# echo $LANG  #显示语言
en
[root@zyshanlinux-01 ~]# stat 003.txt
  File: '003.txt'
  Size: 65        	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 33583275    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2018-05-15 09:11:15.931501934 +0800
Modify: 2018-05-15 09:11:15.931501934 +0800
Change: 2018-05-15 09:12:07.170487846 +0800
 Birth: -
[root@zyshanlinux-01 ~]# 

默认语言,命令: echo $LANG回车出现:zh_CN.UTF-8(中文)。因为安装系统的时候选择了中文安装。







  • Linux和windows互传文件

必须在Xshell(puTTY不支持)下使用一个工具:lrzsz

命令:yum install -y lrzsz


想把Linux的文件传到windows系统

命令:sz+文件名 


windows中的文件传到Linux系统

命令:rz+回车


windows传回的Linux的文件是存放到当前目录

Tip: 推荐用Xftp 5 这个 windows 系统的软件与 linux 系统进行文件互传。



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值