這是一張有關標題為 Ubuntu Server 24.04: Installation, Partitioning, and Network Configuration 的圖片

Ubuntu Server 24.04: Installation, Partitioning, and Network Configuration

Explore Ubuntu Server 24.04 installation, disk partitioning, and network setup. Learn essential commands, advanced configs, and boost security and stability.

Introduction

Due to recent hardware failures, I decided to perform a fresh installation of a Linux-based server, leveraging the opportunity to optimize its configuration.

Deploying a Linux server unlocks a wide range of functionalities, including:

  • DNS server: Used for blocking ads and related applications.
  • Remote development: Allows continuous development on trusted computers.
  • Web server: Set up websites using Nginx or Apache, along with database services.
  • Proxy server: For bypassing restrictions or NAT traversal.
  • Simple file server: Not recommended as a standard NAS, as data maintenance costs are high. It’s better to use a ready-made NAS solution.
  • Local AI Model Deployment: AI models like DeepSeek R1 14B can be deployed on servers or low-power devices such as Raspberry Pi, for tasks involving inference or language processing. This is suitable for development, testing, and automation applications, especially in environments with limited resources where basic NLP tasks are performed.

Performance requirements for the server should be determined based on individual needs. For example, setting up a web server requires considering security measures such as firewalls; whereas running AI models necessitates evaluating external graphics cards, RAM, and CPU performance to ensure smooth operation.

Host Selection

Before installing a Linux server, the first step is to choose the appropriate execution environment. The common options for hosting environments include:

  1. Physical Host (Local Server): Install Linux directly on hardware such as mini PCs, including Intel NUC and Raspberry Pi, among other common options.
  2. Virtualized Host: Use technologies like WSL and Docker to deploy and manage Linux environments within an existing system, enabling lightweight virtualization.
  3. Cloud Hosting:
    • Shared Hosting: Multiple users share server resources, but their environments remain relatively independent. It is commonly used for small websites and blogs. Suitable for beginners as system management is not required.
    • Virtual Private Server (VPS): A physical server is divided into multiple independent virtual servers, each with its own operating system and administrative privileges. It offers full control, making it suitable for developers who need a customized environment.
    • Bare Metal Server: Provides an entire physical server without a virtualization layer, meaning all hardware resources are fully dedicated to a single tenant. Ideal for high-performance applications and enterprise deployments.

My Hosting Choice

I assemble my own hardware, including the case, motherboard, CPU, RAM, SSD, and power supply. The primary use cases include:

  • Remote access and management
  • Web scraping and data processing
  • Hosting web servers and deploying applications

If the goal is to set up an ad-blocking server (e.g., AdGuard Home) or manage smart home devices with Home Assistant, I recommend using a Raspberry Pi. Its ARM architecture provides lower power consumption and cost advantages.

If a dedicated physical server is not an option, a VPS offers a flexible alternative. However, compared to bare-metal deployments, VPS instances often come with hardware resource constraints and potential data security concerns.

  • Amazon Web Services (AWS)
  • Google Cloud Platform (GCP)
  • Microsoft Azure
  • DigitalOcean
  • Vultr
  • Linode

These services charge based on usage or a monthly subscription. The estimated cost for lightweight instances is $0.093–$0.15 per hour or $10–$30 per month.

Choosing a Linux Distribution

Since my primary use case involves hosting services, web development, and infrastructure setup, I chose to install Ubuntu Server 24.04. However, some SDKs require Ubuntu 18.04 as the development environment. When selecting a distribution, it’s essential to base your decision on your specific development needs and environment.

Here’s a comparison of different Linux distributions, their ideal user groups, and key features:

DistributionIdeal UsersKey Features
UbuntuGeneral users, developers, beginnersThe most popular Linux distribution, widely supported. LTS versions are stable, while short-term releases feature newer updates.
Linux MintWindows switchersUser-friendly, stable, ideal for daily use, and offers multiple desktop environments.
MX LinuxLightweight usersSuitable for older computers, includes built-in system management tools, stable and easy to use.
DebianServers, developersHighly stable with long-term support, but software versions may be outdated.
Pop!_OSProductivity, STEM studentsOffers automatic window tiling, suitable for scientific computing and development, with built-in privacy features.
FedoraDevelopers who want the latest techRapid adoption of new technologies, strong official support, great for development but less suited for long-term stability.
openSUSEEnterprise, server usersAvailable in stable (Leap) and rolling release (Tumbleweed) versions, suitable for enterprise and professional users.
EndeavourOSUsers wanting to learn ArchProvides a simplified Arch installation while maintaining a native Arch experience with rolling updates.
ManjaroArch beginnersIncludes built-in driver management, a more stable Arch-based variant, and is desktop-friendly.

The main reason for using Ubuntu is its friendliness to beginners, avoiding many difficulties encountered by professionals. Additionally, most tasks can be developed normally on any distribution. Therefore, when choosing a distribution, you don’t need to be overly concerned, just choose the one you like.

As of June 2024, if you want to develop the NVIDIA Optimized Deep Learning Framework, the official support is only for Ubuntu 22.04, so be sure to check if the framework you want to develop supports your expected Linux distribution.

Disk Partitioning During Ubuntu Server 24.04 Installation

During the installation of Ubuntu Server 24.04, the interface mainly relies on keyboard interaction, making the operation relatively simple.

The installation process involves disk partitioning, which can be referenced from Ubuntu Disk Space Recommendations and Strategies.

PartitionSizeMy Partition
/At least 15 GB30 GB (ext4)
/boot250 MB ~ 1 GB500 MB (ext4)
/var2 GB2 GB (ext4)
swapSize of RAM8 GB (ext4)
/homeRemaining space151.34 GB (ext4)
  1. The / partition serves as the root directory of the system, with an official recommendation of at least 15 GB. Over time, log files, temporary caches, and installed packages may accumulate, potentially leading to system performance degradation or operational failures. To prevent such situations, I used twice the size to reduce errors. It is still recommended to regularly clean up the root partition files or adjust the root partition size when space is insufficient.

  2. /boot for most UEFI systems is 500 MB ~ 1 GB.

  3. For general desktop systems, allocating 1-2 GB for /var is sufficient. For server systems, especially those generating a lot of system logs (such as web servers), it is recommended to allocate 5 GB or more to the /var partition to prevent log files from filling up the root directory. When using Docker, it is recommended to allocate at least 20 to 50 GB of disk space for /var. If the initial space planning is too small, you can modify /etc/docker/daemon.json to change the default path /var/lib/docker/ to another directory or use symbolic links to solve the problem.

  4. Swap should be configured to 1.5 ~ 2 times the system RAM, for example, if you have 4 GB of RAM, set SWAP to 6 GB. If you have more than 8 GB of RAM, 1x SWAP is sufficient.

  5. Leave the remaining space for /home, or plan other partitions as needed.

After verifying that all partition settings are correct, you can proceed with the official installation of the system. Once the installation is complete and the system is restarted, it will successfully boot into Ubuntu.

Network Configuration

⚠️ After completing the system installation, it is recommended to immediately check the network environment to ensure it is functioning properly. This includes verifying IP configuration, DNS server availability, and the ability to connect to external networks. If the network connection is unstable or interrupted, it will severely affect subsequent package installations, remote management, and system maintenance.

1
2
# Display detailed information about network interfaces
ip addr show

You can find the wired network interface name (e.g., enp8s0) in the output as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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 noprefixroute 
       valid_lft forever preferred_lft forever
2: enp8s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.107/24 metric 100 brd 192.168.0.255 scope global dynamic enp8s0
       valid_lft 123456sec preferred_lft 123456sec
    inet6 fd3f:1234:5678:9abc:0011:22ff:fe33:4455/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 1688sec preferred_lft 1688sec
    inet6 fe80::0011:22ff:fe33:4455/64 scope link 
       valid_lft forever preferred_lft forever

Since Ubuntu 17.10, network management has been unified under Netplan. You can check the configuration files located in /etc/netplan using the following command. Common filenames include 01-netcfg.yaml or 50-cloud-init.yaml:

1
ls /etc/netplan

In Netplan configurations, 01-netcfg.yaml has a higher priority. If this file exists, the system will apply its settings first. Currently, my server only contains the 50-cloud-init.yaml configuration file, which can be edited using the following command:

1
sudo nano /etc/netplan/50-cloud-init.yaml

In the file, you can apply the following configuration. This example sets enp8s0 to obtain an IP address automatically via DHCP and uses 1.1.1.1 and 8.8.8.8 as DNS servers. Adjust the settings according to your network environment:

1
2
3
4
5
6
7
8
9
network:
  version: 2
  ethernets:
    enp8s0:  # Ensure this matches your actual network interface name
      dhcp4: true
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8

If you need to set a static IP, you can use the following example and adjust the configuration according to your actual network segment:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
network:
  version: 2
  ethernets:
    enp0s31f6:  # Ensure this matches your actual network interface name
      dhcp4: no
      addresses:
        - 192.168.88.123/24     # Desired static IP
      gateway4: 192.168.88.254  # Gateway
      nameservers:
          addresses: [1.1.1.1, 8.8.8.8]

After editing the configuration, apply the changes to the system using the following command:

1
sudo netplan apply

Once the configuration is successfully applied, use the ping command to test external network connectivity. For example:

1
ping google.com

The output will resemble the following message, indicating that the network is ready:

1
2
3
PING google.com (173.194.174.101) 56(84) bytes of data.
64 bytes from td-in-f101.1e100.net (173.194.174.101): icmp_seq=1 ttl=103 time=17.7 ms
64 bytes from td-in-f101.1e100.net (173.194.174.101): icmp_seq=2 ttl=103 time=21.9 ms

By following the above steps, you can confirm whether the network connection is functioning properly.

Ensure that the system’s software and packages are up to date

1
2
# Update the package list and then upgrade the installed packages.
sudo apt update && sudo apt upgrade

Remote Environment Setup

After securing network connectivity, the next crucial step is to configure remote access via Tailscale and SSH, ensuring a secure and efficient management experience.

Install Tailscale

For secure remote access to this machine, it is highly recommended to use Tailscale instead of exposing the server directly to the public internet.

For a detailed installation guide, refer to: Using Tailscale to Easily Create Secure and Private Tunnels.

1
2
3
4
5
# Install
curl -fsSL https://tailscale.com/install.sh | sh

# Start Tailscale
sudo tailscale up

After executing sudo tailscale up. The terminal will display a URL. Open this URL using a web browser or a third-party device to log in to your Tailscale account. Once authentication is complete, the system will automatically add this device to your tailnet private network.

⚠️ Potential Issues After Installing Tailscale

After installing Tailscale, I encountered issues where the network interface name changed or DNS settings were overridden by Tailscale, preventing successful ping commands.

It is recommended to restart the system after installation and verify that the network connection is functioning properly. If necessary, update the network configuration accordingly.

Configure SSH

Refer to the following guide for more details: Using SSH Keys for Password-Free Remote Connections.

After completing the above configurations, you should be able to access and operate the system remotely using Tailscale + SSH from a third-party device.

System Basic Configuration and Installation

System Timezone and Language Settings

Use the following command to set the system timezone to Asia/Taipei:

1
sudo timedatectl set-timezone Asia/Taipei && sudo systemctl restart systemd-timesyncd

For other regions, you can query all available timezones using timedatectl list-timezones and filter the relevant timezone names using grep, for example:

1
timedatectl list-timezones | grep Asia

Execute the following command to generate the zh_TW.UTF-8 locale:

1
2
sudo apt install language-pack-zh-hant
sudo locale-gen

You can check the available locales on the system using locale -a, which will output something like the following:

1
2
3
4
5
6
C
C.utf8
en_US.utf8
POSIX
zh_HK.utf8
zh_TW.utf8

Use the update-locale command to update the /etc/default/locale file and set LANG to zh_TW.UTF-8:

1
sudo update-locale LANG=zh_TW.UTF-8

Finally, check using the locale command, and the output will be as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
LANG=zh_TW.UTF-8
LANGUAGE=
LC_CTYPE=zh_TW.UTF-8
LC_NUMERIC=zh_TW.UTF-8
LC_TIME=zh_TW.UTF-8
LC_COLLATE=zh_TW.UTF-8
LC_MONETARY=zh_TW.UTF-8
LC_MESSAGES=zh_TW.UTF-8
LC_PAPER=zh_TW.UTF-8
LC_NAME=zh_TW.UTF-8
LC_ADDRESS=zh_TW.UTF-8
LC_TELEPHONE=zh_TW.UTF-8
LC_MEASUREMENT=zh_TW.UTF-8
LC_IDENTIFICATION=zh_TW.UTF-8
LC_ALL=

The system’s default language and locale settings cover character encoding, time formats, number formats, currency formats, etc. Setting it to zh_TW.UTF-8 means the system will use Traditional Chinese (Taiwan) and UTF-8 encoding for character processing. The LANGUAGE variable is used to specify the language priority, controlling the selection of the translation language. If this variable is empty, the system will rely solely on LANG to set the language environment.

Install Net-tools

Net-tools provides a set of useful network management and troubleshooting tools. However, since Ubuntu 18.10, this package is no longer installed by default, and some commands (such as ifconfig and netstat) have gradually been replaced by more modern tools like ip and ss.

If you still need to use Net-tools, you can install it manually:

1
sudo apt install net-tools

Install Build Essentials

Some development tools rely on compilers such as GCC, G++, and Make. It is recommended to install the build-essential package to ensure a basic development and build environment.

1
sudo apt install build-essential

Install ble.sh

ble.sh is a script that provides real-time syntax highlighting and powerful features (such as auto-completion) for Bash.

1
2
3
4
# Clone and Compile ble.sh
git clone --recursive --depth 1 --shallow-submodules https://github.com/akinomyoga/ble.sh.git
make -C ble.sh install PREFIX=~/.local
echo 'source ~/.local/share/blesh/ble.sh' >> ~/.bashrc

Install Homebrew

Homebrew provides a faster way to install software packages on macOS, allowing convenient installation, updates, and management of packages. It also supports other Linux systems like Ubuntu.

1
2
3
4
5
6
7
# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# ==> Next steps:
# - Run these two commands in your terminal to add Homebrew to your PATH:
#     (echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> /home/user/.bashrc
#     eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

After installation, you will need to execute an additional two commands to automatically load Homebrew’s installation directory into Bash.

1
2
3
# Configure Bash to Load Homebrew Installation Path on Startup
(echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> /home/user/.bashrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

Install fzf

fzf is a command-line fuzzy finder tool that efficiently searches through files, command history, Git commits, and more using fuzzy search techniques. It significantly enhances developers’ productivity on the command line.

1
2
3
4
5
# Install using Homebrew
brew install fzf

# For future updates, use
brew update; brew upgrade fzf

Install Node.js

Node.js is a JavaScript runtime environment used for both front-end and back-end development.

The official installation guide provides clear instructions. It is recommended to use nvm for installation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Install nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# Download and install Node.js (may require restarting your terminal)
nvm install 20

# Verify the correct Node.js version is installed
node -v # Should display `v20.14.0`

# Verify the correct npm version is installed
npm -v # Should display `10.7.0`

Install tldr

tldr provides concise and practical command-line tool explanations and examples, offering a more intuitive alternative to the man command on Linux.

1
2
3
4
5
brew install tlrc

# Alternatively, if Node.js is installed, you can install it via npm.
# npm install -g tldr
# tldr --update

To use tldr, simply type tldr <command>.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
tldr zip

  zip

  Package and compress (archive) files into a Zip archive.
  See also: unzip.
  More information: https://manned.org/zip.

  - Add files/directories to a specific archive:
    zip -r path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...

  - Remove files/directories from a specific archive:
    zip --delete path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...

  - Archive files/directories e[x]cluding specified ones:
    zip path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ... --exclude path/to/excluded_files_or_directories

  - Archive files/directories with a specific compression level (0 - the lowest, 9 - the highest):
    zip -r -0..9 path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...

  - Create an encrypted archive with a specific password:
    zip -r --encrypt path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...

  - Archive files/directories to a multi-part [s]plit Zip archive (e.g. 3 GB parts):
    zip -r -s 3g path/to/compressed.zip path/to/file_or_directory1 path/to/file_or_directory2 ...

  - Print a specific archive contents:
    zip -sf path/to/compressed.zip


See also: unzip

Update Git to the Latest Version

Updating Git to the latest version is not always necessary. Some tools may require older versions of Git, and installing the latest version could lead to compatibility issues.

The git-core/ppa is a Personal Package Archive (PPA) maintained by the Git community, providing the latest versions of Git software.

1
2
3
4
5
# Add the PPA
sudo add-apt-repository ppa:git-core/ppa

# Update Git to the latest version
sudo apt update && sudo apt upgrade

Configure Git User

To set up your Git user information, use the following commands:

1
2
git config --global user.name "USER"
git config --global user.email "USER_MAIL"

Other Advanced Settings

Disable IPv6 Connection

If the ISP provides both IPv4 and IPv6 addresses, it is recommended to use only IPv4 for security or specific operational requirements. In this case, you can choose to disable IPv6 to ensure that all network traffic is transmitted exclusively over IPv4, thereby reducing the potential attack surface.

Disable IPv6 via sysctl

Before Ubuntu 17.10, network configuration was managed using traditional tools like ifupdown and sysctl, rather than Netplan. You can disable IPv6 using sysctl:

1
sudo nano /etc/sysctl.conf

Add the following configurations at the end of the file and save the changes:

1
2
3
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Apply the changes immediately by running:

1
sysctl -p

Disable IPv6 via Netplan

Since Ubuntu 17.10, network management has been unified under Netplan. You can disable IPv6 for a specific network interface using Netplan:

1
sudo nano /etc/netplan/50-cloud-init.yaml

Add link-local: [ ipv4 ] to the configuration of the target network interface (e.g., enp8s0). Below is an example configuration. Save the file and exit:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
network:
  ethernets:
    enp8s0:
      dhcp4: true
      link-local: [ ipv4 ]  # Disable IPv6
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8
  version: 2

Verify the configuration and apply the settings:

1
sudo netplan apply

Finally, verify that IPv6 has been successfully disabled by checking the network interface status with ip a or ifconfig. If the configuration was applied correctly, the interface should no longer display an inet6 address.

Set VS Code as the Default Editor for SSH Remote Development

When connecting to Ubuntu via SSH, you can set VS Code as the default editor, making the editing experience in the terminal more intuitive, and allowing you to open files directly using the code command.

First, modify the .bashrc file (if using zsh, modify ~/.zshrc). Execute the following command to open .bashrc in VS Code:

1
code ~/.bashrc

Next, add the following content at the bottom of the file to ensure the system uses VS Code as the default editor:

1
2
3
4
5
6
CODE_PATH=$(command -v code 2>/dev/null)
if [[ -x "$CODE_PATH" ]]; then  # Ensure that `code` is available and executable
    export VISUAL="$CODE_PATH --wait"
    export EDITOR="$CODE_PATH --wait"
    export SUDO_EDITOR="$CODE_PATH --wait"
fi

Once completed, save the file (Ctrl + S) and close it.

Finally, execute the following command to reload the settings, or simply open a new terminal to apply the changes:

1
source ~/.bashrc

With this setup, you can easily edit files that require elevated privileges using sudoedit in the terminal, opening and editing them directly in VS Code. Similarly, running crontab -e to edit cron jobs will automatically open VS Code, making remote development smoother and more efficient.

Configure Ubuntu Pro Security

Enabling Ubuntu Pro allows for real-time system vulnerability fixes, enhancing system security and stability.

First, register and obtain a free Ubuntu Pro subscription from the Ubuntu official website. You can register at Ubuntu Pro.

After registration, subscribe to Ubuntu Pro using sudo pro attach <YOUR_TOKEN>:

1
sudo pro attach YGBDmwR6Y2S

The output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Enabling Ubuntu Pro: ESM Apps
Ubuntu Pro: ESM Apps enabled
Enabling Ubuntu Pro: ESM Infra
Ubuntu Pro: ESM Infra enabled
Enabling Livepatch
Livepatch enabled
This machine is now attached to 'Ubuntu Pro - free personal subscription'

SERVICE          ENTITLED  STATUS       DESCRIPTION
anbox-cloud      yes       disabled     Scalable Android in the cloud
esm-apps         yes       enabled      Expanded Security Maintenance for Applications
esm-infra        yes       enabled      Expanded Security Maintenance for Infrastructure
landscape        yes       disabled     Management and administration tool for Ubuntu
livepatch        yes       enabled      Canonical Livepatch service
realtime-kernel* yes       disabled     Ubuntu kernel with PREEMPT_RT patches integrated

 * Service has variants

NOTICES
Operation in progress: pro attach

For a list of all Ubuntu Pro services and variants, run 'pro status --all'
Enable services with: pro enable <service>

     Account: xxxxx@xxx.com
Subscription: Ubuntu Pro - free personal subscription

Conclusion

Once the above basic environment configurations are set up, you can happily proceed with remote development 🥳.

References

  1. What is a VPS (Virtual Private Server)?
  2. Ubuntu Server documentation
  3. DiskSpace - Community Help Wiki
  4. Ubuntu Bionic: Netplan
  5. ble.sh
  6. fzf
  7. tldr-pages/tldr
  8. First-Time Git Setup
  9. Use UFW to lock down an Ubuntu server
  10. networking - I have both IPv4 and IPv6 public addresses. Why?
  11. Fine-Tuning the Ubuntu 24.04 Kernel for low latency, throughput, and power efficiency
Theme Stack designed by Jimmy