linux 设置固定ip和dns
目录
1. centos
1.1 ifconfig 查看网卡名称
# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.137.128 netmask 255.255.255.0 broadcast 192.168.137.255
inet6 fe80::91b0:54db:c4f3:d9a9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:67:b4:aa txqueuelen 1000 (Ethernet)
RX packets 150 bytes 24587 (24.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 162 bytes 21607 (21.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 738 bytes 1245656 (1.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 738 bytes 1245656 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
由上面信息可知,系统目前是使用网卡ens33访问Internet的,我们要设置的就是ens33的IP地址。
1.2 设置固定ip和dns
# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #静态IP
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=1d993961-7171-4a50-a17b-089cc70f96e4
DEVICE=ens33
ONBOOT=yes #开机启动
IPV6_PRIVACY=no
CONNECTION_METERED=no
IPADDR=172.16.31.186 #本机地址
NETMASK=255.255.0.0 #子网掩码
GATEWAY=172.16.0.1 #默认网关
DNS1=172.16.0.10
DNS2=8.8.8.8
注意,后有备注的部分,按实际要求更改。
1.3 重启网络
# service network restart
2. ubuntu
2.1 ifconfig 查看网卡名称
# ifconfig
eno1 Link encap:Ethernet HWaddr dc:f4:01:e7:11:88
inet addr:172.16.36.212 Bcast:172.16.255.255 Mask:255.255.0.0
inet6 addr: fe80::def4:1ff:fee7:1188/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13838092 errors:0 dropped:23139 overruns:0 frame:0
TX packets:613652 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3479806143 (3.4 GB) TX bytes:65456575 (65.4 MB)
Interrupt:56
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:266991 errors:0 dropped:0 overruns:0 frame:0
TX packets:266991 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:971472461 (971.4 MB) TX bytes:971472461 (971.4 MB)
由上面信息可知,系统目前是使用网卡ens33访问Internet的,我们要设置的就是eno1的IP地址。
2.2 设置固定ip和dns
# sudo vim /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
auto eno1 # 设置网卡自启动
iface eno1 inet static # 设置使用固定ip
address 172.16.36.212 # 设置ip
netmask 255.255.0.0 # 设置子网掩码
gateway 172.16.0.1 # 设置网关
dns-nameserver 172.16.0.10 # 设置DNS
dns-nameserver 8.8.8.8
2.3 重启网络
# sudo /etc/init.d/networking restart