# Linux2 [toc] ## week1 ssh、scp centos: 更改主機名稱 (sudo) 查看`hostname` 更改 `hostname set-hostname [new_name]` > hostnamectl set-hostname mycentos7-1 生效 `bash` ![](https://hackmd.io/_uploads/r1rS_KTA2.png) ssh server > 檢查軟體(ex: ssh)是否已安裝 rpm -qa | grep ssh q: quert a: all rpm: RedHat Package Manager(RedHat軟件包管理工具) 若有 openssh-server 就代表已安裝 > 移除軟體 rpm -e openssh-server e: erase rpm -qa | grep ssh 檢查是否有移除 > 安裝 yum install openssh-server 啟動server >systemctl [action] [server_name] systemctl status sshd action: start、stop、restart、reload(重新加載配置檔)、status Active: active(running) 啟動 Active: inactive(dead) 未啟動 Loaded .... : enabled 重新開機時,自動啟動 Loaded .... : disenabled 重新開機時,不自動啟動 Process ID (黃色) ![](https://hackmd.io/_uploads/SyZ0tFpRh.png) 設定重新開機時,不自動啟動 systemctl disable sshd 設定重新開機時,自動啟動 systemctl enable sshd GUI設定IP address ![](https://hackmd.io/_uploads/ByqPAK6C2.png) ![](https://hackmd.io/_uploads/ryWbRKT03.png) ![](https://hackmd.io/_uploads/SyVpTFT0h.png) 修改 /etc/hosts 透過自訂名稱代表IP ![](https://hackmd.io/_uploads/HJBee96C3.png) 使用 ssh 登錄到其他機器 ![](https://hackmd.io/_uploads/rJGQb96An.png) ## week2 cmdline movement `ctrl` + `a` move to head `ctrl` + `e` move to end ![](https://hackmd.io/_uploads/S10HFhIy6.png) > 0.0.0.0:22 代表 任一介面都能連線 > 1.2.3.4:22 只有 1.2.3.4 可連線 若putty無法連線 1. 檢查server是否running > `systemctl status sshd` 2. 確認port number是否合法 > `netstate -tulnp | grep ssh` t : tcp, u : udp, l : listen, n : 不解析, p : 程式的 process id 3. 檢查selinux (security linux) > `getenforce` 應該為 Disabled > gedit /etc/selinux/config ![](https://hackmd.io/_uploads/SyeRin8JT.png) > SELINUX=disabled > `reboot` 4. 檢查防火牆 `firewalld` `systemctl stop firewalld` 5. 重新新增網路卡 ![](https://hackmd.io/_uploads/Hy6H23816.png) 6. 確認網卡的Subnet Address和VM的相同 ![](https://hackmd.io/_uploads/Byrfa2UJT.png) --- ### 無密碼遠端登入 (重要) step 1. 產生公鑰私鑰 ![](https://hackmd.io/_uploads/rkiRGaL1p.png) > 若有.ssh 刪除它 ssh-keygen ![](https://hackmd.io/_uploads/r14QX6IJp.png) .ssh/id_rsa 私鑰 .ssh/id_rsa.pub 公鑰 把公鑰複製給centos7-1 ssh-copy-id root@centos7-1 ssh root@centos7-1 ### SCP (重要) 檔案拷貝 scp [src] [dst]:[位置] `scp /etc/hosts root@centos7-1:/etc/hosts` or `scp /etc/hosts root@centos7-1:/etc/` 拷貝資料夾要 加 -r mkdir testdir cd testdir touch {a..z} `scp -r testdir/ root@centos7-1:/tmp` 查看結果 (不需要切換VM) `ssh [帳號] [指令]` [centos7-2 ~]# ssh root@centos7-1 ls /tmp ### 改變server埠號 cd /etc/ssh ssh_config (client配置檔) vim sshd_config (server配置檔) 把Port 22改成其他數字(2222) 檢查: systemctl restart sshd netstat -tulnp | grep ssh 連線 ssh -p 2222 root@centos7-1 --- ### 第十章 軟體套件的管理 [CentOS]安裝ssh伺服器 `yum install openssh-server` RPM (一種Redhat的套件軟體標準): 先將軟體編譯好 可直接安裝(cmd) > DPKG 則是Ubuntu使用的套件軟體標準(概念相同) > RPM 和 DPKG 是兩大主流 (重要) 查詢系統是否已安裝的系統套件(httpd) `rpm -qa | grep httpd` q:query a:all 或是 `rpm -qi httpd` (重要!) 0919 04:12~13 i: infomation l: list f: file ![](https://hackmd.io/_uploads/BJLAUCLJp.png) 安裝選項 (少用,因為有依賴性問題,所以常用yum) `rpm -ivh rpm檔名` -i: install -v: verbose 詳細資訊 -h: 進度條 更新套件 rpm -Uvh 套件名稱 -U: 更新 移除套件 rpm -e 套件名稱 -e: earse ![](https://hackmd.io/_uploads/r1lOKNCUyp.png) (重要) noarch是指所有架構都可以! arch: architecture 主要版本.次要版本.更新-修訂 偶數版本通常是穩定版本 --- ## Week3 ### 網頁伺服器 伺服器無法運行時,檢查: 1. 使用super user 2. getenforce->Disabled 3. systemctl status firewalld —>inactive 4. netstat -tunlp | grep xxx 檢查開啟的port --- 1. 檢查是否已安裝網頁server rpm -qu | grep httpd > 安裝 yum install httpd -y 2. 檢查網頁server是否啟動 systemctl status httpd > 啟動 systemctl start httpd 3. 檢查port number > sudo netstat -tunlp | grep httpd 4. 建立web default: /var/www/html echo "hello world, Tom" > hi.htm ![](https://hackmd.io/_uploads/B1HcxeglT.png) 目前架設的www server只能運作在內部網路...外部網路internet是無法存取的 ---- GUI web 將word檔案 以 html 儲存並放到/var/www/html ![](https://hackmd.io/_uploads/r15RfgleT.png) --- 將web放到外網 1. 安裝ngrok > https://dashboard.ngrok.com/get-started/setup 2. 認證 > https://dashboard.ngrok.com/get-started/your-authtoken 3. 啟動 > ./ngrok http 80 ![](https://hackmd.io/_uploads/ByTD7ZlgT.png) (點擊 Visit Site) ![](https://hackmd.io/_uploads/Hy5eEWxxT.png) --- yum的常見功能 1. 安裝套件 > yum install 套件名稱 2. 移除套件 > yum remove 套件名稱 3. 搜尋套件 > yum search 文字 4. 列出所有套件資訊 > yum list --- 系統訊息(ex: 運行時間、使用者數、負載、Task、CPU、Memory...) `top` htop : 進階版top 安裝 wget https://src.fedoraproject.org/lookaside/extras/htop/htop-2.2.0.tar.gz/sha512/ec1335bf0e3e0387e5e50acbc508d0effad19c4bc1ac312419dc97b82901f4819600d6f87a91668f39d429536d17304d4b14634426a06bec2ecd09df24adc62e/htop-2.2.0.tar.gz tar xvzf htop-2.2.0.tar.gz (step1)./configure 檢查library是否足夠 若沒問題,自動產生Makefile ![](https://hackmd.io/_uploads/B1QaiZlxp.png) 若有問題 ![](https://hackmd.io/_uploads/Bk3Z2WllT.png) (step2) make 編譯 (step3) make install 把執行檔、配置檔放到對應位置 (step4) htop 執行 ![](https://hackmd.io/_uploads/SJRpn-xlp.gif) --- ### ![期中考](https://hackmd.io/_uploads/r1R5kGlx6.png) du - estimate file space usage du -h (更易懂的單位) ![](https://hackmd.io/_uploads/r1e5gflx6.png) 93M . 代表當前目錄(/home/user)佔用了93M > 直接看 du -h -s ![](https://hackmd.io/_uploads/SJ_1-MegT.png) du -h -s /etc du -h -s /var 查看/home下第一層的使用情況 du -h --max-depth=1 /home (所有使用者都在/home下) du -h --max-depth=1 /etc ---- df 查看磁碟分割區的情形 df -h ![](https://hackmd.io/_uploads/BJMBffelp.png) df -h | grep /$ | awk '{print $5}' | tr "%" " " 以/結尾 第6個區塊 以" "取代"%" `ava=$(df -h | grep /$ | awk '{print $5}' | tr "%" " ")` 以ava變數 儲存 (自訂變數名稱) 查看 echo $ava ## Week4 ### NFS(Network File System) server > client-server model [nfs-server]機器1 192.168.245.100 [nfs-client]機器2 192.168.245.101 [nfs-clinet]機器3 192.168.245.150 允許所有機器 共用 同一個資料夾(名稱不用統一) 1. 建立 資料夾 [機器1] mkdir /data -p [機器2、3] mkdir /nfs-data -p 2. Server Setting [機器1] yum install nfs-utils -y [機器1] systemctl enable rpcbind [機器1] systemctl enable nfs [機器1] systemctl start rpcbind [機器1] systemctl start nfs [機器1] vim /etc/exports > 輸入以下內容 /data 192.168.245.0/24(rw,sync,no_root_squash,no_all_squash) /data 是 共用的資料夾 192.168.245.0/24 是 共用的網段 [機器1] systemctl restart nfs [機器1] showmount -e localhost > 檢查是否出現 共用資料夾 共用網段 3. Client Setting [機器2、3] yum install nfs-utils -y [機器2、3] systemctl enable rpcbind [機器2、3] systemctl start rpcbind [機器2、3] showmount -e 192.168.245.100 > 192.168.245.100 是 Server IP > 檢查是否出現 共用資料夾 共用網段 [機器2、3] mount -t nfs 192.168.245.100:/data /nfs-data > -t : type > Server:共享的資料夾 192.168.245.100:/data > 這台機器要載的資料夾 /nfs-data [機器2、3] 接著這3台機器的對應資料夾便會同步 --- df -h 也可看到掛載資料夾 ![](https://hackmd.io/_uploads/SkDx_Ntx6.png) ### dd 檔案複製(磁碟機拷貝) 常用來 產生任意大小的檔案 `dd if=/dev/zero of=file3m bs=1M count=3` if: input file of: output file /dev/zero 零裝置 (虛擬裝置) 用來產生很多0 /dev/null 黑洞 (虛擬裝置) 隱藏輸出 bs: block size count 次數 [考試] ex: 產生 100M 的檔案 名稱為file100m dd if=/dev/zero of=file100m bs=1M count=100 ls -lh(查看) ### wc (word count) 用法1 wc [file] [root@centos7-1 data]# cat -n test.txt 1 hello world 2 tom mary john 3 4 1234 nqu 5 peter [root@centos7-1 data]# wc test.txt 5 8 42 test.txt > 行數(l) 字數(c) 字母數(w) 用法2 wc [file] [OPTION] 只顯示當一輸出 [root@centos7-1 data]# wc -l test.txt 5 test.txt [root@centos7-1 data]# wc -c test.txt 42 test.txt [root@centos7-1 data]# wc -w test.txt 8 test.txt 用法3 wc [nofile(standard input)] cat test.txt | wc -i ![](https://hackmd.io/_uploads/HyaU24tlp.png) ### tr 取代或刪除字元 用法1 取代 df -h | grep /$ | awk '{print $5}' > 41% df -h | grep /$ | awk '{print $5}' | tr "%" " " > 41 用法2 轉換 echo "ABCD" | tr [:upper:] [:lower:] > abcd echo "AbCd" | tr [:upper:] [:lower:] > abcd echo "AbCd" | tr 'A-Z' 'a-z' > abcd > 'A-Z'字符集1、'a-z'字符集2 ![](https://hackmd.io/_uploads/SyPybHKxp.png) -d ![](https://hackmd.io/_uploads/S1-bUStxa.png) echo "HELLO WORLD1234" | tr -d '0-9' > 1234(開頭是空白) echo "HELLO WORLD1234" | tr -d '0-9 ' > HELLOWORLD Tab --> Space `cat test.txt ` ``` hello world tom mary john 1234 nqu peter ``` `cat -T test.txt` ``` hello world tom^Imary^Ijohn 1234 nqu peter ``` `cat -T -E test.txt ` ``` hello world $ tom^Imary^Ijohn$ $ 1234 nqu$ peter$ ``` `cat test.txt | tr "\t" " "` ``` hello world tom mary john 1234 nqu peter ``` `echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n'` ``` 1 2 3 4 ``` ![](https://hackmd.io/_uploads/HkjDdBtep.png) `echo "thisssss" | tr -s "s"` > this > -s : 壓縮 (連續多個"s") ``` echo "thisssss an apple" | tr -s "s " ``` > this an apple > -s : 壓縮 (連續多個"s"或" ") ![](https://hackmd.io/_uploads/SJ9gcrFxT.png) ![](https://hackmd.io/_uploads/Sy1IqSKla.png) ![](https://hackmd.io/_uploads/HJT6qHKxT.png) ![](https://hackmd.io/_uploads/rkzrjStga.png) --- ### seq(sequence 序列) ![](https://hackmd.io/_uploads/H1j_hHYgp.png) seq 1 10 > 1 > 2 > 3 > 4 > 5 > 6 > 7 > 8 > 9 > 10 seq -s "+" 1 10 > 1+2+3+4+5+6+7+8+9+10 > -s : 以 "+" 為分隔 seq -s "+" 1 10 | bc > 55 ![](https://hackmd.io/_uploads/H1QfCStep.png) -w : width (對齊) ![](https://hackmd.io/_uploads/ry_P0rYla.png) ## week6 ### SAMBA server > 資源共享 NFS : linux - linux SAMBA : windows - linux 安裝 > sudo yum install samba samba-client samba-common -y 創建共享資料夾並設定權限 >mkdir samba_dir sudo chown nobody ./samba_dir/ sudo chmod 777 samba_dir/ 編輯設定檔 >sudo vim /etc/samba/smb.conf 加上 ``` [final] comment = for final path = /home/user/samba_dir read only = no guest ok = yes browseable = yes ``` 啟動 >sudo systemctl start smb 查看port >sudo netstat -tunlp | grep smb 建立密碼 >sudo smbpasswd -a user 設定 “user” 的密碼 在Windows上開啟 在檔案總管輸入IP \\192.168.245.150 > Samba server IP 在Windows或Linux新增檔案皆能同步存取資料夾 --- ### sort [user@centos7-1 ~]$ cat doc1 >B.this is the first line C.here's second line a.another line A.labalbal [user@centos7-1 ~]$ sort doc1 >a.another line A.labalbal B.this is the first line C.here's second line [user@centos7-1 ~]$ sort -r doc1 >C.here's second line B.this is the first line A.labalbal a.another line [user@centos7-1 ~]$ cat doc1 | sort >a.another line A.labalbal B.this is the first line C.here's second line [user@centos7-1 ~]$ cat doc1 | sort -r >C.here's second line B.this is the first line A.labalbal a.another line #### number [user@centos7-1 ~]$ cat doc2 031 2 1345 001 014 [user@centos7-1 ~]$ sort doc2 001 014 031 1345 2 [user@centos7-1 ~]$ sort -g doc2 001 2 014 031 1345 -g : general numeric sort [user@centos7-1 ~]$ sort -g -r doc2 1345 031 014 2 001 [user@centos7-1 ~]$ cat doc3 031 111 3 2 555 6 1345 222 5 001 666 4 014 444 2 [user@centos7-1 ~]$ sort -g doc3 001 666 4 2 555 6 014 444 2 031 111 3 1345 222 5 [user@centos7-1 ~]$ sort -g -k 2 doc3 031 111 3 1345 222 5 014 444 2 2 555 6 001 666 4 -k : key [user@centos7-1 ~]$ cat doc4 031,111,3 2,555,6 1345,222,5 001,666,4 014,444,2 [user@centos7-1 ~]$ sort -g doc4 001,666,4 2,555,6 014,444,2 031,111,3 1345,222,5 [user@centos7-1 ~]$ sort -g -k 2 doc4 001,666,4 014,444,2 031,111,3 1345,222,5 2,555,6 [user@centos7-1 ~]$ sort -g -k 2 -t , doc4 031,111,3 1345,222,5 014,444,2 2,555,6 001,666,4 cat doc4 | tr "," " " | sort -k 2 | tr " " "," 也可以 [user@centos7-1 ~]$ cat doc5 031,111:3 2,555:6 1345:222,5 001,666,4 014,444,2 [user@centos7-1 ~]$ cat doc5 | tr "," " " | tr ":" " " | sort -k 3 014 444 2 031 111 3 001 666 4 1345 222 5 2 555 6 [user@centos7-1 ~]$ cat doc5 | tr [,:] " " | sort -k 3 014 444 2 031 111 3 001 666 4 1345 222 5 2 555 6 ### uniq 多行合併一行 [user@centos7-1 ~]$ cat doc6 2 2 2 3 4 1 3 4 [user@centos7-1 ~]$ sort doc6 1 2 2 2 3 3 4 4 [user@centos7-1 ~]$ sort doc6 | uniq 1 2 3 4 ### cut [user@centos7-1 ~]$ cat doc7 tom,22,31000 jack,21,29500 eric,18,42000 [user@centos7-1 ~]$ cut -d , -f 2 doc7 22 21 18 -d : 辨識 -f : field [user@centos7-1 ~]$ cut -d , -f 1,3 doc7 tom,31000 jack,29500 eric,42000 order不能更變 >cut -d , -f 1,3,2 doc7 cut -d , -f 3,2,1 doc7 取出2~4位 [user@centos7-1 ~]$ echo "12345" | cut -b2-4 234 -b : byte 隨機生成8位密碼 echo $RANDOM | md5sum | cut -b1-8 分割檔案 [user@centos7-1 ~]$ dd if=/dev/zero of=file3m bs=1M count=3 3+0 records in 3+0 records out 3145728 bytes (3.1 MB) copied, 0.00186633 s, 1.7 GB/s [user@centos7-1 ~]$ ls -h -l | grep file3m -rw-rw-r-- 1 user user 3.0M Oct 17 16:00 file3m [user@centos7-1 ~]$ split -b 1m file3m -rw-rw-r-- 1 user user 1.0M Oct 17 16:01 xaa -rw-rw-r-- 1 user user 1.0M Oct 17 16:01 xab -rw-rw-r-- 1 user user 1.0M Oct 17 16:01 xac ### diff ``` [user@centos7-1 tst]$ cat 4.txt hello1 hello2 hello3 [user@centos7-1 tst]$ cat 5.txt hello1 hello2 hello3 [user@centos7-1 tst]$ diff 4.txt 5.txt [user@centos7-1 tst]$ vim 5.txt [user@centos7-1 tst]$ cat 5.txt hello1 hello2 hello4 [user@centos7-1 tst]$ diff 4.txt 5.txt 3c3 < hello3 --- > hello4 ``` ### traceroute ## week7 ### IPv6架站 安裝 > yum install -y httpd 啟動 > systemctl start httpd vim /var/www/html/test.htm 確認有沒有 IPv6 > ifconfig 用 IPv6 addr 開網頁 --- ### alias 別名 `alias` 查看 >alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias vi='vim' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' `alias c='clear'` 增加 (僅當前視窗生效) `unalias c` 刪除 在~/.bashrc中 添加 才能永久生效 (僅當前使用者) alias c='clear' alias f='fg' alias v='vim' 生效 1. 重開視窗 2. `bash` 3. `source .bashrc` 4. `. .bashrc` 套用到所有使用者 `sudo vim /etc/profile` --- ### 變數的使用 **`"`會替換 `'`不會** ![](https://hackmd.io/_uploads/r12rweSGa.png) 特殊字元 ![](https://hackmd.io/_uploads/SyJM_xBG6.png) ![](https://hackmd.io/_uploads/rJUv_eHzp.png) ### 直接執行(不需要 相對路徑 絕對路徑) ![](https://hackmd.io/_uploads/H1xtsxBM6.png) ~/.bashrc加上 `export PATH=/home/user/bin:$PATH` ### 判斷使用者身分 ![](https://hackmd.io/_uploads/ryCGaxSz6.png) 如果是root回傳1,否則回傳0 ## Week8 #### 系統環境變數 env echo $UID >1000 echo $HISTFILE >/home/user/.bash_history echo $LANG > en_US.UTF-8 echo $PWD > /home/user echo $RANDOM > 13432 新增變數 a="1234" echo $a > 1234 echo a$a a1234 echo $aa > (視為變數 aa) echo ${a}a 1234a echo "a is $a" > a is 1234 echo 'a is $a' > a is $a ### shell script 比大小 ``` [user@centos7-3 ~]$ cat shell_test.sh #!/usr/bin/bash read -p "input the first number: " no1 read -p "input the second number: " no2 test $no1 -eq $no2 && echo "$no1 is equal to $no2" test $no1 -gt $no2 && echo "$no1 is greater than $no2" test $no1 -lt $no2 && echo "$no1 is less than $no2" [user@centos7-3 ~]$ ./shell_test.sh input the first number: 4 input the second number: 8 4 is less than 8 ``` if/else ``` #!/usr/bin/bash read -p "input the first number: " no1 read -p "input the second number: " no2 if test $no1 -eq $no2; then echo "$no1 is euqal to $no2" elif test $no1 -gt $no2; then echo "$no1 is greater than $no2" elif test $no1 -lt $no2; then echo "$no1 is less than $no2" else echo "ERROR" fi ``` for loop ``` #!/usr/bin/bash sum=0 for no in `seq 1 2 10` do echo $no let sum=$sum+$no done echo "sun=", $sum ``` ## week10 VPN server (virtual private) 1. sidt to site (企業) 2. point to site (公司遠端辦公、翻牆) ### PPTP VPN yum install -y epel-release yum install -y ppp pptpd vim /etc/pptpd.conf localip 192.168.0.1 1. 網路選用LAN Segment ![image](https://hackmd.io/_uploads/r15G1ilNp.png) 2. 更改IP ![image](https://hackmd.io/_uploads/SJbUkseE6.png) > 機器1 192.168.10.1 > 機器2 192.168.10.2 ![image](https://hackmd.io/_uploads/BJlpfsx4a.png) > localip 是VPN server的IP > remoteip 是IP ![image](https://hackmd.io/_uploads/SkbDmjlVT.png) > 帳號 伺服器 密碼 可以從哪裡連線(*代表任意) 在/etc/sysctl.conf 加入 net.ipv4.ip_forward = 1 啟動PPTP sudo systemctl restart pptpd sudo systemctl status pptpd 在 Windows 上 新增VPN連線 ## Week11 ### shell script [參考](https://medium.com/vswe/bash-shell-script-cheat-sheet-15ce3cb1b2c7) 顯示字串片段 方法一: a=123456789 echo ${a:1:3} 從位置1開始取3個元素 (第一個元素是位置0) > 234 方法二: echo $a | cut -c 2-4 > 234 -c : char字元 --- 顯示字串長度 a=123456789 echo ${#a} > 9 --- 字串取代 whoami > user greet="hello world" echo ${greet/world/$(whoami)} > hello user $(cmd) 和 `cmd` 相同 ![image](https://hackmd.io/_uploads/ry7GLaYEp.png) --- 迴圈 ![image](https://hackmd.io/_uploads/rkoz_6FVa.png) 注意 (( )) 擴展 echo {1..5} > 1 2 3 4 5 --- inline loop while true; do echo $(whoami); sleep 1; done; ![image](https://hackmd.io/_uploads/SJGt_TF4a.png) a=1 while [[ $a -le 5 ]]; do echo $a; ((a++)); sleep 1; done; ![image](https://hackmd.io/_uploads/ryTAdTFV6.png) --- 多重判斷 -a -o -a : and -o : or if ! [ $a -eq $b -a $a -lt $b ]; then 或是 if ! [ $a -eq $b ] && [$a -lt $b ]; then 數字比較 ![image](https://hackmd.io/_uploads/HyjJtptVa.png) 字串比較 ![image](https://hackmd.io/_uploads/BkNZKpFNa.png) --- 比較 [[]] test [] ![image](https://hackmd.io/_uploads/BJBAYTF4T.png) --- 模糊匹配 [[ a123b == *123* ]] && echo "match" || echo "not match" ![image](https://hackmd.io/_uploads/Skiz5atVp.png) --- regex =~ `^` : to begin with `$` : to end with `+` : 至少有一個 re='^[0-9]+$' [[ 5566 =~ $re ]] && echo "number" || echo "not number" > number ![image](https://hackmd.io/_uploads/SJBsjTFNa.png) --- Array array=("123" "456" "789") echo $array ![image](https://hackmd.io/_uploads/SJv_2aKEa.png) --- 整數計算 echo $((1+100)) > 101 let a=1+100 echo $a > 101 小數計算 echo "1.2*3+4.5" | br > 8.1 --- ### 網路指令與管理 NetworkManager (個人型、移動設備 自動設定,不能客制化) network (伺服器 手動設定) 兩者互斥 需則一使用 ![image](https://hackmd.io/_uploads/SyLPdCtET.png) ![image](https://hackmd.io/_uploads/SkLUJy9V6.png) 清除網路設定 ![image](https://hackmd.io/_uploads/rJ0wK0YEa.png) mtu (max transmission unit) <pre> _______________________________ | ethernet | IP | payload | FCS | ------------------------------- 6+6+2 4 6 dst mac 6 src mac 2 type 全長 64 ~ 1518 最大值 - (6+6+2) -4 = 1500 (mtu) </pre> 更改mtu > ifconfig ens33 mtu 1000 up 更改網卡卡號 > ifconfig ens33 hw ether aa:bb:cc:dd:ee:ff ![image](https://hackmd.io/_uploads/SkACq0KVT.png) 修改網路卡、IP遮罩 ![image](https://hackmd.io/_uploads/S1haARFVp.png) 新增邏輯介面 ![image](https://hackmd.io/_uploads/Hyi6yy5Va.png) --- ### ip 工具 (取代了ifconfig) ![image](https://hackmd.io/_uploads/Byvhekc4a.png) 也可以直接寫 ip a s ip -s link show ens160 ![image](https://hackmd.io/_uploads/SyHTWy9N6.png) -s : 統計 RX : 接收的byte數 TX : 傳送的byte數 為網路卡新增IP ip addr add 192.168.153.131/24 brd + dev ens33 ![image](https://hackmd.io/_uploads/HyLmfkc4p.png) 為網路卡刪除IP ip addr del 192.168.153.131/24 dev ens33 ![image](https://hackmd.io/_uploads/r1xwwzJqE6.png) 路由表設定 查看 route -n(傳統) 或 ip route show 新增內定路由 ip route add default via 192.168.122.1 ![image](https://hackmd.io/_uploads/BJ3t4ycNa.png) 刪除內定路由 ip route delete default #### 期末考 把NetworkManager --> network disable sudo systemctl disable NetworkManager 服務中止 sudo systemctl stop NetworkManager reset設定 ifconfig紀錄IP、遮罩、Gateway(route -n) cd /etc/sysconfig/network-scripts/ sudo vim ifcfg-ens160 TYPE=Ethernet DEVICE=ens160 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.245.144 NETMASK=255.255.255.0 GATEWAY=192.168.245.2 啟動suod network systemctl start network systemctl status network 應該要是 active running 可以ping 8.8.8.8 ## Week12 ### echo server sudo vim echo_server.py ``` #!/usr/bin/env python3 import socket serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.bind(('0.0.0.0', 9000)) serv.listen() while True: conn, addr = serv.accept() print('Client from', addr) while True: data = conn.recv(1024) if not data: break conn.send(data) conn.close() print('Client disconnected') ``` chmod +x ./echo_server.py 設定檔 vim /etc/systemd/system/echo_server.service (自訂名稱) ``` [Unit] Description=Echo Server [Service] Type=simple ExecStart=/home/user/echo_server.py Restart=always [Install] WantedBy=multi-user.target ``` sudo chmod 644 ./echo_server.service 啟動 sudo systemctl daemon-reload sudo systemctl start echo_server sudo systemctl status echo_server 測試 >nc 192.168.254.150 9000 --- DNS 配置檔位置: /etc/resolv.conf 檢查伺服器(以ssh 22 為例)是否啟動 netstat -tunlp | grep 22 -t:tcp -u:udp -n:不解析 -l:listen -p:process id ![image](https://hackmd.io/_uploads/SJmqReXBa.png) ### 排程(crond) *: don’t care 分 時 日 月 周 cmd 1 * * * * 每小時的第1分鐘 */1 * * * * 每1分鐘執行一次 */2 * * * * 每2分鐘執行一次 1 * * * 每小時的第0分鐘 */1 * * * 每1小時執行一次 /2: 代表每2時間(分、時、日…)執行一次 29 9 15 8 * (8/15 09:29 執行一次) 0 17 10 * * (每個月10日 下午5點整) 0 4 * * 6 (每個星期六 零晨4點整) 1,31 17 10 * * (每個17號 17點 1分、31分都執行一次) 1-10 17 10 * * (每個17號 17點 1分、2分…、10分都執行一次) 0 * * * * (每小時第0分鐘) 0 23-1/2,8 * * * 23,4,3,5,7,8 */20 6-12 * 12 * 在12月時,6~12小時間 每20分鐘 執行一次 --- ### telnet 安裝 >yum install -y telnet-server yum install -y telnet yum install -y xinetd 啟動 >systemctl start telnet.socket systemctl start xinetd ## Week13 ### LAMP (Linux Apache MySQL PHP) 1. 架設web server yum install httpd systemctl start httpd netstat -tunlp | grep 80 yum install mariadb-server mariadb mysql_secure_installation >Enter Y 123456 (root password) 123456 Y (romove anonymous user) n (disallow root login remotely) Y (remove test db) Y (reload) 進入mariadb >mysql -u root -p -h localhost ``` /* 顯示目前有的資料庫 */ show databases; /* 創建資料庫 */ create database testdb; /* 使用資料庫 */ use testdb; /* 創建資料表 */ create table addrbook(name varchar(50) not null, phone char(10)); /* 加入資料 */ insert into addrbook(name, phone) values ("tom", "0912123456"); insert into addrbook(name, phone) values ("mary", "0912123567"); /* 選擇資料 */ select name,phone from addrbook; /* */ update addrbook set phone="0987465123"; ``` 2. PHP 安裝PHP $ sudo yum install php php-mysql php-fpm 重啟 $ sudo systemctl restart httpd.service $ sudo vim /var/www/html/test.php > <?php phpinfo(); ?> ![image](https://hackmd.io/_uploads/S1HukS3Ba.png) <?php $servername="127.0.0.1"; $username="root"; $password="123456"; $dbname="testdb"; $conn = new mysqli($servername, $username, $password, $dbname); if($conn->connect_error){ die("connection failed: " . $conn->connect_error); } else{ echo "connect OK!" . "<br>"; } $sql="select name,phone from addrbook"; $result=$conn->query($sql); if($result->num_rows>0){ while($row=$result->fetch_assoc()){ echo "name: " . $row["name"] . "\tphone: " . $row["phone"] . "<br>"; } } else { echo "0 record"; } ?> ![image](https://hackmd.io/_uploads/rkBpHH2S6.png) --- ### 課本CH15 LAMP LNMP (N: nginx) IIS (Internet Information Services) https 架設的挑戰 證書(購買certificate、domain name) 網頁目錄帳號驗證 HTTP的特性 每一次連線的時間通常很短(傳輸完後就斷開連線) > 改進-->long connection 沒有記憶性 > cookie session HTTP回應報文 (status code) 1XX : information 2XX : sucess 3XX : redirect 4XX : client error 5XX : server error ### SELinux 只在centos有,ubuntu沒有 ### 安裝Apache 配置目錄 /etc/httpd 主要配置檔/etc/httpd/conf/httpd.conf 其餘配置檔/etc/httpd/conf.d/ cat httpd.conf | grep -v ^# | grep -v ^$ -v 不包含 ^# 以 # 為開頭 ^$ 空白行 重新加載配置 systemctl reload httpd restart = stop + start 但reload不會關閉server --- ## week14 ### 架設HTTPS Web 1. VM使用橋接模式 2. dynv6.com ![image](https://hackmd.io/_uploads/HybHd_HUp.png) 3. SSL yum -y install epel-release mod_ssl certbot https://letsencrypt.org/zh-tw/ certbot certonly --webroot -w /var/www/html -d gjlin4.dns.army --email linkim0914@gmail.com vim etc/httpd/conf.d/ssl.conf SSLCertificateFile SSLCertificateKeyFile SSLCACertificateFile 申請 >sudo certbot 配置 >cd /etc/httpd/conf.d sudo vim test.conf ``` <VirtualHost *:80> ServerName gjlin.dns.army DocumentRoot "/var/www/html" </VirtualHost> ``` > 使用IPv6 addr 在 dynv6 申請 Domain name (利如gjlin.dns.army) --- ## week15 ### 存取控制 Access Control 對象範圍 允許所有人 後 拒絕Windows ![image](https://hackmd.io/_uploads/HJaXHcSLp.png) ![image](https://hackmd.io/_uploads/B1OvE5S8a.png) 拒絕所有人 後 允許Windows ![image](https://hackmd.io/_uploads/H1sQ89B8T.png) 創建帳號(需要存在於系統帳號) htpasswd -c .htpasswd user (第一次使用要-c) htpasswd .htpasswd tom vim .htaccess >AuthType Basic AuthName "Private File Area" AuthUserFile /var/www/html/files/.htpasswd Require valid-user vim /etc/httpd/conf/httpd.conf ><Directory /var/www/html/files> Options Indexes AllowOverride AuthConfig </Directory> ![image](https://hackmd.io/_uploads/HyOza2AUp.png) ![image](https://hackmd.io/_uploads/BksMTnRLp.png) ### FTP(File Transfer Protocol) 使用TCP 是客戶端-伺服器端模型 server 使用21 port (Contol & Comand) 20 port (資料傳送) server端下 FTP client的操作(CLI) ls 查看目錄 (在本地端使用!ls) get/put 下載/上傳 mget/mput 多檔案下載/上傳 除了傳輸內容只有英文字母,否則要先使用bin ![image](https://hackmd.io/_uploads/HJJHNT08p.png) yum install vsftpd 設定檔 /etc/vsftpd/vsftpd.conf 根目錄 /var/ftp/ ![image](https://hackmd.io/_uploads/HkCC3T08p.png)