隐藏权限,特殊权限,软连接,硬连接,find查找,文件名后缀,linux与windows互传文件

隐藏权限

chattr 设置隐藏权限

  • 用法:chattr +i 在文件以及目录+i权限时候,是不能够更改,移动,删除等操做,不能作任何操做node

  • 用法:chattr +a 在文件以及目录+a权限时候,是不能移动,删除等操做,可是文件能够追加内容,目录里能够新加文件或目录。linux

  • chattr -i 解除 i 的隐藏权限windows

  • chattr -a 解除 a 的隐藏权限centos

首先给1.txt加上 i 的权限,能够ls -l 查看1.txt的权限并无什么不一样。bash

[root@aming1 ~]# ls
1.txt  anaconda-ks.cfg
[root@aming1 ~]# chattr +i 1.txt 
[root@aming1 ~]# ll 1.txt 
-rw-r--r--. 1 root root 111 5月  13 22:52 1.txt

而后vi编辑添加内容。会有以下提示,提示您正在编辑一个只读的文件。即便强制保存也会保存失败。session

输入图片说明

连强大的rm -f 都没法删除ssh

[root@aming1 ~]# rm -f 1.txt 
rm: 没法删除"1.txt": 不容许的操做

移动更是不能够的测试

[root@aming1 ~]# mv 1.txt 111.txt
mv: 没法将"1.txt" 移动至"111.txt": 不容许的操做

touch 也不能够。ui

  • 若是给一个目录添加隐藏 i 权限,该目录也和文件同样不能够更改,建立新目录文件,删除。可是限制的只是目录自己,若是该目录设置权限以前原有的文件则能够追加文件内容。但不能够更改和删除。code

  • chattr +a 试一下 a的隐藏权限。

除了追加文件,能够成功

[root@aming1 ~]# head -n2 /etc/passwd >> 1.txt 
[root@aming1 ~]# cat 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

touch也是能够的

[root@aming1 ~]# touch 1.txt 
[root@aming1 ~]#

通过测试除了追加内容之外更改以前的其余内容是不能够的。

  • 给目录加上a 权限是能够在此目录下添加文件和新目录的, 可是不能删除,更更名字,与手动更改目录下文件内容都是不能够的。

-给目录加上a 权限能够在此目录下的文件追加内容。

lsattr 查看隐藏权限

  • 固然设置了隐藏权限也是能够查看的 用lsattr 查看,能够看到i的隐藏权限。
[root@aming1 ~]# lsattr 1.txt 
----i----------- 1.txt
  • 还有 a 权限
[root@aming1 ~]# lsattr 1.txt 
-----a---------- 1.txt
  • a 与 i 权限能够同时存在,权限取最小
[root@aming1 ~]# lsattr 1.txt 
----ia---------- 1.txt

特殊权限 set_uid

set_uid 的做用:能够在普通用户用到这个命令的时候临时授予普通用户拥有root权限。

以前用到的一个命令就是更改密码的时候用到的passwd命令

[root@aminglinux-01 ~]# which passwd
/usr/bin/passwd
[root@aminglinux-01 ~]# ls -l /usr/bin/passwd 
-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd

输入图片说明

能够看到这个文件是红色的,而且在全部者的第三位权限是个s

  • 这个权限是rws,这个s的权限就是:set_uid

咱们平时普通用户也是能够用passwd来更改密码的。那首先看看密码文件的权限是怎么样的

[root@aminglinux-01 ~]# ls -l /etc/shadow
----------. 1 root root 662 8月   9 21:38 /etc/shadow

全部人都没有任何权限,( 固然root是有至高无上的权利的)

  • 能够看到这个保存密码的文件是很是严谨的,可是普通用户依然有修改密码的权限。所谓的set_uid就是在执行命令时临时赋予root权限给没权限的用户。

  • 给一个文件设置set_uid前提是这个文件是一个可执行的2进制文件。

file 加命令的绝对路径,能够查看文件类型,好比是可读的test文件仍是不可读的二进制文,以下显示的就是二进制文件。

[root@aming1 ~]# file /usr/bin/passwd 

/usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1e5735bf7b317e60bcb907f1989951f6abd50e8d, stripped
[root@aming1 ~]#

怎样添加set_uid的权限:chmod u+s 可执行加二进制文件

  • 好比给ls添加set_uid权限
[root@aminglinux-01 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@aminglinux-01 ~]# chmod u+s /usr/bin/ls
[root@aminglinux-01 ~]# ls -l /usr/bin/ls
-rwsr-xr-x. 1 root root 117656 11月  6 2016 /usr/bin/ls
  • 建立一个普通用户,用普通用户运行ls命令查看root目录是没有权限的
[root@aming1 ~]# useradd aming
[root@aming1 ~]# su - aming
[aming@aming1 ~]$ ls /root/
ls: 没法打开目录/root/: 权限不够
  • 返回root用户给ls添加上set_uid以后在试试,就能够用ls命令了,就是临时用了一下root权限。。
[aming@aming1 ~]$ su - root
密码:
上一次登陆:二 5月 15 20:44:43 CST 2018从 192.168.159.1pts/0 上
[root@aming1 ~]# chmod u+s /usr/bin/ls
[root@aming1 ~]# ls -l /usr/bin/ls 
-rwsr-xr-x. 1 root root 117656 11月  6 2016 /usr/bin/ls
[root@aming1 ~]# su - aming
上一次登陆:二 5月 15 22:35:46 CST 2018pts/0 上
[aming@aming1 ~]$ ls /root/
111  1.txt  anaconda-ks.cfg
[aming@aming1 ~]$
  • 当权限是大写的S时候,说明文件以前没有x权限。好比下面
[root@aming1 ~]# chmod u=rws /usr/bin/ls
[root@aming1 ~]# ls -l /usr/bin/ls 
-rwSr-xr-x. 1 root root 117656 11月  6 2016 /usr/bin/ls
[root@aming1 ~]# chmod u+x /usr/bin/ls
[root@aming1 ~]# ls -l /usr/bin/ls 
-rwsr-xr-x. 1 root root 117656 11月  6 2016 /usr/bin/ls
  • 目录也是能够加set_uid权限的,不过没有什么意义,目录有不能执行。

特殊权限 set_gid

功能:这个特殊权限与set_uid相似,只不过做用在用户组的权限上。普通用户临时拥有root组的权限。

用法:chmod g+s 可执行加二进制文件

能够再用ls命令设置看一下,s权限做用在了用户组的位置上,文件显示为黄色。

输入图片说明

  • 目录也是能够加上set_gid 权限的。而且在加上权限后,更改目录的所属组时候,在此目录新建的子文件或者子目录,所属组都是跟父目录保持一致的。取消set_gid后,在建立就不会跟父目录保持一致。
[root@aminglinux-01 ~]# chown linyu  aaa/
[root@aminglinux-01 ~]# chmod g+s aaa/
[root@aminglinux-01 ~]# ls -ld aaa/
drwxr-sr-x. 2 linyu root 6 8月   9 22:08 aaa/
[root@aminglinux-01 ~]# mkdir aaa/aming11.txt
[root@aminglinux-01 ~]# mkdir aaa/aming12/
[root@aminglinux-01 ~]# ls -l aaa/
总用量 0
drwxr-sr-x. 2 root linyu 6 8月  10 19:47 aming11.txt
drwxr-sr-x. 2 root linyu 6 8月  10 19:47 aming12

特殊权限 stick_bit

功能:特殊权限是“t”,这个t就是防删除位。防止某个用户建立的文件被其余用户删除。root用户除外。

用法:chmod o+t 目录

  • 这个用到的相对比较多,/tmp/目录就是有stick_bit权限的。
[root@aminglinux-01 ~]# ls -ld /tmp/
drwxrwxrwt. 13 root root 4096 8月  10 19:37 /tmp/
  • /tmp/最后一位权限是“t”,这个t就是防删除位。

  • /tmp/默认是777权限,这样每个用户均可以更改删除文件,这样若是一个用户把另外一个用户的文件删除了,这样就乱套了。因此有了stick_bit权限,其余用户看是能够得,更改,删,是不能够的。

首先用aming用户在tmp下建立

[root@aming1 ~]# su - aming
上一次登陆:二 5月 15 22:38:42 CST 2018pts/0 上
[aming@aming1 ~]$ cd /tmp/
[aming@aming1 tmp]$ ls
systemd-private-f5ee0732c20c480e948ac5116d7bc899-vmtoolsd.service-ZjwF4b
[aming@aming1 tmp]$ touch /tmp/1.txt

而后切换为aming2用户对tmp下1.txt进行删除,追加,更改,都是不能够的。

[root@aming1 ~]# su - aming2
最后一次失败的登陆:二 5月 15 23:10:37 CST 2018pts/0 上
最有一次成功登陆后有 1 次失败的登陆尝试。
[aming2@aming1 ~]$ vi /tmp/1.txt                        ----------------------------------------------此处vi编辑会提示该文件为只读。
[aming2@aming1 ~]$ head -n2 /etc/passwd >> /tmp/1.txt 
-bash: /tmp/1.txt: 权限不够
[aming2@aming1 ~]$ rm /tmp/1.txt 
rm:是否删除有写保护的普通空文件 "/tmp/1.txt"?y
rm: 没法删除"/tmp/1.txt": 不容许的操做
  • 父目录的权限决定子目录/文件是否能被改动。果文件或目录设置了stick_bit权限,在文件或目录权限777的状况下。其余用户也只能修改,不能删除。

更改特殊权限的另外一种方式:用权限表明的数字权限更改

set_uid set_gid stick_bit 用数字权限表示分别就是 4 2 1

假如以前的权限是755 ,chmod u+s 用数字表示就是 chmod 4755 ,特殊权限的数字直接加到普通权限的前面 。

chmod g+s 用数字表示就是2755

chmod o+t 用数字表示就是1755


软连接

软链接就像windows里的快捷方式。

用法:ln -s 文件 软链接文件

  • 好比:给yum.log 作一个软连接
[root@aminglinux-01 ~]# ln -s /tmp/yum.log  /root/aaa/yum.log
[root@aminglinux-01 ~]# ls -l /root/aaa/
总用量 0
-rw-r--r--. 1 root root   0 8月  10 19:44 aming1.txt
lrwxrwxrwx. 1 root root  12 8月  10 23:04 yum.log -> /tmp/yum.log
[root@aminglinux-01 ~]#

注意事项: 删除软链接的时候,若是软链接是个目录,删除时敲软链接名字时候 候千万别打最后面的斜杠,不然删除的就是被连接目录的自己。

软链接若是被删除源文件并不会受到任何影响。可是源文件被删除,或者目录有改动,那么软链接文件会失效。

  • /bin就是usr/bin的软连接。
[root@aminglinux-01 ~]# ls -l /bin
lrwxrwxrwx. 1 root root 7 7月  31 22:54 /bin -> usr/bin
  • 软连接的优势:能够很是的节省空间。并且快捷方便。能够把另外一块大空间的磁盘分区软连接到当前正在使用快不够用的文件夹中,实现快捷添加磁盘空间。

软连接文件也是有大小的,它的大小根据源文件路径的长度来变化,源文件所在目录越长越深,其软链接的大小就会越大,固然在大也就是几B大小。

  • 软连接不只能够连接文件,还能够连接目录
[root@aminglinux-01 ~]# ln -s /tmp/111   /root/aming 
[root@aminglinux-01 ~]# ls -l /root/aming 
lrwxrwxrwx. 1 root root 8 8月  10 23:12 /root/aming -> /tmp/111
[root@aminglinux-01 ~]#
  • 软连接有绝对路径页游相对路径,相对路径仅限于当前目录。相对路径有一些弊端。尽可能使用绝对路径。由于软链接文件一旦移动位置就会致使软链接失效。

  • 软连接的用途例子:若是一个分区空间满了,而服务只读那个分区。这样就能够把当前分区的文件cp到更大的分区里面。而后作一个软连接到当前分区。这样服务依然能够读取到文件。还有效的利用了磁盘空间。


硬连接

什么是硬连接:

输入图片说明

  • 硬连接支持对文件作硬连接。不支持目录。

  • 硬连接的inode号,时间,大小都是同样的。

用法: ln 源文件 硬连接

  • 建立一个456.txt文件的硬连接,以下:
[root@aminglinux-01 ~]# ln 456.txt 456_heard.txt
[root@aminglinux-01 ~]# ls -li
总用量 8
33575032 -rw-rw-r--. 2 root  root     0 8月   9 22:14 456_heard.txt
33575032 -rw-rw-r--. 2 root  root     0 8月   9 22:14 456.txt
[root@aminglinux-01 ~]#
  • 能够看到硬连接的源文件456.txt和硬连接后的文件heard_456.txt,二者inode号,时间,大小都是同样的。

  • 因此硬连接其实是两个文件是同一个文件。不分源文件和连接文件。二者是互相硬连接。删除其中一个并不影响文件自己。

  • 一个文件能够建立多个硬连接。

  • 文件名只是文件自己的一个外皮。真正的文件自己是inode号记住的文件。删除其中一个外皮对文件自己并无影响。可是外皮须要最后留一个。不然这个文件就真的被删除啦。

  • 硬连接是不会占用双份空间的,由于使用相同的inode。

  • 硬连接没法跨分区创建,由于每个分区都有一套本身的inode,在格式化分区时都是预先设置好的。

  • 没法手动硬连接目录。系统自带硬连接目录。


查找文件命令 find

做用: 若是只知道一个文件的大概位置。就能够用find来查找。

用法:find 位置 - type 类型 文件名

[root@aminglinux ~]# find /etc/  -name "sshd_config"
/etc/ssh/sshd_config
  • 若是想查找etc下包含sshd的全部目录和文件
[root@aminglinux ~]# find /etc/  -name "sshd*"
/etc/rc.d/init.d/sshd
/etc/pam.d/sshd
/etc/sysconfig/sshd
/etc/ssh/sshd_config
  • 用find -type d -name 只查找目录
[root@aminglinux ~]# find /etc/ -type d -name "sshd*"
[root@aminglinux ~]#
  • 只查找文件 find -type f -name "sshd*"
[root@aminglinux ~]# find /etc/ -type f -name "sshd*"
/etc/rc.d/init.d/sshd
/etc/pam.d/sshd
/etc/sysconfig/sshd
/etc/ssh/sshd_config
[root@aminglinux ~]#

find其余用法 三个time -mtime -ctime -atime

  • 用法 find / -type -name -mtime -ctime -atime

  • 首先用stat 命令,查看一个文件的详细信息

[root@aminglinux ~]# stat 333.txt
  File: "333.txt"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 791054      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access最近访问: 2017-03-25 04:39:31.455979690 +0800
Modify最近更改: 2017-03-25 04:39:31.455979690 +0800
Change最近改动: 2017-03-25 04:39:31.455979690 +0800
[root@aminglinux ~]#

Access最近访问是:-atime (访问一次就会变化)

Modify最近更改是:-mtine (改文件内容)

Change最近改动是:-ctime (改权限若是改动文件内容ctime也会跟着变化)

  • 了解了 三个time后,能够用进行find 利用三个time查找。

  • find / -type f -mtime -1(一天之内的意思) 以下能够看到一天以内更改的文件很是多

[root@aminglinux ~]# find / -type f -mtime -1
/proc/1516/pagemap
/proc/1516/attr/current
/proc/1516/attr/prev
/proc/1516/attr/exec
/proc/1516/attr/fscreate
/proc/1516/attr/keycreate
/proc/1516/attr/sockcreate
/proc/1516/wchan
/proc/1516/stack
/proc/1516/schedstat
/proc/1516/cpuset
/proc/1516/cgroup
/proc/1516/oom_score
/proc/1516/oom_adj
/proc/1516/oom_score_adj
/proc/1516/loginuid
/proc/1516/sessionid
/proc/1516/coredump_filter
/proc/1516/io
  • find / -type f -mtime +1(一天以上的意思)

  • find / -type f -mmin -60(查看60分钟之内的)

  • 还能够加多个判断条件,起到而且的意思,首先要是文件,而后要一天内改动的,而后要名字包含.conf的

find /etc/ -type f -ctime -1 -name "*.conf"

[root@aming1 ~]# find /etc/ -type f -ctime -1 -name "*.conf"
/etc/resolv.conf
  • 使用 -o 还能够加多个判断条件,起到或者的意思,查找文件,或者一天内改动,或者名字包含.conf
[root@aming1 ~]# find /etc/ -type f -o -ctime -1  -o -name "*.conf"

find经常使用选项 -type -name -mtime 偶尔使用 -o

  • 查找一个文件的硬连接 find / -inum 33623781
[root@aming1 ~]# touch 2.txt
[root@aming1 ~]# ln 2.txt /tmp/2.txt_heard
[root@aming1 ~]# ls -li
总用量 8
50332746 drwxr-xr-x. 2 root root   19 5月  15 21:21 111
33599786 -rw-r--r--. 1 root root  130 5月  15 21:09 1.txt
33623781 -rw-r--r--. 2 root root    0 5月  16 10:50 2.txt
33574987 -rw-------. 1 root root 1422 3月  17 19:03 anaconda-ks.cfg
[root@aming1 ~]# find / -inum 33623781
/root/2.txt
/tmp/2.txt_heard
[root@aming1 ~]#
  • 查找一个小时内改动的文件
[root@aming1 ~]# find /root/ -type f -mmin -60
/root/2.txt

find 选项exec

  • 做用:把前面查找出的内容,做为参数在后面让命令执行。

  • 用法 find /root/ -type f -mmin -120 -exec ls -l {} ;

[root@aming1 ~]# find /root/ -type f -mmin -60
/root/2.txt
[root@aming1 ~]#  find /tmp/ -type f -mmin -120 -exec ls -l {} \;
-rw-r--r--. 2 root root 0 5月  16 10:50 /tmp/2.txt_heard

文件名后缀

首先在linux系统下是区分命令大小写的。

[root@aming1 ~]# LS
-bash: LS: 未找到命令

在linux系统下文件也是有后缀名的,可是后缀名并不表明这是个什么文件类型。

通常会压缩文件为.gz 配置文件为 .conf 这个不绝对,后缀并不表明什么, 只是看着方便

查看系统语言。echo $LANG

查看系统版本命令 cat /etc/redhat-release 仅限于centos 红帽


linux和windows互传文件

  • 首先须要安装一个包

yum install -y lrzsz

  • 安装完成以后输入

sz 111.txt 就会弹出一个框,选择把文件放到windows的哪个文件夹下。

输入图片说明

rz 从windows上传到linux命令。也会弹窗。上传到当前目录下。

  • 互传文件软件 能够用 filezilla 或者 winSCP

输入图片说明

更改接入终端提示消息

  • 编辑/etc/motd 复制以下内容。
_oo0oo_
                                 088888880
                                 88" . "88
                                 (| -_- |)
                                  0\ = /0
                               ___/'---'\___
                             .' \\\\|     |// '.
                            / \\\\|||  :  |||//\\
                           /_ ||||| -:- |||||-  \\
                          |   | \\\\\\  -  /// | |
                          | \_|  ''\---/''  |_/  |
                          \  .-\__  '-'  __/-.  /
                        ___'. .'  /--.--\  '. .'___
                     ."" '<  '.___\_<|>_/___.' >'  "".
                    | | : '-  \'.;'\ _ /';.'/ - ' : | |
                    \  \ '_.   \_ __\ /__ _/   .-' /  /
                ====='-.____'.___ \_____/___.-'____.-'=====
                                  '=---='
   
   
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                        佛祖保佑    iii    永不死机

输入图片说明

  • 断开终端从新连接就会显示

输入图片说明

相关文章
相关标签/搜索