Try   HackMD

Linux 伺服器架設

這是一個關於 Linux / CentOs7 伺服器架設的筆記簡志融111010501

[第一篇] Linux 作業系統實務筆記
[第三篇] Linux 自動化運維

摘要

第一週

建立SSH Server(參考前篇)

Commands

查詢(qa = query all)

[root@mycentos7-1 user]# rpm -qa | grep ssh
openssh-server-7.4p1-22.el7_9.x86_64
ksshaskpass-0.5.3-7.el7.x86_64
libssh2-1.4.3-12.el7.x86_64
openssh-7.4p1-22.el7_9.x86_64
openssh-clients-7.4p1-22.el7_9.x86_64

刪除(e = earse)

[root@mycentos7-1 user]# rpm -e openssh-server

系統控制(System control)
動作 : status / start / stop / reload / restrat / enabled / disabled
狀態 : active(running) / inactive(dead)
sshd.service; disabled; 開機不重啟

[root@mycentos7-1 user]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2023-09-12 14:41:10 CST; 6min ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 4596 (sshd)
   CGroup: /system.slice/sshd.service
           └─4596 /usr/sbin/sshd -D

第二週

VirualBox

NAT網卡 10.0.X.X 用於與 Internet 溝通,下載東西用
Host only 網卡 192.168.X.X 用於與其他機器通訊,如其他虛擬機或本機

VMware

只需一張 NAT網卡 就可滿足上述兩種功能

RPM(Redhat) & DPKG(Ubuntu)
用 rpm 安裝有依賴性(相依其他函式庫)問題,現多用 yum


伺服器排錯指南

  1. 查看伺服器狀態
    ​​​​[root@localhost user]# netstat -tunlp | grep 22
    
  2. 查埠號
    ​​​​[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
    
  3. 檢查防火牆
    ​​​​[root@localhost user]# getenforce
    ​​​​Disabled
    
    If enable,關防火牆

無密碼登入

  1. 產生公/私鑰
    path / password 用預設按 enter
    ​​​​[root@mycentos7-2 ~]# ssh-keygen
    
  2. 進入 .ssh 看公/私鑰是否成功生成
    ​​​​[root@mycentos7-1 ~]# cd .ssh
    ​​​​[root@mycentos7-1 .ssh]# ls
    ​​​​id_rsa  id_rsa.pub  known_hosts
    ​​​​[root@mycentos7-1 .ssh]#
    
  3. 將公鑰設定放到伺服器上
    ​​​​[root@mycentos7-1 .ssh]# ssh-copy-id root@mycentos7-2
    
  4. 進行連線
    ​​​​[root@mycentos7-1 .ssh]# ssh root@mycentos7-2
    

更改SSH埠號

  1. 找出要修改的檔案並編輯埠號
    ​​​​[root@mycentos7-1 user]# cd /etc/ssh
    ​​​​[root@mycentos7-1 ssh]# vim sshd_config
    
  2. 重啟sshd
    ​​​​[root@mycentos7-1 ssh]# systemctl restart sshd
    

Commands

跳出SSH連線

[root@mycentos7-2 user]# exit

SSH連線(Using 2222 port)

[root@mycentos7-2 user]# ssh -p 2222 root@mycentos7-2

查詢安裝套件各類資訊的指令
qa = query all
qi = query information
ql = query list
qf = query find >> 找出檔案來源套件

[root@mycentos7-1 ~]# rpm -qa
telepathy-mission-control-5.16.3-3.el7.x86_64
iwl7265-firmware-22.0.7.0-69.el7.noarch
libmpc-1.0.1-3.el7.x86_64
firefox-60.2.2-1.el7.centos.x86_64
libiodbc-3.52.7-7.el7.x86_64
gnome-keyring-pam-3.28.2-1.el7.x86_64
mozilla-filesystem-1.9-11.el7.x86_64
colord-1.3.4-1.el7.x86_64
iwl1000-firmware-39.31.5.1-69.el7.noarch
compat-libcolord1-1.0.4-1.el7.x86_64
...
[root@mycentos7-1 ~]# rpm -qi httpd
Name        : httpd
Version     : 2.4.6
Release     : 98.el7.centos.6
Architecture: x86_64
Install Date: Mon 06 Mar 2023 11:36:07 PM CST
Group       : System Environment/Daemons
Size        : 9829272
License     : ASL 2.0
Signature   : RSA/SHA256, Sat 28 Jan 2023 02:02:31 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : httpd-2.4.6-98.el7.centos.6.src.rpm
Build Date  : Sat 28 Jan 2023 01:38:14 AM CST
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://httpd.apache.org/
Summary     : Apache HTTP Server
Description :
The Apache HTTP Server is a powerful, efficient, and extensible
web server.
[root@mycentos7-1 ~]# rpm -qf /etc/passwd
setup-2.8.71-10.el7.noarch

用rpm安裝
nodeps:不考慮相依問題,硬裝(可能跑不起來)

[root@mycentos7-1 ~]# rpm -ivh 套件

用scp把檔案傳至遠端
-r : recursive

[root@mycentos7-1 ~]# scp -r testdir/ root@mycentos7-2:/tmp

遠端執行指令

[root@mycentos7-1 ~]# ssh root@mycentos7-2 指令

第三週

wget 到網頁伺服器下載檔案(速度快)

資料庫損壞可參考 Here

在 RPM 出現前,安裝軟體大多由原始碼編譯

外網 Http 伺服器

  1. 內網 Http 伺服器
  2. 在本機開 Word 建立一文件,另存成.htm檔
  3. 用 Winscp 把檔案存到 /home/user
  4. 用指令將檔案移至 /var/www/html
    ​​​[root@mycentos7-1 html]# mv /home/user/me1.htm .
    ​​​[root@mycentos7-1 html]# mv /home/user/me1.files .
    
  5. 下載 ngork,註冊帳號取得 authtoken
    ​​​[root@mycentos7-1 html]# wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz
    
  6. 解壓縮
    ​​​[root@mycentos7-1 html]# tar zxvf ngrok-v3-stable-linux-amd64.tgz
    
  7. 授權
    ​​​[root@mycentos7-1 html]# ./ngrok config add-authtoken 2HZe1oWgj...
    
  8. 啟動 ngork
    ​​​[root@mycentos7-1 html]# ./ngrok http 80
    

編譯原始碼並安裝 Htop

  1. 抓 Htop 壓縮檔
    ​​​[root@mycentos7-1 tmp]# wget https://src.fedoraproject.org/lookaside/extras/htop/htop-2.2.0.tar.gz/sha512/ec1335bf0e3e0387e5e50acbc508d0effad19c4bc1ac312419dc97b82901f4819600d6f87a91668f39d429536d17304d4b14634426a06bec2ecd09df24adc62e/
    
  2. 解壓縮
    ​​​[root@mycentos7-1 tmp]# tar zxvf htop-2.2.0.tar.gz
    ​​​[root@mycentos7-1 tmp]# cd htop-2.2.0
    
  3. 檢查環境,成功會看到 "creating makefile"
    ​​​[root@mycentos7-1 htop-2.2.0]# ./configure
    
  4. 編譯
    ​​​[root@mycentos7-1 htop-2.2.0]# make
    
  5. 安裝(必需用root)
    ​​​[root@mycentos7-1 htop-2.2.0]# make install
    
  6. 執行
    ​​​[root@mycentos7-1 htop-2.2.0]# htop
    

Commands

yum
install / remove / update / list /search

[root@mycentos7-1 user]# yum install packages

wget 指定位置下載

[root@mycentos7-1 user]# wget -o /home/user/Downloads URL 

時時更新行程

[root@centos7-2 test]# top

du指令

磁碟使用狀況(-h:以容量單位顯示)

[root@centos7-2 user]# df -h



第四週

NFS (Network File System) : 在網路上提供本地檔案存取 (參考資料)

tr 指令補充

建立 NFS Server

  • 第一台虛擬機
  1. yum install nfs-utils
    ​​​​systemctl enable rpcbind
    ​​​​systemctl enable nfs
    ​​​​systemctl start rpcbind
    ​​​​systemctl start nfs
    
  2. mkdir /datachmod 755 /data
  3. vim /etc/exports
    /data/ 192.168.56.0/24(rw,sync,no_root_squash,no_all_squash)
    • /data: 共享目錄位置。
    • 192.168.0.0/24: 客戶端 IP 範圍,* 代表所有,即沒有限制。
    • rw: 權限設置,可讀可寫。
    • sync: 同步共享目錄。
    • no_root_squash: 可以使用 root 授權。
    • no_all_squash: 可以使用普通用戶授權。
      systemctl restart nfs
  4. 檢查
    ​​​​[root@mycentos7-1 user]# showmount -e localhost
    ​​​​/data 192.168.56.0/24
    
  • 第二台虛擬機
  1. yum install nfs-utils
    systemctl enable rpcbind
    systemctl start rpcbind
  2. 檢查
    ​​​​[root@mycentos7-2 user]# showmount -e 192.168.56.106
    ​​​​Export list for 192.168.0.106:
    ​​​​/data 192.168.56.0/24
    
  3. 創 資料夾 & 掛載
    mkdir /data
    sudo mount -t nfs 192.168.56.106:/data /data
  4. 在其中一邊touch檔案,另一邊也看的到

Commands

dd:原本檔案複製,現用來產生任意大小的檔案
產生100MB的虛擬檔案
if : input file
of : 檔名

[root@mycentos7-1 user]# dd if=/dev/zero of=file100m bs=1M count=100

wc(word count)
5行 8詞 42letters
-l -w -c 分別對應

[root@mycentos7-1 user]# wc t.txt
5 8 42 t.txt
[root@mycentos7-1 user]# cat t.txt | wc -l
5

cat
-T : tab改以^顯示
-E : 每行末加上$

[root@mycentos7-1 user]# cat -T -E t.txt

tr
-d : delete
-c : 除捕集的內容外
"0-9 \n" : 0-9、空白、換行

[root@mycentos7-1 user]#cat a.txt | tr -d -c "0-9 \n"

seq
-s "" : 用""裡的元素取代 \r\n

[root@mycentos7-1 user]# seq -s "+" 1 10
1+2+3+4+5+6+7+8+9+10
[root@mycentos7-1 user]# seq -s "+" 1 10 | bc
55

第六週

SAMBA server 可讓 Linux 共享 Windows 資料夾
shadow檔 存放系統使用者密碼,而 SAMBA 是另外設置儲存

建立 SAMBA Server

  1. 嘗試用 Windows ping Linux

  2. 安裝 SAMBA

    ​​​​[root@mycentos7-1 user]# yum install samba samba-client samba-common -y
    
  3. 建立測試資料夾

    ​​​​[root@mycentos7-1 user]# cd /
    ​​​​[root@mycentos7-1 /]# mkdir /test_samba -p
    
  4. 更改擁有者及權限 : 設成無擁有者並將權限全開,再查看權限

    ​​​​[root@mycentos7-1 user]# chown nobody ./test_samba/
    ​​​​[root@mycentos7-1 /]# chmod 777 ./test_samba/ 
    ​​​​[root@mycentos7-1 /]# ll ./test_samba/ -d
    ​​​​drwxrwxrwx 2 nobody root 6 Oct 17 13:49 ./test_samba/
    
  5. 修改配置檔

    ​​​​[root@mycentos7-1 user]# vim /etc/samba/smb.conf
    

    在最下方加入

    ​​​​[test]  # 設定進入資料夾名稱(在windows上看到的)
    ​​​​    comment = for test  # 註解,標明用意
    ​​​​    path = /test_samba  # 測試資料夾位置(要設到root下)
    ​​​​    read only = no  # 可以寫入的
    ​​​​    guest ok = yes  # 可以給一般使用者使用
    ​​​​    browseable = yes  # 可以瀏覽的
    
  6. 測試設定的參數

    ​​​​[root@mycentos7-1 test_samba]# testparm
    ​​​​Load smb config files from /etc/samba/smb.conf
    ​​​​Loaded services file OK.
    ​​​​Server role: ROLE_STANDALONE
    
    ​​​​Press enter to see a dump of your service definitions
    
    ​​​​# Global parameters
    ​​​​[global]
    ​​​​        printcap name = cups
    ​​​​        security = USER
    ​​​​        workgroup = SAMBA
    ​​​​        idmap config * : backend = tdb
    ​​​​        cups options = raw
    
    
    ​​​​[homes]
    ​​​​        browseable = No
    ​​​​        comment = Home Directories
    ​​​​        inherit acls = Yes
    ​​​​        read only = No
    ​​​​        valid users = %S %D%w%S
    
    
    ​​​​[printers]
    ​​​​        browseable = No
    ​​​​        comment = All Printers
    ​​​​        create mask = 0600
    ​​​​        path = /var/tmp
    ​​​​        printable = Yes
    
    
    ​​​​[print$]
    ​​​​        comment = Printer Drivers
    ​​​​        create mask = 0664
    ​​​​        directory mask = 0775
    ​​​​        force group = @printadmin
    ​​​​        path = /var/lib/samba/drivers
    ​​​​        write list = @printadmin root
    
    
    ​​​​[test]
    ​​​​        comment = share /test_samba directory to windows
    ​​​​        guest ok = Yes
    ​​​​        path = /test_samba
    ​​​​        read only = No
    
  7. 啟動並檢查 SAMBA 的 Port

    ​​​​[root@mycentos7-1 test_samba]# systemctl start smb
    ​​​​[root@mycentos7-1 test_samba]# netstat -tunlp | grep smb
    ​​​​tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      4854/smbd
    ​​​​tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      4854/smbd
    ​​​​tcp6       0      0 :::139                  :::*                    LISTEN      4854/smbd
    ​​​​tcp6       0      0 :::445                  :::*                    LISTEN      4854/smbd
    
  8. 設定 SAMBA 密碼

    ​​​​[root@mycentos7-1 test_samba]# smbpasswd -a user
    ​​​​New SMB password:
    ​​​​Retype new SMB password:
    ​​​​Added user user.
    
  9. 在 Windows 輸入 \\192.168.56.100 連入 Linux

    image

(參考:note/linux/111semester01/3-SAMBA.md)

Commands

sort : 用來為文字檔案內的每一列排序
-r : reverse
-g : general numery,按數字大小比較
-k : key,選擇 colum 做排序
-t : 分割符

[root@mycentos7-1 user]# sort doc1






uniq : 將檔案中相鄰且重覆的多行資料合併為一行

[root@mycentos7-1 user]# cat doc1
1
1
2
3
3
[root@mycentos7-1 user]# cat doc1 | uniq
1
2
3

cut : 從存取檔案部分內容
-f : 選擇 colum 顯示 (不可改變順序 eg.1,3,2)
-d : 分割符
-b : byte,一個字元

[root@mycentos7-1 user]# cut -d , -f 1,3 doc1
[root@mycentos7-1 user]# echo "54321" | cut -b2-4
432
[root@mycentos7-1 testdir]# echo $RANDOM | md5sum | cut -b1-8
8d6535ba

split : 分割檔案
-b X : 以 X 大小分割檔案

[root@mycentos7-1 testdir]# split -b 1m 3mFile

找出檔案差別

[root@mycentos7-1 testdir]# diff doc11 doc22
3c3
< 3
---
> 4

第七週

  • Shell 的功能

    • 簡化複雜工作
    • 降低日常工作複雜度
    • 處理人力難以完成的工作
  • bash 提供許多預設的變數,變數大部份以全大寫命名


  • 寫腳本時,經常需要判斷使用者的身份([[ $USER == "root" ]] 空白很重要)

用 IPv6 架網站

參考 前篇 : 第四周/用 IPv6 架網站

  1. 註冊 dynv6
  2. 新增 Domain name

  3. cjc217.dynv6.net/me1.htm 開啟網頁

Commands

echo $變數名稱,可以呼叫內容
a = 5 ; a =5 ; a= 5 都不行
變數可以底線開頭,但特殊符號和數字不行 eg. _a (O)、5a (X)、+a (X)

[root@mycentos7-1 user]# a=5
[root@mycentos7-1 ~]# echo $a
5

可用 {} 區分變數

[root@mycentos7-1 ~]# a=17
[root@mycentos7-1 ~]# echo ${a}a
17a

process id

[root@mycentos7-1 ~]# echo $$
3369

o o o o o o o o o o o
o Very Imoprtant o
o o o o o o o o o o o



別名
僅限當前終端機,當前process
vim ~/.bashrc 可自行建立 alias 規則,重啟終端機生效(or source ~/.bashrc or . ~/.bashrc)
vim /etc/profile 為所有使用者建立規則( vim ~/.bashrc 只為設定者更改)

[root@mycentos7-1 ~]# alias
alias cp='cp -i'
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 mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@mycentos7-1 ~]# unalias cp
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 mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

cp (copy)
\ 代表還原,用最原始的意思,沒有 -i

[root@mycentos7-1 ~]# \cp a.sh b.sh

第八週

root UID = 0

echo $? >>> if==0 success else fail

  • 指令可用 ;、&&、| | , 三種方式串聯 (可將變數用""包起來,方便執行辨識)
    • ; 不管前方結果如何,後面都會執行
    • && 前面成功才執行後面,反之亦然
    • | | 前面成功執行前面,後面不執行;前面失敗執行後面(擇一執行)

腳本就是將一連串指令寫在一起的合輯
$() = `` (優先執行)
腳本載子行程執行,執行完變數就沒了

Commands

執行歷史指令

[root@centos7-1 user]# history
........
[root@centos7-1 user]# !數字
[root@centos7-1 user]# !關鍵字

隨機賦予一樣長度密碼

[root@centos7-1 user]# echo $RANDOM | md5sum | cut -c 1-8
c8f0ccc0

完整環境變數 env

[root@mycentos7-1 ~]# env
XDG_SESSION_ID=3
HOSTNAME=mycentos7-1
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.56.1 52096 22
CONDA_SHLVL=0
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/1
QT_GRAPHICSSYSTEM_CHECKED=1
USER=root
......
XDG_RUNTIME_DIR=/run/user/0
QT_PLUGIN_PATH=/usr/lib64/kde4/plugins:/usr/lib/kde4/plugins
_=/usr/bin/env

輸入(str型態)

[root@mycentos7-1 ~]# read -p "inout a number : " num
inout a number : 5
[root@mycentos7-1 ~]# echo $num
5

清空檔案內容

[root@mycentos7-1 ~]# > hi.txt

test
-d : directory
-e : exist
-s : 檔案大小是否為0
-r : read
-w : write
-x : execute
-L : link
-eq : equal
-ne : not equal
-gt : greater than
-lt : less than

數字比較勿用符號,易發生錯誤

true = 1 ; false = 0

  • 字串比較
  • 數字比較

第十週

VPN(Virtual Private Network) : site to site (跨私有網路) 、 point to site (點到私有網路)
VPN server type : PPTP、IPSec、L2C

PPTP VPN server

參考:如何使用CentOS 7实例配置PPTP VPN服务端到客户端的连接

  1. 製作 Lan 虛擬網路

    • 第一台 : NAT、Host only、Internal network

      image

    • 第二台 : Internal network

      image

  2. 更改兩台機器的 Internal network IP

    • 第一台

      image

    • 第二台

      image


      螢幕擷取畫面 2023-11-14 152730

  3. 第一台機器安裝 ppp & pptpd 伺服器

    ​​​​[root@mycentos7-1 ~]# yum install -y ppp pptpd
    

    找不伺服器到可試

    ​​​​yum install -y epel-release
    
  4. 修改配置檔

    • vim /etc/pptpd.conf

      螢幕擷取畫面 2023-11-14 160703

    • vim /etc/ppp/chap-secrets

      螢幕擷取畫面 2023-11-14 161414

    • vim /etc/ppp/ip-up

      螢幕擷取畫面 2023-11-14 161221

    • vim /etc/sysctl.conf

      螢幕擷取畫面 2023-11-14 161759
      做完執行 sysctl -p

  5. 重啟 pptpd : systemctl restart pptpd

  6. Windows 設定 VPN 連線
    設定 >> 網路和網際網路 >> VPN >> 新增VPN
    名稱自訂,帳密用上面設定的 : test / 123456

    image

  7. 連線

    螢幕擷取畫面 2023-11-14 160143
    螢幕擷取畫面 2023-11-14 155811

第十一週

Bash Script 語法解析

三大雲端供應商 : AWS、Azure、GCP

Linux網路設定運作由兩個伺服器管理,兩者互斥,只會啟動一個 :
Network Manager(盡量自動化,移動式裝置,非客製)、network(架設伺服器用,可客製) FINAL TEST

取消 NM 改用 network 服務

  1. 先用ifconfigroute -n將原本的設定記住
    image

    image
  2. cd /etc/sysconfig/network-scripts/
    image
  3. vim ifcfg-enp0s8
    ​​​​TYPE=Ethernet
    ​​​​DEVICE=enp0s8
    ​​​​ONBOOT=static
    ​​​​IPADDR=192.168.56.104
    ​​​​NETMASK=255.255.255.0
    ​​​​GATEWAY=0.0.0.0
    
    image
  4. systemctl stop NetworkManagersystemctl start network
    螢幕擷取畫面 2023-11-28 124518
  5. 將不相干的設定檔刪除,留 ifcfg-enp0s8ifcfg-lo
    image
  6. systemctl restart networkping 8.8.8.8
    image

Commands

Bash Script

[root@centos7-1 user]# a=123456789
[root@centos7-1 user]# echo ${a:1:3}
[root@centos7-1 user]# 234

image

[root@centos7-1 user]# a=123456789
[root@centos7-1 user]# echo ${#a}
[root@centos7-1 user]# 9
[root@centos7-1 user]# greet="Hello World"
[root@centos7-1 user]# echo ${greet/World/$(whoami)}
[root@centos7-1 user]# Hello user
for i in {1..10}; do
  if ((i%2==0)); then
    continue
  fi  
  if ((i==7)); then
    break
  fi
  echo "${i}" # print 1, 3, 5                            
done

-a = and;-o = or

image
image


正則表達式
^ : 以為開頭
$ : 以為結尾
+ : 數目不為零

re = '^[0-9]+$'

網路卡操作
down : 開
up : 關
0 : 清除網路設定
mtu : Max Transmision Unit
hw ether : 改網路卡卡號

[root@centos7-1 user]# ifconfig ens33 down
[root@centos7-1 user]# ifconfig ens33 up
[root@centos7-1 user]# ifconfig ens33 0
[root@centos7-1 user]# ifconfig ens33 mtu 1000 up
[root@centos7-1 user]# ifconfig ens33 hw ether aa:bb:cc:dd:ee:ff
[root@centos7-1 user]# ifconfig ens33 192.168.56.100 netmask 255.255.255.0
[root@centos7-1 user]# ifconfig ens33:1 192.168.56.101 netmask 255.255.255.0

ip 網路工具
brd : 接受廣播封包
dev : 裝置

[root@centos7-1 user]# ip addr show
[root@centos7-1 user]# ip a s
[root@centos7-1 user]# ip -s link show ens33
[root@centos7-1 user]# ip route show

新增ip

[root@centos7-1 user]# ip addr add 192.168.56.102/24 brd + dev ens33
[root@centos7-1 user]# ip addr del 192.168.56.102/24 dev ens33

內地路由

[root@centos7-1 user]# ip route add default via 192.168.153.2

cd /etc/sysconfig/network-scripts/

systemctl stop NetworkManager
systemctl start network

第十二週

DNS server : etc/resolv.conf

CH14 : systemd & crond (cron與時間有關)

  • Linux 的兩種服務 :
    • 隨時運行,隨時回復(占用資源)
    • client連線,agent通知server,server進入服務(不佔用資源,但速度慢)

atd vs crond : atd 只執行一次;crond可週期執行

早期 Linux 使用 init 管理系統;現多用 systemd (init & systemd 為第一支行程,pid=1)

init 為何被取代? 因其啟動行程時,一個接一個,所以要啟動的行程越多,開機時間越長;而 systemd 會考慮行程的相依性,並減少啟動時間

/usr/lib/systemd/system/sshd.service

  • Unit : 說明及應在何時服務後啟動
  • Service : 執行環境及指令
  • Install : 定義在什麼模式啟動
    image

systemd 的 target 取代 init 的 runlevel
0:單人模式;3:文字模式;5:圖形化模式

image

Echo server

  1. 安裝python3 yum install -y python3

  2. vim /opt/echo_server.py

    ​​​​#!/usr/bin/env python3 ​​​​import socket ​​​​# 建立 socket ​​​​serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ​​​​# 綁定所有網路介面的 9000 連接埠 ​​​​serv.bind(('0.0.0.0', 9000)) ​​​​# 開始接受 client 連線 ​​​​serv.listen() ​​​​while True: ​​​​ # 接受 client 連線 ​​​​ 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')
  3. 加上執行權限 chmod +x /opt/echo_server.py

  4. 啟動測試

    • 第一台終端機
      python3 /opt/echo_server.py
    • 第二台終端機
      nc localhost 9000
      (nc找不到 : yum install -y nc)
      image
  5. vim /etc/systemd/system/echo_server.service

    ​​​​[Unit]
    ​​​​Description=Echo Server
    
    ​​​​[Service]
    ​​​​Type=simple
    ​​​​ExecStart=/usr/bin/python3 /opt/echo_server.py
    ​​​​Restart=always
    
    ​​​​[Install]
    ​​​​WantedBy=multi-user.target
    

    chmod 644 /etc/systemd/system/echo_server.service

  6. systemctl daemon-reloadsystemctl start echo_server

    image

Telnet

  1. 安裝伺服器
    ​​​​yum -y install telnet-server
    ​​​​yum -y install telnet
    ​​​​yum -y install xinetd
    
  2. Enable & 啟動(須照順序)
    ​​​​systemctl enable xinetd.service
    ​​​​systemctl enable telnet.socket
    ​​​​systemctl start telnet.socket
    ​​​​systemctl start xinetd
    
  3. vim /etc/securetty,最下方加入下面兩行
    ​​​​...
    ​​​​pst/0
    ​​​​pst/1
    
  4. 連線
    image

    image

Commands

網路測試工具
t : tcp
u : udp
n : 不解析(顯示port而非協定名)
l : linsten
p : process

[root@mycentos7-1 ~]# netstat -tunlp | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3132/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      3132/sshd

systemctl

[root@mycentos7-1 ~]# systemctl | grep sshd
sshd.service
loaded active running   OpenSSH server daemon

切換 UI 的模式

[root@mycentos7-1 ~]# systemctl isolate multi-user.target
[root@mycentos7-1 ~]# systemctl isolate graphical.target

第十三週

Apache LAMP : 主流
nginx LNMP : 效能好,迎頭趕上
Windows IIS : 資安問題

Http : 80 port ; Https: 443 port
Https 需 證書 (Certificate)綁定域名 (Domain name)

Http是一種沒有記憶性的通訊協定,requset 結束後,連線就斷了
架構於TCP上 >> 三向交握 & 四次揮手 >> 效能差
後有 long connection 改善效能問題,或用 section(server) 或 cookie(client) 記憶

Http status code:

  • 1XX info
  • 2XX success
  • 3XX 跳轉
  • 4XX client error
  • 5XX server error

正常情況用 systemctl stop htppd
伺服器不正常用 pkill -9 httpd

LAMP server

LAMP : Linux Apache MySQL PHP
筆記參考 : stereomp3/note/linux/111semester01/13-.md

  1. 安裝 & 啟動 Apache
    ​​​​[root@mycentos7-1 ~]# yum install httpd
    ​​​​[root@mycentos7-1 ~]# systemctl start httpd.service
    
  2. 安裝 & 啟動 mariadb
    ​​​​[root@mycentos7-1 ~]# yum install mariadb-server mariadb
    ​​​​[root@mycentos7-1 ~]# systemctl start mariadb
    
  3. 設定 root 密碼,其他選項 Disallow rootRemove test 選n,其他都 Enter 跳過
    ​​​​[root@mycentos7-1 ~]# mysql_secure_installation
    
  4. 設定開機啟動 mariadb
    ​​​​[root@mycentos7-1 ~]# systemctl enable mariadb.service
    
  5. 啟動 MySQL
    ​​​​[root@mycentos7-1 ~]# mysql -u root -p
    
  6. SQL concept & commands
    • DB (資料庫)
      • table (資料表)
        • record (表的資料)
          • field (資料欄位 col)
    • SQL commands 補充
    ​​​​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"  #更新資料
    
    ​​​​[root@mycentos7-1 ~]# mysql -u root -p
    ​​​​Enter password:
    ​​​​Welcome to the MariaDB monitor.  Commands end with ; or \g.
    ​​​​Your MariaDB connection id is 7
    ​​​​Server version: 5.5.68-MariaDB MariaDB Server
    
    ​​​​Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    ​​​​Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    ​​​​MariaDB [(none)]> show databases;
    ​​​​+--------------------+
    ​​​​| Database           |
    ​​​​+--------------------+
    ​​​​| information_schema |
    ​​​​| mysql              |
    ​​​​| performance_schema |
    ​​​​| test               |
    ​​​​+--------------------+
    ​​​​4 rows in set (0.00 sec)
    
    ​​​​MariaDB [(none)]> create database testdb;
    ​​​​Query OK, 1 row affected (0.00 sec)
    
    ​​​​MariaDB [(none)]> show databases;
    ​​​​+--------------------+
    ​​​​| Database           |
    ​​​​+--------------------+
    ​​​​| information_schema |
    ​​​​| mysql              |
    ​​​​| performance_schema |
    ​​​​| test               |
    ​​​​| testdb             |
    ​​​​+--------------------+
    ​​​​5 rows in set (0.00 sec)
    
    ​​​​MariaDB [(none)]> use testdb
    ​​​​Database changed
    ​​​​MariaDB [testdb]> create table addrbook(name varchar(50) not null, phone char(10));
    ​​​​Query OK, 0 rows affected (0.01 sec)
    
    ​​​​MariaDB [testdb]> show tables
    ​​​​    -> show tables;
    ​​​​ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'show tables' at line 2
    ​​​​MariaDB [testdb]> show tables;
    ​​​​+------------------+
    ​​​​| Tables_in_testdb |
    ​​​​+------------------+
    ​​​​| addrbook         |
    ​​​​+------------------+
    ​​​​1 row in set (0.00 sec)
    
    ​​​​MariaDB [testdb]> insert into addrbook(name, phone) values ("tom", "0912123456");
    ​​​​Query OK, 1 row affected (0.02 sec)
    
    ​​​​MariaDB [testdb]> insert into addrbook(name, phone) values ("mary", "0912123567");
    ​​​​Query OK, 1 row affected (0.01 sec)
    
    ​​​​MariaDB [testdb]> select name,phone from addrbook;
    ​​​​+------+------------+
    ​​​​| name | phone      |
    ​​​​+------+------------+
    ​​​​| tom  | 0912123456 |
    ​​​​| mary | 0912123567 |
    ​​​​+------+------------+
    ​​​​2 rows in set (0.00 sec)
    
    ​​​​MariaDB [testdb]> select name,phone from addrbook where name="tom";
    ​​​​+------+------------+
    ​​​​| name | phone      |
    ​​​​+------+------------+
    ​​​​| tom  | 0912123456 |
    ​​​​+------+------------+
    ​​​​1 row in set (0.00 sec)
    
    ​​​​MariaDB [testdb]>
    
  7. 安裝 PHP
    ​​​​[root@mycentos7-1 ~]# yum install php php-mysql php-fpm
    ​​​​[root@mycentos7-1 ~]# systemctl restart httpd.service
    
  8. 編輯 info.php,寫入 <?php phpinfo(); ?>
    ​​​​[root@mycentos7-1 ~]# vim /var/www/html/info.php
    
  9. Test PHP
    http://192.168.56.100/info.php
    image
  10. 刪除 info.php,並新增 test.php
    ​​​​[root@mycentos7-1 ~]# cd /var/www/html
    ​​​​[root@mycentos7-1 html]# ls
    ​​​​hihi.htm  info.php  me1.files  me1.htm
    ​​​​[root@mycentos7-1 html]# rm -rf info.php
    ​​​​[root@mycentos7-1 html]# vim test.php
    
    • test.php
      ​​​​​​​​<?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";
      ​​​​​​​​}
      ​​​​​​​​?>
      
  11. 開啟網頁
    http://192.168.56.100/test.php
    image

預設公用目錄

  1. 設定 httpd
    ​​​​[root@mycentos7-1 ~]# cd /etc/httpd/conf.d
    ​​​​[root@mycentos7-1 conf.d]# ls
    ​​​​autoindex.conf  php.conf  README  userdir.conf  welcome.conf
    ​​​​[root@mycentos7-1 conf.d]# vim userdir.conf
    
    • userdir.conf
    ​​​​<IfModule mod_userdir.c>
    ​​​​#
    ​​​​# UserDir is disabled by default since it can confirm the presence
    ​​​​# of a username on the system (depending on home directory
    ​​​​# permissions).
    ​​​​#
    ​​​​UserDir enabled
    
    ​​​​#
    ​​​​# To enable requests to /~user/ to serve the user's public_html
    ​​​​# directory, remove the "UserDir disabled" line above, and uncomment
    ​​​​# the following line instead:
    ​​​​#
    ​​​​UserDir public_html
    ​​​​</IfModule>
    
  2. 創建 public_html & index.html,並更改設定
    ​​​​[root@mycentos7-1 ~]# cd /home/user
    ​​​​[root@mycentos7-1 user]# mkdir public_html
    ​​​​[root@mycentos7-1 user]# cd public_html
    ​​​​[root@mycentos7-1 public_html]# echo "Hello 111010501" > index.html
    ​​​​[root@mycentos7-1 public_html]# chmod 755 /home/user  # 可讀不可寫
    
  3. 重新加載 httpd & 開啟網頁
    ​​​​[root@mycentos7-1 public_html]# systemctl reload httpd
    
    http://192.168.56.100/~user/index.html
    image

別名(虛擬目錄) Aliases

  1. 創建資料夾 & 文件

    ​​​​[root@mycentos7-1 ~]# cd /
    ​​​​[root@mycentos7-1 /]# mkdir /data1
    ​​​​[root@mycentos7-1 /]# cd /data1
    ​​​​[root@mycentos7-1 data1]# echo "Hello 111010501" > hi.htm
    
  2. 連結

    ​​​​[root@mycentos7-1 data1]# cd /etc/httpd/conf
    ​​​​[root@mycentos7-1 conf]# vim httpd.conf
    

    在檔案最下面加入 (同意在此目錄的所有存取)

    ​​​​Alias /abc /data1
    ​​​​<Directory /data1>
    ​​​​ Required all granted
    ​​​​</Directory>
    

    修改設定後systemctl restart httpd

  3. 開啟 http://192.168.56.100/abc/hi.htm

    image

Commands

-v : 反轉
^ : 以為開頭
^$ : 空白行 (以空白開頭及結尾)

[root@mycentos7-1 conf]# cat httpd.conf | grep -v ^# | grep -v ^$

重新加載配置檔
restart = stop + start 連線會中斷
reload 重新加載而已,現存的連線可以保存 (溫和手段)

[root@mycentos7-1 ~]# systemcrl reload httpd

強制刪除 httpd 相關行程

[root@mycentos7-1 ~]# pkill -9 httpd

第十四週

  • 後面的規則會覆蓋前面的
    image

架設 Https 網站 (CAN'T)

  • 先搞出 Ipv6 (橋接網卡only)
    yum -y install epel-release mod_ssl certbot
    certbot certonly --webroot -w /var/www/html/ -d linux217.dns.army --email alex24922665@gmail.com

  • 參考PDF

第十五週

FTP古老好用但不安全(內網使用相對安全)
網路上可使用SFTP,較安全但會失去一些功能
使用TCP,21Port連線永遠存在(Control & Command),20Port只在傳資料時存在

FTP指令

所有指令都是Server端操作,指令前加 ! 可在Client端操作

image

存取控制-IP

  1. cd /var/www/htmlmkdir b
  2. cd /becho "HI JUNG">a.txt
  3. vim /etc/httpd/conf/httpd.conf
    ​​​​<Directory /var/www/html>
    ​​​​    ...
    ​​​​    AllowOverride All
    ​​​​    ...    
    ​​​​</Directory>   
    ​​​​
    ​​​​<Directory /var/www/html/b>
    ​​​​    Order deny,allow
    ​​​​    Deny from all
    ​​​​    Allow from 192.168.56.1
    ​​​​</Directory>   
    
    systemctl restart httpd
  4. 連線
    image

存取控制-驗證帳號與密碼

  1. 建立資料夾 cd /var/www/htmlmkdir files
  2. 新增檔案 cd /filesecho "aaa">a.txtecho "bbbb">b.txtecho "ccccc">c.txt
  3. 新增使用者 htpasswd -c .htpasswd userhtpasswd .httpasswd alex
  4. 新增編輯存取配置檔
    vim .htaccess
    ​​​​AuthType Basic
    ​​​​AuthName "Private File Area"
    ​​​​AuthUserFile /var/www/html/files/.htpasswd
    ​​​​Require valid-user
    
  5. 編輯 httpd 配置檔
    vim /etc/httpd/conf/httpd.conf
    ​​​​<Directory /var/www/html/files>
    ​​​​    Options Indexes
    ​​​​    AllowOverride AuthConfig
    ​​​​</Directory>
    
    systemctl restart httpd
  6. 連線
    螢幕擷取畫面 2023-12-19 145906

vsftpd(very secure ftpd)

主要設定檔 : /etc/vsftpd/vsftpd.conf

  1. 安裝 & 啟動yum install vsftpd -ysystemctl start vsftpd
  2. 用 Winscp 測試匿名者&使用者登入
    image
    image

    image
    image

第十六週

期末考

  1. 安裝linux
  2. ssh server (無密碼登錄, sftp)
  3. ngrok(外網 Http 伺服器)
  4. NFS
  5. samba
  6. ipv6架站
  7. https架站
  8. VPN (pptpd)
  9. httpd + php + mysql (LAMP)
  10. httpd(資料夾限制ip存取, 密碼登錄下載檔案)
  11. FTP server (vsftpd)
  12. 使用network設定網路

第十七週

Telnet、echo server