Linux伺服器 Linux伺服器

資工所專業課程上課教材

資工所專業課程 > 課程內容 > 專題十五 - 其他

專題十五 - 其他

上次更新日期 2019/06/14

要架設一部或多部 Server 時,仍有許多需要注意的事項!這邊提出幾個可能會經常遇到的問題來解決一下囉!

  • Let's Encrypt 的 https 憑證發放
  • 取得 SNMP 的相關資料
  • 參考資料

Let's Encrypt 的 https 憑證發放

傳統的 https 憑證取得,需要你自己製作 https 的公/私鑰資料,然後將這兩把鑰匙製作出向 CA 提供的憑證檔, 將此檔案進行註冊後,就可以取得正確的憑證了。不過,這都需要 $$ 的。如果想要免費的 https 憑證供應,可以參考 Let's Encrypt 這個公益單位的文件, 輕鬆註冊處理好我們的 https 憑證。

  1. 理解你的 https server 的 domain name 控制權:
    1. 你這部主機需要有 public IP 的 domain name,舉例來說,我們以 www.dic.ksu.edu.tw 來說,這部主機可以解析出 IP, 意思就是如此。如果你的 https server 並沒有主機名稱,那基本上是無法使用 https 憑證的。 所以,請參考 DNS 的設定章節,或者是直接與 ISP 聯繫來取得你的主機名稱。
    2. 我們這邊使用 pc510.ev.ncku.edu.tw 這部主機來作為示範,這部主機同時也擁有 cs.ncku.edu.tw 這個主機名稱喔! 所以,同一個 IP 具有兩個主機名稱的意思。
      # dig cs.ncku.edu.t
      # dig pc510.ev.ncku.edu.tw
      # 都會得到 IP 是 140.116.44.179 喔!
      
    3. 另外,既然要使用 https ,當然就得要有 https 的伺服器,這裡我們使用 apache 這個方便好用的 web server, 所以你需要 httpd, mod_ssl 等軟體,同時得要啟用才行!
      # systemctl status httpd
      ● httpd.service - The Apache HTTP Server
         Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
         Active: active (running) since 六 2019-06-08 23:54:22 CST; 4 days ago
           Docs: man:httpd(8)
                 man:apachectl(8)
      ....
      
      # netstat -tlnup
      Active Internet connections (only servers)
      Proto Recv-Q Send-Q Local Address   Foreign Address   State   PID/Program name
      tcp6       0      0 :::80           :::*              LISTEN  3432/httpd
      tcp6       0      0 :::443          :::*              LISTEN  3432/httpd
      
  2. 開始安裝及處理所需要的軟體:
    1. 開始前往 Let's Encrypt 取得說明文件,因為該網站的說明文件已經太清楚了!直接使用就 OK 囉! 最重要的就是最後的連結,幾乎用了該網頁的介紹就好了!我們這裡使用的是 CentOS 7 以及 apache 軟體的搭配!
    2. 軟體的安裝,這個軟體需要大概就是兩個,一個是 certbot 主程式,一個則是 python 函式庫:
      # yum --enablerepo=epel search certbot
      python2-certbot.noarch : Python 2 libraries used by certbot
      python2-certbot-apache.noarch : The apache plugin for certbot
      ...
      certbot.noarch : A free, automated certificate authority client
      
      # yum --enablerepo=epel install certbot python2-certbot-apache
      # 這樣就安裝好了你的 certbot 支援軟體!
      
    3. 這個 certbot 主要的目的是在根據你的 https server 來設定好你的憑證檔!其實處理的方式也很簡單! 不過鳥哥人比較龜毛,希望自己先查看看有沒有問題,沒問題才安裝!所以,先來處理憑證事宜:
      # certbot --apache certonly
      Saving debug log to /var/log/letsencrypt/letsencrypt.log
      Plugins selected: Authenticator apache, Installer apache
      Enter email address (used for urgent renewal and security notices) (Enter 'c' to
      cancel): dmtsai@mail.ksu.edu.tw
      Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
      
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Please read the Terms of Service at
      https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
      agree in order to register with the ACME server at
      https://acme-v02.api.letsencrypt.org/directory
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      (A)gree/(C)ancel: A
      
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Would you be willing to share your email address with the Electronic Frontier
      Foundation, a founding partner of the Let's Encrypt project and the non-profit
      organization that develops Certbot? We'd like to send you email about our work
      encrypting the web, EFF news, campaigns, and ways to support digital freedom.
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      (Y)es/(N)o: N
      
      Which names would you like to activate HTTPS for?
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      1: cs.ncku.edu.tw
      2: pc510.ev.ncku.edu.tw
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Select the appropriate numbers separated by commas and/or spaces, or leave input
      blank to select all options shown (Enter 'c' to cancel): 1, 2
      Obtaining a new certificate
      Performing the following challenges:
      http-01 challenge for cs.ncku.edu.tw
      http-01 challenge for pc510.ev.ncku.edu.tw
      Waiting for verification...
      Cleaning up challenges
      Resetting dropped connection: acme-v02.api.letsencrypt.org
      
      IMPORTANT NOTES:
       - Congratulations! Your certificate and chain have been saved at:
         /etc/letsencrypt/live/cs.ncku.edu.tw/fullchain.pem
         Your key file has been saved at:
         /etc/letsencrypt/live/cs.ncku.edu.tw/privkey.pem
         Your cert will expire on 2019-09-11. To obtain a new or tweaked
         version of this certificate in the future, simply run certbot
         again. To non-interactively renew *all* of your certificates, run
         "certbot renew"
       - Your account credentials have been saved in your Certbot
         configuration directory at /etc/letsencrypt. You should make a
         secure backup of this folder now. This configuration directory will
         also contain certificates and private keys obtained by Certbot so
         making regular backups of this folder is ideal.
       - If you like Certbot, please consider supporting our work by:
      
         Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
         Donating to EFF:                    https://eff.org/donate-le
      
      # ll /etc/letsencrypt/options-ssl-apache.conf
      -rw-r--r--. 1 root root 1591  6月 13 22:59 /etc/letsencrypt/options-ssl-apache.conf
      # 上面這個檔案,就是最重要的給 apache 使用的設定擋了!
      # 連同憑證、相關其他設定等,都在這個 /etc/letsencrypt 目錄下喔!
      
    4. 基本上,上面的動作如果看過後,覺得沒啥大問題,接下來,就可以直接修改 apache 自動處理的機制了:
      # 1. 先移除之前建立的憑證資料,免得後來互相干擾:
      # cd /etc/letsencrypt
      # rm -r *
      
      # 2. 開始建立整體的憑證資料:
      # certbot --apache -d cs.ncku.edu.tw -d pc510.ev.ncku.edu.tw -d aerosol.ev.ncku.edu.tw
      Saving debug log to /var/log/letsencrypt/letsencrypt.log
      Plugins selected: Authenticator apache, Installer apache
      Enter email address (used for urgent renewal and security notices) (Enter 'c' to
      cancel): dmtsai@mail.ksu.edu.tw
      Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
      
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Please read the Terms of Service at
      https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
      agree in order to register with the ACME server at
      https://acme-v02.api.letsencrypt.org/directory
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      (A)gree/(C)ancel: A
      
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Would you be willing to share your email address with the Electronic Frontier
      Foundation, a founding partner of the Let's Encrypt project and the non-profit
      organization that develops Certbot? We'd like to send you email about our work
      encrypting the web, EFF news, campaigns, and ways to support digital freedom.
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      (Y)es/(N)o: N
      Obtaining a new certificate
      Performing the following challenges:
      http-01 challenge for aerosol.ev.ncku.edu.tw
      http-01 challenge for cs.ncku.edu.tw
      http-01 challenge for pc510.ev.ncku.edu.tw
      Waiting for verification...
      Cleaning up challenges
      Resetting dropped connection: acme-v02.api.letsencrypt.org
      Deploying Certificate to VirtualHost /etc/httpd/conf.d/ssl.conf
      Created an SSL vhost at /etc/httpd/conf.d/vbird-le-ssl.conf
      Deploying Certificate to VirtualHost /etc/httpd/conf.d/vbird-le-ssl.conf
      
      We were unable to find a vhost with a ServerName or Address of aerosol.ev.ncku.edu.tw.
      Which virtual host would you like to choose?
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      1: ssl.conf                       | cs.ncku.edu.tw        | HTTPS | Enabled
      2: dmtsa.conf                     | cs.ncku.edu.tw        |       | Enabled
      3: dmtsa-le-ssl.conf              | Multiple Names        | HTTPS | Enabled
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Select the appropriate number [1-3] then [enter] (press 'c' to cancel): 1
      Deploying Certificate to VirtualHost /etc/httpd/conf.d/ssl.conf
      
      Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      1: No redirect - Make no further changes to the webserver configuration.
      2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
      new sites, or if you're confident your site works on HTTPS. You can undo this
      change by editing your web server's configuration.
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
      
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Congratulations! You have successfully enabled https://cs.ncku.edu.tw,
      https://pc510.ev.ncku.edu.tw, and https://aerosol.ev.ncku.edu.tw
      
      You should test your configuration at:
      https://www.ssllabs.com/ssltest/analyze.html?d=cs.ncku.edu.tw
      https://www.ssllabs.com/ssltest/analyze.html?d=pc510.ev.ncku.edu.tw
      https://www.ssllabs.com/ssltest/analyze.html?d=aerosol.ev.ncku.edu.tw
      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      
      IMPORTANT NOTES:
       - Congratulations! Your certificate and chain have been saved at:
         /etc/letsencrypt/live/cs.ncku.edu.tw/fullchain.pem
         Your key file has been saved at:
         /etc/letsencrypt/live/cs.ncku.edu.tw/privkey.pem
         Your cert will expire on 2019-09-11. To obtain a new or tweaked
         version of this certificate in the future, simply run certbot again
         with the "certonly" option. To non-interactively renew *all* of
         your certificates, run "certbot renew"
       - Your account credentials have been saved in your Certbot
         configuration directory at /etc/letsencrypt. You should make a
         secure backup of this folder now. This configuration directory will
         also contain certificates and private keys obtained by Certbot so
         making regular backups of this folder is ideal.
       - If you like Certbot, please consider supporting our work by:
      
         Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
         Donating to EFF:                    https://eff.org/donate-le
      
      這樣就搞定了!直接重新啟動 httpd 之後,連進你的 https://your.host.name ,就可以不用填寫一堆怪怪的確認單, 直接進入你的網站囉!真愉快吧!
  3. 定期自動更新憑證:
    1. Let's Encrypt 所發放的憑證,只能提供 3 個月而已,因此, 3 個月後,你得要自己處理相關的事宜才行! 不過, Let's Encrypt 沒有這麼可惡~其實這個 certbot 指令就有提供自動更新的功能!那就是 certbot-renew.timer 這個計時器! 你只要將它啟動即可~其他什麼事都不用動作!這個計時器每 6 個小時會去檢查你的憑證有沒有過期~ 如果過期,就會自動的去更新,讓它重新可在用 3 個月!所以你都不用擔心啊!
      # systemctl enable certbot-renew.timer
      # systemctl start certbot-renew.timer
      # systemctl status certbot-renew.timer
      ● certbot-renew.timer - This is the timer to set the schedule for automated renewals
         Loaded: loaded (/usr/lib/systemd/system/certbot-renew.timer; enabled; vendor preset: disabled)
         Active: active (waiting) since 四 2019-06-13 23:56:59 CST; 21s ago
      
    2. 那你怎麼知道你的系統有沒有正確的更新憑證呢?其實在剛剛安裝憑證時,系統有提供幾個連結, 就可以測試你的憑證囉!例如這個網站的分析結果:

取得 SNMP 的相關資料

在某些機器的運作當中,我們可能需要取得這些機器的運作狀態,但是卻無法登入該機器設備時,可以使用這個設備提供的 snmp 協定來處理! 哪些機器你無法登入呢?有可能是印表機、switch、webcam、IPcam 等等設備!這個時候,透過區域網路內的 Linux 系統, 直接以 snmp 相關指令來處理,就能取得該設備運作時的許多狀態了!包括 CPU 狀態、啟動的時間、網路參數狀態等等喔!

  1. 理解 SNMP 協定:
    1. SNMP 協定全名為『 Simple Network Management Protocol, SNMP 』,就是『簡單網路管理協定』的意思。 這個協定主要是運作於 OSI 七層協定內的應用層,並不是網路基礎啦!根據 wiki 的說明,它的功能有點像這樣:『 該協定能夠支援網路管理系統,用以監測連接到網路上的裝置是否有任何引起管理上關注的情況。 它由一組網路管理的標準組成,包含一個應用層協定 (application layer protocol)、資料庫模式 (database schema),和一組資料物件。』
    2. SNMP 管理的網路,大致上由以下三個元件組成:
      1. 網路管理系統 (NMSs, Network Management Systems):在這個系統上面執行應用程式 (如 snmpwalk 等), 以用來管理、監控被管理的網路裝置。管理員大致上就是在這個系統上面與網路裝置進行互動。一般來說, 大部分的應用程式,主要都在監控而已,比較少用於設定與管理。
      2. 被管理的裝置 (managed device):一個網路節點,例如主機系統、switch系統、printer系統等等。通常這個被管理的系統上, 會安裝代理程式 (一般就是 snmp 協定相關的應用程式),透過管理資訊庫 (Management Information Base, MIB) 收集、儲存資訊, 並讓 NMSs 可以透過 SNMP 代理程式取得這些資訊。
      3. 代理程式 (agent):通常就是代表在被管理裝置 (managed device) 上面所安裝的管理軟體模組, 可以收集被管理裝置的總總資訊,並透過 SNMP 相容的格式傳送這些訊息。
    3. SNMP 的發展,因為不同的時刻提出的資料完整性不同,因此有不一樣的版本。如果只是要用於監控系統, 大概使用第 2 版會比較好,因為第一版資料傳輸使用明碼,安全性可能較不足。
      1. SNMPv1
        提供簡單資料型態與泛用資料型態,且第一版可以提供資料在傳輸層傳輸,包括 UDP 與 IP 封包等。 只是第一版使用明碼進行傳輸,資安保障較不佳。不過第一版還是常見於某些提供簡單監控的設備上, 提供的資料就不是很重要的資訊而已。
      2. SNMPv2, SNMPv2c, SNMPv2u
        第二版加強了第一版的 SM 規格資料型態,包括應用了位元串、網路位址跟計數器資料等。 同時第二版也提供了社群版本的 SNMP 功能,以及使用者功能 (v2u),增加了較多的傳輸安全,也是目前較常使用的版本。
      3. SNMPv3
        提供了認證功能與資料加密功能,如果需要進行遠端組態的設定管理,就得要使用這個版本! 前兩個版本大致上都是在『監控』而已喔!
    4. SNMP 所監測的資料,可以透過所謂的 Object identifiers (OID) 的定義來取得!OID (目標元件識別碼) 是由國際電信聯盟 (ITU) 所定義出來的一些識別碼,可以透過這些識別碼,來取得某些特定元件的資訊。這些元件可以透過一組數字來了解該項目為何, 其定義有點像底下這樣:
      1.3.6.1.4.1.343
      iso.identified-organization.dod.internet.private.enterprise.intel
      
      上面這個 OID 的意義有點像這樣:
      • 1: ISO
      • 1.3: 定義的組織
      • 1.3.6: dod
      • 1.3.6.1: internet
      • 1.3.6.1.4: private
      • 1.3.6.1.4.1: IANA 企業代表號碼
      • 1.3.6.1.4.1.343: intel 這家企業
      透過這些數字,可能就能抓到被管理裝置的某些特定的元件數值,例如網卡卡號、IP、CPU、型號等等特徵喔!
    5. 另外,SNMP 可以自由定義某些管理訊息資訊 (management information base, MIB),但這些 MIB 通常還是與 OID 有點關係, 這樣比較不容易造成誤解。不過,因為目前網際網路社會有提供公認的 MIB 模組 (MIB-module),然後透過 libsmi 這個 API, 就可以將 MIB 的資訊轉化成為易讀的資料了。
      1. MIB 主要透過 ASN.1 的標準定義來處理的,其中,又以 SMI (Structure of Management Information, 管理訊息結構) 的定義較常見。
      2. MIB 的資訊主要是以樹狀結構來處理,由根 (root) 向下延伸出許多不同的組成元件。因為受管理的裝置,通常有很多裝置特徵, 這些特徵就透過樹狀結構來表達。
      至於常見的 MIB 索引大致上有:
      • SNMPv2-SMI: RFC 2578 — Structure of Management Information Version 2 (SMIv2)
      • SNMPv2-MIB: RFC 3418 — Management Information Base (MIB) for the SNMP
      • TCP-MIB: RFC 4022 — Management Information Base for the TCP
      • UDP-MIB: RFC 4113 — Management Information Base for the UDP
      • IP-MIB: RFC 4293 — Management Information Base for the IP
      • IF-MIB: RFC 2863 — The Interfaces Group MIB
  2. # yum install net-snmp-utils libsmi
    # snmpwalk -v2c -c public 192.168.201.252
    # snmpwalk -v2c -c public 192.168.201.252 system
    

分隔線

# 
# 

參考資料題