下一篇 分享链接 返回 返回顶部

Linux 快捷命令集合

发布人:矩阵云擎 发布时间:2025-08-05 13:58

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

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

注意事项

  1. 执行命令前建议备份关键配置文件(如/etc/fstab/etc/resolv.conf),避免操作失误导致系统异常。
  2. 第三方脚本(如开心版宝塔)可能存在安全风险,生产环境建议使用官方版本。
  3. 不同云厂商(如阿里云、腾讯云)的磁盘名称、网络配置可能略有差异,需根据实际环境调整命令中的参数(如磁盘路径/dev/vdb)。

 

通过以上命令,可快速完成 Linux 服务器的基础配置与工具安装,适合新手或批量操作场景。
目录结构
全文
微信客服 微信客服
QQ群聊 QQ群聊
服务热线: 18020165261
电子邮箱: 971732136@qq.com