温馨提示×

如何在Linux上更改域名服务器

小樊
46
2025-11-10 14:22:51
栏目: 云计算

在Linux上更改域名服务器(DNS)可以通过修改网络配置文件来实现。以下是一些常见的Linux发行版及其相应的步骤:

1. Ubuntu/Debian

  1. 编辑网络接口配置文件: 打开终端并使用文本编辑器(如nanovim)编辑网络接口配置文件。通常是/etc/network/interfaces/etc/netplan/*.yaml

    • 对于/etc/network/interfaces

      sudo nano /etc/network/interfaces
      

      找到你的网络接口配置部分,例如:

      auto eth0
      iface eth0 inet dhcp
      

      改为:

      auto eth0
      iface eth0 inet static
          address 192.168.1.100
          netmask 255.255.255.0
          gateway 192.168.1.1
          dns-nameservers 8.8.8.8 8.8.4.4
      
    • 对于/etc/netplan/*.yaml

      sudo nano /etc/netplan/01-netcfg.yaml
      

      修改为:

      network:
        version: 2
        renderer: networkd
        ethernets:
          eth0:
            dhcp4: no
            addresses:
              - 192.168.1.100/24
            gateway4: 192.168.1.1
            nameservers:
              addresses: [8.8.8.8, 8.8.4.4]
      
  2. 应用更改

    • 对于/etc/network/interfaces
      sudo ifdown eth0 && sudo ifup eth0
      
    • 对于/etc/netplan/*.yaml
      sudo netplan apply
      

2. CentOS/RHEL

  1. 编辑网络接口配置文件: 打开终端并使用文本编辑器(如nanovim)编辑网络接口配置文件。通常是/etc/sysconfig/network-scripts/ifcfg-eth0

    sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
    

    修改为:

    DEVICE=eth0
    BOOTPROTO=static
    ONBOOT=yes
    IPADDR=192.168.1.100
    NETMASK=255.255.255.0
    GATEWAY=192.168.1.1
    DNS1=8.8.8.8
    DNS2=8.8.4.4
    
  2. 重启网络服务

    sudo systemctl restart network
    

3. Fedora

  1. 编辑网络接口配置文件: 打开终端并使用文本编辑器(如nanovim)编辑网络接口配置文件。通常是/etc/sysconfig/network-scripts/ifcfg-eth0

    sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
    

    修改为:

    DEVICE=eth0
    BOOTPROTO=static
    ONBOOT=yes
    IPADDR=192.168.1.100
    NETMASK=255.255.255.0
    GATEWAY=192.168.1.1
    DNS1=8.8.8.8
    DNS2=8.8.4.4
    
  2. 重启网络服务

    sudo systemctl restart network
    

4. Arch Linux

  1. 编辑网络接口配置文件: 打开终端并使用文本编辑器(如nanovim)编辑网络接口配置文件。通常是/etc/systemd/network/eth0.network

    sudo nano /etc/systemd/network/eth0.network
    

    修改为:

    [Match]
    Name=eth0
    
    [Network]
    Address=192.168.1.100/24
    Gateway=192.168.1.1
    DNS=8.8.8.8 8.8.4.4
    
  2. 重启网络服务

    sudo systemctl restart systemd-networkd
    

验证更改

无论你使用哪种方法,都可以通过以下命令验证DNS更改是否生效:

cat /etc/resolv.conf

你应该能看到新的DNS服务器地址。

请注意,某些Linux发行版可能会使用其他网络管理工具(如nmclinmtui),你需要根据具体情况进行调整。

0