Cody Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 科算期末考古 ## 登入提醒 > 主要會有兩台機器,一台是本機端(master);另一台是slave。 > **期末考主要是想透過兩台電腦之間的設定,去了解linux語法以及理解系統間的運作原理。** ### master ```cpp= ssh <帳號名稱>@140.112.12.122 -p <外部連入埠號> #i.e., ssh student18@140.112.12.122 -p 10022 ``` ### slave ```cpp= ssh <帳號名稱>@140.112.12.122 -p <外部連入埠號> #i.e., ssh student18@140.112.12.122 -p 10122 ``` ## 第一題 > 請設定主機名稱 (hostname),讓兩台虛擬機不必經由IP便認得彼此 (包括設定/etc/hosts檔。) ### 1.1 set hostname - Set net hostname ```cpp= hostnamectl set-hostname <NEW hostname> #NEW hostname = slave or master ``` - Show current hostname ```cpp= hostnamectl or hostname ``` ### 1.2 不必經由ip辨認得彼此 (etc/hosts) #### First: 在各自虛擬機打 ip a (把兩者的ip記下) ```cpp= ip a # i.e., slave:192.168.30.4 (inet) / master: 192.168.30.3 (inet) ``` #### Second: 在各自虛擬機打 vi /etc/hosts,加上slave及master的ip > vi / vim的編輯語法 > 按i編輯,按esc跳出去,並打:wq存檔 ![](https://i.imgur.com/pE2Epe9.png) ```cpp= vim /etc/hosts ``` #### Third: 確認是否不必經由IP便認得彼此 ```cpp= ping master ping slave #ping完,按ctrl+c跳出去 or cat /etc/hosts ``` ## 第二題 > 請在兩台虛擬機上都設定防火牆,禁止指定網段能連進虛擬機。 ### 2.1 網段的表示 #### ![2](https://lh5.googleusercontent.com/-x69xzePFVP0/TdesS4b6UsI/AAAAAAAABaU/iDGAZV8PP_8/s0/subnettingcfe8.png) #### 題目給定網段,若題目說明(61.56.x.x) ip = 61.56.0.0/16 #### 若題目說明(61.56.1.x) ip = 61.56.1.0/24 ### 2.2 禁止指定網段連到兩台虛擬機。 #### First: 在各自虛擬機打以下指令 ##### 測試 (master&slave都需要打) ```cpp= firewall-cmd --zone=<zone> --add-rich-rule='rule family="ipv4" source address="<IP>" reject' firewall-cmd --list-all firewall-cmd --reload ``` :::warning **zone** 打 public/internal 先測試不要加上 **--permanent** ::: ##### 最後要記得打permanent (master&slave都需要打) ```cpp= firewall-cmd --permanent --zone=<zone> --add-rich-rule='rule family="ipv4" source address="<IP>" reject' firewall-cmd --reload firewall-cmd --list-all or firewall-cmd --permanent --zone=<zone> --<add/remove>-rich-rule='rule family="ipv4" source address="<IP>" port protocol="<tcp/udp>" port="<port>" <accept/drop/reject>' ``` ## 第三題(其實該題可以等用完nfs再回去設立,這樣可以做一次即可) > 請讓 root 帳戶能從 master 以 SSH 方式免驗證密碼連入 slave。 - What is SSH? ssh就是Security Shell,能夠使用CLI透過**加密的網路傳輸協定遠端操作電腦**。通常ssh服務的port位在22。以下例子為使用user登入遠端操作ip為192.168.7.245的電腦,並透過port22進入。 ``` $ssh user@192.168.7.245 -p 22 ``` ### 3.1 先在master及slave下載openssh ```cpp= dnf install openssh openssh-server #install openssh firewall-cmd --permanent --zone=<zone> --add-service=ssh firewall-cmd --reload #加上新的功能記得firewall要reload(add-service or add-rich-rule..) ``` ### 3.2 更改/etc/ssh/sshd_config檔案 ```cpp= vim /etc/ssh/sshd_config PubkeyAuthentication yes #把註解拿掉 #PermitRootLogin no : : #Match Address <IP/Netmask>,<IP/Netmask>,... PermitRootLogin yes ``` ### 3.3 Build SSH Key ```cpp! su <user_name> mkdir ~/.ssh #mkdir=make directories chmod 700 ~/.ssh #chmod = change file mode bits cd ~/.ssh #cd = change directory ssh-keygen chmod 400 id_rsa.pub mv id_rsa.pub authorized_keys #mv = move ``` ### 3.4 Firewall ```cpp= firewall-cmd --permanent --zone=<zone> --add-service=ssh firewall-cmd --reload ``` ## 第四題 > 請在 master 虛擬機建立 10 個一般帳號,帳號名稱及密碼自訂。 ### 4.1 在master中創建一個資料夾並建立一個script ```cpp= mkdir add_user #rm -f filename(remove file) cd ~/add_user touch add.sh chmod 755 add.sh #執行檔案,若檔案無法執行,則打下圖顯示的command ``` ![](https://i.imgur.com/lIRylEi.png) ### 4.2 在檔案內更改內容 ```cpp= vim ~/add_user/add.sh ``` 複製以下內容貼上 #!/bin/bash useradd -u 1000 teacher1 useradd -u 1001 teacher2 useradd -u 1002 teacher3 useradd -u 1003 teacher4 useradd -u 1004 teacher5 useradd -u 1005 teacher6 useradd -u 1006 teacher7 useradd -u 1007 teacher8 useradd -u 1008 teacher9 useradd -u 1009 teacher10 執行 ```cpp= ~/add_user/add.sh ``` 檢查在home下有無10個帳號 ```cpp= cd /home/ ls -lh ``` #### 4.3 更改密碼 複製並貼上密碼兩次 passwd teacher1 EtzVBQBV passwd teacher2 fB57KXM= passwd teacher3 S5Fm%nTz passwd teacher4 3rFfHY88 passwd teacher5 @vaDbMct passwd teacher6 qFwVxkWD passwd teacher7 Fft-z%P= passwd teacher8 GxE%eh83 passwd teacher9 zfP5zPs7 passwd teacher10 cteTvWc. #### 4.4 檢查帳號及密碼 ```cpp= vim /etc/passwd vim /etc/shadow ``` ## 第五題 + 第六題 > Q5 請將 master 虛擬機的 /home 資料夾以 NFS 方式分享給 slave,並設定防火牆。 > Q6 請在 master 虛擬機建立指定資料夾,將其以 NFS 分享給 slave,並設定防火牆。 ### 5.1+6.1 下載nfs套件(slave及master都需要安裝)+ 建立指定資料夾 Install NFS Utility with DNF dnf -y install nfs-utils Create NFS share directory. For example, mkdir /opt #/opt(指定資料夾可以改) ### 5.2 去"master"的/etc/exports更改資料 vim /etc/exports #按i編輯,按esc跳出去,並打:wq存檔 ### 5.3 在/etc/exports中加上以下的command並啟動server ![](https://i.imgur.com/0W3fdtm.png) /home <<slave的inet>>(rw,sync,no_root_squash,no_all_squash) /home 192.168.30.4(rw,sync,no_root_squash,no_all_squash) /指定資料夾 192.168.30.4(rw,sync,no_root_squash,no_all_squash) #ip之間不能有空格 #r = read, w = write ![](https://i.imgur.com/CnqqyBE.png) ##### master端 (按ctrl+c跳出去) cat /etc/exports systemctl start rpcbind nfs-server systemctl status rpcbind nfs-server systemctl enable rpcbind nfs-server ##### slave端 (按ctrl+c跳出去) systemctl start rpcbind systemctl status rpcbind systemctl enable rpcbind ### 5.4 建立防火牆 ##### master端 firewall-cmd --permanent --zone=<zone> --add-service=nfs firewall-cmd --permanent --zone=<zone> --add-service=rpc-bind firewall-cmd --permanent --zone=<zone> --add-service=mountd firewall-cmd --reload firewall-cmd --list-all ##### slave端 firewall-cmd --permanent --zone=<zone> --add-service=rpc-bind firewall-cmd --reload firewall-cmd --list-all ### 5.5 驗證並設定mount ##### step1:slave端 showmount -e master ![](https://i.imgur.com/K8wYK9Y.png) ##### step2:master端 cd /home/ ls -lh ![](https://i.imgur.com/L8bHYwh.png) ##### step3:slave端到/etc/fstab加上master:/home /home nfs defaults 0 0 vim /etc/fstab master:/home /home nfs defaults 0 0 mount /home/ df -h ![](https://i.imgur.com/UmgHrkp.png) ##### step4:slave端到/etc/fstab加上master:/指定資料夾 /指定資料夾 nfs defaults 0 0 vim /etc/fstab master:/指定資料夾 /指定資料夾 nfs defaults 0 0 mount /指定資料夾/ df -h ## 第七題 + 第八題 > Q7 請在 master 虛擬機建立指定 NIS domain 名稱的 YP 伺服器,並設定防火牆。 > Q8 請在 slave 虛擬機設定建立 NIS client 端連結上 master。 ### 7.1 下載NIS套件(master及slave各裝的東西不同) On Server **master**, dnf -y install yp-tools ypbind ypserv rpcbind On client **slave**, dnf -y install yp-tools ypbind authconfig rpcbind ### 7.2 Create NIS domain (master+slave都需要做) #### step1 (master+slave都需要做) nisdomainname <domain_name> which nisdomainname (告訴你nisdomainname的位置在哪) Setup NIS domain name when startup by adding one line in the configuration file, "**/etc/rc.d/rc.local**" vim /etc/rc.d/rc.local /usr/bin/nisdomainname <domain_name> ![](https://i.imgur.com/4yojtTw.png) #### step2 建立 NIS Group touch /etc/netgroup #在etc資料夾中建立netgroup cat /etc/netgroup #檢查用(若沒有出現No such file表示有存在) ![](https://i.imgur.com/WqmQPE4.png) #### step3 在/etc/yp.conf中加上以下指令 <server_ip> = master ip a (master) vim /etc/yp.conf domain <domain_name> server <server_ip> ![](https://i.imgur.com/usqx3d0.png) ### 7.3 各自設置需要的東西 (master與slave不同) ### On server master ##### step1 /etc/sysconfig/network vim /etc/sysconfig/network NISDOMAIN=<domain_name> YPSERV_ARGS="-p 944" YPXFRD_ARGS="-p 945" ##### step2 /etc/sysconfig/yppasswdd vim /etc/sysconfig/yppasswdd YPPASSWDD_ARGS="--port 946" ##### step3 Security 為了限制哪些ip可以查到master的資料,總共有兩步驟 vim /var/yp/securenets 255.0.0.0 127.0.0.0 255.255.255.255 <slave ip> #將slave的ip打上去 ![](https://i.imgur.com/lUbjlCD.png) vim /etc/ypserv.conf 用成如下圖的樣式把三個註解拿掉 * : * : passwd.byname : port * : * : shadow.byname : port * : * : passwd.adjunct.byname : port ![](https://i.imgur.com/LPk3QRR.png) ##### step4 /etc/nsswitch.conf vim /etc/nsswitch.conf passwd: files nis shadow: files nis group: files nis hosts: files nis dns ![](https://i.imgur.com/lQdJvoA.png) ##### step5 建立防火牆 firewall-cmd --permanent --add-service=rpc-bind firewall-cmd --permanent --zone=<zone> --add-port=944-946/tcp firewall-cmd --permanent --zone=<zone> --add-port=944-946/udp firewall-cmd --reload ##### step6 Create Database for Server /usr/lib64/yp/ypinit -m 按ctrl+D y ![](https://i.imgur.com/o4HUJjb.png) ##### step7 重新啟動yp服務 systemctl restart rpcbind ypserv ypxfrd yppasswdd #老師的內容原本為start但在上課改restart systemctl status rpcbind ypserv ypxfrd yppasswdd ### On client slave ##### step1 /etc/sysconfig/network vim /etc/sysconfig/network NISDOMAIN=<domain_name> ##### step2 /etc/nsswitch.conf (與master一樣) vim /etc/nsswitch.conf passwd: files nis shadow: files nis group: files nis hosts: files nis dns ##### step3 重新啟動yp服務 systemctl restart rpcbind ypbind systemctl status rpcbind ypbind ### 7.4 在client端(slave)測試 yptest or ypcat passwd ### 7.5 在master及client端設定SELinux (讓SSH及NIS成功執行) vim /etc/selinux/config SELINUX=disabled #字一定要打對,那不然就完了,要關掉它 ![](https://i.imgur.com/slcfQdF.png) ## 第九題 (送給老師) > 請在 slave 虛擬機上建立 Jupyterhub 服務,以虛擬機本機埠號 8001 作為入口。外部用來測試 Jupyterhub 服務的網址埠號將另行以信件告知。 ### 9.1 slave上,下載wget及python dnf -y install wget cd /opt/src/ wget https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tgz tar xfz Python-3.8.10.tgz chown root.root -Rf * ### 9.2 Configuration cd /opt/src/Python-3.8.10/ #底下其實有個configure但看不到 所以不用加vim,直接打./configure ./configure --prefix=/opt/python-3.8.10 --enable-shared --enable-optimizations --enable-loadable-sqlite-extensions dnf -y install make make make install ### 9.3 下載pip pip3 install --upgrade pip ## 第十題 > 請在 slave 虛擬機上設定防火牆,只讓特定網段的 IP 能連進來使用 Jupyterhub 服務。 ## Online Resources - Operations of Linux [Overthewire/Wargame](https://overthewire.org/wargames/bandit/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully