Linux静态IP及DNS配置

本文详细介绍了如何在Linux系统下为eth0网络接口配置静态IP地址和DNS。主要内容包括修改ifcfg-eth0配置文件,设置IPADDR、NETMASK、GATEWAY和DNS1参数,以及使用ifdown和ifup命令重启网络服务。最后通过ifconfig和ping命令检查配置是否成功并验证网络连通性。

以eth0为例,下面是其配置文件示例。

主要是配置红色部分。

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:A3:48:38
TYPE=Ethernet
UUID=afeca381-c5c1-4dfe-9e8a-ec573dbd04f7
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.124.250
NETMASK=255.255.255.0
GATEWAY=192.168.124.1
DNS1=192.168.124.1


配置完成后,通过如下两条命令重启网卡即可。

注意,注意,注意,

如下的第一条命令执行后,所有基于此网口的网络连接都会中断。
所以,首先请确保您当前不是通过此网口在做远控操作,

同时妥善处置可能给他人带来的不利影响。

[root@localhost ~]# ifdown eth0
[root@localhost ~]# ifup eth0


最后查看配置是否生效,并实测ping网关与互联网网站,看看通不通。

[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:A3:48:38  
          inet addr:192.168.124.250  Bcast:192.168.124.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fea3:4838/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2473 errors:0 dropped:0 overruns:0 frame:0
          TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:310768 (303.4 KiB)  TX bytes:1293 (1.2 KiB)
          Interrupt:19 Base address:0x2000 


[root@localhost ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
domain localdomain
search localdomain
nameserver 192.168.124.1
nameserver 192.168.13.1


[root@localhost ~]# ping 192.168.124.1
PING 192.168.124.1 (192.168.124.1) 56(84) bytes of data.
64 bytes from 192.168.124.1: icmp_seq=2 ttl=128 time=15.2 ms
64 bytes from 192.168.124.1: icmp_seq=3 ttl=128 time=10.6 ms
64 bytes from 192.168.124.1: icmp_seq=4 ttl=128 time=3.63 ms
^C
--- 192.168.124.1 ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3380ms
rtt min/avg/max/mdev = 3.636/9.853/15.253/4.778 ms


[root@localhost ~]# ping www.baidu.com
PING www.baidu.com (180.97.33.107) 56(84) bytes of data.
64 bytes from 180.97.33.107: icmp_seq=1 ttl=57 time=428 ms
64 bytes from 180.97.33.107: icmp_seq=2 ttl=57 time=4.28 ms
64 bytes from 180.97.33.107: icmp_seq=3 ttl=57 time=74.0 ms
64 bytes from 180.97.33.107: icmp_seq=4 ttl=57 time=29.7 ms
^C
--- www.baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3420ms
rtt min/avg/max/mdev = 4.285/134.260/428.909/171.940 ms
[root@localhost ~]# 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值