Debian 修改 IP 和 DNS

Debian 作为服务器在安装的时候会自动设置 IP 为 DHCP 自动获取,也可以设置为静态 IP ,设置完了后续环境改变了要更改 IP 怎么搞?一般使用 Linux 作为服务器都是会提前规划好 IP 不会使用 DHCP 但是也不是没有例外。如果是桌面环境,那么静态 IP 和 DHCP 都无所谓,因为有交互设置相对简单。今天这篇是对 Debian 修改 IP 和 DNS 文章供各位参考。

0x01 查看配置

查看网卡配置的命令有 ifconfig、ip a、ip addr 等,可以根据自己习惯使用命令查看网卡的配置情况。查看网卡 ens192 现有的 ip 为:172.0.0.250 我需要把它修改为:172.0.0.222;

ifconfig # 查看网卡配置;
cat /etc/resolv.conf # 查看 dns 服务器;

使用 cat 命令查看当前主机的 dns 的配置;

0x02 修改配置

修改 IP 和 DNS 只需要三条命令即可,先修改 IP DNS 重启网卡服务。在 IP 的配置页面可以修改 DNS /etc/resolv.conf 也是有 DNS 记录的。在网卡的配置页面可以设置网卡为静态 IP 还是 DHCP 获取;

vim /etc/network/interfaces # 网卡配置;
vim /etc/resolv.conf # 配置 dns;
systemctl restart networking # 重启网卡服务;

0x03 配置静态

执行 vim /etc/network/interfaces 命令打开网卡的配置文件如下图,DNS 服务可以设置3个,其实两个就够了;

vim /etc/network/interfaces # 编辑网卡配置;
# The primary network interface
auto ens192 # 网卡默认没有此参数;
allow-hotplug ens192
iface ens192 inet static # 设置网卡为静态IP;
address 172.0.0.222/24 # 网卡IP地址,/24为子网掩码;
gateway 172.0.0.1 # 设置网关;
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 172.0.0.1 119.29.29.29 114.114.114.114 # 填写dns服务器;

需要注意 Debian 默认配置文件中是没有 auto ens192(网卡名称) 这个参数 ,从而是 allow-hotplug ens192(网卡名称) 这个参数替代。这里有个坑,如果没有添加 auto ens192 那么执行完网卡重启命令后,网卡起不来并且查询不到网卡的配置,只能重启服务器。

如果设置的是 auto 不管你插不插网线,网卡都会启用,而且运行 /etc/init.d/networking restart 之后网卡能自动起来;

如果设置的是 allow-hotplug 它会在开机时启动插网线的网卡,运行 /etc/init.d/networking restart 之后网卡不能自动重启;

0x04 配置 DHCP

配置网卡为 DHCP 自动获取,同配置静态 IP 一样。将 static 修改为 dhcp 并注释 address、gateway、dns-nameservers 这三个配置参数;这样就会使用 dhcp 的配置;

vim /etc/network/interfaces # 编辑网卡配置;
# The primary network interface
auto ens192
allow-hotplug ens192
iface ens192 inet dhcp
# address 172.0.0.222/24 # 注释掉静态ip和掩码;
# gateway 172.0.0.1 # 注释掉网关配置;
# dns-* options are implemented by the resolvconf package, if installed
# dns-nameservers 172.0.0.1 119.29.29.29 114.114.114.114 # 注释掉dns服务器;

0x05 配置 DNS

使用编辑命令 vim /etc/resolv.conf 打开配置文件,修改 nameserver 后面的即可,注意 nameserver 和 dns 的 IP 地址之间有空格,编辑完保存即可;

vim /etc/resolv.conf # 编辑dns配置;

0x06 确认配置

以上配置文件修改完毕后,重启网卡服务。shell 重新连接到服务器使用 ifconfig 命令查看网卡的 IP 的地址已经修改为我需要的;

systemctl restart networking # 重启网卡服务;
相关推荐
python ERROR: Command errored out with exit status 1:
Windows Server 2016 搭建 SMB 共享文件
Windows Server 2019 域环境搭建 SMB 共享文件服务
Parallels Desktop 15.1.3