Linux静态IP及DNS配置

时间:2022-07-24 14:49:30

以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


配置完成后,通过ifdown/ifup脚本重启网卡。注意,不要简单的使用ifconfig命令。

[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 ~]#