1 什么是 Ubuntu
1.1 概述

Ubuntu(乌班图)是基于Debian架构开发的开源 Linux 操作系统,由 Canonical 公司主导维护,其名称源自非洲祖鲁语,意为'人性'或'我为人人'。作为 Debian 系列的重要分支,Ubuntu 继承了 Debian 的稳定性和丰富的软件生态,同时在用户体验和易用性上进行了大量优化,成为全球最受欢迎的 Linux 发行版之一。
Ubuntu 操作系统的基础概述及版本选择,详细演示了基于 VMware 的虚拟机创建与系统安装流程,包括分区、用户设置及 SSH 服务启用。内容涵盖 Netplan 网络配置(静态 IP、桥接等)、APT 与 DEB 包管理软件安装方法,以及内核参数优化、SSH 安全加固和防火墙配置。最后提供了常见问题的排查方案,适用于生产环境下的 Ubuntu 服务器部署与管理。

Ubuntu(乌班图)是基于Debian架构开发的开源 Linux 操作系统,由 Canonical 公司主导维护,其名称源自非洲祖鲁语,意为'人性'或'我为人人'。作为 Debian 系列的重要分支,Ubuntu 继承了 Debian 的稳定性和丰富的软件生态,同时在用户体验和易用性上进行了大量优化,成为全球最受欢迎的 Linux 发行版之一。
dpkg(底层)和 apt(高层)组合,支持软件的安装、升级、卸载及依赖关系自动处理,apt 的设计理念后来被 Red Hat 的 YUM 借鉴;stable(稳定版,适合生产环境)、testing(测试版,包含较新软件)和 unstable(开发版,更新频繁)三个版本,用户可根据需求选择。Ubuntu Server 是专为服务器环境设计的版本,剔除了桌面环境,专注于稳定性、安全性和性能优化,广泛应用于:
| 对比维度 | Ubuntu Desktop(桌面版) | Ubuntu Server(服务器版) |
|---|---|---|
| 核心定位 | 面向个人用户,提供图形化桌面环境 | 面向服务器场景,无图形界面,通过命令行管理 |
| 预装软件 | Firefox 浏览器、LibreOffice 办公套件、视频播放器等 | 仅包含基础系统工具和服务组件 |
| 硬件资源需求 | 较高(需显卡、内存≥4GB) | 较低(内存≥1GB,无显卡要求) |
| 管理方式 | 图形界面为主,命令行为辅 | 纯命令行管理,支持自动化脚本 |
| 典型应用 | 日常办公、软件开发、娱乐 | 网站托管、数据库服务、云计算 |










http://proxy.example.com:8080),否则留空;
http://archive.ubuntu.com/ubuntu(国外服务器,速度较慢),建议修改为国内镜像源:
http://mirrors.aliyun.com/ubuntu;
/dev/sda),点击'Add GPT Partition Table',创建分区表;/boot 分区:
/(根分区):





ubuntu-server),点击'Done';andy),点击'Done';
apt 安装;




andy@ubuntu-server:~$)。
Ubuntu 默认禁用 root 账户登录,需通过普通用户设置 root 密码:
andy@ubuntu-server:~$ sudo passwd root # 执行 sudo 命令,输入普通用户密码
[sudo] password for andy: # 输入普通用户密码(如 andy 的密码)
New password: # 输入 root 密码(如 123456)
Retype new password: # 重复输入 root 密码
passwd: password updated successfully # 提示密码更新成功
andy@ubuntu-server:~$ su - root # 切换到 root 账户
Password: # 输入 root 密码
root@ubuntu-server:~# # 成功切换到 root 用户
Ubuntu Server 默认启用 ufw(Uncomplicated Firewall)防火墙,为方便测试,可暂时关闭:
root@ubuntu-server:~# systemctl status ufw # 查看防火墙状态(默认 active)
root@ubuntu-server:~# systemctl stop ufw # 停止防火墙
root@ubuntu-server:~# systemctl disable ufw # 禁用防火墙开机自启
root@ubuntu-server:~# systemctl status ufw # 验证防火墙状态(应为 inactive)
默认情况下,Ubuntu 禁止 root 用户通过 SSH 登录,需修改 SSH 配置文件:
root@ubuntu-server:~# vim /etc/ssh/sshd_config # 编辑 SSH 配置文件
# 找到以下行,将 PermitRootLogin 的值改为 yes(默认可能为 prohibit-password 或 no)
PermitRootLogin yes
# 保存退出(vim 操作:按 ESC,输入 :wq,回车)
root@ubuntu-server:~# systemctl restart ssh # 重启 SSH 服务
root@ubuntu-server:~# apt update # 更新软件包列表
root@ubuntu-server:~# apt install -y \
tree \ # 目录树查看工具
gcc \ # C 语言编译器
make \ # 编译工具
net-tools \ # 网络工具(ifconfig、route 等)
openssh-server \ # SSH 服务(已安装可忽略)
vim \ # 文本编辑器
curl \ # HTTP 请求工具
wget # 文件下载工具
为避免后续操作失误导致系统损坏,建议制作快照:
Netplan 是 Ubuntu 17.10 及更高版本默认的网络配置工具,替代了传统的 ifupdown(ifconfig、/etc/network/interfaces)。它通过YAML 格式的配置文件描述网络接口,自动生成底层网络管理工具(如 systemd-networkd 或 NetworkManager)的配置,简化了网络配置流程。

systemd-networkd(服务器默认)和 NetworkManager(桌面版默认);netplan apply 即可生效,无需重启网络服务;/etc/netplan/.yaml 结尾,建议命名为 50-cloud-init.yaml(cloud-init 生成的默认文件)或自定义名称(如 01-netcfg.yaml),文件名数字越小,优先级越高。key: value,冒号后必须加空格;# 开头,注释内容会被忽略。列表:使用 - 表示列表项,如:
addresses:
- 192.168.48.150/24
- 10.0.0.1/8
适用于网络中有 DHCP 服务器的场景(如家庭、办公网络):
root@ubuntu-server:~# vim /etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens32: # 网卡名称(通过 ip addr 查看)
dhcp4: true # 启用 IPv4 DHCP
dhcp6: false # 禁用 IPv6 DHCP(可选)
version: 2 # Netplan 配置版本(固定为 2)
应用配置:
root@ubuntu-server:~# netplan apply
适用于服务器环境,需要固定 IP 地址:
应用配置并验证:
root@ubuntu-server:~# netplan apply # 应用配置
root@ubuntu-server:~# ip addr show ens32 # 验证 IP 地址
root@ubuntu-server:~# ping baidu.com # 验证网络连通性
编辑配置文件:
root@ubuntu-server:~# vim /etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens32:
dhcp4: no # 禁用 DHCP
addresses: # 静态 IP 地址(支持多个)
- 192.168.48.150/24 # IP 地址/子网掩码
routes: # 路由配置
- to: default # 默认路由(所有流量)
via: 192.168.48.2 # 网关地址
nameservers: # DNS 配置
addresses:
- 114.114.114.114 # 国内 DNS
- 8.8.8.8 # Google DNS
version: 2
查看网卡名称和网络信息:
root@ubuntu-server:~# ip addr # 查看网卡名称(如 ens32)
root@ubuntu-server:~# route -n # 查看网关(如 192.168.48.2)
适用于服务器需要绑定多个 IP 的场景:
root@ubuntu-server:~# vim /etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens32:
dhcp4: no
addresses:
- 192.168.48.150/24 # 主 IP
- 192.168.48.151/24 # 第二个 IP
- 10.0.0.100/8 # 第三个 IP(不同网段)
routes:
- to: default
via: 192.168.48.2
nameservers:
addresses: [114.114.114.114, 8.8.8.8]
version: 2
适用于需要创建桥接接口供虚拟机使用:
root@ubuntu-server:~# vim /etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens32: # 物理网卡
dhcp4: no
bridges:
br0: # 桥接接口名称
interfaces: [ens32] # 绑定物理网卡
dhcp4: no
addresses: [192.168.48.150/24]
routes:
- to: default
via: 192.168.48.2
nameservers:
addresses: [114.114.114.114]
version: 2
netplan apply(无需重启网络服务,推荐);netplan generate && netplan apply;netplan apply --debug(调试模式,显示详细日志)。netplan apply 提示 Error in network definition;yamllint 工具验证语法。ip addr 查看不到配置的 IP;dhcp4 设置为 no,重新应用配置。route -n);cat /etc/resolv.conf)。Ubuntu 提供多种软件安装方式,各有优缺点,适用于不同场景:
| 安装方式 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| APT 工具安装 | 自动处理依赖、官方维护、安全可靠 | 软件版本可能较旧 | 安装系统组件和常用服务 |
| DEB 包安装 | 可安装特定版本软件 | 需手动解决依赖关系 | 安装第三方软件(如 Google Chrome) |
| 源码编译安装 | 可自定义编译参数、获取最新版本 | 编译过程复杂、耗时较长 | 安装需要定制化的软件 |
| Snap 包安装 | 自动更新、跨发行版兼容 | 占用磁盘空间较大、启动速度较慢 | 安装最新版桌面应用 |
APT(Advanced Package Tool)是 Ubuntu/Debian 系统的核心包管理工具,基于 dpkg 实现,支持软件的安装、更新、升级和卸载,是最常用的安装方式。
# 1. 更新软件包列表(安装/升级前必须执行)
apt update
# 2. 安装软件包
apt install <软件包名> -y # -y 自动确认安装
apt install <包 1> <包 2> <包 3> -y # 批量安装
# 3. 卸载软件包
apt remove <软件包名> -y # 卸载软件,保留配置文件
apt purge <软件包名> -y # 彻底卸载软件及配置文件
# 4. 升级软件包
apt upgrade -y # 升级所有已安装软件(不删除旧包)
apt full-upgrade -y # 全面升级(可能删除/安装包以解决依赖)
# 5. 搜索软件包
apt search <关键词> # 搜索软件包名称和描述
# 6. 查看软件包信息
apt show <软件包名> # 查看软件版本、依赖、描述等
# 7. 查看已安装软件
apt list --installed # 列出所有已安装软件包
apt list --upgradeable # 列出可升级的软件包
# 8. 清理 APT 缓存
apt clean # 清除所有下载的软件包缓存
apt autoclean # 清除过期的软件包缓存
apt autoremove -y # 自动删除不再需要的依赖包
APT 软件源决定了从哪里下载软件包,默认源为国外服务器,速度较慢,建议更换为国内镜像源。
更新软件包列表:
root@ubuntu-server:~# 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-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security 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://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
编辑源配置文件:
root@ubuntu-server:~# vim /etc/apt/sources.list
备份默认源配置:
root@ubuntu-server:~# cp /etc/apt/sources.list /etc/apt/sources.list.bak
DEB 包是 Ubuntu/Debian 系统的二进制软件包,扩展名为 .deb,适用于安装官方源中没有的软件(如 Google Chrome、微信等)。
dpkg 是 Debian 系统的底层包管理工具,直接操作 DEB 包,apt 工具底层也依赖 dpkg。
# 1. 安装 DEB 包
dpkg -i <软件包.deb> # 安装 DEB 包,可能因依赖问题失败
# 2. 解决依赖问题
apt install -f -y # 自动修复依赖关系
# 3. 查看 DEB 包信息
dpkg -I <软件包.deb> # 查看软件包名称、版本、依赖等
# 4. 查看 DEB 包内容
dpkg -c <软件包.deb> # 列出软件包包含的文件和目录
# 5. 卸载 DEB 包安装的软件
dpkg -r <软件包名> # 卸载软件,保留配置文件
dpkg -P <软件包名> # 彻底卸载软件及配置文件
# 6. 查看已安装的 DEB 包
dpkg -l # 列出所有已安装的软件包
dpkg -l | grep <关键词> # 搜索特定软件包
启动 Google Chrome:
root@ubuntu-server:~# google-chrome-stable
修复依赖问题(若安装失败):
root@ubuntu-server:~# apt install -f -y
安装 DEB 包:
root@ubuntu-server:~# dpkg -i google-chrome-stable_current_amd64.deb
下载 DEB 包:
root@ubuntu-server:~# wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
源码编译安装是最灵活的安装方式,可自定义软件功能和安装路径,但过程较复杂,适用于需要安装特定版本或定制化配置的场景。
验证安装:
root@ubuntu-server:~# curl http://localhost # 应返回 Nginx 默认页面
启动 Nginx:
root@ubuntu-server:~# /usr/local/nginx/sbin/nginx
编译与安装:
root@ubuntu-server:~/nginx-1.24.0# make # 编译
root@ubuntu-server:~/nginx-1.24.0# make install # 安装
配置编译参数:
root@ubuntu-server:~/nginx-1.24.0# ./configure \
--prefix=/usr/local/nginx \
# 安装路径
--with-http_ssl_module \
# 启用 SSL 模块
--with-http_stub_status_module # 启用状态模块
解压源码包:
root@ubuntu-server:~# tar -zxvf nginx-1.24.0.tar.gz
root@ubuntu-server:~# cd nginx-1.24.0
下载源码包:
root@ubuntu-server:~# wget https://nginx.org/download/nginx-1.24.0.tar.gz
安装编译依赖:
root@ubuntu-server:~# apt install -y gcc make libpcre3-dev zlib1g-dev libssl-dev
编辑 /etc/sysctl.conf 文件,添加以下参数以提升系统性能和稳定性:
root@ubuntu-server:~# vim /etc/sysctl.conf
# 网络优化
net.core.somaxconn = 65535 # 最大 TCP 连接数
net.ipv4.tcp_max_syn_backlog = 65535 # TCP 最大半连接队列
net.ipv4.tcp_fin_timeout = 30 # TCP 连接超时时间
net.ipv4.tcp_keepalive_time = 1200 # TCP keepalive 超时时间
# 文件描述符限制
fs.file-max = 655350 # 系统最大文件描述符数
# 内存优化
vm.swappiness = 10 # 减少 swap 使用,优先使用内存
应用配置:
root@ubuntu-server:~# sysctl -p
重启 SSH 服务:
root@ubuntu-server:~# systemctl restart ssh
禁用密码登录,启用密钥登录:
root@ubuntu-server:~# vim /etc/ssh/sshd_config
PasswordAuthentication no # 禁用密码登录
PubkeyAuthentication yes # 启用密钥登录
修改 SSH 默认端口:
root@ubuntu-server:~# vim /etc/ssh/sshd_config
Port 2222 # 将默认端口 22 改为 2222
在生产环境中,建议启用 ufw 防火墙,仅开放必要端口:
# 启用防火墙
root@ubuntu-server:~# ufw enable
# 开放 SSH 端口(假设已改为 2222)
root@ubuntu-server:~# ufw allow 2222/tcp
# 开放 HTTP/HTTPS 端口
root@ubuntu-server:~# ufw allow 80/tcp
root@ubuntu-server:~# ufw allow 443/tcp
# 查看防火墙状态
root@ubuntu-server:~# ufw status
/etc/netplan/ 配置文件,确保网络配置正确;journalctl -b,排查异常服务;systemctl disable <服务名>。ufw status,确保 SSH 端口开放;sshd -t,检查语法错误;ip addr,确保客户端使用正确 IP 登录。/etc/apt/sources.list,确保地址正确;apt update;apt install -f -y;apt clean && apt autoclean。ip addr,确保与网关在同一网段;ping <网关 IP>;cat /etc/resolv.conf,尝试使用 ping baidu.com 测试 DNS 解析。
微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online