修改静态ip

✅ 第一步:查看自己的ip

1
ip a

✅ 第二步:查看当前Netplan配置文件
Netplan配置文件通常位于 /etc/netplan/ 目录下,例如:

1
ls /etc/netplan/

常见的文件名是:

1
2
3
01-netcfg.yaml
50-cloud-init.yaml
00-installer-config.yaml

找到主配置文件(通常只有一个 .yaml 文件)。

✅ 第三步:编辑Netplan配置文件(示例)
假设你的网卡是 ens33,你想设置:

1
2
3
4
5
IP地址:192.168.1.100
子网掩码:24(即 255.255.255.0)
网关:192.168.1.1
DNS:8.8.8.8, 1.1.1.1
使用 sudo 编辑该文件:
1
sudo nano /etc/netplan/00-installer-config.yaml

修改内容如下(⚠️注意YAML缩进,必须使用空格,不能用Tab):

1
2
3
4
5
6
7
8
9
10
11
12
13
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1

如果你使用桌面版Ubuntu且希望使用NetworkManager,可将 renderer: networkd 改为 renderer: NetworkManager
Ubuntu 22.04+ 中 gateway4 和 nameservers 可能需放在 routes 和 nameservers 下,但上述写法仍兼容
✅ 第四步:应用配置
保存文件后,执行:

1
2
3
4
sudo netplan apply

# 如果提示错误,可以用以下命令诊断:
sudo netplan --debug apply

✅ 第五步:验证配置

1
2
3
ip a
ping -c 4 google.com
systemd-resolve --status # 或查看 /etc/resolv.conf

确认IP已变为你设置的静态地址,并能正常上网。

换源

1
2
sudo cp /etc/apt/sources.list.d/ubuntu.sources  /etc/apt/sources.list.d/ubuntu.sources.bak
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

# 清华

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

# 中科大
Types: deb
URIs: http://mirrors.ustc.edu.cn/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

#阿里
Types: deb
URIs: http://mirrors.ustc.edu.cn/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

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

引用来源

更新源

1
2
3
sudo apt-get update
#看个人需求是否需要
sudo apt-get upgrade

安装软件

1
2
# 安装  apt install python3.12-venv
apt install python3.12-venv