Ubuntu初始化换清华源方法,apt使用,ibus拼音输入法

一个很牛逼的换源sh脚本linuxmirrors

官网:https://linuxmirrors.cn/use/
只需要一条命令,就能快速换源,不过还没有测试过。还支持一键安装Docker。

1
bash <(curl -sSL https://linuxmirrors.cn/main.sh)

24.04 换源方法不一样了,请注意!

20240814更新
Ubuntu换清华apt源方法

Ubuntu换清华apt源方法

通过sudo vim /etc/apt/sources.list.d/ubuntu.sources来替换成下面的内容

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
32
33
34
35
36
37
38
39
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
# Types: deb-src
# URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
# Suites: noble noble-updates noble-backports
# Components: main restricted universe multiverse
# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# Types: deb-src
# URIs: http://security.ubuntu.com/ubuntu/
# Suites: noble-security
# Components: main restricted universe multiverse
# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 预发布软件源,不建议启用

# Types: deb
# URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
# Suites: noble-proposed
# Components: main restricted universe multiverse
# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# # Types: deb-src
# # URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
# # Suites: noble-proposed
# # Components: main restricted universe multiverse
# # Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

sudo apt update会有如下奇怪的错误,可以不用管

Ubuntu换清华apt源方法

(一)换清华apt源方法

(1)首先要用root去登录Ubuntu

1
2
sudo -i
# 切换到root用户

(2)去到/etc/apt/找找有没有sources.list文件

1
2
cd /etc/apt/
# 切换到/etc/apt/目录
1
2
3
4
root@DESKTOP-FH09R9R:/etc/apt# ls
apt.conf.d keyrings sources.list trusted.gpg.d
auth.conf.d preferences.d sources.list.d
# 用ls找到/etc/apt/里面有sources.list文件

(3)备份sources.list文件

1
2
3
4
5
root@DESKTOP-FH09R9R:/etc/apt# cp sources.list sources.list.backup
root@DESKTOP-FH09R9R:/etc/apt# ls
apt.conf.d keyrings sources.list sources.list.d
auth.conf.d preferences.d sources.list.backup trusted.gpg.d
# 使用cp命令备份sources.list成为sources.list.backup

(4)修改sources.list

1
2
root@DESKTOP-FH09R9R:/etc/apt# vi sources.list
# 用vim修改sources.list

进入网址:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/
复制里面的源的代码,替换sources.list的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

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

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

Ubuntu换清华apt源方法

vi或vim的使用方法说明:

  1. 进入后多按几次esc,确保进入了命令模式。
  2. 光标在第一行,不断按dd,按两次d键(小写的),直到所有行都被删除完了。如果内容特别多,可以不同按100dd,这样可以一次删除100行。
  3. 然后去复制上面的清华源代码,再按yy,按两次y(小写的)。被复制的清华源代码就会被粘贴进来。
  4. 如果第3步不行,就按i(小写的),进入插入模式,然后鼠标右键粘贴清华源代码。
  5. 如果清华源代码一字不差都粘贴进去了,就多按几次esc,确保自进入了命令模式。
  6. 按:wq(英文小写的),回车。就能成功保存。

Ubuntu换清华apt源方法

1
2
root@DESKTOP-FH09R9R:/etc/apt# cat sources.list
# 用cat命令看看是否修改成功,不成功就多试几次,直到成功为止。

(5)测试换源是否成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo apt update # 看看能不能读到有软件更新。
apt update # root用户不需要加sudo也行

#返回值如下,有Hit返回值就证明成功了。
Hit:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
Hit:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease
Hit:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Hit:5 https://ppa.launchpadcontent.net/jonathonf/vim/ubuntu jammy InRelease
Fetched 110 kB in 4s (25.1 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

# 如果上面有Hit值了,就可以尝试更新软件。
# 如果都能更新了,证明这个源是没有问题的了。
sudo apt-get update # root不用加sudo也行

(6)还原回官方的源。
上面我们在/etc/apt/中备份sources.list.backup就是官方的源,如果出国了,可能官方的源比清华的好用。

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 这样就能换回去官方的源了
root@DESKTOP-FH09R9R:/etc/apt# cp sources.list.backup sources.list

# cat看看是非成功换回来官方源了
root@DESKTOP-FH09R9R:/etc/apt# cat sources.list
# 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

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

Ubuntu换清华apt源方法

(7)通过Unbuntu的内置桌面配置也能进行换源

Ubuntu换清华apt源方法

(8)Unbuntu同系列的发行版LinuxMint的内置桌面配置也能进行换源

Ubuntu换清华apt源方法

(二)apt使用方法

sudo apt update(获取包列表更新)

换源是否成功,就看看执行sudo apt update是否能有HIT就行。有HIT就是成功了。

Ubuntu换清华apt源方法

sudo apt list –upgradable(查看可更新包列表明细)

执行完sudo apt update后,用sudo apt list --upgradable就可以看到有哪些软件可以更新。如果执行sudo apt upgrade的话就会把这些软件全部进行更新了。

1
2
apparmor/jammy-updates 3.0.4-2ubuntu2.3 amd64 [upgradable from: 3.0.4-2ubuntu2.2]
# 举个栗子,3.0.4-2ubuntu2.2可以更新到3.0.4-2ubuntu2.3了

Ubuntu换清华apt源方法

sudo apt list –installed(列出安装了什么)

因为安装了的软件包可能会超级多。使用apt list --installed | grep "mysql"这个grep命令就可以做一下关键字的过滤。

1
2
3
4
5
6
7
8
9
# 列出安装过哪些叫mysql的包
# brian @ DESKTOP-FH09R9R in ~ [9:02:33] C:100
$ apt list --installed | grep "mysql"

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

mysql-client-8.0/jammy-security,now 8.0.36-0ubuntu0.22.04.1 amd64 [installed,automatic]
mysql-client-core-8.0/jammy-security,now 8.0.36-0ubuntu0.22.04.1 amd64 [installed,automatic]
......

Ubuntu换清华apt源方法

sudo apt list –all-versions(列出所有包的版本)

快速列出含有mysql关键字的包版本

sudo apt list --all-versions | grep "mysql" | grep "8.0"这样可以同时搜索两个关键字and逻辑

sudo apt list --all-versions | grep -e "mysql" -e "8.0"这样可以同时搜索两个关键字or逻辑

sudo apt list --all-versions | grep -v "lib" | grep "mysql"这样可以排除一些不需要的关键字后再搜索,是的逻辑

Ubuntu换清华apt源方法

sudo apt upgrade(更新所有包软件)

sudo apt update只有哪些软件包需要更新后,就可以执行sudo apt upgrade来全部更新。

1
2
sudo apt update && sudo apt upgrade -y
# 这样可以同时更行软件包的可更新列表,并且接着继续升级软件版,并一路自动yes。
1
2
sudo apt full-upgrade # 卸载了再安装新版
# 一般不选择这种升级方式,虽然达到的效果一样。

Ubuntu换清华apt源方法

sudo apt search <包名>(查找包命令)

查找包列表库中的包,除了包名,包中简单描述也有。通过sudo apt search <包名>这个命令可以找到需要的包的正确名字,然后通过sudo apt install来安装。当然,用sudo apt list --all-versions | grep "mysql"命令来找到软件包的正确名字也行。

Ubuntu换清华apt源方法

sudo apt-cache show <包名> (查找包的详细信息)

1
2
3
4
apt-cache show package # 显示指定包的详细信息,包括版本号、依赖关系、描述等。
apt-cache policy package # 显示指定包的版本政策和所有可用的版本。
apt-cache depends package # 显示指定包的所有依赖关系。
apt-cache rdepends package # 显示所有依赖指定包的包。

一般是在安装或卸载软件包前查找一下具体情况,内容过于详细,除非知道绝对正确的报名,不然不要用这个命令。
sudo apt show <包名> # 可以达到同样的搜索效果。

Ubuntu换清华apt源方法

Ubuntu换清华apt源方法

sudo apt install <包名>(安装软件包)

需要知道绝对的包名,安装的时候一般会把相关的依赖一起安装了。但是很多时候还会差很多配套的包,如安装fcitx的时候,很多配套的fcitx-qt5、im-chooser、fcitx-pinyin、fcitx-configtool、fcitx5-chinese-addons等的包都需要安装才能使用,需要慢慢摸索,看官方文档,看别人的解决方法,不同的Linux发行版还会有差别。这就是为什么Linux为什么让新手感觉非常不好用的原因,太自由就有不少的混乱,很多时候老鸟都解决不了输入法的问题

1
2
# 同时安装多个包
sudo apt install <包1> <包2> <包3>

Ubuntu换清华apt源方法

如果这个软件包已经安装过了,但是有新版本,就会提示进行升级,而不是全新安装。

Ubuntu换清华apt源方法

sudo apt remove <包名>(删除软件包)

1
2
sudo apt remove <包名> # 删除二进制文件,但是会留下配置文件。一般只用这个就够。
sudo apt purge <包名> # 删除所有内容,包括配置文件。

Ubuntu换清华apt源方法

sudo apt autoremove(自动删除不需要的依赖)

上面的sudo apt remove <包名>是不会删除相关的依赖的,如果要删除没有用了的依赖,就可以执行sudo apt autoremove

Ubuntu换清华apt源方法

sudo apt reinstall <包名>(重新安装包)

1
2
sudo apt reinstall neofetch
# 重新安装neofetch,刚好neofetch坏了,重装解决了

(三)iBus中文拼音输入法 22.04

在安装系统的时候选择用中文(简体)
Ubuntu换清华apt源方法
选择键盘布局是Chinese - Chinese Hanyu Pinyin(with AleGr dead keys)
Ubuntu换清华apt源方法

安装完系统后
在【设置】中确认【键盘】的输入法有没有“中文(智能拼音)”
在右上角点击输入法,看看有没有“中文(智能拼音)”
Ubuntu换清华apt源方法

如果没有,就要确保在【设置】中的【语言(L)】是否“汉语”?
如果不是汉语就要修改回来,详细见下图:
Ubuntu换清华apt源方法

测试一下,可以中文输入了
Ubuntu换清华apt源方法

点击这里可以做一些输入法的配置,默认按shift键来切换中英文输入。
Ubuntu换清华apt源方法