2025-03-19    2025-03-19    1165 字  3 分钟

一、CentOS 7.9 网卡名称更改与恢复

CentOS 7.9 默认网卡名称(如 enp0s3)可能不符合传统习惯(如 eth0),或因硬件环境导致网口乱序。以下是更改和恢复网卡名称的两种场景及其解决方法。

  1. 更改网卡名称为 eth0 风格

    • 查看当前网卡状态

      1
      2
      3
      4
      5
      
      [root@docker02 ~]# ip r
      default via 10.0.2.2 dev enp0s3 proto static metric 100
      10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.18 metric 100
      172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
      192.168.56.0/24 dev enp0s8 proto kernel scope link src 192.168.56.105 metric 101
      
    • 修改 GRUB 配置 编辑 /etc/default/grub:

      1
      
      GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
      

      在 crashkernel=auto 后添加 biosdevname=0 net.ifnames=0,修改后为:

      1
      
      GRUB_CMDLINE_LINUX="crashkernel=auto biosdevname=0 net.ifnames=0 rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
      
    • 刷新内核

      1
      
      grub2-mkconfig -o /boot/grub2/grub.cfg
      

      输出示例:

      1
      2
      3
      4
      
      Generating grub configuration file ...
      Found linux image: /boot/vmlinuz-3.10.0-1160.119.1.el7.x86_64
      Found initrd image: /boot/initramfs-3.10.0-1160.119.1.el7.x86_64.img
      done
      
    • 重启系统

      1
      
      reboot
      
    • 修改网卡配置文件 进入目录:

      1
      
      cd /etc/sysconfig/network-scripts
      

      重命名并编辑文件:

      1
      2
      
      mv ifcfg-enp0s3 ifcfg-eth0
      vi ifcfg-eth0
      

      确保包含:

      1
      2
      
      NAME="eth0"
      DEVICE="eth0"
      
    • 再次重启

      1
      
      reboot
      
  2. 恢复旧网卡名称并解决网口乱序

    • 更新内核参数

      1
      
      grubby --update-kernel=ALL --args="net.ifnames=0 biosdevname=0"
      
    • 修改网卡配置文件 示例配置文件(位于 /etc/sysconfig/network-scripts/):

       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
      30
      31
      32
      
      [root@QZ network-scripts]# cat ifcfg-lan0
      TYPE="Ethernet"
      BOOTPROTO="static"
      DEFROUTE="yes"
      NAME="lan0"
      DEVICE="lan0"
      ONBOOT="yes"
      IPADDR=192.168.242.130
      NETWORK=255.255.255.0
      GATEWAY=192.168.242.1
      
      [root@QZ network-scripts]# cat ifcfg-lan1
      TYPE="Ethernet"
      BOOTPROTO="static"
      DEFROUTE="yes"
      NAME="lan1"
      DEVICE="lan1"
      ONBOOT="yes"
      IPADDR=192.168.35.130
      NETWORK=255.255.255.0
      GATEWAY=192.168.35.2
      DNS1=192.168.35.2
      
      [root@QZ network-scripts]# cat ifcfg-lan2
      TYPE="Ethernet"
      BOOTPROTO="static"
      DEFROUTE="yes"
      NAME="lan2"
      DEVICE="lan2"
      ONBOOT="yes"
      IPADDR=192.168.242.131
      NETWORK=255.255.255.0
      
    • 解决网口乱序(适用于带光口的服务器) 根据网卡 MAC 地址绑定名称:

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      
      vi /etc/systemd/network/10-lan0.link
      [Match]
      MACAddress=00:0c:29:36:a6:c7
      [Link]
      Name=lan0
      
      vi /etc/systemd/network/11-lan1.link
      [Match]
      MACAddress=00:0c:29:36:a6:b3
      [Link]
      Name=lan1
      
      vi /etc/systemd/network/12-lan2.link
      [Match]
      MACAddress=00:0c:29:36:a6:bd
      [Link]
      Name=lan2
      
    • 重启设备

      1
      
      reboot
      

二、CentOS 7 进入救援模式

当根目录变为只读(如执行 yum 报错 [Errno 30] 只读文件系统),可进入救援模式修复:

  1. 进入单用户模式

    • 重启机器,在 GRUB 菜单按 e,找到 linux 开头行。

    • 添加 single 或 init=/bin/bash,按 Ctrl+X 启动。

  2. 检查挂载状态

    1
    
    mount | grep '/'
    

    若显示 ro,则为只读。

  3. 重新挂载为读写

    1
    
    mount -o remount,rw /
    

    再次检查确认 rw。

  4. 修复权限

    1
    
    chmod +x /etc/fstab
    

    恢复 /etc/fstab 内容。

  5. 重启

    1
    
    reboot
    

三、Linux 开机启动服务搭建

以一个 Python 脚本为例,配置开机自启动服务:

  1. 准备脚本

    • 路径:/root/workdir/mydate/python3_date.py

    • 内容:

      1
      2
      3
      4
      5
      6
      7
      
      import datetime
      import time
      import sys
      while True:
          print(datetime.datetime.now())
          sys.stdout.flush()
          time.sleep(3)
      
  2. 创建服务

    • 文件:/usr/lib/systemd/system/python3_date.service

    • 内容:

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      
      [Unit]
      Description=python3_date_py
      After=network.target
      [Service]
      Type=simple
      ExecStart=/usr/bin/python3 /root/workdir/mydate/python3_date.py
      StandardOutput=append:/root/workdir/mydate/out.log
      StandardError=append:/root/workdir/mydate/out.log
      Restart=on-failure
      [Install]
      WantedBy=multi-user.target
      
  3. 启用服务

    1
    2
    
    systemctl daemon-reload
    systemctl enable --now python3_date.service
    
  4. 查看日志

    • journalctl -u python3_date -n 10

    • tail -f /root/workdir/mydate/out.log

  5. 关闭服务

    1
    2
    
    systemctl disable --now python3_date
    rm /usr/lib/systemd/system/python3_date.service
    

四、iptables 限速配置

使用 iptables 限制网络流量:

  1. 方法一:丢弃超限流量

    1
    2
    
    iptables -I INPUT -i eth0 -m hashlimit --hashlimit-above 128/s --hashlimit-mode srcip --hashlimit-name in -j DROP
    iptables -I OUTPUT -o eth0 -m hashlimit --hashlimit-above 128/s --hashlimit-mode dstip --hashlimit-name out -j DROP
    
  2. 方法二:允许指定速率

    1
    2
    3
    4
    
    iptables -A INPUT -i eth0 -m hashlimit --hashlimit 128/s --hashlimit-burst 128 --hashlimit-mode srcip --hashlimit-name in -j ACCEPT
    iptables -A INPUT -i eth0 -j DROP
    iptables -A OUTPUT -i eth0 -m hashlimit --hashlimit 128/s --hashlimit-burst 128 --hashlimit-mode dstip --hashlimit-name out -j ACCEPT
    iptables -A OUTPUT -i eth0 -j DROP
    

五、CentOS 7.9 安装 jq

安装 JSON 处理工具 jq:

  1. 安装 EPEL 源

    1
    
    yum install epel-release
    
  2. 检查 jq 包

    1
    
    yum list jq
    
  3. 安装

    1
    
    yum install jq
    

六、CentOS 7.9 安装 MySQL 8.0

在 CentOS 7.9 上安装 MySQL 8.0(原文仅提供环境和标题,假设为标准安装流程):

  1. 添加 MySQL 源

    1
    
    rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    
  2. 安装

    1
    
    yum install -y mysql-community-server
    
  3. 启动并配置

    1
    2
    3
    4
    5
    
    systemctl start mysqld
    systemctl enable mysqld
    grep 'temporary password' /var/log/mysqld.log
    mysql -uroot -p
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword123!';
    

安装环境 centos7.9 mysql8.0

参考链接