隱藏權限,特殊權限,軟連接,硬連接,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    永不死機

輸入圖片說明

  • 斷開終端從新連接就會顯示

輸入圖片說明

相關文章
相關標籤/搜索