Ubuntu Server 20.04.1 安裝和設定教學
![Ubuntu](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/d51a23cd2c95764219a9d151b3986ae4.png?fit=450%2C236&ssl=1)
使用 Virtual Box 安裝 Ubuntu Server 20.04.1,於安裝完成後更新 Ubuntu、設定系統時區、以及手動設定網路固定 IP,並安裝基於 Web 介面的應用程式 Cockpit,來方便管理伺服器及監視系統資源。
下載 Ubuntu Server
至 Ubuntu 官網下載 Ubuntu Server ISO 映像檔。
![](https://i2.wp.com/www.footmark.info/wp-content/uploads/2021/01/2a47c12509d2c0d3f1bb7e7f08dcfb5b.png?fit=1024%2C419&ssl=1)
![](https://i0.wp.com/www.footmark.info/wp-content/uploads/2021/01/f72158dfe9a862cdb9d3264cf93b9782.png?fit=1024%2C579&ssl=1)
![](https://i2.wp.com/www.footmark.info/wp-content/uploads/2021/01/5b2c3d9041e2d4adbf7ddd6443a96e53.png?fit=1024%2C579&ssl=1)
Virtual Box 安裝 Ubuntu Server
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/17e0cb7e47cfe2e69d9ba8dd44c8407c.png?resize=960%2C770&ssl=1)
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/fe1ea2dfd0775a0cad19ac404cbf4f80.png?resize=587%2C536&ssl=1)
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/1165b0e1af576d948a4ce1810b58ddc8.png?resize=960%2C770&ssl=1)
選擇要安裝的 Ubuntu Server ISO 檔。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/5ff21e7af0d36f2f7195cfb4df7a38d3.png?resize=642%2C569&ssl=1)
選擇語系。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/745179897d3bc324c03ad2261ef0615a.png?resize=802%2C689&ssl=1)
不更新繼續。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/6fb3abcfff11fd07564d96b3a8f88b01.png?resize=802%2C689&ssl=1)
鍵盤配置,預設即可。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/83b0652edabbe7158efcc9be9d217d4d.png?resize=802%2C689&ssl=1)
網路連線相關設定,預設為 DHCP 自動分配 IP。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/0f39a8064f7cb004269fe5d8ef97d4cb.png?resize=802%2C689&ssl=1)
Proxy 代理伺服器設定,沒有要設定的話直接繼續。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/1d367ad5cec63e99f6a823fc043afab2.png?resize=802%2C689&ssl=1)
Ubuntu 映射伺服器位址使用預設,直接繼續。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/51aa571bdff2ee7c7d94bb15cfc03570.png?resize=802%2C689&ssl=1)
儲存配置使用預設使用
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/0fc25ffb18ab23c6aa8045f90ba1eaef.png?resize=802%2C689&ssl=1)
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/1c704b73c5f64902eb44212465adff54.png?resize=802%2C689&ssl=1)
再次確認 Ubuntu 系統安裝所使用的硬碟,沒問題就繼續。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/c10b360f7292d1f567af45287c67872d.png?resize=802%2C689&ssl=1)
- Your name:要顯示的帳戶名稱 (非系統登入帳戶)。
- Your server's name:伺服器名稱。
- Pick a username:系統帳戶名稱。
- choose a password:帳戶密碼。
- confirm your password:再次確認帳戶密碼。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/b471cfa70fb76fa3ff7957a9efa6c20c.png?resize=802%2C689&ssl=1)
SSH 設定,通常都會安裝 OpenSSH server,即可使用 SSH 來連結伺服器。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/ba7d22e2bab0f116cc633b1ff091ca98.png?resize=802%2C689&ssl=1)
可選擇是否安裝清單中的套件。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/d8956c629a9a7262cfa617db2fbf7bbe.png?resize=802%2C689&ssl=1)
安裝系統中。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/723af4f01f2cb4572552af75edc34181.png?resize=802%2C689&ssl=1)
系統安裝完成,重新啟動系統。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/e6733f6be7ed6d3a010aef06b2ccd3f5.png?resize=802%2C689&ssl=1)
重啟完成後即可登入。
![](https://i0.wp.com/footmark.com.tw/wp-content/uploads/2021/01/9b280bf6b35a1855a20e477b663ff844.png?resize=802%2C689&ssl=1)
更新 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
設定 Cockpit 開機自動啟用 (enable) 且立即啟用 (--now):
sudo systemctl enable --now cockpit.socket
查看 Cockpit 狀態:
sudo sudo systemctl status cockpit.socket
cockpit.socket - Cockpit Web Service Socket
Loaded: loaded (/lib/systemd/system/cockpit.socket; enabled; vendor preset: enabled)
Active: active (listening) since Sat 2022-03-05 07:45:25 CST; 8min ago
Triggers: ● cockpit.service
Docs: man:cockpit-ws(8)
Listen: [::]:9090 (Stream)
Tasks: 0 (limit: 9830)
Memory: 984.0K
CGroup: /system.slice/cockpit.socket
Mar 05 07:45:25 www.test.com.tw systemd[1]: Starting Cockpit Web Service Socket.
Mar 05 07:45:25 www.test.com.tw systemd[1]: Listening on Cockpit Web Service Socket.
開啟瀏覽器輸入上述網址 or IP:9090 即可連結到 Cockpit。
![](https://i1.wp.com/www.footmark.info/wp-content/uploads/2021/01/389d91e4ebff520d1daccf3d2ce9adeb.png?fit=1024%2C564&ssl=1)
![](https://i0.wp.com/www.footmark.info/wp-content/uploads/2021/01/f79cb6ea002e0c4585335b41d4407982.png?fit=1024%2C538&ssl=1)
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。
![](https://i2.wp.com/www.footmark.info/wp-content/uploads/2021/01/923d4ac061060b55b09416ffa4a6c446.png?fit=1024%2C538&ssl=1)
參考
![創用 CC 授權條款](https://i.creativecommons.org/l/by-sa/3.0/tw/88x31.png)
本著作係採用創用 CC 姓名標示-相同方式分享 3.0 台灣 授權條款授權.