跳到主要内容
08:43:52
🔅🔆

ubuntu使用记录

记录 ubuntu 各版本使用中的配置与踩坑,涉及版本差异的内容用选项卡区分(同一页面的版本选项卡联动切换)。

版本说明
版本代号状态
18.04bionic标准支持已结束(2023-05),仅 ESM 扩展安全维护
22.04jammyLTS,标准支持至 2027-04
26.04resoluteLTS(2026-04 发布),安装过程见 Ubuntu26.04安装

配置网络​

ubuntu 各版本均使用 netplan 管理网络,配置文件在 /etc/netplan/ 目录下,但不同版本的默认配置文件名和语法有差异,注意区分:

22.04 / 24.04 / 26.04 静态 ip 写法通用

静态 ip 的 YAML 语法三版本完全一致(addresses + routes: - to: default via: + nameservers),同一份配置稍改网卡名/网段即可通用,24.04 参照 22.04 选项卡即可。仅三个边缘差异:

  • gateway4 旧写法 22.04 起已废弃(26.04 彻底删除),一律用 routes 写法
  • 24.04 起配置文件全局可读会告警,建议 chmod 600
  • /etc/netplan/50-cloud-init.yaml 只在云镜像上存在,本地/虚机安装是安装器生成的 00-installer-config.yaml

默认配置文件是 /etc/netplan/01-network-manager-all.yaml(desktop 版默认由 NetworkManager 接管)

//编辑文件
root@ubuntu18:~# cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
ethernets:
enp0s5: #⚠️这里要看一下网卡的名称
dhcp4: false
addresses: [10.0.0.15/24]
gateway4: 10.0.0.1
nameservers:
addresses: [223.5.5.5,114.114.114.114]
version: 2
renderer: networkd

//使设置生效
netplan apply
注意

gateway4 写法只在老版本 netplan(18.04)可用,22.04 起已废弃、26.04 已彻底移除,新版必须用 routes 写法(见 22.04 / 26.04 选项卡)。

查看IP信息​

说明

ubuntu 最小化/服务器安装不带 net-tools(没有 ifconfig),统一用 ip / netplan / resolvectl 这套。

ip addr # 所有网卡及 IP(简写 ip a)
ip -br a # 精简输出,一行一个网卡,日常查 IP 最快
ip -4 addr show enp0s5 # 只看某网卡的 IPv4(静态地址 valid_lft 显示 forever,DHCP 显示倒计时)
ip route # 路由表(确认网关和默认路由)
resolvectl status # DNS 配置
sudo netplan status # netplan 视角总览:网卡、IP、后端(networkd)、配置文件来源

开启 SSH 服务​

说明

Server 版安装时勾选了 Install OpenSSH server 的无需此步,18.04 desktop 版默认只装了 ssh-agent,需要手动装 sshd:

sudo apt -y install openssh-server

//启动服务
sudo service ssh start

安装 python3.6​

说明

ubuntu18.04 默认 python 版本是 3.7

root@ubuntu18:~# python3
Python 3.7.5rc1 (default, Oct 8 2019, 16:47:45)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.

root@ubuntu18:~# python3.7
Python 3.7.5rc1 (default, Oct 8 2019, 16:47:45)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.

安装依赖包

apt -y install python3-dev libffi-dev libssl-dev zlib*

源码编译安装 python3.6

./configure --enable-optimizations --prefix=/usr/local/python36
make
make altinstall #防止覆盖原来的版本

设置环境变量

echo "PATH=/usr/local/python36/bin:$PATH" >/etc/profile.d/python36.sh && source /etc/profile.d/python36.sh

设置 pip 国内源

mkdir /root/.pip
cat >/root/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
EOF

ubuntu安装python3.6问题

配置国内安装源​

先备份原有文件:

cp /etc/apt/sources.list{,.bak}

各版本的源配置文件与代号不同:

配置文件为 /etc/apt/sources.list,代号 bionic:

cat >/etc/apt/sources.list<<'EOF'
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF

//更新
apt update

禁用防火墙​

sudo ufw disable

修改时区/时间​

修改时区​

查看时区

$ timedatectl
Local time: Thu 2025-03-13 03:19:45 UTC
Universal time: Thu 2025-03-13 03:19:45 UTC
RTC time: Thu 2025-03-13 03:19:45
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

列出可用时区

timedatectl list-timezones

设置时区

sudo timedatectl set-timezone Asia/Shanghai

验证修改

$ timedatectl
Local time: Thu 2025-03-13 11:23:50 CST
Universal time: Thu 2025-03-13 03:23:50 UTC
RTC time: Thu 2025-03-13 03:23:49
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no

修改时间显示格式​

ubuntu22.04/26.04默认是12小时制

$ date
Thu Mar 13 03:09:43 AM UTC 2025

方法一:编辑 /etc/default/locale ,增加如下内容(22.04 实测)

LC_TIME=en_DK.UTF-8

使配置生效

sudo locale-gen
sudo update-locale

方法二:localectl 一条命令(26.04 实测,同样重新登录后生效)

sudo locale-gen en_GB.UTF-8
sudo localectl set-locale LC_TIME=en_GB.UTF-8

验证(重新登录后)

$ date
Thu Mar 13 11:27:05 CST 2025

设置历史命令显示时间​

编辑内容并使配置生效

cat >> ~/.bashrc << EOF
export HISTTIMEFORMAT='%F %T '
EOF

source ~/.bashrc

输出示例

$ history
1 2025-03-13 11:09:38 ping baidu.com
2 2025-03-13 11:09:38 cd /etc/netplan/
3 2025-03-13 11:09:38 ls

修改默认编辑器​

ubuntu默认编辑器是 nano ,例如执行 visudo 的时候就是使用nano打开的,但是用不习惯,使用如下命令修改为 vim

export EDITOR=vim
export VISUAL=vim

配置 sudo su - 不需要密码​

在ubuntu22.04 /etc/sudoers 中进行如下配置是不生效的

youruser ALL=(ALL) NOPASSWD: ALL

如下配置是生效的

echo "youruser ALL=(ALL) NOPASSWD: /bin/su -" | sudo tee /etc/sudoers.d/youruser
🛡️本站已安全运行2334天23时18分48秒
Bottom GIF
Top GIF