# Linux課程筆記
**資工二111010520黃柏鈞**
---
### 2023/02/14
課程使用centos7
使用書籍:https://www.books.com.tw/products/0010755095
安裝連結:http://ftp.iij.ad.jp/pub/linux/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-LiveKDE-1810.iso

顯示現行網路介面配置資訊
`[user@localhost ~]$ ifconfig`

切換成管理員模式
一般使用者$
管理員#
```
[user@localhost ~]$ su
Password:******
[root@localhost user]#
```

退出
```
[root@localhost user]# exit
exit
[user@localhost ~]$
```

顯示目前工作路徑
```
[user@localhost ~]$ pwd
/home/user
```

---
### 2022/02/21
cd /目錄 從目前目錄移動到其他目錄
```
[user@localhost ~]$ pwd
/home/user
[user@localhost ~]$ cd /tmp
[user@localhost tmp]$ pwd
/tmp
```

cd ~ 回到家目錄
cd - 回到上一個目錄
```
[user@localhost ~]$ pwd
/home/user
[user@localhost ~]$ cd /tmp
[user@localhost tmp]$ pwd
/tmp
[user@localhost tmp]$ cd ~
[user@localhost ~]$ pwd
/home/user
[user@localhost ~]$ cd -
/tmp
[user@localhost tmp]$ pwd
/tmp
```
關閉selinux
```
[user@localhost ~]$ su
Password:******
[root@localhost user]# gedit /etc/selinux/config
```

SELINUX=enforce 改成 SELINUXS=disabled
重開
```
[root@localhost user]# reboot
//重開完
[root@localhost user]# getenforce
Disabled
```

檢視系統防火牆狀態 systemctl status firewalld
```
[root@localhost user]# systemctl status firewalld
```

關閉防火牆
```
[root@localhost user]# systemctl stop firewalld
[root@localhost user]# systemctl disable firewalld
[root@localhost user]# systemctl status firewalld
```


ssh server install: yum install openssh-server -y
yum install -y openssh-server
---
### 2023/03/07
架設ipv6網站
設定橋接網路卡(為了連接有ipv6的手機網路)

連接ipv6位置
http://[2001:b400:e735:22b9:e3e:cd68:f21:8dfe]/hi.htm

ipv6的本地端:fe80::8b92:85b1:e142:1594 //fe80開頭是在本底端使用

橋接式網路的功用:讓虛擬機上的系統可以連接本地端的系統而且兩邊都能連接到internet上
例如:vmware上的linux連到本地端的windows
缺點:虛擬機的ip會暴露在internet上
---
Linux的基本架構圖

kernal包含memery mangement、process、network、driver...
通常linux的kernal不會頻繁更新環境和功能,會以穩定、安全,不太會隨意更新,通常只會更新一些小補丁

shell:核心與應用程式之間的介面,會把外面應用程式執行的東西翻譯成核心看得懂的,核心處理完再回傳回來翻譯成我們看得懂的畫面
---
```
bash
echo $?
```
顯示上一條指令有無問題(有問題會顯示非0值)


```
bash
gcc -o 檔名 程式檔名
```
編譯檔案
```
bash
./檔名
```
執行當前目錄下的此檔案

---
### 2023/03/14
建立快照版本

可以讓使用者使用此快照系統壞了時回到上一個快照版本
---
安裝檔案
```
[root@localhost user]# yum install 要安裝的檔案
```
移除檔案
```
[root@localhost user]# yum remove 要移除的檔案
```
若想在linux上裝python,可以先安裝虛擬環境miniconda
安裝wget
```
[root@localhost user]# yum install wget -y //-y使後續詢問是否安裝為yes
```
安裝miniconda3
```
[root@localhost user]# wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
下載完後,回到一般使用者
[root@localhost user]# exit
[user@localhost ~]$ bash Miniconda3-latest-Linux-x86_64.sh
```

按下enter後

會出現授權的文章,按下q可跳過

輸入yes同意授權

確認路徑,按下enter確認
輸入
```
[user@localhost ~]$ /home/user/miniconda3/bin/conda config --set auto_activate_base false
```
新增/home/user/miniconda3/bin到PATH路徑裡面
```
[user@localhost ~]$ cd
[user@localhost ~]$ gedit .bashrc
```
跳到圖形化介面後,在底下輸入:
export PATH=$PATH:/home/user/miniconda3/bin //$PATH指原路徑

重開終端機
輸入conda指令
```
[user@localhost ~]$ conda
```

這樣就安裝完畢
---
建立python編譯環境
輸入 conda create -n mpython3.10 python=3.10
用conda建立python3.10版本的虛擬環境
```
[user@localhost ~]$ conda create -n mpython3.10 python=3.10
```


按下y,完成後輸入 conda activate mpython3.10
```
[user@localhost ~]$ conda activate mpython3.10
```

進入python3.10的環境,這樣就可以開始編譯了
輸入 python
```
(mpython3.10) [user@localhost ~]$ python
```

會顯示你目前安裝python的版本

如果出現以上圖片
輸入 conda init
```
[user@localhost ~]$ conda init
```
如果又出現此問題,請重開終端機
---
進入環境
輸入 conda activate 環境名稱
```
[user@localhost ~]$ conda activate mpython3.10
(mpython3.10) [user@localhost ~]$
```
退出環境
輸入 conda deactivate
```
(mpython3.10) [user@localhost ~]$ conda deactivate
[user@localhost ~]$
```
若進入python,要退出
輸入exit()
```
(mpython3.10) [user@localhost ~]$ python
Python 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(mpython3.10) [user@localhost ~]$
```
helloworld

---
寫一個在youtube上產生字幕的檔案
可參考: https://sysadminxpert.com/install-ffmpeg-on-centos-7/
Step 1: Install EPEL Repository on CentOS 7
安裝第三方軟體資料庫
輸入: yum install epel-release
```
[root@localhost user]# yum install epel-release
```

Step 2: Install RPM Fusion repository
安裝RPM Fusion
輸入: yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
```
[root@localhost user]# yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
```
Step 3: Install FFmpeg on CentOS
安裝FFmpeg,FFmpeg主要是在處理影像壓縮、播放、解壓縮的軟體
輸入: yum install ffmpeg ffmpeg-devel
```
[root@localhost user]# yum install ffmpeg ffmpeg-devel
```
再來安裝vlc,vlc是一個多媒體撥放器
可參考網址:https://www.myfreax.com/how-to-install-vlc-on-centos-7/
```
sudo yum install epel-release
sudo yum install https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
sudo yum install vlc
```
建立一個sub.py的檔案
檔案內容寫一個自動產生字幕的檔案
程式碼來源:https://github.com/smallko/test-whisper/blob/main/gen_sub.py
```
# Import the module
from pytube import YouTube
import whisper
import torch
import os
from whisper.utils import get_writer
# Initialize the device
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the model
whisper_model = whisper.load_model("small", device=device)
def download_video(video_URL, destination, final_filename):
video = YouTube(video_URL)
progMP4 = video.streams.filter(progressive=True, file_extension='mp4')
#print(progMP4)
targetMP4 = progMP4.order_by('resolution').desc().first()
#print(targetMP4)
video_file = targetMP4.download()
#print(video_file)
_, ext = os.path.splitext(video_file)
new_file = final_filename + '.mp4'
# Change the name of the file
os.rename(video_file, new_file)
# Video to Audio
video_URL = 'https://www.youtube.com/watch?v=-yLR4ynEK4I&t=91s' //影片連結
destination = "."
final_filename = "p4switch1"
download_video(video_URL, destination, final_filename)
print("download is done")
mp4file = final_filename + ".mp4"
result = whisper_model.transcribe(mp4file)
# Print the final result
#print(result["text"])
# save SRT
srt_writer = get_writer("srt", destination)
srt_writer(result, final_filename)
```
安裝openai-whisper
參考連結:https://github.com/openai/whisper
到python的環境輸入: pip install -U openai-whisper
```
(mypython3.10) [user@localhost ~]$ pip install -U openai-whisper
```
輸入:pip install git+https://github.com/openai/whisper.git
然後安裝pytube
輸入:pip install pytube
安裝完成後
執行剛剛建立好的產生字幕的檔案sub.py
vlc就會撥放下載下來的影片,影片內容會產生字幕
---
虛擬環境介紹:
1.VMWare
2.VitualBox(Oracle)
3.Hyper-V(windows)
4.kvm(linux)
---
### 2023/03/21
安裝linux至少要有兩個磁碟分割區
1. / 根目錄
2. swap
linux的管理者名稱:root
windows的管理者名稱:administrator
---
linux的編輯指令:
ctrl+a:移到命令字首
ctrl+e:移到命令字尾
ctrl+d:刪除浮標後的所有指令

linux有自動補權的功能
ex:
有一個檔案叫做test.c
```
[user@localhost ~]$ echo t
按下tab
[user@localhost ~]$ echo test.c
會自動補完後面的字
```
指令:touch
用來快速創建立檔案
linux下的大小寫是有區分的
```
[user@localhost ~]$ touch 123123123.txt
[user@localhost ~]$ ls
123123123.txt
[user@localhost ~]$ touch aaa
[user@localhost ~]$ touch AAA
[user@localhost ~]$ ls
aaa
AAA
```

執行檔的副檔名是用來參考的或提醒此檔案可能是什麼檔案類型
ex:test.txt可能是一個文字檔或執行檔
```
[user@localhost ~]$ echo "echo hi" > a.txt
[user@localhost ~]$ cat a.txt
echo hi
[user@localhost ~]$ ./a.txt
bash: ./a.txt: Permission denied
[user@localhost ~]$ chmod +x a.txt
[user@localhost ~]$ ./a.txt
hi
```
Permission denied(沒有執行權限)
chmod +x (加入可執行的權限)
chmod(change mode), x(executable)

---
linux的開機流程
1. 載入BIOS
BIOS:基本檢測硬體,檢測硬體資訊
檢測完畢後,讀取硬碟的MBR
MBR:載入開機的讀取程式Bootloader
Bootloader:作業系統的載入程式,會從磁碟分割區載入作業系統到記憶體,在記憶體裡開始執行配置一些載入驅動程式
載入第一支應用程式名稱:systemd(new),init(old),這兩支程式的pid=1
pid(process id),process(行程)
指令:pstree
顯示linux作業系統的行程表
載入第一支應用程式名稱是systemd

2. GRUB系統啟動軟體
GRUB:linux預設使用的開機管理系統
可利用GRUB進入單人模式(維護系統時使用),進行系統的修復
如何進入單人模式,和更改root password
參考連結: https://www.unixmen.com/reset-root-password-centos-7/
開機選單系統

3.10.0-1160,3為主要版本,10為次要版本,1160為補丁的次數
選擇進入第一個核心後
找到ro開頭,然後編輯

ro改成 “**rw init=/sysroot/bin/sh**”

按下**Ctrl+X**進入


輸入:
```
:/# chroot /sysroot
:/# passwd root
:/# touch /.autorelabel
:/# exit
:/# reboot
```
這邊可以變更超級使用者密碼
3. Linux核心載入
核心是一套與硬體、BIOS溝通的複雜程式
和新黨讀取至電腦的主記憶體
負責與硬體的溝通工作
4. 執行init(systemd)
啟動系統中所配置的服務程式
設定環境變數、檢查分割區、掛載檔案系統等
預設的系統啟動模式(runlevel)
若想看開機時執行訊息
輸入 dmesg | more
```
[root@localhost user]# dmesg | more
```

---
指令:netstat -tunlp | grep port
netstat(看系統服務有無執行)
-t(tcp), u(udp), n(不解析), l(listen), p(press)
```
[root@localhost user]# netstat -tunlp | grep 80
tcp6 0 0 :::80 :::* LISTEN 2566/httpd
[root@localhost user]# netstat -tunlp | grep 22
```
顯示出httpd有開啟,sshd無開啟
---
系統啟動模式




---
指令:uname
顯示現在使用的作業系統
```
[root@localhost user]# uname
Linux
[root@localhost user]# uname -a
Linux localhost.localdomain 3.10.0-1160.90.1.el7.x86_64 #1 SMP Thu May 4 15:21:22 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost user]# uname -r
3.10.0-1160.90.1.el7.x86_64
```
-a(顯示完整的詳細資訊)
-r(顯示kernal的資訊)
指令:cat /etc/redhat-release
```
[root@localhost user]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
```

---
[user@localhost ~]$
使用者名稱:user
主機名稱:localhost
目前目錄:~

想知道主機名稱
指令:hostname
```
[root@localhost user]# hostname
localhost.localdomain
```
更改主機名稱
指令:hostnamectl set-hostname 新的主機名稱
```
[root@localhost user]# hostnamectl set-hostname centos7-2
[root@localhost user]# bash
[root@centos7-2 user]#
```

指令:echo $PS1
PS1(系統使用者名稱)
```
[root@centos7-2 user]# echo $PS1
[\u@\h \W]\$
```
使用者名稱:\u
主機名稱:\u
目前目錄:\W
---
Shell簡介

Shell的按鍵

ls用法
指令:ls -a
```
[user@localhost ~]$ ls -a
. .cache miniconda3
.. cmd Miniconda3-latest-Linux-x86_64.sh
123123123.txt .conda Miniconda3-latest-Linux-x86_64.sh.1
20230530 .condarc .mozilla
3M .config Music
a c.txt Pictures
aa d Public
aaa date.txt .python_history
a.aaa .dbus sub.py
aaa.txt Desktop subtittle.py
a.txt Documents Templates
A.txt Downloads test
A.Txt .esd_auth test.c
b .gstreamer-0.10 testdir
.bash_history .gtkrc-2.0-kde4 testfifo
.bash_logout hello test.py
.bash_profile hello.c test.txt
bashrc helloworld.py Videos
.bashrc hi.txt .viminfo
b.txt .kde
c .local
[user@localhost ~]$ ls
123123123.txt b hello subtittle.py
20230530 bashrc hello.c Templates
3M b.txt helloworld.py test
a c hi.txt test.c
aa cmd miniconda3 testdir
aaa c.txt Miniconda3-latest-Linux-x86_64.sh testfifo
a.aaa d Miniconda3-latest-Linux-x86_64.sh.1 test.py
aaa.txt date.txt Music test.txt
a.txt Desktop Pictures Videos
A.txt Documents Public
A.Txt Downloads sub.py
[user@localhost ~]$ ls -l
total 147232
-rw-rw-r-- 1 user user 0 Jun 4 17:35 123123123.txt
-rw-rw-r-- 1 user user 0 May 30 09:23 20230530
-rw-rw-r-- 1 user user 3145728 Apr 18 11:51 3M
-rw-rw-r-- 1 user user 0 May 16 09:26 a
-rw-rw-r-- 1 user user 0 May 2 09:24 aa
-rw-rw-r-- 1 user user 0 May 2 09:24 aaa
-rw-r--r-- 1 root root 0 Apr 25 10:49 a.aaa
-rwxrwxr-x 1 user user 8 Mar 21 13:25 aaa.txt
-rwxrwxr-x 1 user user 8 Jun 4 17:43 a.txt
-rw-rw-r-- 1 user user 0 Apr 25 10:56 A.txt
-rw-rw-r-- 1 user user 0 Apr 25 10:56 A.Txt
-rw-rw-r-- 1 user user 0 May 16 09:26 b
-rw-rw-r-- 1 user user 0 Mar 14 10:17 bashrc
-rw-rw-r-- 1 user user 49 Apr 25 09:47 b.txt
-rw-rw-r-- 1 user user 0 May 16 09:26 c
drwxrwxr-x 2 user user 22 Apr 18 11:37 cmd
-rw-rw-r-- 1 user user 56 Apr 25 09:51 c.txt
-rw-rw-r-- 1 user user 0 May 16 09:26 d
-rw-r--r-- 1 root root 29 May 16 10:27 date.txt
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Desktop
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Documents
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Downloads
-rwxr-xr-x 1 root root 8360 May 29 22:58 hello
-rw-r--r-- 1 root root 92 May 29 22:58 hello.c
-rw-rw-r-- 1 user user 20 Jun 2 20:05 helloworld.py
-rw-rw-r-- 1 user user 3 Mar 14 09:25 hi.txt
drwxrwxr-x 17 user user 267 Mar 14 09:59 miniconda3
-rw-r--r-- 1 root root 74403966 Feb 8 11:48 Miniconda3-latest-Linux-x86_64.sh
-rw-r--r-- 1 root root 73134376 Apr 24 22:26 Miniconda3-latest-Linux-x86_64.sh.1
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Music
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Pictures
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Public
-rw-rw-r-- 1 user user 1188 Jun 2 21:24 sub.py
-rw-rw-r-- 1 user user 1188 Mar 14 10:56 subtittle.py
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Templates
drwxrwxr-x 2 user user 58 Apr 25 11:21 test
-rwxrwxr-x 1 user user 8440 Mar 7 15:37 test.c
drwxrwxr-x 2 user user 168 May 16 09:49 testdir
prw-rw-r-- 1 user user 0 Apr 25 10:33 testfifo
-rw-rw-r-- 1 user user 21 Mar 14 10:36 test.py
-rw-rw-r-- 1 user user 8 Feb 21 11:38 test.txt
drwxr-xr-x. 2 user user 6 Feb 21 10:16 Videos
```
-a(-all)會顯示所有的檔案
.開頭的是隱藏檔案,也會顯示出隱藏檔案
-l 顯示長格式,會顯示此檔案的使用者權限,使用檔案的時間
Linux的指令規格

完整指令ex:ls -l /home(ls指令 -l選項 /home參數)

---
變更使用者密碼
指令:passwd 使用者


查詢目前身分
指令:whoami
```
[user@localhost ~]$ whoami
user
[user@localhost ~]$ su
Password:
[root@centos7-2 user]# whoami
root
```
---
### 2023/03/28
Linux的檔案系統
* 單藝術狀結構
* 只有一個最高節點-根目錄 /
* 所有檔案或目錄都是由根目錄往下延伸

Linux的主要目錄
/bin
此目錄放的是可執行檔,內容為binary機器碼的檔案

/etc
根目錄下的etc目錄是Linux系統最重要的目錄之一。/etc/裡放置所有系統的設定檔

/dev
系統設備目錄。所有裝置與目錄在Linux,都在檔案或目錄方式存在於/dev/目錄中
/boot
核心檔案目錄,專門存放開機必須使用的核心檔案,系統安裝時都會備份此目錄
/var
變動性與系統等待排隊處理的檔案都放在/var/目錄

/tmp
存放暫時性的檔案,通常系統重開機或關機會自動清除此目錄下的檔案
/mnt
暫時性的檔案系統掛載目錄
Windows的檔案系統
FAT16
FAT32
exFAT
NTFS
檔案詳細資訊
指令:ls -l


快速生成檔案
指令:touch {a..d}.副檔名

複製檔案
指令:cp 來源檔案 目的地/檔案名稱


移動檔案
指令:mv 檔名 目的地

刪除檔案
指令:rm 檔案名稱


rm -i 會詢問是否刪除檔案

---
線上協助文件
指令:man 完整指令名稱
會查詢一般的指令

ex:man ls

查詢程式語言指令
whatis 完整指令

再用man打開操作手冊


info 說明書

cat指令高級用法 cat <<結束指令 > 檔案名稱
```
[user@centos7-2 test]$ cat <<EOF > b.txt
> hello world
> 123
> 456
> EOF
[user@centos7-2 test]$ cat b.txt
hello world
123
456
```
若沒有文字編輯器可以用這方法

---
head和tail的用法
head 顯示頭10個

tail 顯示尾10個

head -n 數字 顯示頭 "幾個"

tail -n 數字 顯示尾 "幾個"

若要顯示頭3個的尾2個,可以利用管道 |

---
### 2023/04/18
**Windows**下"**捷徑**"可以連結到任意的"**檔案**"或者是"**資料夾**"
左下角有個**小箭頭**

**Centos7**使用的檔案系統是"**XFS**"


EXT3檔案系統
存取資料時是以區塊傳遞到區塊,傳遞間可能會發生游離
檔案資訊記錄檔inode -> meta data
example:今天儲存a.txt a.txt的inode會說明a.txt的存放屬性:
file size/owner/group/time
指令stat 可以看到以下文件更詳細的屬性

指令ls -l -d/tmp 可以看到tmp資料夾的詳細屬性

指令su - tom 只從user切換到root再切換到tom

連結
建立硬連結限制:不可跨分割區、目錄
符號連結與硬連結的不同


建立符號連結
指令:ls -s
```
[root@centos7-2 user]# ln -s data slink
[root@centos7-2 user]# ls -i data slink
503357 data 503361 slink
```


data被刪除後,就連節不到
指令file可以查詢檔案的詳細內容

### 2023/04/25
硬碟使用空間情況
指令:df
```
[root@centos7-2 user]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 1995844 0 1995844 0% /dev
tmpfs 2012948 0 2012948 0% /dev/shm
tmpfs 2012948 12320 2000628 1% /run
tmpfs 2012948 0 2012948 0% /sys/fs/cgroup
/dev/mapper/cl-root 52403200 7077836 45325364 14% /
tmpfs 2012948 8 2012940 1% /tmp
/dev/nvme0n1p1 1038336 214372 823964 21% /boot
/dev/mapper/cl-home 49250820 3620332 45630488 8% /home
tmpfs 402592 48 402544 1% /run/user/1000
```
/dev/mapper/cl-root 52403200 7077836 45325364 14% /
根目錄磁碟使用情況,平時比較需要注意
df -h
```
[root@centos7-2 user]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 2.0G 13M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/cl-root 50G 6.8G 44G 14% /
tmpfs 2.0G 8.0K 2.0G 1% /tmp
/dev/nvme0n1p1 1014M 210M 805M 21% /boot
/dev/mapper/cl-home 47G 3.5G 44G 8% /home
tmpfs 394M 48K 394M 1% /run/user/1000
```
另一個能得到磁碟空間使用情況是利用du
想知道目前目錄使用的總空間可用
du -sh
```
[root@centos7-2 user]# du -sh
3.5G
```
---
指令的輸出入

標準輸入:鍵盤 0
標準輸出:螢幕 1
標準錯誤輸出:螢幕 2
ex:



---
管線的應用
A指令的輸出會變成B指令的輸入
指令B一定要能處理stdin,若不行就不能直接放

指令:which
用來搜尋執行檔,搜尋環境變數PATH裡有沒有特定的執行檔
```
[root@centos7-2 user]# echo $PATH
/home/user/miniconda3/condabin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/var/lib/snapd/snap/bin:/home/user/miniconda3/bin:/home/user/.local/bin:/home/user/bin:/home/user/miniconda3/bin
[root@centos7-2 user]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@centos7-2 user]# which find
/usr/bin/find
```
指令:locate
用來查詢特定的檔案位置,也可以找執行檔
使用前先輸入
updatedb
locate 想搜尋的檔案名稱
```
[root@centos7-2 user]# updatedb
[root@centos7-2 user]# locate a.txt
/home/user/a.txt
/home/user/aaa.txt
/usr/lib/firmware/brcm/brcmfmac43340-sdio.pov-tab-p1006w-data.txt
/usr/share/doc/vim-common-7.4.629/README_extra.txt
/usr/share/gnupg/help.ca.txt
/usr/share/gnupg/help.da.txt
/usr/share/gnupg/help.ja.txt
/usr/share/kde4/apps/khangman/ca.txt
/usr/share/kde4/apps/khangman/da.txt
/usr/share/kde4/apps/khangman/ga.txt
/usr/share/libhangul/hanja/hanja.txt
/usr/share/vim/vim74/doc/ft_ada.txt.gz
/usr/share/vim/vim74/doc/if_lua.txt.gz
/usr/share/vim/vim74/doc/os_amiga.txt.gz
/usr/share/vim/vim74/doc/uganda.txt.gz
```

指令:find
用來搜尋特定檔案
find進階用法參考網址:https://blog.gtwang.org/linux/unix-linux-find-command-examples/
```
[root@centos7-2 user]# find /home -name a
/home/user/.cache/pip/http/5/e/c/a
/home/user/.cache/pip/http/5/5/7/4/a
/home/user/.cache/pip/http/5/a
/home/user/.cache/pip/http/0/4/a
/home/user/.cache/pip/http/a
/home/user/.cache/pip/http/c/0/7/a
/home/user/.cache/pip/http/3/c/a
/home/user/.cache/pip/http/8/8/1/a
/home/user/.cache/pip/http/8/c/4/5/a
/home/user/.cache/pip/http/8/e/9/6/a
/home/user/.cache/pip/http/2/b/3/9/a
/home/user/.cache/pip/http/2/e/0/a
/home/user/.cache/pip/http/e/a
/home/user/.cache/pip/http/d/0/b/f/a
/home/user/miniconda3/pkgs/ncurses-6.4-h6a678d5_0/share/terminfo/a
/home/user/miniconda3/share/terminfo/a
/home/user/miniconda3/envs/mypython3.10/share/terminfo/a
/home/user/miniconda3/envs/mpython3.10/share/terminfo/a
/home/user/testdir/a
```
備份實務應用

準備一個資料夾
```
[root@centos7-2 test]# ls
file1 file2 file3
[root@centos7-2 test]# tree
.
├── file1
├── file2
└── file3
```
初次備份
```
[root@centos7-2 test]# tar cvfz backup-0425.tar.gz ./*
./file1
./file2
./file3
[root@centos7-2 test]# ls
backup-0425.tar.gz file1 file2 file3
```
備份完可以做個時間戳記
```
[root@centos7-2 test]# touch timebase
```
增量備份
```
[root@centos7-2 test]# tar cvfz backup-0425-2.tar.gz `find -type f -cnewer timebase`
./file1
./file4
```
會先執行find -type f -cnewer timebase
查詢那些檔案比時間戳記timebase還新的檔案作打包
---
### 2023/05/02
篩選特定文字
指令:grep 特定文字檔案 對象檔案
awk / grep / sedLinux三劍客
參考網址:https://blog.gtwang.org/linux/linux-grep-command-tutorial-examples/
```
[user@centos7-2 ~]$ grep Linux /etc/os-release
NAME="CentOS Linux"
PRETTY_NAME="CentOS Linux 7 (Core)"
```
在/etc/os-release搜尋Linux關鍵字
```
[user@centos7-2 ~]$ ls /usr/lib | grep java
java
java-1.5.0
java-1.6.0
java-1.7.0
java-1.8.0
java-ext
[user@centos7-2 ~]$ ls /usr/lib
alsa fontconfig java-ext locale sendmail
binfmt.d games jvm modprobe.d sendmail.postfix
cpp gcc jvm-commmon modules sse2
crda gems jvm-exports modules-load.d sysctl.d
cups grub jvm-private mozilla systemd
debug java kbd NetworkManager tmpfiles.d
dracut java-1.5.0 kde3 os-release tuned
environment.d java-1.6.0 kde4 polkit-1 udev
firewalld java-1.7.0 kdump python2.7 yum-plugins
firmware java-1.8.0 kernel rpm
```
搜尋出在/usr/lib目錄下的java字樣的資料
---
文字編輯器
vim
開啟檔案
指令:vi 檔名
按下esc輸入i、o或a可進入編輯模式
離開vi
按下: 輸入:wq
```
[user@centos7-2 ~]$ vi test.txt
文字編輯畫面
~
~
```
---
設備目錄
指令:ls -l /dev |more 顯示該目錄內檔案清單
```
[user@centos7-2 ~]$ ls -l /dev |more
total 0
crw-rw---- 1 root video 10, 175 Jun 8 15:36 agpgart
crw------- 1 root root 10, 235 Jun 8 15:36 autofs
drwxr-xr-x 2 root root 180 Jun 8 15:36 block
crw------- 1 root root 10, 234 Jun 8 15:36 btrfs-control
drwxr-xr-x 3 root root 60 Jun 8 15:36 bus
drwxr-xr-x 2 root root 2960 Jun 8 15:36 char
drwxr-xr-x 2 root root 100 Jun 8 15:36 cl
crw------- 1 root root 5, 1 Jun 8 15:36 console
lrwxrwxrwx 1 root root 11 Jun 8 15:36 core -> /proc/kcore
drwxr-xr-x 3 root root 60 Jun 8 15:36 cpu
crw------- 1 root root 10, 61 Jun 8 15:36 cpu_dma_latency
crw------- 1 root root 10, 62 Jun 8 15:36 crash
drwxr-xr-x 5 root root 100 Jun 8 15:36 disk
brw-rw---- 1 root disk 253, 0 Jun 8 15:36 dm-0
brw-rw---- 1 root disk 253, 1 Jun 8 15:36 dm-1
brw-rw---- 1 root disk 253, 2 Jun 8 15:36 dm-2
crw-rw----+ 1 root audio 14, 9 Jun 8 15:36 dmmidi
drwxr-xr-x 2 root root 80 Jun 8 15:36 dri
crw-rw---- 1 root video 29, 0 Jun 8 15:37 fb0
lrwxrwxrwx 1 root root 13 Jun 8 15:36 fd -> /proc/self/fd
crw-rw-rw- 1 root root 1, 7 Jun 8 15:36 full
crw-rw-rw- 1 root root 10, 229 Jun 8 15:36 fuse
crw------- 1 root root 246, 0 Jun 8 15:36 hidraw0
crw------- 1 root root 10, 228 Jun 8 15:36 hpet
drwxr-xr-x 2 root root 0 Jun 8 15:36 hugepages
crw------- 1 root root 10, 183 Jun 8 15:36 hwrng
lrwxrwxrwx 1 root root 25 Jun 8 15:36 initctl -> /run/systemd/initctl
/fifo
drwxr-xr-x 4 root root 300 Jun 8 15:36 input
--More--
```
掛載儲存設備
Linux中可使用mount來掛載設備
指令:mount -t檔案類型 設備檔案 掛載至系統目錄
使用fdisk指令為/dev/sdb 設備建立新的分割區
fdisk是Linux的硬碟分割區工具,可新增、刪除與修改分割區資料,按下**m**Enter後,顯示指令說明
常用的命令:
p:列出硬碟的分割區資訊
n:在硬碟中新增分割區
d:刪除分割區
t:變更某分割區的檔案類型
w:儲存目前對硬碟的修改,寫入硬碟
q:離開fdisk工具
格式化指令:mkfs -t 檔案類型 目的檔案名稱
---
### 2023/05/09
新增群組
指令:groupadd 群組名稱
建立完的群組會新增在**/etc/group**檔案最後
```
[root@centos7-2 user]# groupadd rd
[root@centos7-2 user]# cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
.
.
.
rd:x:1016:
[root@centos7-2 user]#
```
新增使用者帳號
指令:useradd 使用者名稱
若沒指定使用者在那些群組,會自動生成使用者名稱的群組
```
[root@centos7-2 user]# useradd mike
[root@centos7-2 user]# cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
.
.
.
rd:x:1016:
mike:x:1017:
[root@centos7-2 user]#
```
使用者的帳號資料儲存在**/etc/passwd**中
```
[root@centos7-2 user]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
.
.
.
mike:x:1016:1017::/home/mike:/bin/bash
[root@centos7-2 user]#
```
mike:x:1016:1017::/home/mike:/bin/bash
此帳號資料有七項資訊,每一項都用:隔開
1. 帳號名稱
2. 密碼
3. 帳號編號
4. 所屬群組編號
5. 註解
6. 家目錄
7. 登陸時所使用的shell
指令:useradd 使用者 -g rd -G group
rd主要社團,group次要社團
```
[root@centos7-2 user]# useradd mikey -g rd -G group
[root@centos7-2 user]# id mikey
uid=1017(mikey) gid=1016(rd) groups=1016(rd),1018(group)
```
查詢使用者相關資訊,有無此使用者
指令:id 使用者名稱
```
[root@centos7-2 user]# id mike
uid=1016(mike) gid=1017(mike) groups=1017(mike)
```
刪除帳號
指令:userdel 已有的使用者
```
[root@centos7-2 user]# userdel mikey
[root@centos7-2 user]# id mikey
id: mikey: no such user
[root@centos7-2 user]#
```
但在家目錄的使用者檔案不會被刪除
指令:userdel -r 已有的使用者
加上 -r可刪除家目錄的資料
```
[root@centos7-2 user]# ls -l /home
total 4
drwx------ 6 bojun bojun 158 May 29 16:10 bojun
drwx------ 3 jack jack 78 May 16 11:02 jack
drwx------ 3 mike mike 78 Jun 10 00:25 mike
drwx------ 3 1017 rd 78 Jun 10 10:48 mikey
[root@centos7-2 user]# userdel -r mike
[root@centos7-2 user]# ls -l /home
total 4
drwx------ 6 bojun bojun 158 May 29 16:10 bojun
drwx------ 3 jack jack 78 May 16 11:02 jack
drwx------ 3 1017 rd 78 Jun 10 10:48 mikey
```
刪除群組
指令:groupdel 群組名稱
但刪除群組前必須先清空群組內的使用者
```
[root@centos7-2 user]# groupadd testgroup
[root@centos7-2 user]# groupdel testgroup
[root@centos7-2 user]# groupdel mike
groupdel: group 'mike' does not exist
```
查詢使用者的群組
指令:groups 使用者
```
[root@centos7-2 user]# groups bojun
bojun : bojun
```
停止帳號登入
使用文字編輯器打開/etc/passwd,將想停用的使用者的紀錄中,第二個密碼欄位**x**,修改成**!**
```
[root@centos7-2 user]# vi /etc/passwd
.
.
jack:!:1015:1015::/home/jack:/bin/bash
-- INSERT --
```
指令:chsh 使用者
chsh(change shell)用來更改帳號登入後的shell
```
[root@centos7-2 user]# chsh jack
Changing shell for jack.
New shell [/bin/bash]: /sbin/nologin
Shell changed.
```
---
### 2023/05/16
認識檔案與目錄權限
mkdir -p 目錄名稱: 加上-p,如果資料夾不存在,則創建資料夾,如果資料夾存在,則不動作,也不會有錯誤發生
指令:ls -ld 資料夾
查看資料夾的屬性
指令:ls -l 資料夾
查看資料夾內部檔案的屬性
```
[root@centos7-2 user]# mkdir testdir
mkdir: cannot create directory ‘testdir’: File exists
[root@centos7-2 user]# mkdir testdir -p
[root@centos7-2 user]# ls -l testdir
total 0
-rw-rw-r-- 1 user user 0 May 16 09:27 a
-rw-rw-r-- 1 user user 0 May 16 09:50 a1.txt
-rw-rw-r-- 1 user user 0 May 16 09:50 a2.txt
-rw-rw-r-- 1 user user 0 May 16 09:50 a3.txt
-rw-rw-r-- 1 user user 0 May 16 09:27 b
-rw-rw-r-- 1 user user 0 May 16 09:50 b1.txt
-rw-rw-r-- 1 user user 0 May 16 09:50 b2.txt
-rw-rw-r-- 1 user user 0 May 16 09:50 b3.txt
-rw-rw-r-- 1 user user 0 May 16 09:27 c
-rw-rw-r-- 1 user user 0 May 16 09:50 c1.txt
-rw-rw-r-- 1 user user 0 May 16 09:50 c2.txt
-rw-rw-r-- 1 user user 0 May 16 09:50 c3.txt
-rw-rw-r-- 1 user user 0 May 16 09:27 d
[root@centos7-2 user]# ls -ld testdir
drwxrwxr-x 2 user user 168 Jun 9 23:16 testdir
```
-rw-rw-r-- 1 user user 0 May 16 09:50 a1.txt
-rw-rw-r-- 此為權限
rw-(擁有者權限)rw-(群組權限)r--(其他人權限)
變更檔案權限
指令:chmod 更改的權限 檔案
各種權限代表的值
**0 _
1 x
2 _w_
3 _wx
4 r
5 r_x
6 rw_
7 rwx**
數字表示法
指令:chmod 數字組合 檔案
```
[root@centos7-2 user]# ls -ld testdir
drwxrwxr-x 2 user user 168 Jun 9 23:16 testdir
[root@centos7-2 user]# chmod 644 testdir
[root@centos7-2 user]# ls -ld testdir
drw-r--r-- 2 user user 168 Jun 9 23:16 testdir
```
語意描述法
使用英文(u、g、o、a)加上+或-(+增加權限,-刪除權限)來更改權限
u(user) 使用者
g(group) 群組
o(other) 其他人
a(all) 所有人=ugo
```
[root@centos7-2 user]# ls -ld testdir
drwxrwxr-x 2 user user 168 Jun 9 23:16 testdir
[root@centos7-2 user]# chmod 644 testdir
[root@centos7-2 user]# ls -ld testdir
drw-r--r-- 2 user user 168 Jun 9 23:16 testdir
[root@centos7-2 user]# chmod u+x testdir
[root@centos7-2 user]# ls -ld testdir
drwxr--r-- 2 user user 168 Jun 9 23:16 testdir
```
變更檔案擁有者
指令:chown 使用者帳號.群組 檔案或目錄
```
[root@centos7-2 user]# chown jack.rd testdir
chown: invalid user: ‘jack.rd’
```
---
查看行程狀態
指令:ps
```
[root@centos7-2 user]# ps
PID TTY TIME CMD
7414 pts/1 00:00:00 su
7421 pts/1 00:00:00 bash
8986 pts/1 00:00:00 ps
[root@centos7-2 user]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 7414 7362 0 11:02 pts/1 00:00:00 su
root 7421 7414 0 11:03 pts/1 00:00:00 bash
root 8993 7421 0 11:32 pts/1 00:00:00 ps -f
```
加上 -f能看到更詳細的資料
互動工具-top
指令:top
top是個互動式的行程觀察工具,可以看到cpu與記憶體的整體資訊
按下q可退出
```
[root@centos7-2 user]# top
```

第一行top
top-11:34:46為目前時間,up 1 day 19:58代表主機已開機1天又19小時58分鐘load average:0.00, 0.01, 0.05三個數字代表系統1, 5, 15分鐘的平均執行的行程數,越低代表系統負荷越輕
第二行Tasks
Tasks:189 total代表系統中共有189個行程,1個行程正在執行,其餘188個在睡眠
第三行Cpu(s)
cpu的負載狀況,us代表使用率
第四行Mem
系統記憶體的使用狀況,圖中共有402MB(4025896k)的記憶體
第五行Swap
系統中swap暫存檔的使用狀態
背景行程
指令:想放在背景執行的程式 &
```
[root@centos7-2 user]# sleep 3
_
[root@centos7-2 user]# sleep 60 &
[1] 9864
```
sleep 3會在前景執行,會停頓個3秒
sleep 60 &會丟到背景執行,會停頓個60秒
```
[root@centos7-2 user]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 7414 7362 0 11:02 pts/1 00:00:00 su
root 7421 7414 0 11:03 pts/1 00:00:00 bash
root 9949 7421 0 11:51 pts/1 00:00:00 ps -f
[1]+ Done sleep 60
```
可以觀察到sleep 60在背景中執行
快捷鍵:Ctrl+z 可以將目前前景行程停止
指令:jobs
用來觀察背景與前景行程
```
[root@centos7-2 user]# sleep 60 &
[1] 10079
[root@centos7-2 user]# sleep 24 &
[2] 10091
[root@centos7-2 user]# sleep 55 &
[3] 10096
[root@centos7-2 user]# jobs
[1] Running sleep 60 &
[2]- Running sleep 24 &
[3]+ Running sleep 55 &
```
指令:fg
把背景行程移到前景
指令:bg
把前景行程移到背景
```
[root@centos7-2 user]# sleep 60 &
[1] 10252
[root@centos7-2 user]# sleep 24 &
[2] 10256
[root@centos7-2 user]# sleep 55 &
[3] 10260
[root@centos7-2 user]# fg 2
sleep 24
^Z
[2]+ Stopped sleep 24
[root@centos7-2 user]# bg 2
[2]+ sleep 24 &
[root@centos7-2 user]# jobs
[1] Running sleep 60 &
[2]- Done sleep 24
[3]+ Running sleep 55 &
[root@centos7-2 user]#
```
---
### 2023/05/23
Linux細部權限ACL
ACL:存取(access),控制(control),列表(list)
可以讓檔案的權限更細緻化、客製化
```
權限:
ls -l _ _ _(owner)_ _ _(group)_ _ _(others)
```
目前只有user擁有更改的權限

```
bash
[bojun@localhost tmp]$ getfacl a.txt //f(file)acl(access control list)
# file: a.txt
# owner: user
# group: user
user::rw-
group::rw-
other::r--
```

```
bash
[user@localhost tmp]$ setfacl -m u:bojun:rw- a.txt //-m(modify) u:(user)
[user@localhost tmp]$ getfacl a.txt
# file: a.txt
# owner: user
# group: user
user::rw-
user:bojun:rw-
group::rw-
mask::rw-
other::r--
```
增加了bojun使用者可編輯的權限


---
SRE(Site Reliability Engineering)"網站可靠性工程"
影響可靠性的因素很多,其中有幾個重要的指標:
MTBF(Mean Time Between Failure)"平均故障間隔時間"
越高的MTBF表示故障頻率越低
MTTR(Mean Time To Repair)"平均修復時間"
越低的MTTR表示每次發生錯誤時造成個影響會更小
系統管理

spf(single point failure)
若伺服器的系統有些硬體設備只有一份,若某天壞掉,伺服器就掛了
FirewallID防火牆
指令:firewall-cmd --state
查詢防火牆是否執行中
```
[root@centos7-2 user]# firewall-cmd --state
not running
```
---
### 2023/0530
```
bash
date +%Y%m%d //Y年 m月 d日
```
NTP(網路校時協定)
指令:ntpdate
可連接至NTP伺服器進行校時
```
[root@centos7-2 user]# ntpdate watch.stdtime.gov.tw
10 Jun 12:22:57 ntpdate[11575]: adjust time server 118.163.81.63 offset 0.001855 sec
[root@centos7-2 user]#
```
Linux的排程工作檔案是/etc/crontab
```
[root@centos7-2 user]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
[root@centos7-2 user]#
```