跳转至

Ubuntu 24.04LTS Server 配置教程

(1) 场景:

笔者穿越回大清, 来到机房, 将一台来自新石器时代的centos7远古机器, 换成新鲜出炉的ubuntu24.04LTS机器

(2) Roadmap:

从 "烧写U盘" 开始, 直至最终配置完成一台 "Fish Shell + 国内软件源 + Anaconda 安装/换源完毕" 的服务器

  1. 烧U盘
  2. 原系统数据备份
  3. 系统安装
  4. 一系列配置
    1. 网络
    2. SSH
    3. 镜像源
  5. 基础配置
    1. fish shell
    2. miniconda
    3. vim
  6. 新用户配置

(3) Ref:

  1. 安装 Ubuntu24.04 LTS 图文教程
  2. 如何快乐地从零安装 Ubuntu Server
  3. Ubuntu 24.04 配置镜像源
  4. Ubuntu安装Anaconda并且配置国内镜像教程
  5. Linux开启SSH

烧U盘

传统的U盘烧写, 一个盘只能烧一个iso系统镜像, 这可太不方便了!

好在已经有相应的开源工具了! 它就是 Ventoy! 我们可以基于它在一个U盘里装多个iso!

  • 有了 Ventoy,就无需反复地格式化U盘,你只需要把 ISO/WIM/IMG/VHD(x)/EFI 等类型的文件直接拷贝到U盘里面就可以启动了,无需其他操作
  • 你可以一次性拷贝很多个不同类型的镜像文件,Ventoy 会在启动时显示一个菜单来供你进行选择

在U盘中安装Ventoy

  1. 在当前电脑登陆 Ventoy 官网
  2. 将U盘插进现在的电脑里
  3. 按照步骤安装,这里给出的是基于linux WebUI的安装方式

现在你的U盘就安装好Ventoy了,你什么都不用做,开箱即用,我们将目光转向linux镜像文件

下载ubuntu镜像并安装

这里download对应版本的ubuntu镜像文件 (.iso)

注意是server版本, 而不是desktop!

下载完毕后直接将iso文件拖入U盘中即可

alt text

原系统数据备份

TODO. 笔者没做, 感觉就是把原来系统里的一些重要 "配置文件(尤其是网络) + 代码文件" copy 备份 进一个硬盘?

系统安装

这一部分我们将逐步演示如何从USB启动系统安装:

  1. 设置BIOS 启动顺序
  2. 设置系统安装信息
  3. 配置BIOS启动顺序
  4. 正式进入新系统

设置BIOS启动顺序

这一部分根据服务器物理厂商因人而异, 我们以机房里的 DELL EMC 服务器为例:

(0) 把U盘插上服务器

(1) 系统重启, 看见BIOS信息时, 迅速点击 F2:

alt text

(2) 点击 System BIOS:

alt text

(3) 选择 Boot Settings:

alt text

(4) 配置允许从USB设备启动:

alt text

(5) 更改启动顺序:

alt text

确保USB这一项在最上面 (表示highest priority), 比如这里是 Disk connected to front USB 1: SanDisk 3.2Gen1.

最终应该呈现这个效果:

alt text

(6) 保存并退出:

alt text

alt text

此时会自动重启, 然后走USB启动!

BIOS里的'指法'

BIOS如何移动光标?

  1. 移动普通项的顺序: 方向键
  2. 移动优先级: +/-
  3. 移动“确认选项”: 用tab移动, 直至来到想要点的键

alt text

设置系统安装信息

直接点:

alt text

选择English, 下一步:

alt text

保持默认, 下一步:

alt text

选择默认的ubuntu server, 下一步:

alt text

这里很关键! 但不着急, 我们先正常填着, 别管gateway啥的是否正确

等系统安装好, 我们后面在netplan里统一改:

alt text

先不管, 下一步:

alt text

保持默认, 下一步:

alt text

保持默认, 下一步:

alt text

保持默认, 下一步:

alt text

整个磁盘都使用, 不要上锁, 确保与图中一致:

alt text

保持默认, 看一下服务器磁盘分区即可, 下一步:

alt text

保持默认, 下一步:

alt text

设置名称, 有点tricky. 比较推荐的懒人做法是所有的名字都一样:

alt text

保持默认, 下一步:

alt text

现在已经开始安装了! 等显示绿色的reboot now时点击它即可:

alt text

配置BIOS启动顺序

现在我们已经装好系统了! 紧承上述 reboot now:

系统重启的时候, 我们再次来到 BIOS

跟之前完全一致, 但现在我们需要调整一下启动顺序 (毕竟U盘是要拔掉的)

来到 System BIOS > BIOS Settings, 把 Ubuntu 放在 Highest Priority 的位置:

即, RAID Oontroller in Slot 4: ubuntu 在最上方

alt text

alt text

跟先前一样, 保存退出, 正式重启:

alt text

正式启动

选择Ubuntu系统进入:

alt text

看见熟悉的界面了! 稳了!

alt text

这里 systemd-networkd 会卡一会, 很正常. 亲测2min后就会自动continue, no worry:

alt text

输入密码后, 我们就成功进来了!

alt text

很显然网络之类的内容都不对, 下面我们来逐一更改

一系列配置

网络配置

IP配置

(1) 首先查看网口:

Bash
1
ip addr

alt text

选择最下方的网口, 记住它的名字, 稍后只需要改它即可

为什么选择的是最下方者

笔者也不知道,这是纯经验教训

可能机房就这么配置的吧...

(2) 修改 netplan:

Bash
1
2
3
4
5
# 进入对应文件夹
cd /etc/netplan
# 查看yaml配置文件
ls
# 理论上应该有一些yaml

正常的输出:

Bash
1
2
3
root@ubuntu ~# cd /etc/netplan
root@ubuntu /e/netplan# ls
50-cloud-init.yaml

现在来修改这个yaml文件:

Bash
1
2
3
4
5
6
7
8
9
network:
  version: 2
  ethernets:
    enp59s0f1: # 刚才提到的"最下方网口"
      addresses:
      - "101.6.21.133/25" # 本机IP / 掩码
      routes:
      - to: "default"
        via: "101.6.21.129" # Gateway

这里特别特别重要! 一定要提前找机房管理员获知这台机器的:

  • 本机IP
  • subnet
  • gateway ip

如果管理员不在, 可以连接这个“小集群”的其他server, 看其netplan配置!

注意! 一定要找“离得近”的, 不同的"小集群"配置规则不一样!

比如: 笔者在为 101.6.21.133 配置, 参考 101.6.21.134 的成功了, 但是与 101.6.21.22 的完全不同

(3) 重启 netplan 并测试:

Bash
1
2
sudo netplan try
sudo netplan apply

检查1: ip addr && ip route

alt text

检查2: ping

Bash
1
2
# ping 另一台子网内机器. 测试一下 icmp 发包情况
ping 101.6.21.20

alt text

DNS配置

现在需要配置DNS了, 不然 ping baidu.com 之类的是天方夜谭

(1) 更新 DNS 配置文件

Bash
1
sudo vim /etc/systemd/resolved.conf

找到DNS这里, 修改其为:

Text Only
1
2
3
[Resolve]
    DNS=8.8.8.8 1.1.1.1
    FallbackDNS=8.8.4.4

(2) 重启 systemd 服务, 确保DNS的配置在重启后依然有效

Bash
1
2
3
4
# 每次开机都会自动开启 DNS 服务
sudo systemctl restart systemd-resolved.service
# 检查
resolvectl status

正常的输出应该类似于:

Bash
1
2
3
4
5
6
Global
           Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    resolv.conf mode: stub
  Current DNS Server: 1.1.1.1
         DNS Servers: 8.8.8.8 1.1.1.1
Fallback DNS Servers: 8.8.4.4

现在DNS配置也结束了!

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 拿 ping 检查一下
root@ubuntu /e/netplan# ping baidu.com
PING baidu.com (39.156.70.37) 56(84) bytes of data.
64 bytes from 39.156.70.37: icmp_seq=1 ttl=47 time=6.61 ms
64 bytes from 39.156.70.37: icmp_seq=2 ttl=47 time=6.32 ms
64 bytes from 39.156.70.37: icmp_seq=3 ttl=47 time=6.10 ms
^C
--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 6.097/6.342/6.608/0.209 ms

SSH配置

现在要启动SSH服务了, 用于客户/学生远程连接

Bash
1
sudo vim /etc/ssh/sshd_config

找到并用#注释掉这行: PermitRootLogin prohibit-password

新建一行并添加: PermitRootLogin yes

重启服务并设置密码:

Bash
1
2
3
4
# 重启服务
sudo service ssh restart
# 设置root用户密码
sudo passwd root

然后 ssh [email protected] 就可以登录了

Bash
1
2
3
# 客户端本机CLI ssh [email protected]
[email protected]'s password:

至于其他操作, 比如 "把本地public key上传到server以实现免密登陆", 教程全网满天飞, 此处省略

换镜像源

在"不允许私建代理"的前提下 (亲测会被网信中心监测), 由于系统默认是国外的软件源, 很慢, 故需要替换到国内...

我们采用阿里云的Ubuntu24.04LTS镜像源:

注意在Ubuntu24.04做了更新, 原来软件源的配置文件是 /etc/apt/sources.list

现在已经是: /etc/apt/sources.list.d/ubuntu.sources

Bash
1
2
3
4
5
6
# 来到位置
cd /etc/apt/sources.list.d
# 备份
cp ubuntu.sources ubuntu.sources.bak
# 准备改内容了
vim ubuntu.sources

将原本的内容变成:

Text Only
1
2
3
4
5
6
# 阿里云
Types: deb
URIs: http://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

更新完软件源后, 即可立即使用新的镜像源:

Bash
1
2
sudo apt-get update
sudo apt-get upgrade

观察一下输出, 发现在从ali cloud获取信息了:

Bash
1
2
3
4
5
6
root@ubuntu ~# sudo apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu noble InRelease
Get:2 http://mirrors.aliyun.com/ubuntu noble-updates InRelease [126 kB]
Hit:3 http://mirrors.aliyun.com/ubuntu noble-security InRelease
Fetched 126 kB in 0s (285 kB/s)
Reading package lists... Done

All Good!

基础配置

fish shell

Bash
1
2
3
4
5
6
7
8
9
# 安装前确保先更新
sudo apt update
sudo apt upgrade
# 安装
sudo apt install fish
# 检查
fish --version
# 改变当前用户(root)的shell
sudo chsh -s $(which fish)

现在重新ssh进来一遍, 发现已经是fish了:

Bash
1
2
3
4
5
6
7
8
9
# 本机CLI ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 6.8.0-84-generic x86_64)

Last login: Tue Sep 30 07:50:36 2025 from 183.173.27.129
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
root@ubuntu ~#

大功告成!

miniconda

(1) 在清华镜像源anaconda archive里选择: miniconda, 把sh脚本下载到本地

(2) 下载到本机后, scp上去 (确保路径对得上)

笔者的习惯是: 将这些shell下载文件放进服务器的 /tmp

Bash
1
scp ./Miniconda3-latest-Linux-x86_64.sh [email protected]:/tmp

(3) 服务器上运行脚本安装:

Bash
1
2
cd /tmp
bash Miniconda3-latest-Linux-x86_64.sh

走流程, 默认miniconda3安装在 ~/miniconda3. 建议保持默认.

(4) 启用:

Bash
1
2
3
4
5
6
7
# conda init
cd ~
./miniconda3/bin/conda init fish
# shell refresh
exec fish
# 检查
conda --version

这里给一个正常的输出:

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
root@ubuntu ~ [127]> ./miniconda3/bin/conda init fish
no change     /home/root/miniconda3/condabin/conda
no change     /home/root/miniconda3/bin/conda
no change     /home/root/miniconda3/bin/conda-env
no change     /home/root/miniconda3/bin/activate
no change     /home/root/miniconda3/bin/deactivate
no change     /home/root/miniconda3/etc/profile.d/conda.sh
no change     /home/root/miniconda3/etc/fish/conf.d/conda.fish
no change     /home/root/miniconda3/shell/condabin/Conda.psm1
no change     /home/root/miniconda3/shell/condabin/conda-hook.ps1
no change     /home/root/miniconda3/lib/python3.13/site-packages/xontrib/conda.xsh
no change     /home/root/miniconda3/etc/profile.d/conda.csh
modified      /home/root/.config/fish/config.fish

==> For changes to take effect, close and re-open your current shell. <==
root@ubuntu ~> exec fish
root@ubuntu ~> conda --version
conda 25.7.0

(5) 换源:

现在我们来给conda换源, 理由同之前"给ubuntu换镜像源"一致 😅

Bash
1
2
conda config --set show_channel_urls yes
vim ~/.condarc

.condarc文件里内容换成:

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
Bash
1
2
# 清除索引缓存
conda clean -i

现在一切就绪了!

vim

这个属于个人折腾行为, 在此省略.

Vim真是个好东西, 高度客制化. 如感兴趣, 可以参考笔者的 vim config file.

新建用户

新建个人用户

不要做"root战神"

管理员新建用户:

Bash
1
sudo adduser bxhu # 新建用户bxhu

对应的正常输出/交互:

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
info: Adding user `bxhu' ...
info: Selecting UID/GID from range 1000 to 59999 ...
info: Adding new group `bxhu' (1001) ...
info: Adding new user `bxhu' (1001) with group `bxhu (1001)' ...
info: Creating home directory `/home/bxhu' ...
info: Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for bxhu
Enter the new value, or press ENTER for the default
 Full Name []: Boxuan Hu
 Room Number []: 217
 Work Phone []:
 Home Phone []:
 Other []:
Is the information correct? [Y/n] Y
info: Adding new user `bxhu' to supplemental / extra groups `users' ...
info: Adding user `bxhu' to group `users' ...

用户切换:

Bash
1
2
su - bxhu # 进入用户bxhu
su # 切换回管理员账户

注意, 后面我们会对新建的用户bxhu进行基础配置, 这些步骤与先前root的不完全一致!

检查新用户权限:

Bash
1
groups bxhu
  • 如果输出是: bxhu : bxhu
    • 这表示 bxhu 只在自己的同名组里,没有 sudo 权限
  • 如果输出是: bxhu : bxhu sudo
    • 这表示 bxhu 同时在 bxhu 组和 sudo 组里,拥有 sudo 权限

给新用户root权限:

Bash
1
2
3
4
5
6
7
8
su
# a: append
# G: group
usermod -aG sudo bxhu
# 检查
groups bxhu
# 退出
exit

第一次检查:

Bash
1
2
root@ubuntu /h/bxhu# groups bxhu
bxhu : bxhu sudo users

此时断开ssh连接bxhu, 并重新进去, 再用 sudo apt update检查一下:

Bash
1
sudo apt update

万事大吉!

fish shell

Bash
1
2
3
4
5
6
# 回到管理员账户
su
# 管理员可以修改任意用户的shell
chsh -s $(which fish) bxhu
# 退出
exit

此时断开ssh连接, 并重新进去, 发现 every thing good!

Bash
1
2
3
4
5
6
7
# 本机CLI ssh [email protected]
[email protected]'s password:

Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
bxhu@ubuntu ~>

现在新用户bxhu也是fish shell了!

miniconda

(1) 在清华镜像源anaconda archive里选择: miniconda

(2) 下载到本机后, scp上去 (确保路径对得上)

笔者的习惯是: 将这些shell下载文件放进服务器的 /tmp

Bash
1
scp ./Miniconda3-latest-Linux-x86_64.sh [email protected]:/tmp

(3) 服务器上运行脚本安装:

Bash
1
2
cd /tmp
bash Miniconda3-latest-Linux-x86_64.sh

走流程, 默认miniconda3安装在 ~/miniconda3. 建议保持默认.

(4) 启用:

Bash
1
2
3
4
5
6
7
# conda init
cd ~
./miniconda3/bin/conda init fish
# shell refresh
exec fish
# 检查
conda --version

这里给一个正常的输出:

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
bxhu@ubuntu ~ [127]> ./miniconda3/bin/conda init fish
no change     /home/bxhu/miniconda3/condabin/conda
no change     /home/bxhu/miniconda3/bin/conda
no change     /home/bxhu/miniconda3/bin/conda-env
no change     /home/bxhu/miniconda3/bin/activate
no change     /home/bxhu/miniconda3/bin/deactivate
no change     /home/bxhu/miniconda3/etc/profile.d/conda.sh
no change     /home/bxhu/miniconda3/etc/fish/conf.d/conda.fish
no change     /home/bxhu/miniconda3/shell/condabin/Conda.psm1
no change     /home/bxhu/miniconda3/shell/condabin/conda-hook.ps1
no change     /home/bxhu/miniconda3/lib/python3.13/site-packages/xontrib/conda.xsh
no change     /home/bxhu/miniconda3/etc/profile.d/conda.csh
modified      /home/bxhu/.config/fish/config.fish

==> For changes to take effect, close and re-open your current shell. <==
bxhu@ubuntu ~> exec fish
bxhu@ubuntu ~ [2]> conda --version
conda 25.7.0

换conda源的步骤, 和之前一样, omitted here :))

vim

跟之前完全一致