---
title: "Ubuntu 20.04 Focal 升级 Ubuntu 22.04 Jammy"
tags: ['Ubuntu']
published: 2022-04-21 20:08:45
updated: 2022-04-21 00:00:00
excerpt: "本文将指导如何升级 Ubuntu 20.04 Focal 到 Ubuntu 22.04 Jammy。"
---
> 本文件是烧饼博客文章的 Markdown 原文，正式版本见 https://u.sb/ubuntu-upgrade/
> 全站文章与页面索引见 https://u.sb/llms.txt，建议先读索引再继续抓取。

本文将指导如何升级 Ubuntu 20.04 Focal 到 Ubuntu 22.04 Jammy。

## 前言

Ubuntu 22.04 Jammy Jellyfish 已正式发布，如果您使用 Ubuntu 20.04，那么可以参考本站教程升级。

## 准备工作

除非你是物理服务器，以及没有用过奇奇怪怪定制或修改的内核的 KVM 构架的 VPS 和云主机，否则升级大版本更新内核是有一定机率导致 Grub 加载失败的，切记备份重要数据！

*OpenVZ 6 和 LXC 构架的 VPS 是无法升级的，因为他们没有自己独立的内核*

再强调一遍，一定要备份重要数据！

以下操作需要 root 权限，本文命令均已带上 `sudo` 前缀，请确保当前用户拥有 `sudo` 权限；或者直接使用 root 用户操作并省略命令中的 `sudo`。

## 更新系统

首先需要更新你当前的系统

```bash
sudo apt update
sudo apt upgrade -y
sudo apt dist-upgrade -y
sudo apt autoclean
sudo apt autoremove -y
```

如果内核更新了，建议重启让最新的内核生效。

## 升级系统

我们有两种方法升级到最新的系统，第一种是和 [Debian](/debian-upgrade/) 类似，手工修改 `apt` 源文件：

首先更新 `apt` 源，替换 `focal` 为 `jammy`：

```bash
sudo sed -i 's/focal/jammy/g' /etc/apt/sources.list
sudo sed -i 's/focal/jammy/g' /etc/apt/sources.list.d/*.list
```

默认的系统 `apt` 源文件 `/etc/apt/sources.list` 应该是类似这样的：

```bash
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy universe
deb http://archive.ubuntu.com/ubuntu/ jammy-updates universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu/ jammy multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu jammy partner
# deb-src http://archive.canonical.com/ubuntu jammy partner

deb http://archive.ubuntu.com/ubuntu jammy-security main restricted
# deb-src http://archive.ubuntu.com/ubuntu jammy-security main restricted
deb http://archive.ubuntu.com/ubuntu jammy-security universe
# deb-src http://archive.ubuntu.com/ubuntu jammy-security universe
deb http://archive.ubuntu.com/ubuntu jammy-security multiverse
# deb-src http://archive.ubuntu.com/ubuntu jammy-security multiverse

```

*国内服务器可以替换 `archive.ubuntu.com` 为 `mirrors.tuna.tsinghua.edu.cn`*

然后我们再次执行更新系统：

```bash
sudo apt update
sudo apt upgrade -y
sudo apt dist-upgrade -y
```

*更新过程中会提示一些软件是否需要自动重启，选 Yes 即可，以及一些软件的配置文件是否需要更新，按照自己的情况选择即可，默认回车即视为使用旧的配置文件，一般会出现在 OpenSSH 等软件的更新上。*

更新后删除不必要的软件和依赖：

```bash
sudo apt autoclean
sudo apt autoremove -y
```

然后我们使用 `sudo reboot` 命令重启系统，耐心等待后，查看最新的系统版本：

```bash
root@ubuntu ~ # lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04 LTS
Release:        22.04
Codename:       jammy
```

```bash
root@ubuntu ~ # uname -a
Linux ubuntu 5.15.0-25 #4-Ubuntu SMP Fri Apr 1 07:36:38 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
```

这时我们就已经更新到了最新的 Ubuntu 22.04 Jammy 和内核了。

第二种方法是使用 `do-release-upgrade` 命令

首先安装 `update-manager-core` 软件包：

```bash
sudo apt install update-manager-core
```

然后运行 `sudo do-release-upgrade -d` 即可更新，其他方法和上面一样，不再详细解释。

如果添加过一些 PPA 源，如果他们还未发布最新版本的软件，可以临时先取消，比如

```bash
sudo add-apt-repository --remove ppa:ondrej/php
```