Ubuntu Server 20.04.1 安裝和設定教學 - MIS 腳印
MIS 腳印 logo

MIS 腳印

記錄 IT 學習的軌跡

Ubuntu Server 20.04.1 安裝和設定教學

使用 Virtual Box 安裝 Ubuntu Server 20.04.1,於安裝完成後更新 Ubuntu、設定系統時區、以及手動設定網路固定 IP,並安裝基於 Web 介面的應用程式 Cockpit,來方便管理伺服器及監視系統資源。

Ubuntu

下載 Ubuntu Server

Ubuntu 官網下載 Ubuntu Server ISO 映像檔。

Virtual Box 安裝 Ubuntu Server

選擇要安裝的 Ubuntu Server ISO 檔。

選擇語系。

不更新繼續。

鍵盤配置,預設即可。

網路連線相關設定,預設為 DHCP 自動分配 IP。

Proxy 代理伺服器設定,沒有要設定的話直接繼續。

Ubuntu 映射伺服器位址使用預設,直接繼續。

儲存配置使用預設使用 LVM 即可,直接繼續。

再次確認 Ubuntu 系統安裝所使用的硬碟,沒問題就繼續。

  • Your name:要顯示的帳戶名稱 (非系統登入帳戶)。
  • Your server's name:伺服器名稱。
  • Pick a username:系統帳戶名稱。
  • choose a password:帳戶密碼。
  • confirm your password:再次確認帳戶密碼。

SSH 設定,通常都會安裝 OpenSSH server,即可使用 SSH 來連結伺服器。

可選擇是否安裝清單中的套件。

安裝系統中。

系統安裝完成,重新啟動系統。

重啟完成後即可登入。

更新 Ubuntu

將 Ubuntu 系統套件與版本更新到最新:

sudo apt-get update && sudo apt-get upgrade -y

系統時區

Ubuntu Server 預設使用 UTC 時區,通常都會設定為所在地區的時間。

查看系統上目前的時間和時區:

timedatectl

列出所有可用時區:

timedatectl list-timezones

將時區設定為台灣的 Asia/Taipei:

timedatectl set-timezone Asia/Taipei

設定伺服器主機名稱

設定伺服器主機名稱:

hostnamectl set-hostname www.footmark.com.tw

網路設定

查看所有網路裝置:

ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:66:f0:2e brd ff:ff:ff:ff:ff:ff
    inet 61.216.xx.xx/24 brd 61.216.xx.xx scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe66:f02e/64 scope link 
       valid_lft forever preferred_lft forever

Netplan

Ubuntu 使用 Netplan 來配置網路,它會讀取 /etc/netplan 目錄下的所有 *.yaml 設定檔,來進行網路的設定,且所有網路的設定都會放在這裡。

系統如果有使用到網路,那麼在 /etc/netplan 目錄下就會自動產生網路設定檔,但若沒有的話,可以使用以下指令來自動產生網路設定檔:

sudo netplan generate

DHCP 自動分配 IP

查看網路設定檔,可看到預設是使用 DHCP 來自動分配網路 IP:

cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s3:
      dhcp4: true
  version: 2

手動設定固定 IP

一開始在安裝 Ubuntu 網路連線設定時,就可選擇要手動 (manual) 設定 IP 或使用預設的 DHCP 自動分配 IP,本文是使用預設的 DHCP,因此就得另外進行固定 IP 的設定。

先將預設產生的設定檔複製留存:

sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak

編輯網路設定檔,來手動設定網路管理模式、固定 IP、網路遮罩、預設閘道、DNS Server 等資訊:

sudo vim /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  renderer networkd                         # 網路管理模式使用 Network Manager,未設定預設使用 systemd-workd
  ethernets:
    enp0s3:                                 # 網路裝置
      dhcp4: false                          # false: 關閉 DHCP;true: 啟用
      addresses: [61.216.xx.xx/24]          # 固定 IP 與網路遮罩
      gateway4: 61.216.xx.254               # 預設閘道
      nameservers:
        addresses: [8.8.8.8, 168.95.1.1]    # DNS Server
  version: 2

測試新網路設定是否正確,若設定檔語法正確,就會套用新的網路設定,並讓管理者進行確認。如果在 120 秒內沒有 Enter 確認,就會自動恢復成未設定前原來的網路設定,此功能可避免在遠端更改網路設定有誤時,將自己檔在伺服器外面:

sudo netplan try
Warning: Stopping systemd-networkd.service, but it can still be activated by:
  systemd-networkd.socket
Do you want to keep these settings?


Press ENTER before the timeout to accept the new configuration


Changes will revert in 108 seconds
Configuration accepted.

在本機操作,也可直接套用新的網路設定檔:

sudo netplan apply

UFW 防火牆

請參考 Ubuntu Server 20.04.1 預設 UFW 防火牆 Firewall 設定詳解和教學

Cockpit

Cockpit 是基於 Web 介面的應用程式,可用來管理伺服器並監視和調整系統資源。安裝 cockpit:

sudo apt install cockpit

開啟瀏覽器輸入 IP:9090 即可連結到 Cockpit。

Firewalld

Ubuntu 預設安裝的 Firewall (防火牆) 組件是 UFW (預設未啟用),但 Cockpit 僅支援 Firewalld 管理器,也就是說要能夠在 Cockpit 上設定 Firewall 必須安裝 Firewalld 管理器:

sudo apt install firewalld

系統啟動啟用 firewalld,並立即啟用:

sudo systemctl enable firewalld --now
[sudo] password for jacky: 
Synchronizing state of firewalld.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable firewalld

設定 firewall 允許 cockpit (port 9090):

sudo firewall-cmd --add-service=cockpit --permane

重新載入設定才會生效:

sudo firewall-cmd --reload

上述完成後即可在 Cockpit 設定 Firewall。

參考

發表迴響