--- tags: 1091, lsa --- - Book mode https://hackmd.io/@ncnu-opensource/book :::danger 不要公開自己的個人 EC2 的資訊喔 ::: # Week 10(2020/11/26) ## 1. SSH 連線 :::success ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCMCsWRObWBj5mv8FdWFNEb17TDTnlVH6yT1QAOemfsCsjIc1VTTGi1t7HkQJegSJdORNJe2ezSU1vPYhuOCWsnzC2x7tD2aAhrs3SlecR5pMYyhfCsOW+hP8ypb6aaHFSMK49fQf2QGhg80z3pwF1CDIO/3SfhjZR2eA69IsoopgmZ71Q8XmS790GPCNCsBdMkHRgTqJGp8DloxLkuMJQr8ouzm9JpKnnq2Uh0QCHRuRlonKkt/TQXntLR7htaniHjGe/wOcqfzxYfGTLDsPP7rbgZ3tMdttB0IFqGi1jNCdmnpQH15LSY+GnapaBgQekwtIrXsGucUYhM8e2L9dDz 1091lsamideumtest ::: ```shell= # 要先換到token下喔 ssh -i <token> ubuntu@<ip> #ex: ssh -i 1091-lsa.pem ubuntu@x.x.x.x ``` - 公鑰位置 : 在 `.ssh` 的目錄下,authorized_keys - 如何放進去 (2 種方式) 1. echo 公鑰 >> authorized_keys 2. sudo vim 放進去(不要蓋掉原本公鑰匙原則下,在其下,複製貼上?!) ## 2. rwx 改權限 ![](https://i.imgur.com/Q0rzG1H.png) - 在家目錄(~) - 建立資料夾 mkdir 資料夾名稱 `mkdir 1091-lsa-<學號>` - 查看資料夾權限 `ls -al` - 改權限 ```shell= chmod 754 <filename> 4 : r 2 : w 1 : x ``` ![](https://i.imgur.com/4ulKHTk.png) ## 4. 查詢主機狀態資訊 ![](https://i.imgur.com/O4yM5Ss.png) - `-a` 把所有列出來(本題不需要) - 完整指令 ```shell= sudo netstat -ntpl | awk '{print $4"\t"$7}' | grep 80 | sudo tee /etc/output.txt ``` - 第一題 ![](https://i.imgur.com/GOh811l.png) - 第二題 ![](https://i.imgur.com/9WN9CHM.png) - 第三題 ![](https://i.imgur.com/mlIJ3oG.png) - 第四題 ![](https://i.imgur.com/b7eudTb.png) ```shell= sudo vim /etc/output.txt ``` ![](https://i.imgur.com/K7nPfje.png) ## 5. 安裝服務 ![](https://i.imgur.com/iUQAG0s.png) - 安裝 ```shell= #新的虛擬機 需要先update一下 sudo apt update sudo apt install proftpd ``` - 開放匿名 - 把 <Anonymous ~ftp> 註解拿掉 ```shell= sudo vim /etc/proftpd/proftpd.conf ``` anonymous ~ftp 註解拿掉 ```shell= <Anonymous> ... ... ... </Anonymous> ``` ![](https://i.imgur.com/BDoiAfx.png) >不用客氣 - 使用ftp連本機 ```shell= #記得先把服務重啟! sudo service proftpd restart ftp 127.0.0.1 ``` - 名稱:anonymous(==一定要小寫喔==) - 密碼:(無) ![](https://i.imgur.com/NR9YCon.png) ## 6. Hostname / DNS ![](https://i.imgur.com/cWdOwAp.png) - 改 Hostname,改完要reboot(也可以重開終端機'exit'再重新連線) ```shell= sudo hostnamectl set-hostname <hostname> ``` - ping www 自動導到 `ncnu.edu.tw` - 意即不論 ping 什麼東西,找不到時都會去尋找 ncnu.edu.tw ```shell= sudo vim /etc/resolv.conf search ncnu.edu.tw # 新增這一行 ping www ``` ![](https://i.imgur.com/s8y8MeM.png) ## 3. Web Server ![](https://i.imgur.com/qISvrFB.png) ### 建立彈性IP、虛擬機 * 2 (apache and lighttpd) ***1. Apache2*** - AWS左邊功能選單 選擇 `Elastic IPs`(彈性IP) - ![](https://i.imgur.com/uYe4b1S.png =250x) - ![](https://i.imgur.com/NzaiJOG.png) - 點選 **配置彈性IP** - 點 **配置**(設定都不用改) - 回**instance**開新伺服器 - **Step3:** - subnet: 可選 `1a`或`1f`,但需跟lighttpd的相同 - Auto-assign Public IP: disable - ![](https://i.imgur.com/5Nuy4ue.png) - **Step6:**(讓apache lighttpd透過private IP連入) - 取名: for apache and lighttpd - ![](https://i.imgur.com/5VEU2my.png) - 再回**Elastic IPs** - 點右上角 Associate Elastic IP address - 選擇剛建立的虛擬機 ![](https://i.imgur.com/vYUwpdA.png) --- 連入剛建立的apache虛擬機 - 先改 hostname ```shell= sudo hostnamectl set-hostname lsa_apache ``` ***2. Lighttpd*** - 先在**Elastic IPs**再建立一個ip - **step 6** 可以選擇已建立好的 ![](https://i.imgur.com/tioWHEu.png) - 再回**Elastic IPs** - 同上步驟,選擇剛建立的Lighttpd ![](https://i.imgur.com/iYOPv6G.png) --- 連入虛擬機 - 更改 hostname ```shell= sudo hostnamectl set-hostname lsa_lighttpd ``` ### 回虛擬機上操作 - 在apache上安裝: ```shell= sudo apt install apache2 ``` <!-- ### --> - 在lighttpd上安裝 ```shell= sudo apt update sudo apt install lighttpd ``` ### 設定 apache ![](https://i.imgur.com/DUPHpKV.png =200x) #### 設定 80 port - 確認預設路徑 ```shell= cd /etc/apache2/sites-enabled sudo vim 000-default.conf ``` ![](https://i.imgur.com/g4pxUOq.png) - 到`/var/www/html`目錄下建立檔案 ```shell= cd /var/www/html sudo rm index.html # 把原本的index.html 刪除 sudo vim index.html # 建自己的檔案(內容) ``` - 在 `index.html` 輸入需求資料(TA們的名字) ```htmlembedded= <head> <meta charset='UTF-8'> <h1>助教們的中文名字</h1> <h2>歐芷欣、蔡佳軒、郭子緯、黃雅琳、李漢偉</h2> </head> ``` - ![](https://i.imgur.com/C7Dp1yc.png) #### 設定 :80/< user > - 開啟userdir功能 ```shell= sudo a2enmod userdir ``` - 在 `~` 建立自己的資料夾 ```shell= mkdir public_html ``` - 在資料夾內建立一個 html 檔 ```shell= vim public_html/index.html #輸入題目要求(學生資訊) ``` - 重新啟動 apache ```shell= sudo service apache2 restart ``` - 可以複製自己的public ip,檢查剛剛做的是否有誤 - ![](https://i.imgur.com/3wCYLkt.png) ### 設定 lighttpd ![](https://i.imgur.com/XRRfum1.png) - 先關掉lighttpd服務 ```shell= sudo service lighttpd stop ``` #### 設定 80 port - 直接改預設 ```shell= cd /etc/lighttpd ``` ```shell= sudo vim lighttpd.conf ``` - 到 /var/www/html 建立 index.html(原本沒有) ```shell= cd /var/www/html/ sudo vim index.html ``` - 輸入題目要求(老師資訊) - 有錯幫忙更正XD ```=html <head> <meta charset='UTF-8'> <h1>LSA 的老師</h1> <h2>中文名字:練喆明</h2> <h2>英文全名:BlueT Matthew Lien(Che-Ming Lien)</h2> <h2>常用 ID:BlueT</h2> <h2>Telegram ID:BlueT_Lien</h2> <h2>Facebook 個人檔案的名稱:Bluet Matthew Lien(練喆明)</h2> </head> ``` - 要輸入ip檢查看是否成功記得先開啟lighttpd ```shell= sudo service lighttpd restart ``` #### 設定 8080 port proxy 到 BT 的網站 - 新開一個分頁,確認bt網站的ip ```shell= ping lsa.bluet.org ``` - 到 `etc/lighttpd/conf-available` 新增檔案 ```shell= cd /etc/lighttpd/conf-available sudo vim proxy.conf ``` - 同時聽不同的 port - lighttpd socket ```shell= server.modules += ( "mod_proxy" ) $SERVER["socket"]== ":8080" { proxy.balance = "hash" proxy.server = ( "" => (("Host" => "188.166.188.226", "port" => "80",))) } ``` - enable剛剛的設定 - 建立 softlink ```shell= cd ../conf-enabled sudo ln -s ../conf-available/proxy.conf proxy.conf ``` - `ls -al` 確定一下,是否成功 ![](https://i.imgur.com/skB49Zi.png) - 開啟服務: ```shell= sudo service lighttpd start ``` - 檢查是否成功 ```shell= sudo service lighttpd status ``` - 又可以複製自己的public ip,檢查剛剛做的是否有誤了! ### 設定 nginx #### 建立一台虛擬機(不用使用 Elastic Ips) - **step 3** 改 **subnet** 到和剛剛設定相同的(1a) - 不用改 Auto-assign Public IP ![](https://i.imgur.com/55oLSz9.png) #### 連入虛擬機做設定 - 更改 hostname ```shell= sudo hostnamectl set-hostname <hostname> ``` - 下載 nginx ```shell= sudo apt install nginx ``` #### 設定用 public ip 連進來的(80 port) ![](https://i.imgur.com/hwLXOul.png) - 改 default ```shell= cd /etc/nginx/sites-enabled sudo vim default ``` - 可以考慮把註解刪掉 (`行數`dd) - 由第一行 將內容刪到如下圖! -![](https://i.imgur.com/1WKmQnB.png) - 到 `/var/www/html` 創 `index.html` ```shell= cd /var/www/html/ sudo vim index.html ``` - 輸入題目需求(課程相關) - 有錯幫忙更正~ ```=html <head> <meta charset='UTF-8'> <h1>課程相關</h1> <h2>中文全名:Linux 系統管理實務(一)</h2> <h2>英文全名:Linux System Administration(Practical Linux System Administration - Level 1)</h2> <h2>英文縮寫:LSA</h2> </head> ``` #### 設定用 public ip proxy 到某網站 (8080 port) - 去 `/etc/nginx/sites-available` 複製一份 default ```shell= cd /etc/nginx/sites-available sudo cp default proxy_vincent ``` - 把內容改成要 proxy 到助教的網站 ```shell= sudo vim proxy_vincent ``` ```shell= server { listen 8080 default_server; listen [::]:8080 default_server; server_name _; location /{ proxy_pass https://vincentinttsh.github.io; } } ``` - 在8080進入enable之前先關閉服務 ```shell= sudo service nginx stop ``` - 開啟nginx ```shell= sudo service nginx start ``` #### 設定 domain name [NO-IP 免費提供 public domain name]( https://www.noip.com/) - log in - 將IPv4 改成AWS上的public IP(nginx) #### 設定 Nginx proxy 到 apache ![](https://i.imgur.com/GAoIi0z.png) - 在 **nginx** 那台server上做更改 - 在 `/etc/nginx/sites-available` ```shell= #複製一份 sudo cp proxy_vincent proxy_apache sudo vim proxy_apache ``` ```shell= server { listen 80 ; listen [::]:80 ; server_name {No_IP申請到的IP 之host name}; location /{ proxy_pass http://{apache 那台的 private ip}; } } ``` :::success * private ip 可以在安裝 apache 的那台 server 中查看 ```shell= ifconfig ``` * host name 舉例 > 109213507-apache.hopto.org ::: #### 設定 Nginx proxy 至 Lighttpd ![](https://i.imgur.com/TVAM2zY.png) - proxy 到 80 port ```shell= sudo cp proxy_apache proxy_lighttpd1 sudo vim proxy_lighttpd1 ``` ```shell= server { listen 80; listen [::]:80; server_name {No_IP申請到的IP 之host name}; location /{ proxy_pass http:// {lighttpd 那台的 private ip}; } } ``` - proxy 到 8080 port ```shell= sudo cp proxy_apache proxy_lighttpd2 sudo vim proxy_lighttpd2 ``` ```shell= server { listen 8080; listen [::]:8080; server_name {No_IP申請到的IP 之host name}; #109213507-lighttpd.hopto.org location /{ proxy_pass http:// {lighttpd 那台的 private ip:8080}; } } ``` #### 使用 nginx 建立 load balance ![](https://i.imgur.com/0JtEdxy.png) ```shell= sudo vim load_balance ``` ```shell= upstream backend { server <lighttpd的server private ip>:8080; server <lighttpd的server private ip>; server <apache的server private ip>; } server { listen 80; server_name <AWS 給的 nginx 的 domain name>;(nginx的公有 IPv4 DNS) location /{ proxy_pass http://backend; } } ``` - 瘋狂地把上面的設定softlink起來 (6個) - 在 `/etc/nginx/sites-enabled` - `sudo ln -s <從 available 到 enabled>` - default、proxy_apache、proxy_vincent、proxy_lighttpd1、proxy_lighttpd2、load_balance ```shell= sudo ln -s ../sites-available/default default sudo ln -s ../sites-available/proxy_apache proxy_apache sudo ln -s ../sites-available/proxy_vincent proxy_vincent sudo ln -s ../sites-available/proxy_lighttpd1 proxy_lighttpd1 sudo ln -s ../sites-available/proxy_lighttpd2 proxy_lighttpd2 sudo ln -s ../sites-available/load_balance load_balance ``` - 可以看有沒有錯 ```shell= sudo nginx -t ``` - 開啟久違的nginx ```shell= sudo service nginx start ``` - WHY 不設定 /etc/hosts - 他是只給自己用的 ## Iptables DMZ - DMZ: 非軍事區 (有點危險,但又得開放給大家用) - ![](https://i.imgur.com/pG2MY3X.png) ### LAB1 - ![](https://i.imgur.com/2AQFaW5.png) - ACCEPT 已建立連線的連線(ESTABLISHED)、相關的連線的 Forward ```shell= sudo iptables -A FORWARD -m conntrack --ctstate "ESTABLISHED,RELATED" ``` :::danger 不要再DROP POLICY了 ::: #### firewall(紫色線) - 確認三台虛擬機的ip ```shell= ifconfig ``` ![](https://i.imgur.com/IU0twnB.png) - 接受學生(student)的 ping - if對象是來自學生的ip要從這張網卡出去的要ACCEPT ```shell= sudo iptables -A INPUT -s <學生的ip> -p icmp --icmp-type 8 -j ACCEPT ``` - 接受學生(student)的ssh ```shell= sudo iptables -A -s {學生 ip} -p tcp --dport 22 -j ACCEPT ``` - 把其他部分丟掉 - 不要先下這個指令喔~ ```shell= sudo iptables -A INPUT -j DROP ``` - POSTROUTING - 2020/11/12 共筆有喔 - `sudo iptables -t nat -A POSTROUTING -o {wan} -j MASQUERADE` #### firewall(綠色線) - PREROUTING - 從 **firewall 80 port** 要先導到 **http 的 80 port** ```shell= sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to {http ip}:80 ``` - FORWARD - 接受要去 http 80 port 的封包 ```shell= sudo iptables -A FORWARD -d {http ip} --dport 80 -j ACCEPT ``` - 把其他的東西都丟掉 ```shell= sudo iptables -A FORWARD -j DROP ``` #### student 加個往firewall的gateway ```shell= sudo route add default gw {firewall ip} ``` #### http - 加個往firewall的gateway - 不論是否有要往外,說不定哪一天就會用到XD ```shell= sudo route add default gw {firewall ip} ``` ## LAB2 ![](https://i.imgur.com/9Sdd2Qp.png) ### BT補充 - DMZ: - 放些有點危險但還是要讓他危險的東西(例如moodle還是要讓著外面的人可以連,不是只有在學校才能用) - 讓外網有限制的存取內網資源 - 需要讓外網能夠連進來,但不希望隨便讓外網連進DMZ的所有東西 #### DMZ區塊 - if要讓外網連進DMZ S1(Apache) - 要允許的port - http 80 (tcp) - https 443 (tcp) - if 要讓外網連進DMZ S2(lighttpd) - 要允許的port - http 80 (tcp) - https 443 (tcp) - if 要讓外網連進DMZ S3(SSH) - 要允許的port - ssh 22 (tcp) #### NAT區塊 - 會允許連線跳板機(DMZ那區塊的s3 SSH)連進去NAT(透過紅色線) - 通常會開 ssh 22 port - 從DMZ的ssh發起連線到NAT要什麼設定? - 從**DMZ**的介面卡進來,從**NAT**的介面卡出去 - 從172.16.0.4進來 開**FORWARD** 去n1 n2 - 只接受經由s3 22 port(tcp) 往NAT 的封包 - BBI 想連線到 NAT 可不可以成功 - 不行喔QAQ - NAT 想連線到 Google 可不可以成功 - 可以 - 允許從NAT出發 經由firewall 往外網的所有封包 - 從 NAT 到 DMZ 要不要通呢 - 要,設一個指到 s1 和 s2 的80、443(https) 都要開,s3 的 22port(tcp) 也要給他過 - 那DMZ要不要給他出去(連到BBI)呢? - source是DMZ網段的,destination是whatever(除了NAT)的都DROP掉 - 當外網發request時要給予回應 - accept **ESTABLISHED、RELATED**時.. - 什麼是 default policy ? - 預設行為:如果沒有符合任何已知規則的話 - 把 default policy 改成 DROP :::info 設規則的時候要知道 **封包從哪裡來,要去哪裡,裡面是什麼東西** ::: > 要不要讓封包通過很簡單,要不要讓學生過就... [name=BlueT] #### 補充小東西 怎麼拔掉彈性IP - 把彈性IP拔掉 - 全選(如果全部要刪掉拉) - 選取 **動作**、取消關聯IP - ![](https://i.imgur.com/69rJCea.png) - 公布彈性ip(還給 AWS) - 停止或終止你執行的東西 ![](https://i.imgur.com/mkTrkxM.png) ## 助教名單 - 李漢偉 歐芷欣 蔡佳軒 郭子緯 黃雅琳 ## 老師資料 - LSA 的老師 - 中文名字:練喆明 - 英文全名:Che-Ming Lien - 常用 ID:BlueT - Telegram ID:BlueT_Lien - Facebook 個人檔案的名稱:Bluet Matthew Lien