# Linux 作業系統實務
> 這是一個關於 Linux / CentOs7 的筆記[name=簡志融111010501]
> [[第二篇] Linux 伺服器架設](https://hackmd.io/@Jung217/nqu_linux2)
> [[第三篇] Linux 自動化運維](https://hackmd.io/@Jung217/nqu_linux3)
## 摘要
[TOC]
## 第一週
### 安裝 CentOs7
* 依系統下載 VirtualBox & Extension Pack
[VirtualBox/Downloads](https://www.virtualbox.org/wiki/Downloads)

* 下載 [Centos7 iso檔](https://ftp.iij.ad.jp/pub/linux/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-LiveKDE-1810.iso) ([sauce](https://ftp.iij.ad.jp/pub/linux/centos-vault/7.6.1810/isos/x86_64/))
* 安裝 VirtualBox 後,到下圖處安裝 Extension Pack

* 裝好的樣子

1. 新增虛擬機 : 取名、新增映像檔(ISO)

2. 設定 記憶體 & CPU(視情況,memory=2048,CPU=2)

3. 設定硬碟大小(100G為上限,用多少才佔多少)

4. 真正將機器安裝至硬碟上

5. 設定重點
* 語言選 English / US
* 
* 硬碟自動分割

* root passwd : centos

* 新增 user,passwd : user

* 等待系統設定完成

* 完成後關機,在設定中將ISO檔移除,將第二張網卡設成 Host only

* 重開後,同意 License,然後 Finish

* 看到 user 就成功了

### 滑鼠 issue
[Solution](https://blog.csdn.net/qq_41583666/article/details/107092694)
### Commands
顯示 IP 位置
```
[user@localhost ~]$ ifconfig
```
<br/>
## 第二週
開啟Terminal,會看到
```
[user@localhost ~]$
```
user : 使用者名稱
localhost : 主機名稱
**~ : 家目錄**
<hr/>
### 關防火牆
1. 切成root
2. 關閉 **selinux**
```
[root@localhost user]# gedit /etc/selinux/config
```
```
SELINUX=disabled
```
3. 儲存檔案
4. 重開機
5. 檢查
```
[root@localhost user]# getenforce
Disabled
```
6. 關防火牆並檢查狀態
```
[root@localhost user]# systemctl stop firewalld (d : daemon)
[root@localhost user]# systemctl disable firewalld (開機不重啟)
[root@localhost user]# systemctl status firewalld
```

### 建立SSH server
> SSH 埠號:22 用於遠端登入
1. 安裝 & 更新 openssh-server
```
[root@localhost user]# yum install openssh-server -y (-y後續不用確認)
```
2. 啟動
```
[root@localhost user]# systemctl start sshd
```
3. 安裝 [Putty](https://www.putty.org/) & [WinScp](https://winscp.net/eng/download.php)
4. 輸入虛擬機IP,埠號22,開啟遠端連線
Putty選SSH;WinScp選SFTP


### 建立Http server
1. 安裝 & 更新 Http
```
[root@localhost user]# yum install httpd
```
2. 啟動
```
[root@localhost user]# systemctl start sshd
[root@localhost user]# systemctl status sshd
```
3. 到預設資料夾建hi.htm
```
[root@localhost user]# cd /var/www/html
[root@localhost user]# echo "hi" > hi.htm
```
5. 在瀏覽器輸入**虛擬機IP/hi.htm**

<hr/>
### Commands
顯示當前工作目錄(Print work directory)
```
[user@localhost ~]$ pwd
/home/user
```
<br/>
切換目錄(Change directory)
```
[user@localhost ~]$ cd /要換到的目錄
[user@localhost ~]$ cd /tmp
[user@localhost ~]$ cd /~ (家目錄)
[user@localhost ~]$ cd - (上一個路徑)
```
<br/>
清除頁面
```
[user@localhost ~]$ clear
```
<br/>
暫時提升至管理者權限
```
[user@localhost ~]$ sudo 指令
```
<br/>
切成超級使用者
```
[user@localhost ~]$ su
Password:
[root@localhost user]#
```
<br/>
重開機
```
[user@localhost ~]$ reboot
```
<br/>
系統控制(System control)
動作 : status/start/stop/reload/restrat
```
[user@localhost ~]$ systemctl 動作 server
```
<br/>
清除背景執行動作(暴力,不鼓勵)
pid : process id
```
[user@localhost ~]$ kill -9 pid (-9:強制刪除)
```
<br/>
讀取檔案
```
[user@localhost ~]$ cat 檔名
```
<br/>
如果 **ifconfig** 無法使用(古老指令,有些版本可能去除)
```
[user@localhost ~]$ sudo yum install net-tool
```
<br/>
如果 **gedit** 無法使用
```
[user@localhost ~]$ sudo yum install gedit
```
## 第三週
> 228放假
## 第四週
> 橋接網路會將虛擬機網路層級提升至與主系統相當,使其能與主機互相溝通,
> 並能直接與Internet接觸(暴露在網上,相對不安全)
> 一般網路卡設兩張: NAT(對外載軟體) & Host only(和Windows溝通)
> Fedora追求快速更新;CentOs追求穩定;Ubuntu追求創新
### 用 IPv6 架網站
1. 在虛擬機設定>網路>將一網路卡設成**橋接網路**,其他網路卡不啟用
2. 確認防火牆關閉,啟動 httpd
3. 取得IPv6位置(fe80...的是系統對內使用的)

4. 在瀏覽器啟動**http://[IPv6]/hi.htm**

### ChatGPT on Line Bot
> AI要取代我們了啊啊啊!
> 詳細教學 : https://mrmad.com.tw/chatgpt-line-robot-creation-teaching
<hr/>
### Commands
顯示當前檔案目錄(list)
```
[user@localhost ~]$ ls
```
<br/>
上次指令執行狀態
0:正常 ; 非零值:錯誤
```
[user@localhost ~]$ echo $?
0
```
<br/>
## 第五週
> Virtual Box > 檔案 > 匯出/入應用裝置
> CentOs自第七版開始,只提供64為原版本
> 常見的虛擬機 : VMWare(.VDI)、VirtualBox(.VMDK)、Hyper-V(Windows)、Kvm(Linux)
>Linux開機時,一般有兩種模式 : 圖形化 / 文字
### 快照
1. 點選快照

2. 擷取 > 取名 > 寫描述 > 確認(正確)

<br/>
### 測試網站是否正常
1. 先執行 **wget url**
2. 再執行 **echo $?**
3. if==0 : 正確 ; Not 0 : 錯誤
### 安裝Miniconda3 & 建立Python環境
1. 用wget下載Miniconda3檔案
```
[user@localhost ~]$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
```
2. 執行Miniconda3檔案
```
[user@localhost ~]$ bash Miniconda3-latest-Linux-x86_64.sh
```
3. Q跳過使用協議
<hr/>
### Commands
關機(Pause)
```
[user@localhost ~]$ halt -p
```
<br/>
安裝/移除軟體
install/remove
```
[user@localhost ~]$ yum install software
[user@localhost ~]$ yum remove software
```
<br/>
執行當前目錄下的檔案
```
[user@localhost ~]$ ./檔案
```
<br/>
## 第六週
> 在Linux中,檔名大小寫有區別;副檔名僅參考,可含多種屬性
> 在Linux中,第一隻被執行的程式 : **[systemd(newer) / init(older)] (pid=1)**
> GRUB是Linux的開機管理系統,當硬碟存在兩種系統時,可選擇要載入何者
> GRUB開機可進入**單人模式**,用於系統修復和管理
>systemd會啟動具**enable**屬性的服務(sshd、httpd)
>指令帶中括號,代表可有可無
* 在終端機中:
**Ctrl + A** >>> 移到最前
**Ctrl + E** >>> 移到最後
**Ctrl + C** >>> 中斷執行
**Ctrl + Z** >>> 暫停執行
**Alt + D** >>> 整行刪除
**Tab** >>> 補齊檔名(檔名相同時,再按一下可列出挑選)
**Ctrl + Alt + F1~F6** >>> 切換虛擬終端
* 在開機時 :
```
CentOs Linnux (3.10.0-957.e17.x86_64) 7 (core)
```
3 : 主要版本
10.0 : 次要版本
957 : 修正次數(patch)
x86_64 : x86架構,64位元
<hr/>
### Commands
產生新檔案/更新時間戳記
```
[user@localhost ~]$ touch a.txt
```
<br/>
改變檔案模式 (+x:賦予可執行的權限)
```
[user@localhost ~]$ chmod +x 檔案
```
<br/>
Dump message ( | : pipe ; 前者的輸出,後者的輸入)
```
[root@localhost user]# dmesg | more
```
<br/>
查看網路伺服器狀態 ( | : pipe)
grep : 篩選
80 : port
t : tcp
u : udp
n : 不解析
l : listen
p : process
***0.0.0.0代表任一網路介面可連線***
```
[root@localhost user]# netstat -tunlp | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 4588/sshd
tcp6 0 0 :::22 :::* LISTEN 4588/sshd
```
<br/>
系統資訊
-a : 完整資訊
-r : 核心版本
```
[root@localhost user]# uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC
2018 x86_64 x86_64 x86_64 GNU/Linux
```
<br/>
主機名稱
```
[root@localhost user]# hostname
localhost.localdomain
```
<br/>
更改主機名稱
```
[root@localhost user]# hostnamectl set-hostname centos7-2
[root@localhost user]# bash
[root@centos7-2 user]#
```
<br/>
命令提示字串環境變數(只在當前生效) [Link](https://blog.gtwang.org/linux/how-to-make-a-fancy-and-useful-bash-prompt-in-linux-1/)
u:表使用者的帳號名稱。
h:主機名稱。
W:目前的工作目錄名稱
```
[root@localhost user]# echo $PS1
[\u@\h \W]\$
[root@centos7-2 user]# PS1="u@h [$(date +%k:%M:%S)]>"
u@h [11:22:30]>
```
<br/>
顯示當前使用者資訊
```
(base) [user@centos7-2 ~]$ w
11:23:38 up 54 min, 4 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user :0 :0 10:30 ?xdm? 60.00s 0.07s /bin/sh /usr/bi
user pts/0 :0 10:30 52:53 0.00s 0.80s kdeinit4: kded4
user pts/1 :0 10:31 9:38 0.16s 0.08s /bin/bash
user pts/2 192.168.56.1 11:23 2.00s 0.18s 0.00s w
```
<br/>
暫停執行&恢復執行(fg : foreground)
```
(base) [user@centos7-2 ~]$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=115 time=19.4 ms
^Z
[1]+ Stopped ping 8.8.8.8
(base) [user@centos7-2 ~]$ fg
ping 8.8.8.8
64 bytes from 8.8.8.8: icmp_seq=2 ttl=115 time=26.1 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=115 time=15.6 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=115 time=17.2 ms
^C
--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 5289ms
rtt min/avg/max/mdev = 15.642/19.597/26.104/3.988 ms
(base) [user@centos7-2 ~]$
```
<br/>
選擇繼續執行的工作
```
(base) [user@centos7-2 ~]$ jobs
[1]- Stopped ping 8.8.8.8
[2]+ Stopped sleep 100
(base) [user@centos7-2 ~]$ fg 1
ping 8.8.8.8
```
<br/>
指令手冊(指令使用和參數說明)
```
[user@centos7-2 ~]$ man ls
(base) [user@centos7-2 ~]$ man ls
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐
fied.
Mandatory arguments to long options are mandatory for short options
too.
-a, --all
do not ignore entries starting with .
-A, --almost-all
do not list implied . and ..
......
```
<br/>
使用者名稱身分
```
[user@centos7-2 ~]$ whoami
user
```
<br/>
關機(10mins)
```
[user@centos7-2 ~]$ shotdown -h +10
```
<br/>
## 第七週
> 資料夾可設成磁碟分割區,使用不同的檔案系統(Windows : FAT32、EXFAT、NTFS)
> 在Linux系統中,萬物皆檔案
> Linux 遵循 FHS(Filesystem Hierarchy Standard)

> **dev** (device) 儲存任何虛擬、實體的設備
> sd(SCSI硬碟、SATA)、hd(IDE硬碟)、lp(印表機)、cdrom(光碟機)
> **bin** (binary) 機器碼、執行檔存在的目錄
> **sbin** (super user binary) 超級使用者的執行檔存在的目錄
> mke2fs : 格式化磁碟分割區指令
> **home** 儲存一般使用者
> **usr** (unix resource) 第三方軟體、管理者執行檔、packages安裝目錄
> **var** (variable) log檔、一些伺服器、可變動大小的東西
> **lib** (variable) 函式庫
> lib名稱.so.版本 >> 動態函式庫 (編譯時檔案較小,但執行要搭配函式庫)
> lib名稱.a.版本 >> 靜態函式庫 (編譯時檔案大,但可獨立執行)
> **etc** 使用者帳號密碼、系統設定、伺服器配置
> resolv.conf : 管理系統DNS伺服器的檔案
> inittab : 開機時選擇進入模式(圖形、文字)
> **boot** 開機的核心檔案 (vmlinuz)
> **root** 管理者的家目錄
> **tmp** 暫時存取檔案的位置(任何使用者都可)
> **mnt** (mount) 掛載(隨身碟)
> **media** 掛載(CD、DVD)
> 掛載 : mapping
> **proc** (process) 行程,執行程式時產生對應資料夾
<hr/>
### Commands
List(長格式)
```
[user@localhost ~]$ ls -l
```

**d** : 目錄
**-** : 檔案
**b** : 塊狀設備(硬碟、隨身碟)
**c** : 字元設備(滑鼠、鍵盤)
**l** : 連結
-(rw-)(r--)(r--).
1. owner 擁有者權限
2. snap 群組權限
3. other 其他人權限
rwx : 可讀、可寫、可執行
連結數 : 超連結數(1代表沒有,只有自己)
<br/>
顯示資料夾屬性
```
[user@localhost ~]$ ls -l -d test
```
可顯示隱藏的檔案(.開頭)
```
[user@localhost ~]$ ls -a
```
轉換檔案大小(K、MB)
```
[user@localhost ~]$ ls -l -h
```
List當前目錄(.可省略)
```
[user@localhost ~]$ ls .
```
切到上一層目錄
```
[user@localhost ~]$ ls ..
```
<br/>
切到上兩層目錄,再下一層
```
[user@localhost ~]$ cd ../../tmp
```
切到原本的目錄
```
[user@localhost ~]$ cd -
```
<br/>
產生多個空白檔案
```
[user@localhost ~]$ touch {a..d}{1..12}
```
<br/>
***將整個目錄複製到指定地點***
```
[user@localhost ~]$ cp -r /dir1 /tmp
```
<br/>
***改檔名/目錄名***
```
[user@localhost ~]$ mv aa ab
```
<br/>
***inquery***
```
[user@localhost ~]$ rm -i ab
```
<br/>
## 第十週
> 在Linux中,link分為兩種 : hard link (hlink) / symbolic link (slink);windows'捷徑'可連到任意檔案或資料夾
> slink 連到 hlink,hlink 連到 檔案 (hlink不見,slink失效)
> hlink與原本檔案屬性一樣,slink屬性全開(lrwxrwxrwx,but不可執行)
> 增加hlink才會增加連結數;hlink跟data用一個inode
> hlink不可跨分割區及目錄
> 
> CentOS7 使用 XFS(64bit) 檔案系統 (早期 ext2、ext3、ext4...;Ext3比Ext2多了日誌系統;Ext4減少寫入磁碟的游離)
> inode 是 儲存檔案屬性的檔案 (meta data;file size、rwx、onwer、group、atime、ctime、block、location)
> 具可執行屬性就是執行檔
<hr/>
### Commands
**檔案屬性**
Access: (0775/-rwxrwxr-x)
Access
Modify
Change
```
[root@centos7-2 user]# stat a.txt
File: ‘a.txt’
Size: 19 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 50358687 Links: 1
Access: (0775/-rwxrwxr-x) Uid: ( 1000/ user) Gid: ( 1000/ user)
Access: 2023-03-21 09:50:48.871000000 +0800
Modify: 2023-03-21 09:42:30.649000000 +0800
Change: 2023-03-21 09:50:40.263000000 +0800
Birth: -
```
</br>
inode
```
[root@centos7-2 user]# ls -i
```
<br/>
找出執行檔
```
[root@centos7-2 user]# which passwd
/usr/bin/passwd
[root@centos7-2 user]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
[root@centos7-2 user]# stat /usr/bin/passwd
File: ‘/usr/bin/passwd’
Size: 27832 Blocks: 56 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 264277 Links: 1
Access: (4755/-rwsr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-02-14 11:20:17.871312245 +0800
Modify: 2014-06-10 14:27:56.000000000 +0800
Change: 2023-02-14 11:20:17.871312245 +0800
Birth: -
```
<br/>
切換一般使用者(- : 切換到使用者所在目錄 ; no - : 切到同目錄)
```
[user@localhost ~]$ su - user1
password:
[user1@localhost ~]$
```
<br/>
#### 時間校準
```
[root@centos7-2 user]# timedatectl set-timezone Aisa/Taipie
[root@centos7-2 user]# ntpdate tock.stdtime.gov.tw
```
<br/>
**顯示檔案架構**
```
[root@centos7-2 user]# file 檔名
```

<br/>
執行不用加./(僅當前終端)
```
[root@centos7-2 user]# echo $PATH
[root@centos7-2 user]# expert PATH=$PATH:路徑
```
<br/>
執行不用加./
```
[root@centos7-2 user]# gedit .bashrc
加入 expert 路徑
```
<br/>
**磁碟**使用狀況(-h:以容量單位顯示)
```
[root@centos7-2 user]# df -h
```
<br/>
## 第十一週
> 在Linux中,0=標準輸入(鍵盤) 1=標準輸出(檔案) 2=標準錯誤輸出(螢幕)
> 並非所有指令都可以用管道(|後指令要可以讀取、處理標準輸入)
> rm 就不行
<hr/>
### Commands
把所有**檔案**大小列出來(包含子目錄)(-h:以容量單位顯示)
```
[root@centos7-2 user]# du -h
```
<br/>
把所有檔案大小總和列出來(包含子目錄)(-s : summary)
```
[root@centos7-2 user]# du -h -s /目錄
```
<br/>
**把第N層資料夾檔案大小列出來**(-h:以容量單位顯示)
```
[root@centos7-2 user]# du -h --max-depth 1 /目錄
```
<br/>
將結果寫入檔案(1可省略;表示標準輸出)
```
[root@centos7-2 user]# echo hi 1>hi.txt
```
<br/>
將錯誤結果寫入檔案(2不可省略;表示標準錯誤輸出)
```
[root@centos7-2 user]# ls aaa 2>err.txt
```
<br/>
將結果寫入檔案(依正確or錯誤分別寫入)
```
[root@centos7-2 user]# ls aaa hi.txt 1>a.txt 2>err.txt
```
<br/>
將結果寫入檔案(都放入a.txt;順序不可顛倒)
```
[root@centos7-2 user]# ls aaa hi.txt > a.txt 2>&1
```
<br/>
將結果寫入檔案(/dev/null不顯示任何結果)
```
[root@centos7-2 user]# ls aaa hi.txt > /dev/null 2>&1
```
<br/>
將find結果放到rm後面執行(xargs的功能)
```
[root@centos7-2 user]# find name "*.aaa" | xargs rm
```
<br/>
**創建有名管道**
```
[root@centos7-2 user]# makefifo testfifo
```
<br/>
追加
```
[root@centos7-2 user]# echo 0123 >> a.txt
```
<br/>
清空
```
[root@centos7-2 user]# > a.txt
```
<br/>
從$PATH找執行檔
```
[root@centos7-2 user]# which a.sh
```
<br/>
找出所有包含a.txt的檔案(從資料庫找)
```
[root@centos7-2 user]# updatedb
[root@centos7-2 user]# locate a.txt
```
<br/>
從當前目錄 find a.txt(從磁碟一個一個找;雙引號可省;速度慢,但可以設定特殊條件)
```
[root@centos7-2 user]# find . -name "a.txt"
```
<br/>
從當前目錄 type find a.
-iname : 不分大小寫
-type d : 目錄(資料夾)
-type p : 管道
-type f : 檔案(deafult)
-type l : 硬連結
-type s : socket
-perm 0644 : 權限條件
! -perm 0644 : 非權限條件
-empty : 找空白目錄
***[FIND大全](https://blog.gtwang.org/linux/unix-linux-find-command-examples/)*** (Very Important)
```
[root@centos7-2 user]# find -type f -iname "a.*"
```
```
[root@centos7-2 user]# find -type d -empty -exec rm -rf {} \;
```
<br/>
增量式備份
c : create
v : verbose 完整顯示過程
f : file name
z : zip
-cnewer : change time 比比較對象新的
```
[root@centos7-2 user]# tar cvfz bp0425.tar.gz ./*
.
.
[root@centos7-2 user]# tar cvfz bp0425-1.tar.gz `find -type f -cnewer timebase`
```
還原
```
[root@centos7-2 user]# tar xvfz bp0425.tar.gz
[root@centos7-2 user]# tar xvfz bp0425-1.tar.gz
```
<br/>
## 第十二週
> 通配符(匹配檔案**名稱**用) / 正則表達式(匹配檔案**內容**用) [LINK](https://bpdocs.blueprism.com/bp-7-1/zh-hans/helpWildcardsAndRegex.htm)
> **awk / grep / sed** : Linux三劍客
> [Linux 匹配文字 grep 指令用法教學與範例](https://blog.gtwang.org/linux/linux-grep-command-tutorial-examples/)

>
> 
> vi、vim 文字編輯器,按i、o、a可編輯,按:並輸入wq儲存離開
> 權限不足,!q 強制離開
> 
> /dev/hda、hdb... /dev/sda、sdb... (Linux命名法)
> crw--w---- tty 字元終端設備

> 8:主設備號(區分不同裝置) ;0、1、2:次設備號(相同裝置)
> mount:掛載;umonut:卸載;設備必須掛載才能使用(將設備跟檔案連結)
> eg. mount -t iso9660 /dev/cdrom /media/cdrom
### 新增硬碟
1. 進入虛擬機設定/存放裝置/控制器:SATA/加入硬碟

2. 選VDI

3. DEAFULT

4. 設定名稱、大小

5. Choose

6. 確定&完成

7. 開虛擬機,用"dmesg | grep sd"確認是否掛載(sdb)

8. 輸入指令"fdisk /dev/sdb",用fdisk工具控制硬碟


9. 分割硬碟


10. 掛載硬碟([掛載不了處理方式](https://blog.csdn.net/llwy1428/article/details/93848439))

<hr/>
### Commands
將有"ABC"的地方找出來
-i : 不分大小寫
-n : 標出行數
```
[root@centos7-2 user]# grep -i -n "ABC" a.txt
```
<br/>
將有"ABC"的**以外**地方找出來
-v : 排除
```
[root@centos7-2 user]# grep -v "ABC" a.txt
```
<br/>
將在testfile下,有"a"的地方找出來
-r : 遞迴
```
[root@centos7-2 user]# grep -r "a" testfile
```
<br/>
將在testfile下,有"a"的地方找出來
-A : after
-B : before
```
[root@centos7-2 user]# grep -A 1 -B 1 "ABC" a.txt
```

<br/>
指令前加\,回到指令原貌
```
[root@centos7-2 user]# \grep -v "ABC" a.txt
```

<br/>
計算系統使用者數量
```
[root@centos7-2 user]# cat etc/password | grep "nologin$" |wc -l
```
<br/>
找出空白行並標出行數
```
[root@centos7-2 user]# cat a.txt | grep -n "^$"
```
<br/>
設定檔中,去掉註解,去掉空白
```
[root@centos7-2 ssh]# cat sshd_config | grep -v "^#" | grep -v "^$"
```
<br/>
A 或 a 為開頭的
```
[root@centos7-2 ssh]# cat a.txt | grep "^[Aa]"
```
<br/>
ABC 或 AbC
```
[root@centos7-2 ssh]# cat a.txt | grep "A[Bb]C"
```
<br/>
A[任一]C
```
[root@centos7-2 ssh]# cat a.txt | grep "A[0-9 a-z A-Z]C"
```
<br/>
## 第十三週
> USB使用FAT32,Linux看見可自動掛載
> 沒有自動掛載,可手動用mount指令掛載


> [NTFS格式掛載指南](https://it001.pixnet.net/blog/post/328444228)

> vfat(Linux) = FAT32(Windows)
> 補充 : [John the Ripper(破解密碼)](https://trendoceans.com/how-to-install-john-the-ripper-on-all-platforms/)


<hr/>
### Commands
列出USB裝置
```
[root@centos7-2 user]# lsusb
```
<br/>
掛載USB裝置
```
[root@centos7-2 user]# mount /dev/sdc /myUSB
```
<br/>
退出USB裝置
```
[root@centos7-2 user]# umount /myUSB
```
<br/>
新增/刪除群組
groupadd/groupdel
```
[root@centos7-2 user]# groupadd sales
```
From /etc/group
sales : x : 1001 :
群組名稱 : 密碼 : Gid :
<br/>
新增使用者
此舉會在/home下,建立alex群組資料夾,並新增使用者alex

```
[root@centos7-2 user]# useradd alex
```
From /etc/shadow
alex: x : 1001 : 1001 :: /home/alex : /bin/bash
使用者名稱 : 密碼 : Uid : Gid : 註解 : 家目錄 : shell
***root 的 Uid 是 0***
**shell = /sbin/nologin 是 系統帳號**
<br/>
查看使用者
```
[root@centos7-2 ~]# id alex
uid=1001(alex) gid=1001(alex) groups=1001(alex)
```
<br/>
設定使用者密碼
互動式:
```
[root@centos7-2 ~]# passwd alex
```
直接式:
```
[root@centos7-2 ~]# echo "password" | passwd --stdin alex
```
<br/>
更改權限
數字表示法:
```
[root@centos7-2 ~]# chmod 744 a
```
給所有人執行權限:
```
[root@centos7-2 ~]# chmod a+x a
```
去除所有群組執行權限:
```
[root@centos7-2 ~]# chmod g-x a
```
<br/>
**看資料夾權限**
```
[root@centos7-2 ~]# ls -ld file
```
<br/>
## 第十四週
> no r >> 沒辦法列出資料夾內容
> no x >> 沒辦法cd進去
> **no w >> 沒辦法新增檔案、新增子目錄、刪除、更改名稱(修改內容看檔案本身)
> 如果目錄權限沒有w,就無法刪除檔案....但這只是對一般使用者,管理者還是可以照常刪除**
> [補充 : 在 Linux 中預防檔案被更改或刪除(chattr)](https://blog.gtwang.org/linux/how-to-make-file-immutable-on-linux-chattr-command/)
<hr/>
### Commands
創建目錄
-p:如果資料夾不存在 >> 創建資料夾
如果資料夾存在 >> 則不動作、也不會有錯誤發生
```
[root@centos7-2 user]# mkdir -p file
```
<br/>
查看資料夾屬性(-d:資料夾本身;只有-l,資料夾裡面)
```
[root@centos7-2 user]# ls -ld file
```
<br/>
切換使用者(-:回到user1的家目錄,不加則不變)
```
[root@centos7-2 user]# su - user1
```
<br/>
檔案上i鎖(root都無法新增、刪除、修改)
+/-i : +上鎖 -解鎖
```
[root@centos7-2 user]# chattr +i a.txt
```
<br/>
檔案上a鎖(只能用echo新增)
+/-a : +上鎖 -解鎖
```
[root@centos7-2 user]# chattr +a a.txt
```
<br/>
查看進階屬性(a鎖、i鎖)
```
[root@centos7-2 user]# lsattr a.txt
```
<br/>
睡眠n秒
& : 背景執行
```
[root@centos7-2 user]# sleep 100 &
```
<br/>
查看CPU資訊
```
[root@centos7-2 test]# cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 23
model : 96
model name : AMD Ryzen 7 4800H with Radeon Graphics
stepping : 1
microcode : 0xffffffff
cpu MHz : 2894.574
cache size : 512 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm constant_tsc art rep_good nopl nonstop_tsc extd_apicid pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch retpoline_amd ssbd vmmcall fsgsbase bmi1 bmi2 rdseed clflushopt arat
bogomips : 5789.14
TLB size : 3072 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : AuthenticAMD
cpu family : 23
model : 96
model name : AMD Ryzen 7 4800H with Radeon Graphics
stepping : 1
microcode : 0xffffffff
cpu MHz : 2894.574
cache size : 512 KB
physical id : 0
siblings : 2
core id : 1
cpu cores : 2
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm constant_tsc art rep_good nopl nonstop_tsc extd_apicid pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch retpoline_amd ssbd vmmcall fsgsbase bmi1 bmi2 rdseed clflushopt arat
bogomips : 5789.14
TLB size : 3072 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management:
[1]+ Done sleep 5
```
<br/>
查看Memory資訊
```
[root@centos7-2 test]# free -h
total used free shared buff/cache available
Mem: 3.9G 539M 2.8G 12M 591M 3.1G
Swap: 2.0G 0B 2.0G
[root@centos7-2 test]#
```
```
[root@centos7-2 test]# cat /proc/meminfo
MemTotal: 4045332 kB
MemFree: 2886424 kB
MemAvailable: 3206412 kB
Buffers: 2116 kB
Cached: 499212 kB
SwapCached: 0 kB
Active: 474424 kB
Inactive: 417116 kB
Active(anon): 391148 kB
Inactive(anon): 12360 kB
Active(file): 83276 kB
Inactive(file): 404756 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Dirty: 4 kB
Writeback: 0 kB
AnonPages: 390212 kB
Mapped: 120352 kB
Shmem: 13296 kB
Slab: 104860 kB
SReclaimable: 70172 kB
SUnreclaim: 34688 kB
KernelStack: 5760 kB
PageTables: 32048 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 4119812 kB
Committed_AS: 2325236 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 32848 kB
VmallocChunk: 34359701504 kB
HardwareCorrupted: 0 kB
AnonHugePages: 120832 kB
CmaTotal: 0 kB
CmaFree: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 79808 kB
DirectMap2M: 4114432 kB
```
<br/>
查看終端執行的行程
-f : 更多詳細資訊、完整指令
```
[root@centos7-2 test]# ps
PID TTY TIME CMD
5117 pts/2 00:00:00 su
5118 pts/2 00:00:00 bash
5620 pts/2 00:00:00 sleep
5621 pts/2 00:00:00 ps
```
PPID : parent pid
```
[root@centos7-2 test]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 5117 4598 0 10:38 pts/2 00:00:00 su
root 5118 5117 0 10:38 pts/2 00:00:00 bash
root 5649 5118 0 11:26 pts/2 00:00:00 sleep 5
root 5650 5118 0 11:26 pts/2 00:00:00 ps -f
```
systemd(pid=1) : 父行程
ModemManager : 子行程
2*[{ModemManager}] : 孫行程
```
[root@centos7-2 test]# pstree
systemd─┬─ModemManager───2*[{ModemManager}]
├─NetworkManager─┬─2*[dhclient]
│ └─2*[{NetworkManager}]
├─2*[abrt-watch-log]
├─abrtd
├─accounts-daemon───2*[{accounts-daemon}]
├─akonadi_control─┬─5*[akonadi_agent_l───{akonadi_agent_l}]
│ ├─akonadi_maildis
│ ├─akonadi_nepomuk
│ ├─akonadiserver─┬─mysqld───31*[{mysqld}]
│ │ └─14*[{akonadiserver}]
│ └─{akonadi_control}
├─alsactl
├─at-spi-bus-laun─┬─dbus-daemon
│ └─3*[{at-spi-bus-laun}]
├─at-spi2-registr───2*[{at-spi2-registr}]
├─atd
├─auditd─┬─audispd─┬─sedispatch
│ │ └─{audispd}
│ └─{auditd}
├─boltd───2*[{boltd}]
├─colord───2*[{colord}]
├─crond
├─cupsd
├─2*[dbus-daemon]
├─dbus-launch
├─dconf-service───2*[{dconf-service}]
├─gam_server
├─gdm─┬─X───3*[{X}]
│ ├─gdm-session-wor─┬─startkde─┬─kwrapper4
│ │ │ └─ssh-agent
│ │ └─2*[{gdm-session-wor}]
│ └─3*[{gdm}]
├─gssproxy───5*[{gssproxy}]
├─gvfs-udisks2-vo───2*[{gvfs-udisks2-vo}]
├─gvfsd───2*[{gvfsd}]
├─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}]
│ └─2*[{ibus-daemon}]
├─ibus-x11───2*[{ibus-x11}]
├─imsettings-daem───3*[{imsettings-daem}]
├─irqbalance
├─kactivitymanage───5*[{kactivitymanage}]
├─kded4───7*[{kded4}]
├─kdeinit4─┬─abrt-applet───2*[{abrt-applet}]
│ ├─klauncher
│ ├─ksmserver─┬─kwin
│ │ └─{ksmserver}
│ ├─tracker-extract───13*[{tracker-extract}]
│ ├─tracker-miner-a───3*[{tracker-miner-a}]
│ ├─tracker-miner-u───3*[{tracker-miner-u}]
│ └─xsettings-kde───4*[{xsettings-kde}]
├─kglobalaccel
├─klipper
├─kmix───{kmix}
├─knotify4───{knotify4}
├─konsole─┬─bash───su───bash
│ └─{konsole}
├─krunner───{krunner}
├─kuiserver
├─kwalletd
├─lsmd
├─lvmetad
├─master─┬─pickup
│ └─qmgr
├─plasma-desktop─┬─ksysguardd
│ └─4*[{plasma-desktop}]
├─polkit-kde-auth───2*[{polkit-kde-auth}]
├─polkitd───6*[{polkitd}]
├─pulseaudio───2*[{pulseaudio}]
├─rngd
├─rpcbind
├─rsyslogd───2*[{rsyslogd}]
├─rtkit-daemon───2*[{rtkit-daemon}]
├─smartd
├─sshd───sshd───sshd───bash───su───bash───pstree
├─start_kdeinit
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tracker-store───7*[{tracker-store}]
├─tuned───4*[{tuned}]
├─udisksd───4*[{udisksd}]
├─upowerd───2*[{upowerd}]
└─wpa_supplicant
```
<br/>
最詳細的行程資訊
```
[root@centos7-2 test]# ps aux | more
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 125628 4056 ? Ss 09:39 0:00 /usr/lib/systemd/systemd --switched-root
--system --deserialize 22
root 2 0.0 0.0 0 0 ? S 09:39 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 09:39 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 09:39 0:00 [kworker/0:0H]
.
.
.
root 705 0.0 0.0 0 0 ? S< 09:39 0:00 [ata_sff]
root 1215 0.0 0.0 0 0 ? S 09:39 0:00 [scsi_eh_0]
root 1216 0.0 0.0 0 0 ? S< 09:39 0:00 [scsi_tmf_0]
root 1217 0.0 0.0 0 0 ? S 09:39 0:00 [scsi_eh_1]
root 1218 0.0 0.0 0 0 ? S< 09:39 0:00 [scsi_tmf_1]
```
<br/>
時時更新行程
```
[root@centos7-2 test]# top
```
<br/>
## 第十五週
> 使用**ACL**可以更加客製化的調整檔案權限
> [補充 : ACL(Acess Control List)](https://ithelp.ithome.com.tw/articles/10221185)
> 系統管理員負責運(作)維(護)(***排程、9-2***會考)
>[網站可靠性工程 Site Reliability Engineering](https://ithelp.ithome.com.tw/m/articles/10264860)
> 單點錯誤 spf(single point failure) : 某東西只有一個,如:硬碟、電供,當損壞時,整個系統癱瘓,fault tolerance極低
> iptables 修改、新增規則須重啟,會中斷連線
> firewallD 可動態設定、不須重啟,提供多個zone(所在地區)供管理者選擇
> XXX.tar.gz = XXX.tgz (gz、bz2、xz) [補:各種壓縮與解壓縮指令](http://note.drx.tw/2008/04/command.html)
> /etc 有使用者帳密、伺服器配置設定
> /home 代表各使用者
> /var 含log、html ; /var/spool 含mail
>
> [時間設定](https://blog.gtwang.org/linux/centos-linux-change-system-timezone-command-tutorial/)
> 
> 
<hr/>
### Commands
取得檔案高級權限資訊(get file acl)
```
[root@centos7-2 user]# getfacl file
```
<br/>
設定檔案高級權限資訊
```
[root@centos7-2 user]# setfacl -m u:user1:rw- file
```
<br/>
修改檔案最高權限遮罩
```
[root@centos7-2 user]# setfacl -m m:r-- file
```

<br/>
查看防火牆狀態
```
[root@centos7-2 user]# firewall-cmd --state
```
<br/>
查看防火牆zones
```
[root@centos7-2 user]# firewall-cmd --get-zones
```

<br/>
查看public防火牆的資訊(port、白名單)
```
[root@centos7-2 user]# firewall-cmd --zone=public --list-all
```
<br/>
新增/移除防火牆規則(標準port)
動作 : add/remove
```
[root@centos7-2 user]# firewall-cmd --zone=public --動作-service=http
```
<br/>
新增/移除防火牆規則(非標準port)
動作 : add/remove
```
[root@centos7-2 user]# firewall-cmd --zone=public --動作-port=2222/tcp
```
<br/>
重新讀取防火牆檔案
```
[root@centos7-2 user]# firewall-cmd --reload
```
<br/>
壓縮檔案
```
[root@centos7-2 user]# gzip a.txt
```
解壓縮檔案
```
[root@centos7-2 user]# gunzip a.zip
```
<br/>
壓縮檔案
```
[root@centos7-2 user]# zip -r a.zip a.txt
```
<br/>
打包
x : 還原
c : create
v : verbose 完整顯示過程
f : file name
```
[root@centos7-2 user]# tar -cvf text.tar a.txt b.txt c.txt
```
解打包
```
[root@centos7-2 user]# tar -xvf text.tar
```
看打包內容(非解開)
```
[root@centos7-2 user]# tar -tvf text.tar
```
把所有file開頭的檔案打包並壓縮成myfile

<br/>
## 第十六週
> 網路矯時協定(NTP ; Network Time Protocol) 做 time sync(synchronization)
> cluster(集群) 含 master & slave,由**主**為**從**更新時間
> 時間會影響到日誌及同步,很重要(/var/log)
> 伺服器除錯SOP : 安裝、啟動伺服器 >> 防火牆selinux >> 檢查port number
> 1 * * * * >> 每小時第1分鐘執行一次
> */1 * * * * >> 每1分鐘執行一次
> ***X點正,分的地方寫0
> 0,30 17 10 * * (0、30分各一次); 1-10 17 10 * * (1-10分各一次)***
> 盡量使用絕對路徑
> 
> 
> 
> 
> 
> Linux磁碟配額有 soft limit & hard limit
<hr/>
### Commands
先執行後方再touch
```
[root@centos7-2 user]# touch `date +%Y%m%d`
[root@centos7-2 user]# touch $(date +%Y%m%d)
touch 20230530
```
<br/>
更新時間(要先確定[時區](https://hackmd.io/40Lf81P3SwOU1NB1lHznrg#%E6%99%82%E9%96%93%E6%A0%A1%E6%BA%96)再調)
```
[root@centos7-2 user]# ntpdate tock.stdtime.gov.tw
5 Jun 10:26:06 ntpdate[4563]: step time server 211.22.103.157 offset 1.299281 sec
```
<br/>
將時間寫入Bios(確保下次開機時間正確)
-w : write ; -r : read
```
[root@centos7-2 user]# hwclock -w
```
<br/>
執行歷史指令
```
[root@centos7-2 user]# history
........
[root@centos7-2 user]# !數字
[root@centos7-2 user]# !關鍵字
```
<br/>
查看的 httpd log
status code:
1XX : 資訊
2XX : 成功(200)
3XX : 跳轉
4XX : 伺服器錯誤(404)
5XX : 客戶端錯誤(連不存在的網址)

IP、跳轉網址、目錄(存取檔案)、status code、device、browser(user agent)
```
[root@centos7-2 user]# cd /var/log/httpd
[root@centos7-2 httpd]# ls
access_log error_log error_log-20230523
access_log-20230502 error_log-20230502
[root@centos7-2 user]# cat access_log | more
192.168.56.1 - - [05/Jun/2023:10:41:34 +0800] "GET / HTTP/1.1" 403 4897 "-" "Moz
illa/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Ch
rome/114.0.0.0 Safari/537.36"
```
<br/>
追蹤,有新資料會印出來
```
[root@centos7-2 user]# tail -f /temp/time.log
```
<br/>