1. 程式人生 > >linux 必備基礎知識 (week2)

linux 必備基礎知識 (week2)

centos 7 各個目錄作用 (sbin 只能管理員使用)

/bin/ 存放系統命令的目錄,普通使用者和超級使用者都可以執行 (centos 7 中取消) /sbin/ 儲存和系統環境設定的相關命令,只有超級使用者可以使用這些命令進行系統環境變數設定(centos7 取消) /usr/bin/ 存放系統命令目錄 /usr/sbin/ 存放根檔案不必要的管理系統命令 /boot/ 系統啟動目錄 /dev/ 裝置檔案儲存位置 /etc/ 配置檔案儲存位置 /home/ 使用者家目錄 /lib/ 系統呼叫的函式庫儲存位置 (centos 7 中取消) /lost +found / 當系統意外奔潰或機器意外關機,而產生的檔案碎片放在這裡,每個分割槽中都有

/mnt/ 掛載目錄 /media/ 掛載目錄 掛載媒體裝置 /opt/ 第三方安裝的軟體儲存位置 /proc / 虛擬檔案系統,該目錄中的資料並不儲存到硬碟當中。而是儲存記憶體當中的。主要儲存系統核心,程序等。 /sys/ 虛擬檔案系統 ,和/proc 目錄相似。都是儲存在記憶體當中的。主要儲存核心相關資訊 /root/ 超級使用者家目錄 普通使用者家目錄都在/home/下 超級使用者 直接在/下 /src/ 服務資料目錄 一些系統服務啟動之後,可以在這和個目錄中儲存所需要的資料 /tmp/ 臨時資料 /usr/ 系統軟體資源目錄 ,注意usr不是user 的縮寫 而是 unix softwre resource 縮寫 /var/ 動態資料儲存位置 主要儲存快取 ,日誌以及軟體執行所產生的檔案

檔案時間 ls -l 檢視的是檔案的修改時間 linux 中有4種時間

atime : 訪問時間 ( 檢視內容 ) mtime : 修改時間 修改內容 ctime : 改變時間 改變檔案屬性 如大小 許可權 dtime : 刪除時間 檔案被刪除的時間

檔案型別 主要有這幾種

ls -l 檔名 //看第一個字元

  •  普通檔案
    

d 目錄檔案 b 裝置檔案(塊裝置)儲存裝置硬碟 c 裝置檔案(字元裝置) 印表機 終端 l 連線檔案

[[email protected] ~]# ll
total 28892
-rwxr--r--. 1 root root        0 Sep 24 01:02 1
drwxr-xr-x. 2 root root       55 Sep 10 05:59 192.168.0.134
-rw-------. 1 root root     1244 Aug 12 08:08 anaconda-ks.cfg
-rw-r--r--. 1 root root      928 Sep 10 05:56 index.html
-rw-r--r--. 1 root root      928 Sep 10 05:56 index.html.1
-rw-r--r--. 1 root root    10240 Sep 16 01:43 sss.tar
drwx------. 2 tom  tom        73 Sep 21 23:42 ssw
-rw-r--r--. 1 root root    20480 Sep 16 01:46 sxd.tar
-rw-r--r--  1 root root  9867092 Sep 28 09:15 sxd.tar.gz
-rw-r--r--. 1 root root 19671632 Sep 10 05:57 VNC-Server-6.3.1-Windows.exe

file 檢視檔案詳細資訊

[[email protected] ~]# file ssw
ssw: directory
[[email protected] ~]# file sss.tar
sss.tar: data

檔案管理

cd 切換目錄命令 . 當前目錄 … 上一級目錄

  •   上次目錄
    

**檔案的建立 複製 移動 刪除 操作 **

建立 touch 檔名

[email protected] tmp]# touch file{1..50}
[[email protected] tmp]# ls
file1   file12  file15  file18  file20  file23  file26  file29  file31  file34  file37  file4   file42  file45  file48  file50  file8
file10  file13  file16  file19  file21  file24  file27  file3   file32  file35  file38  file40  file43  file46  file49  file6   file9
file11  file14  file17  file2   file22  file25  file28  file30  file33  file36  file39  file41  file44  file47  file5   file7

建立目錄 mkdir

mkdir 目錄名

[[email protected] tmp]# mkdir /tmp/{dir4,dir5}
[[email protected] tmp]# ls
dir4  dir5

複製 cp 主要引數 -r :遞迴處理 -v : 顯示過程

[[email protected] dir4]# cp  -r sxd   /tmp
[[email protected] dir4]# cd /tmp
[[email protected] tmp]# ls
dir4  dir5  sxd

**移動 ** MV mv file1 /tmp/ 將file1 移動到/tmp/下

刪除 rm

-r 遞迴 -f 強制 -v 過程

如 rm -rf /tmp

檢視檔案內容

文字檔案 (cat less more head tail tailf grep …) /etc/profile // 設定系統環境變數

cat -n 顯示行號 -A 包括控制字元

cat 檔名 head -n 顯示檔案前幾行 tail -n 顯示檔案後幾行 grep 主要用於文件過濾 在此不詳細說明

linux 連結概念

軟連結 : 軟連結檔案相當於win中的快捷方式 許可權為777 作用是指向原始檔 ,大小很小 (可以跨分割槽) 硬連結: 與原始檔同步更新 不能對目錄 (不能跨分割槽)

建立軟連結

[[email protected] tmp]# ln -s 1    2
[[email protected] tmp]# ls
1  2  dir4  dir5  sxd
[[email protected] tmp]# ll
total 0
-rw-r--r-- 1 root root  0 Sep 29 09:46 1
lrwxrwxrwx 1 root root  1 Sep 29 09:46 2 -> 1
drwxr-xr-x 5 root root 41 Sep 29 08:51 dir4
drwxr-xr-x 2 root root  6 Sep 29 08:49 dir5
drwxr-xr-x 3 root root 16 Sep 29 09:15 sxd

建立硬連線

[[email protected] tmp]# ln 1 3
[[email protected] tmp]# ll
total 0
-rw-r--r-- 2 root root  0 Sep 29 09:46 1
lrwxrwxrwx 1 root root  1 Sep 29 09:46 2 -> 1
-rw-r--r-- 2 root root  0 Sep 29 09:46 3
drwxr-xr-x 5 root root 41 Sep 29 08:51 dir4
drwxr-xr-x 2 root root  6 Sep 29 08:49 dir5
drwxr-xr-x 3 root root 16 Sep 29 09:15 sxd

硬連線其實就是同一個檔案具有多個別名,具有相同inode 1.檔案具有相同的inode 和data block 2.不能對目錄進行建立 只能對檔案建立硬連線 3.刪除一個硬連線並不影響其他具有相同inode號的檔案

軟連結檔案具有自己inode ,及具有自己的檔案。只是這個檔案存放的內容是另一個檔案的路徑名,因此軟連結具有自己的inode和 data block 1.軟連結檔案可以對檔案或目錄建立 2.建立軟連結檔案時 link 不會增加

檔案許可權

U : 屬主 G :屬組 O :其他人 讀  :r   4 寫  :w   2 執行 :x   1

設定許可權 chown chown 屬主 檔名

chgrp chgrp 屬組 檔名

更改許可權

chmod 使用字母 chmod u+x file1 // 屬主增加執行許可權 chmod a=rwx file 1 // 所有人都有讀寫執行許可權

chmod 使用數字 chmod 644 file1

r w x 對檔案和目錄的意義 檔案 r: 可以獲取檔案的資料 w :可以修改檔案內容 x :可以執行該檔案為程序 目錄 r:可以使用ls 獲取其下所有檔案列表 w:可以修改目錄檔案列表 刪除或者建立 x :可cd 進去此目錄 注意 : 目錄w許可權必須和x 配合使用 單獨無法使用該許可權

基本許可權

setfacl 命令用於管理檔案的acl 規則 可以針對單一檔案或者目錄來進行讀,寫,執行許可權 setfacl 引數 檔名 -m 新增acl許可權 -x 刪除許可權

新增許可權

[[email protected] tmp]# getfacl  1
# file: 1
# owner: sxd
# group: root
user::rwx
group::rwx
other::rwx

[[email protected] tmp]# setfacl -m u:sxd:rwx 1
[[email protected] tmp]# getfacl  1
# file: 1
# owner: sxd
# group: root
user::rwx
user:sxd:rwx
group::rwx
mask::rwx
other::rwx

設定預設許可權
[[email protected] tmp]# setfacl -m m::r 1
[[email protected] tmp]# getfacl 1
# file: 1
# owner: sxd
# group: root
user::rwx
user:sxd:rwx                    #effective:r--
group::rwx                      #effective:r--
mask::r--
other::rwx

注意 預設許可權是該檔案最大許可權  凡是比這大的許可權都不能使用。

刪除檔案所有acl許可權 setfacl -b file1

在這裡插入程式碼片

umask

umask表示要系統要減掉的許可權

[[email protected] tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 29 10:52 sxd
drwxr-xr-x 2 root root 6 Sep 29 10:52 sxd1
[[email protected] tmp]# umask
0022

系統預設umask值為0022 第一位為特殊許可權 暫且不管 注意 :所創檔案一般都沒有執行許可權 ,只有目錄被減去了022 許可權

修改 umask值 臨時修改 umask 要修改的值 永久修改 修改其配置檔案 VI /etc/profile if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then umask 002 else umask 022 fi 然後source /etc/profile 使配置檔案立即生效

高階許可權的型別 suid 4 sgid 2 sticky 1

SUID : 是一種對二進位制程式進行設定的特殊許可權,可以二進位制程式的執行者臨時擁有屬組的許可權 ( 僅對擁有執行許可權的二進位制程式有效) 如果屬主 原有執行許可權,顯示為小寫 s 否則 顯示大寫S SGID 主要實現如下兩種功能 讓執行者臨時擁有屬組許可權 (僅擁有執行許可權的二進位制程式進行設定) 在某個目錄中建立的檔案 自動繼承該目錄的使用者組(只可以對目錄進行設定) Sticky 特殊許可權為可以確保使用者只能刪除自己的檔案,而不能刪除其他使用者的檔案 如果其他使用者原本有執行許可權顯示為 t 否則顯示為大寫T

 設定方法
  chmod  u+s   檔案
  chmod   g+s  檔案
  chmod    o+t    檔案
數字 設定方法
   chmod   4777檔案
   chmod   7777 檔案
   chmod   2770  檔案
    
  
  例如一
   passwd 命令
[[email protected] /]$ ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

該檔案U上有s  說明賦予了suid許可權  使執行者臨時擁有屬主許可權  
 所以普通使用者可以修改密碼
  
[[email protected] sxd]# ll /tmp
total 0
drwxrwxrwt 2 root root 28 Sep 29 20:55 sxd

在/tmp 下 有sticky許可權
表名在其目錄下使用者只能刪除自己的檔案

[[email protected] sxd]# ll
total 0
-rwxrwxrwx 1 sxd sxd 0 Sep 29 20:53 123
-rw-rw-r-- 1 dxs dxs 0 Sep 29 20:55 456

列如   123  的權限盡管為777
[[email protected] sxd]# su dxs
[[email protected] sxd]$ ll
total 0
-rwxrwxrwx 1 sxd sxd 0 Sep 29 20:53 123
-rw-rw-r-- 1 dxs dxs 0 Sep 29 20:55 456
[[email protected] sxd]$ rm -rf 123
rm: cannot remove ‘123’: Operation not permitted

檔案屬性 chattr

charrt 命令使用者設定檔案隱藏許可權 charrt [引數] 檔案 lsattr 命令使用者顯示檔案的隱藏許可權

檔案查詢

1.which 主要查詢可執行檔案(在環境變數中查詢) which 是通過PATH環境變數到該路徑內可 執行檔案,所以基本功能是尋找可執行檔案

[[email protected] sxd]# which  cd
/usr/bin/cd

檢查PATH環境變數中是否有cd所在目錄 給予顯示

2.whereis 和locate 尋找特定檔案(通過 數庫查詢) locate資料庫在/var/lib/mlocate裡面資料查詢的

find

語法 find 路徑 引數

可以按照 檔名 檔案大小 目錄深度 時間 檔案所有者 檔案型別 檔案許可權

1.按照檔名查詢

find  /  -name  ifcfg     在跟目錄下查詢 ifcfg

find  /etc     -iname    sxd               在etc下查詢sxd  -i忽略字母大小寫

2.按檔案大小查詢

find   /etc   -size  +4M   查詢超過4m的檔案
find   /etc   -size  4M      查詢等於4m的檔案
find   /etc   -size  -4M     查詢小於4m的檔案

3.查詢目錄深度

find  /etc/  -maxdepth 1      查詢最大深度為1的檔案

4.按時間查詢 atime 訪問過的時間 mtime 修改的時間 ctime 檔案許可權修改的時間

    find   /etc/   -mtime -1        查詢一天以內的檔案

5.按檔案所有者查詢

find  /etc  -user   u1      查詢屬主為u1的檔案
find   /etc  -gourp  g1     檢視屬組是g1的檔案
find   /etc/   -nouser     查詢沒有所有者的檔案

6.按檔案型別查詢

find  /etc/   -type  f     dds    查詢檔案型別為普通的檔案
find  /etc/  -type    d     sxd    查詢名字為sxd 的目錄

7.按檔案許可權查詢

find  /etc/    -perm 664   查詢檔案許可權為664 的檔案