Linux 配置网络方法

配置网络

临时生效

1
2
3
4
# 配置 ip、子网掩码
ifconfig eth0 10.130.6.180 netmask 255.255.0.0
# 配置网关
route add default gw 10.130.6.1

永久生效

1
2
3
4
5
6
7
8
9
vi /etc/sysconfig/network-scripts/ifcfg-eth0

# 加入下面三行
IPADDR=10.130.6.180
GATEWAY=10.130.6.1
NETMASK=255.255.0.0

# 重启网络
systemctl restart network

查看本机 ip,出现 ip 说明配置成功

1
ifconfig eth0
1
2
3
4
5
6
7
8
9
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr FE:FC:FE:4D:5C:36
inet addr:10.130.6.180 Bcast:10.130.255.255 Mask:255.255.0.0
inet6 addr: fe80::e21e:f543:a6de:be92/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4105467 errors:165000 dropped:0 overruns:0 frame:165000
TX packets:24777 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:356007648 (339.5 MiB) TX bytes:49129900 (46.8 MiB)

查看路由配置,出现网关说明配置成功

1
route -n
1
2
3
4
5
6
7
8
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.130.6.1 0.0.0.0 UG 100 0 0 eth0
10.130.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-2e21f71c782e
172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-1645c3e61b32

配置 DNS

1
echo "nameserver 8.8.8.8" >> /etc/resolv.conf