這是一張有關標題為 Ubuntu Server 24.04 Installation and Configuration Guide 的圖片

Ubuntu Server 24.04 Installation and Configuration Guide

Learn how to install Ubuntu, set up disk partitions, network configuration, and related software installation.

Introduction

Recently, my server experienced some hardware issues, so I took the opportunity to reinstall the Linux server. This article will introduce how to install Ubuntu as your work server.

Having a Linux server allows you to do many things, such as:

  • 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.

Determine the server’s performance based on your personal needs. For example, if you want to set up a web server, consider firewall and other security factors.

There are some ready-made virtual hosting solutions on the market, where the services are already set up, and users only need to upload files. Most are configured through pre-written programs on the web, making them very user-friendly for those who do not understand systems at all.

For more advanced needs, there are virtual private servers (VPS), which are hosted on trusted third-party providers. These hosts might have Linux or Windows installed, and users can connect remotely via SSH to set up services or reset remotely.

Common third-party providers include:

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

These services are billed based on usage or on a monthly basis. The cost is around USD $0.15 per hour or USD $30 per month.

Choosing a Linux Distribution

Since I mainly use it for service, web hosting, and development, I chose to install Ubuntu Server 24.04. However, some SDKs require the development environment to use Ubuntu 18.04. When choosing a distribution, you should decide based on your personal development needs and environment.

Comparing different distributions, such as Ubuntu, Debian, Fedora, CentOS, here are their characteristics:

DistributionCharacteristics
UbuntuSuitable for beginners, provides an intuitive installation process and default configurations.
Ubuntu ServerDesigned for servers, no graphical interface, includes server-related tools, supports remote management.
DebianStable and secure, suitable for environments requiring high stability.
FedoraCutting-edge technology, suitable for developers wanting to experience the latest software.
CentOSEnterprise-grade distribution, suitable for server deployments in corporate environments.

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. / is the root partition. The official recommendation is to allocate at least 15 GB. Due to log files, temporary files, and installation packages, the root partition may gradually fill up, causing partial system operation errors. 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.

System Configuration and Installation

Network Configuration

⚠️ It’s challenging to perform remote operations without network connectivity, so the first task is to ensure the network is working properly.

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

In the output, you can see that the device name for the wired network is enp8s0:

 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, the network management tool has been unified to netplan. You can check the configuration file using ls /etc/netplan, usually named 01-netcfg.yaml or 50-cloud-init.yaml. Among them, 01-netcfg.yaml has higher configuration priority and will be prioritized if it exists.

On my server, there is only the 50-cloud-init.yaml configuration file. Use sudo nano /etc/netplan/50-cloud-init.yaml to edit this file, fill in the wired network device name, and set it to obtain an IP automatically or use a static IP. In my configuration, I use a dynamic IP and set the DNS servers.

Here is an example configuration, adjust it according to your network environment:

1
2
3
4
5
6
7
8
9
network:
  version: 2
  ethernets:
    enp8s0:  # Adjust to your network card name
      dhcp4: true
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8

This configuration makes the network device enp8s0 obtain an IP automatically via DHCP and use 1.1.1.1 and 8.8.8.8 as DNS servers.

If you use a static IP, refer to the following configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
network:
  version: 2
  ethernets:
    enp0s31f6:  # Adjust to your network card name
      dhcp4: no
      addresses:
        - 192.168.66.123/24     # Desired static IP
      gateway4: 192.168.88.254  # Gateway
      nameservers:
          addresses: [1.1.1.1, 8.8.8.8]

After confirming the configuration file is correct, execute the following command to apply the settings:

1
sudo netplan apply

This command will reload netplan and apply the new settings, ensuring the network configuration is effective.

Next, you can check if the network is functioning properly using the ping command:

1
ping google.com

The output will be as follows:

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.

Setting System Time and Date

Change the timezone to Asia/Taipei

1
sudo timedatectl set-timezone Asia/Taipei

Setting Chinese Language Environment

Edit the locale configuration file for language settings.

1
sudo nano /etc/default/locale

After adding the following content inside, save and exit:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
LANG="zh_TW.UTF-8"
LC_NUMERIC="zh_TW.UTF-8"
LC_TIME="zh_TW.UTF-8"
LC_MONETARY="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"

Execute the following command to update the language configuration:

1
2
3
# Generate the language environment file for Traditional Chinese (Taiwan) and update:
sudo locale-gen zh_TW.UTF-8
sudo update-locale

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

Install Tailscale

If there is a need to access this computer externally, it is strongly recommended to install Tailscale instead of exposing the server directly to the entire Internet.

For detailed installation instructions, please 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

At this point, a URL will be displayed. Visit this URL on your smartphone or another device and log in.

Linux will automatically retrieve the Token and join your personal Tailscale network.

After installing Tailscale, some users may experience issues such as network interface names changing or DNS conflicts that prevent outbound pings.

It is recommended to reboot your system after installing Tailscale to ensure normal internet connectivity. If necessary, update your network configuration files again.

Configure SSH

You can refer to Using SSH Keys for Password-Free Remote Connections.

Install Net-tools

Net-tools provides a suite of network management and troubleshooting tools, although some commands have been replaced by more modern tools like ip.

Install network management and troubleshooting tools:

1
sudo apt install net-tools

Install Build Essentials

Some development tools depend on the following packages. It is recommended to install them to have a basic build environment:

1
sudo apt install build-essential

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

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/使用者/.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/使用者/.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"

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
Theme Stack designed by Jimmy