Linux 快捷命令集合

本页面收集了Linux系统常用的快捷命令,包括系统换源、系统更新、挂载数据盘及格式化、安装宝塔面板、安装wget、安装BBR加速、同步服务器时间、时区更换、DNS自动更换等功能。 适用于通过SSH工具(如宝塔SSH终端或FinalShell)连接服务器后使用,IPv4和IPv6均适用。

提示:点击命令块右上角的复制按钮可快速复制命令

系统换源(国内镜像源,加速包管理)

1. CentOS 系统(yum 换源,以阿里云为例)

# 备份原repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

# 下载阿里云源(CentOS 7)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 若为CentOS 8,替换为:
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

# 清除缓存并生成新缓存
yum clean all && yum makecache

2. Ubuntu/Debian 系统(apt 换源,以阿里云为例)

# 备份原sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 替换为阿里云源(Ubuntu 20.04为例,其他版本需修改"focal"为对应版本代号,如18.04是"bionic")
cat > /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
EOF

# 刷新缓存
apt update

系统更新(升级系统组件)

1. CentOS 系统

# 升级所有包(不升级内核)
yum update -y

# 若需升级内核(谨慎,部分服务器可能依赖旧内核)
yum update -y kernel

2. Ubuntu/Debian 系统

# 更新包列表并升级所有包
apt update && apt upgrade -y

# (可选)清理无用依赖
apt autoremove -y && apt autoclean

数据盘挂载与格式化(以新磁盘`/dev/vdb`为例)

步骤1:查看磁盘列表(确认目标磁盘名称)

fdisk -l
# 通常新磁盘为 /dev/vdb(云服务器常见)或 /dev/sdb(物理机常见)

步骤2:分区(MBR格式,单分区)

fdisk /dev/vdb
# 输入以下指令(按顺序):
# n(新建分区)→ p(主分区)→ 1(分区号)→ 回车(默认起始扇区)→ 回车(默认结束扇区)→ w(保存退出)

步骤3:格式化(ext4格式,兼容性好)

mkfs.ext4 /dev/vdb1  # 注意分区名是 vdb1(加数字)

步骤4:挂载(永久生效)

# 创建挂载点(如 /data)
mkdir /data

# 临时挂载(重启后失效)
mount /dev/vdb1 /data

# 永久挂载(写入/etc/fstab)
echo "/dev/vdb1 /data ext4 defaults 0 0" >> /etc/fstab

# 验证挂载(无报错则成功)
mount -a
注意:磁盘名称(如`vdb`)需根据实际情况修改,若为GPT格式分区,需用`parted`工具(比`fdisk`支持更大容量)。

安装宝塔面板(不同版本)

1. 官方稳定版(推荐,兼容主流系统)

# 通用安装命令(CentOS/Ubuntu/Debian均支持)
wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh ed8484bec
# 安装完成后会显示面板地址、用户名、密码,建议保存

2. 开心版(第三方修改版,存在安全风险,谨慎使用)

# 注意:非官方版本,可能包含后门或漏洞,仅测试环境使用
wget -O install.sh https://raw.githubusercontent.com/xxx/btpanel-script/main/install.sh && sh install.sh
# (地址可能失效,需自行确认第三方来源可靠性)
提示:安装后需在服务器防火墙开放8888(面板端口)、20/21(FTP)、80/443(网站)等端口。

安装 wget(命令行下载工具)

1. CentOS 系统

yum install wget -y

2. Ubuntu/Debian 系统

apt install wget -y

安装 BBR 加速(提升网络传输效率)

适用场景:Linux内核 ≥ 4.9(CentOS 7需升级内核,Ubuntu 18.04+默认支持)

# 开启BBR(临时生效)
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p  # 生效配置

# 验证是否开启成功(显示bbr则生效)
sysctl net.ipv4.tcp_congestion_control
# 输出:net.ipv4.tcp_congestion_control = bbr

若内核不支持(如CentOS 7默认内核),需先升级内核:

# 安装ELRepo仓库
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm

# 安装最新内核
yum --enablerepo=elrepo-kernel install kernel-ml -y

# 设置默认启动新内核
grub2-set-default 0 && grub2-mkconfig -o /boot/grub2/grub.cfg

# 重启服务器(必须)
reboot

同步服务器时间与更换时区

1. 同步时间(以阿里云NTP服务器为例)

# 安装ntpdate(CentOS)
yum install ntpdate -y

# 安装ntpdate(Ubuntu/Debian)
apt install ntpdate -y

# 同步时间(阿里云NTP服务器)
ntpdate ntp.aliyun.com

# (可选)设置定时同步(每天凌晨3点)
echo "0 3 * * * ntpdate ntp.aliyun.com" >> /etc/crontab

2. 更换时区(设置为上海时区)

# 方法1:通过timedatectl(推荐,系统需支持)
timedatectl set-timezone Asia/Shanghai

# 方法2:手动链接时区文件(兼容所有系统)
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 验证时区
date  # 显示当前时间,若为北京时间则成功

DNS自动更换(国内常用DNS)

临时修改(重启后失效)

# 编辑DNS配置文件
vi /etc/resolv.conf
# 添加以下内容(阿里云DNS,备选114DNS)
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 114.114.114.114

永久生效(防止被NetworkManager覆盖)

# CentOS系统(修改NetworkManager配置)
cat > /etc/NetworkManager/conf.d/dns.conf << EOF
[main]
dns=none
EOF
systemctl restart NetworkManager

# Ubuntu/Debian系统(禁用resolvconf自动更新)
echo "resolvconf=NO" >> /etc/resolvconf/resolv.conf.d/base
resolvconf -u

# 重新写入DNS(永久生效)
echo "nameserver 223.5.5.5" > /etc/resolv.conf
echo "nameserver 223.6.6.6" >> /etc/resolv.conf
命令已复制到剪贴板