linux配置网络的方法 linux使用配置文件配置网络


linux配置网络的方法 linux使用配置文件配置网络

文章插图
1.Centos 6网络配置
? 查看 IP 地址
[root@localhost ~]# ifconfigeth0Link encap:EthernetHWaddr FA:79:75:1E:74:00inet addr:10.154.0.8Bcast:10.154.0.255Mask:255.255.255.0inet6 addr: fe80::f879:75ff:fe1e:7400/64 Scope:LinkUP BROADCAST RUNNING MULTICASTMTU:1500Metric:1RX packets:176883 errors:0 dropped:0 overruns:0 frame:0TX packets:112967 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:18585161 (17.7 MiB)TX bytes:18936319 (18.0 MiB)loLink encap:Local Loopbackinet addr:127.0.0.1Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNINGMTU:65536Metric:1RX packets:132 errors:0 dropped:0 overruns:0 frame:0TX packets:132 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0RX bytes:15346 (14.9 KiB)TX bytes:15346 (14.9 KiB)
? 修改 IP 地址
[root@bjdb4 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0#网卡驱动名称 TYPE=EthernetUUID=4c0289bd-ca9b-433c-85bf-8b593d5b4c42#网卡 UUID 号 ONBOOT=yes#网卡开启状态 NM_CONTROLLED=yesBOOTPROTO=static#把 DHCP 状态修改城 staticHWADDR=28:80:23:B8:B4:88#mac 地址 IPADDR=172.18.1.40#IP 地址 PREFIX=24#子网掩码位数 GATEWAY=172.18.1.254#网关 DEFROUTE=yesIPV4_FAILURE_FATAL=yesIPV6INIT=noNAME=\"System eth0\"#网卡名称
? 修改 DNS
[root@1234 ~]$ vi /etc/resolv.conf# Generated by NetworkManager# No nameservers found; try putting DNS servers into your# ifcfg files in /etc/sysconfig/network-scripts like so:## DNS1=xxx.xxx.xxx.xxx# DNS2=xxx.xxx.xxx.xxx# DOMAIN=lab.foo.com bar.foo.comnameserver 202.198.98.167#修改为当地运营商的 DNS 地址 nameserver 8.8.8.8
? 修改网关地址
[root@1234 ~]$ vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=1234#主机名 GATEWAY=172.18.1.254#网关地址
? 静态路由设置(根据需要配置)
– 查看静态路由
[root@1234 ~]# route -nKernel IP routing tableDestinationGatewayGenmaskFlags Metric RefUse Iface172.18.1.00.0.0.0255.255.255.0U000 eth0169.254.0.00.0.0.0255.255.0.0U100200 eth00.0.0.0172.18.1.2540.0.0.0UG000 eth0
– 设置永久静态路由
【linux配置网络的方法 linux使用配置文件配置网络】 [root@1234 ~]# vi /etc/rc.local#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don\'t# want to do the full Sys V style init stuff.touch /var/lock/subsys/localroute add -net 192.168.3.0/24 dev eth0#新增路由地址段与子网掩码,出口网卡名称 route add -net 192.168.2.0/24 gw 192.168.2.254#新增路由地址段与子网掩码,网关地址 [jqsoft@bjdb4 ~]$ vi /etc/sysconfig/static-routes any net 192.168.3.0/24 gw 192.168.3.254#新增路由地址段与子网掩码,出口网关地址 any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129#新增路由地址段与子网掩码,出口网关地址 [root@1234 ~]# vi /etc/sysconfig/network-script/route-eth0#默认格式:network/prefix via gateway dev intf0.0.0.0/0 via 172.16.10.2 dev eth0#新增路由
? 重启网卡
[root@1234 ~]#service network restart
2.Centos 7 网络配置
? 查看 IP 地址
[root@localhost ~]# ifconfigens33: flags=4163mtu 1500inet 192.168.217.134netmask 255.255.255.0broadcast 192.168.217.255inet6 fe80::d7cc:dd17:7991:6c64prefixlen 64scopeid 0x20ether 00:0c:29:03:62:63txqueuelen 1000(Ethernet)RX packets 3455bytes 248841 (243.0 KiB)RX errors 0dropped 0overruns 0frame 0TX packets 637bytes 54026 (52.7 KiB)TX errors 0dropped 0 overruns 0carrier 0collisions 0lo: flags=73mtu 65536inet 127.0.0.1netmask 255.0.0.0inet6 ::1prefixlen 128scopeid 0x10looptxqueuelen 1(Local Loopback)RX packets 68bytes 5908 (5.7 KiB)RX errors 0dropped 0overruns 0frame 0TX packets 68bytes 5908 (5.7 KiB)TX errors 0dropped 0 overruns 0carrier 0collisions 0
? 修改 IP 地址
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33TYPE=\"Ethernet\"BOOTPROTO=\"static\"#把 DHCP 修改成 staticDEFROUTE=\"yes\"PEERDNS=\"yes\"PEERROUTES=\"yes\"IPV4_FAILURE_FATAL=\"no\"NAME=\"ens33\"UUID=\"8c7abe35-e956-4d21-bed2-31a92cbd17ea\"DEVICE=\"ens33\"ONBOOT=\"yes\"#网卡状态 IPADDR=192.168.0.230 #静态 IPGATEWAY=192.168.0.1 #默认网关NETMASK=255.255.255.0 #子网掩码DNS1=192.168.0.1 #DNS 配置
? 修改 DNS
[root@1234 ~]# vi /etc/resolv.conf# Generated by NetworkManager# No nameservers found; try putting DNS servers into your# ifcfg files in /etc/sysconfig/network-scripts like so:## DNS1=xxx.xxx.xxx.xxx# DNS2=xxx.xxx.xxx.xxx# DOMAIN=lab.foo.com bar.foo.comnameserver 202.198.98.167#修改为当地运营商的 DNS 地址 nameserver 8.8.8.8
? 修改网关
[root@centos]# vi /etc/sysconfig/networkNETWORKING=yesHOSTNAME=1234#主机名 GATEWAY=172.18.1.254#网关地址
? 配置静态路由(根据需要配置)