要寫共筆喔!!! by 助教 - Book mode: https://hackmd.io/@ncnu-opensource/book [TOC] --- ## Vim  * Update 系統套件資訊: `sudo apt update` * Download : `sudo apt install vim` * Vim 最主要有分成兩種模式,normal mode 和 insert mode,在進入 vim 後會是 normal mode。 - 在 normal mode 可以使用方向鍵或 `h`、`j`、`k`、`l` 移動游標。 - 在 normal mode 輸入 `i` 會進入 insert mode - 在 insert mode 按下 Esc(鍵盤左上角) 會回到 normal mode - 在 normal mode 輸入 `:` 會進入 command mode,接著輸入 `wq` (`w`:儲存、`q`:離開)後按下 Enter 會存檔離開 vim > 更多 vim 的指令參考 : [Vim Cheat Sheet](https://vim.rtorr.com/) ## 常用指令 ### `cp` filename new_filename - stand for **copy** - 複製資料夾 cp -r - 複製權限 cp -p ```bash= # 複製 test.txt 這個檔案並將其命名為 new.txt cp test.txt new.txt # 檢查 ls ``` - `cp -a` : archieve。 > 複製完的檔案會和原本檔案的權限一樣 ### `mv` - stand for **move** - 移動檔案/重新命名 ```bash= # 改變路徑 : 將 test.txt 移動到 /folder 底下 mv test.txt /folder # 重新命名 : 將 test.txt 重新命名為 new.txt mv test.txt new.txt # 檢查 ls ``` ### `rm` - stand for **remove** - `-r` recursive - `-f` force `su` username - 切換使用者 - 不要用su切換root帳號 - `exit` 退出 ### `adduser` - 會自動建立使用者 home 目錄 - `sudo adduser username` - 會多建一個group `useradd` :::info 1. `cat /etc/passwd` * user 的帳號密碼檔,每一筆都是一位 user 的資料。 * etc 的檔案為設定檔 * 欄位 : 使用者名稱`:` 密碼 `:` user ID `:` group ID `:` 使用者描述 `:` home 目錄 `:` 登入 shell 2. `cat /etc/sudoers` * 設定 user 的權限,例如:如果 user 需要 sudo 權限。 3. `cat /etc/group` * 欄位 : 使用者名稱`:` 密碼 `:` group ID ` ::: :::info #### 為何沒有特殊理由不要用 su,而是用 sudo 除非你非常清楚你在幹麻,或想切換使用者,理由: 1. 不必共享 root 密碼 2. 可用 sudoers 控管誰有超級使用者權限,因為下 sudo 是用自己帳號密碼 3. 做錯事情可追溯,是誰用了 sudo 做一件事都會紀錄在案 ::: ### 環境變數 * `export` * `env` : * = environment * 印出所有環境變數 : 大部分和使用者相關的參數, * PWD : 目前所在目錄 * SHELL * LANG(=language) * OLDPWD(剛剛所在目錄) * PATH * 重要,可以做惡意攻擊 * 決定下的指令時找到否,該去哪些檔案找 -> 根據順序找,檔案根據`:`隔開) * `cat ~/.bashrc` : bash 啟動時會讀家目錄底下的這個檔案 * `cat ~/.profile` : * `cat /etc/skel/.` -> Tab 雙擊(印出底下所有 `.` 開頭的檔案/目錄) * 在新增使用者並建立家目錄時,就會把skel目錄中的所有檔案複製過來。 ### `diff` - 印出兩個檔案的內容不同之處 - DEMO : `diff /etc/skel/.bashrc ~/.bashrc` - 完全一樣(不會輸出任何內容) ### `man` command_name - stand for manual - `man 你想查的指令` ### `date` - 顯示日期、時間 - DEMO: - `locale` : 該 user 所有和語言相關的設定 - `env | grep LANG` : 抓 env 裡`LANG`內容 ### UID GID - 查看UID`id -u username` - 查看GID`id -g groupname` - 更改ID`sudo vim /etc/passwd` 例:把b的UID改成與a一樣,su b 就會切換到a ### `groupadd` - 建立一個新的群組 - `sudo groupadd <groupname>` ### `chmod` * 用二進位的方式設計 * 順序 : * `r` : 讀 (4) * `w` : 寫 (2) * `x` : 執行 (1) * SGID:設定之後可以讓群組內的使用者一起使用 * SUID:讓能使用檔案的人,都以 owner 的身分使用 :::info - SUID 的小 LAB - `vim printUid.c` - ```c= #include <stdio.h> #include <unistd.h> int main() { printf("The uid is: %d\n", geteuid()); return 0; } ``` - `sudo apt install gcc` - `gcc printUid.c -o printUid` - `./printUid` - `sudo chown root printUid` - `sudo chmod 4755 printUid` - `./printUid` ::: ### head * 開頭前幾筆資料,預設10筆 * `-n` : 可以指定顯示開頭 n 筆資料 ```bash= head /etc/passwd head -n 5 /etc/passwd ``` ### tail * 最後幾筆資料,預設10筆 * `-n` : 可以指定顯示最後 n 筆資料 * `-f` : 實時監控檔案的變動(常用於監控日誌) ```bash= tail /etc/passwd tail -n 5 /etc/passwd tail -f logfile.log ``` ### more ### echo - `echo $$` 顯示目前檔案的Pid ### top - 系統目前狀況 ### ps - process 資訊 ### history * 顯示執行過的指令 ### alias * 設置指令別名(可以設置經常用、太長、不容易記住的指令,下次執行時可直接下該別名) - 簡化指令(例:顯示所有檔案) ```bash= # 在 ~/.bashrc 或 ~/.bash_aliases 中加入 alias la='ls -A' ``` 重新載入 `.bashrc` ```bash= source ~/.bashrc ``` ### lsblk - 系統磁碟分配狀況 ### mktemp * 在 /tmp 目錄底下自動新建一個檔案(檔名隨機) ### 常見的文字處理組合技 combo #### 寫入 `>` vs. `>>` ```bash= # 新建 tt.txt touch tt.txt ## 將 'ls' 寫入檔案 # '>' 覆蓋原本內容 ls > tt.txt # '>>' 寫到原本內容後方(append) ls >> tt.txt ``` #### pipe : `|` ```bash= # 將前面的 standard output / stdout(file descriptor= 1) 丟到後面的 standard input / stdin (file descriptor = 0) ls | tee tt.txt ``` #### standard error `2>&1` * 寫一個有錯誤的 python 檔 hello.py * `python3 hello.py` -> 此時顯示錯誤訊息 * `python3 hello.py > HW.txt` -> 嘗試寫入 HW.txt,會發現 HW.txt沒有內容 * `python3 hello.py > HW.txt 2>&1` * `1` : 標準輸出, standard output * `2` : 標準錯誤輸出, standard error * `2>&1` : 將標準錯誤輸出重導向(redirect)到標準輸出 #### `grep` with REGEX - stand for **global regular expression print** - `ls | grep passwd` - `ls | grep p.*d` - `.` : 任意字元 - `*` : 前一個字元包含 0 到任意的數量 - 所以 `p.*d` 代表配對開頭是 `p`,結尾是 `d` 的字串 - `ls -l | grep 2022` - 找出 2022 的檔案 #### `awk` - `ls -l | grep 2022 | awk '{print $5 "\t" $9}'` - `awk` 講解: - `$<num>` : 每筆資料根據空格隔開計算第幾欄 - `print $5` 印出第 5 欄的意思 #### `sort` - `ls -l | grep 2022 | awk '{print $5 "\t" $9}' | sort -n` - `sort` : 排序,預設 ascii 第一字元大小 - `sort -n` : 數字排序 - `sort -r` : reverse #### `uniq` - 針對指令輸出唯一值 - 如果檔案長這樣, ex: wooo.txt ```= a a a b ``` - `cat wooo.txt | uniq` - `cat wooo.txt | uniq -c` - `-c` : 計算重複出現幾次 - `cat wooo.txt | sort | uniq` - `cat wooo.txt | sort | uniq -c` - `cat wooo.txt | uniq -c | sort -n` - `cat wooo.txt | uniq -c | sort -nr` - `r` : reverse反向排序 - 對系統管理者要看 log 檔很有用,例如 - `cat /var/log/syslog | awk '{print $2}' | uniq -c` ### `sl` 會考喔 * 用來嘲笑的 * `sudo apt install sl` * `sl`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up