Skip to main content

Ubuntu 24.04 伺服器升級及基本調校

一樣,在安裝完基礎系統後,需進行基本的升級工作,建議使用安裝過程式的用戶名稱登入,並使用 sudo 指令進行系統更新作業。

基本升級

在安裝完基礎系統後,需進行基本的升級工作,建議使用安裝過程式的一般用戶名稱登入,並使用 sudo 指令進行系統更新作業。

$ sudo apt update

一樣第一次會請你輸入 sudo 的密碼:

[sudo] password for ossii:

輸入正確的密碼後,就會以 Root 身份執行 apt update 的工作了,接下來會出現類似於以下的訊息。

Hit:1 http://tw.archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://tw.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:3 http://tw.archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:4 http://tw.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages [1,067 kB]
Get:5 http://tw.archive.ubuntu.com/ubuntu noble-updates/main Translation-en [230 kB]
Get:6 http://tw.archive.ubuntu.com/ubuntu noble-updates/main amd64 Components [161 kB]
Get:7 http://tw.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Components [212 B]
Get:8 http://tw.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Packages [1,062 kB]
Get:9 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:10 http://tw.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Components [376 kB]
Get:11 http://tw.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Components [940 B]
Get:12 http://tw.archive.ubuntu.com/ubuntu noble-backports/main amd64 Packages [62.5 kB]
Get:13 http://tw.archive.ubuntu.com/ubuntu noble-backports/main amd64 Components [7,072 B]
Get:14 http://tw.archive.ubuntu.com/ubuntu noble-backports/restricted amd64 Components [216 B]
Get:15 http://tw.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Packages [30.4 kB]
Get:16 http://tw.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Components [16.3 kB]
Get:17 http://tw.archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Get:18 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [822 kB]
Get:19 http://security.ubuntu.com/ubuntu noble-security/main Translation-en [153 kB]
Get:20 http://security.ubuntu.com/ubuntu noble-security/main amd64 Components [21.5 kB]
Get:21 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Components [212 B]
Get:22 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Packages [838 kB]
Get:23 http://security.ubuntu.com/ubuntu noble-security/universe Translation-en [183 kB]
Get:24 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Components [52.3 kB]
Get:25 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Components [212 B]
Fetched 5,462 kB in 3s (1,713 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
65 packages can be upgraded. Run 'apt list --upgradable' to see them.

接下來請執行以下指令:

$ sudo apt upgrade

然後接著會出現要升級的套件資訊,如下所示:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following upgrades have been deferred due to phasing:
  ubuntu-drivers-common
The following packages will be upgraded:
  apparmor apport apport-core-dump-handler cloud-init cryptsetup cryptsetup-bin cryptsetup-initramfs distro-info-data dmeventd
  dmsetup dracut-install fwupd gir1.2-packagekitglib-1.0 initramfs-tools initramfs-tools-bin initramfs-tools-core
  landscape-common libapparmor1 libcryptsetup12 libdevmapper-event1.02.1 libdevmapper1.02.1 libfwupd2 libldap-common libldap2
  liblvm2cmd2.03 libnetplan1 libnss-systemd libnvme1t64 libopeniscsiusr libpackagekit-glib2-18 libpam-systemd libplymouth5
  libsystemd-shared libsystemd0 libudev1 linux-base lvm2 netplan-generator netplan.io open-iscsi open-vm-tools packagekit
  packagekit-tools pci.ids plymouth plymouth-theme-ubuntu-text pollinate python3-apport python3-netplan python3-problem-report
  python3-software-properties rsyslog snapd software-properties-common sosreport systemd systemd-dev systemd-resolved
  systemd-sysv systemd-timesyncd ubuntu-pro-client ubuntu-pro-client-l10n udev update-notifier-common
64 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
1 standard LTS security update
Need to get 52.7 MB of archives.
After this operation, 687 kB of additional disk space will be used.
Do you want to continue? [Y/n]

請輸入大寫的 Y,並按下 Enter 鍵開始下載更新,第一次更新完請重新開機,指令如下:

$ sudo reboot
安裝基礎套件

基本上會建議安裝以下常用的基本套件,這樣在之後的管理工作上會比較簡單,這份清單會隨著時間更新,列表及說明如下:

項次 套件名稱 說明
1 vim 提供更多 vi 編輯器效果的延伸套件,讓你編輯純文字文件時不再只是單調的畫面。
2 wget 下載網路連結檔案的工具,常常會在安裝其它非 RPM Base 的工具中用到。
3 net-tools 基本網路查詢工具,在確認網路或是服務狀態時會常常用上。
4 unzip 解壓縮工具。

安裝指令如下:

$ sudo apt install vim wget net-tools unzip

一樣在輸入 Root 密碼後按下「Y」鍵,系統即會自動安裝完成。

變更密碼

如果您想要變更使用者(例如:root)密碼,可以使用「passwd」指令,變更密碼的操作方式如下:

$ sudo passwd root
New password:
Retype new password:
passwd: password updated successfully
基本防火牆設定

Ubuntu Server 24.04 預設是沒有開啟防火牆的,有些資訊要求等級高的單位,可能還是需要開啟本機防火牆,Rocky Linux 預設會啟用防火牆規則,一開始只會開放 SSH 的服務,可使用以下指令設定及查詢:

  • 啟動 ufw 防火牆
sudo ufw enable

系統會提供你,啟動之後,預設會中斷目前的 SSH 連線,所以請你要確認可以從 console 端來執行指令,不然就會被擋在機器外,請特別注意。

Command may disrupt existing ssh connections. Proceed with operation (y|n)?

如果確認的話,請輸入 y 並按下 Enter 鍵繼續,正常應該就無法透過 SSH 連線了,直接設定開放 SSH Port。

Firewall is active and enabled on system startup
  • 查詢目前狀態
sudo ufw status
Status: active
  • 設定開放 22、80、443 服務
$ sudo ufw allow 22
Rule added
Rule added (v6)
ossii@ubuntu-nextcloud:~$ sudo ufw allow 80
Rule added
Rule added (v6)
ossii@ubuntu-nextcloud:~$ sudo ufw allow 443
Rule added
Rule added (v6)

重新再查詢一下:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

確認目前的 ufw 已經生效,基本的防火牆已設定完成。

變更系統 IP

先查詢看看目前的 IP 及網卡資訊為何,我們可以使用 ifconfig 指令:

$ ifconfig
ens18: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.69  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::be24:11ff:fe9f:f262  prefixlen 64  scopeid 0x20<link>
        ether bc:24:11:9f:f2:62  txqueuelen 1000  (Ethernet)
        RX packets 1207  bytes 513657 (513.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 408  bytes 40928 (40.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

所以會看到,網路卡是 ens18,IP 則是 192.168.3.69,如果我們要變更網路資訊的話,請用 vim 指令(vim 指令教學)編輯網路設定檔,本例如下:

$ sudo vim /etc/netplan/00-installer-config.yaml

內容如下:

network:
  version: 2
  ethernets:
    ens18:
      addresses:
      - "192.168.3.69/24"
      nameservers:
        addresses:
        - 8.8.8.8
        search: []
      routes:
      - to: "default"
        via: "192.168.3.1"

請就上面欄位資訊,照實際的網路環境變更即可,存檔之後執行以下的指令即可重新取得新的 IP 資訊。

$ sudo netplan apply

這個時候若是透過 SSH 連線的話,應該會斷線,請重新以新的 IP(本例為:192.168.3.79) 連入,資訊如下:

$ ifconfig
ens18: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.68  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::be24:11ff:fe9f:f262  prefixlen 64  scopeid 0x20<link>
        ether bc:24:11:9f:f2:62  txqueuelen 1000  (Ethernet)
        RX packets 4199  bytes 840440 (840.4 KB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 778  bytes 79659 (79.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
調整系統時間

Ubuntu 20.04 預設的時區資訊可能是錯誤的,請先使用 date 的指令來查詢,結果如下:

$ date
Wed May 14 06:29:17 AM UTC 2025

若要調整時區,請使用 timedatectl 指令來設定,若是 Asia/Taipei 時區,請使用下以下指令:

$ sudo timedatectl set-timezone Asia/Taipei
[sudo] password for ossii:
$ date
Wed May 14 02:32:25 PM CST 2025

如此就完成了 Ubuntu 20.04 基本的主機調整工作啦~