Try   HackMD

W4 (2023/10/03)

NFS

NFS Network File System

在Server端有個 /data ,export /data 後,Client端mount Server端export出來的資料夾/檔案 ,之後在兩端編輯都能看見修改的內容。(資料的存放是在Server端)

CentOS 7 下 yum 安装和配置 NFS

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • Server端
    安裝:
    sudo yum install nfs-utils
    sudo systemctl enable rpcbind
    sudo systemctl enable nfs

    getenforce 跟 firewall 先關閉

    配置共享目錄、目標:
    sudo vim /etc/exports 在這裡新增要共享的資料夾跟權限等
    /data/ 192.168.68.0/24(rw,sync,no_root_squash,no_all_squash)
    * /data/ 為共享的目錄
    * 192.168.68.0/24 Client IP範圍
    * rw 讀寫權限
    * sync 同步目錄
    * no_root_squash 可以使用root授權
    * no_all_squash 可以使用普通用戶授權

    儲存後重啟 NFS:
    sudo systemctl restart nfs

    查看共享目錄:

    ​​​​[root@centos7 /]# showmount -e localhost
    ​​​​Export list for localhost:
    ​​​​/data 192.168.68.0/24
    
  • Client端
    安裝:
    sudo yum install nfs-utils
    sudo systemctl enable rpcbind
    sudo systemctl enable nfs

    查看Server端共享的目錄:

    ​​​​[root@centos7_two user]# showmount -e 192.168.68.131
    ​​​​Export list for 192.168.68.131:
    ​​​​/data 192.168.68.0/24
    

    建立連結的目錄(名稱可以不一樣):
    mkdir /nfs-data -p

    掛載:
    mount -t nfs 192.168.68.131:/data /nfs-data
    * -t nfs 檔案格式 nfs
    * 192.168.68.131掛載的主機IP
    * :/data /nfs-data 掛載的檔案 (前為server,後為client)

  • 測試

    • c -> s

      ​​​​​​​​client:
      ​​​​​​​​[root@centos7_two mydata_in_c]# echo "hi" > a.txt
      ​​​​​​​​    
      ​​​​​​​​server:
      ​​​​​​​​[root@centos7 /]# ls mydata/
      ​​​​​​​​a.txt
      ​​​​​​​​[root@centos7 /]# cat /mydata/a.txt 
      ​​​​​​​​hi
      
    • s -> c

      ​​​​​​​​server:
      ​​​​​​​​[root@centos7 mydata]# echo "hello" > b.txt
      ​​​​​​​​    
      ​​​​​​​​client:
      ​​​​​​​​[root@centos7_two /]# ls mydata_in_c/
      ​​​​​​​​a.txt  b.txt
      ​​​​​​​​[root@centos7_two /]# cat /mydata_in_c/b.txt 
      ​​​​​​​​hello
      ​​​​​​​​    
      ​​​​​​​​&delete
      ​​​​​​​​    
      ​​​​​​​​server:
      ​​​​​​​​[root@centos7 mydata]# rm -rf a.txt
      ​​​​​​​​    
      ​​​​​​​​client:
      ​​​​​​​​[root@centos7_two /]# ls mydata_in_c/
      ​​​​​​​​b.txt
      

課本

  • dd
    創造任意大小檔案的指令
    /dev/zero 檔案大小為0
    of=file3m file3m為檔案名稱
    bs=1M 產生內容大小
    count=3 循環次數

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • wc (word conut)

wc 指令出來的結果分別為

  1. 行數 (6)
  2. 詞數 (9)
  3. 字數 (45)
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

其他參數
-l :只顯示行數
-c :只顯示字元數
-w :只顯示英文字節

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • tr 取代或刪除字元

上周的df範例

[root@centos-vm1 ~]# df -h | grep /$ | awk '{print $5}'
13%
[root@centos-vm1 ~]# df -h | grep /$ | awk '{print $5}' | tr "%" " "
13

tr 共定義了很多集合,常用的有:
[:alnum:] 代表所有大小寫字母與數字的集合
[:alpha:] 代表所有大小寫字母的集合
[:blank:] 代表空白
[:digit:] 代表所有數字的集合
[:lower:] 代表所有小寫字母的集合
[:upper:] 代表所有大寫字母的集合

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

大小寫轉換
tr "a-z" "A-Z"

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

參數 -d (刪除字母)tr -d "a-zA-Z"

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

(刪除數字&空白)
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

cat 的參數:

-T 抓出tab
-E 顯示結尾

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

替換tab tr "\t" " "

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

刪除與指定內容相反 tr -d -c "0-9 \n" (-c : 除了後方選定內容)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

重複的字符以單一一個呈現 tr -s "s"

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

(重複的 s n 空格縮成1個而已)
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

利用 tr 做一個簡易的加解密(字符替代)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

多個換行轉換

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

tr 簡易加解密

[root@centos-vm2 user]# echo "12345" | tr [0-9] '9876543210'
76543
[root@centos-vm2 user]# echo "87654" | tr '9876543210' [0-9]
01234

tr 命令,Linux tr 命令详解:将字符进行替换压缩和删除 - Linux 命令搜索引擎

  • 產生序列數字 seq (sequence)
[user@centos-vm1 ~]$ seq 1 5
1
2
3
4
5
[user@centos-vm1 ~]$ seq 1 2 10
1
3
5
7
9

產生文字的序列:

[user@centos-vm1 ~]$ seq -s "+" 1 10
1+2+3+4+5+6+7+8+9+10

文字計算機(bc):

[user@centos-vm1 ~]$ seq -s "+" 1 10 | bc
55

自動對齊:

[user@centos-vm1 ~]$ seq -w 12 -1 1 
12
11
10
09
08
07
06
05
04
03
02
01