一、网络服务、配置文件
二、ifconfig常用命令
三、route常用命令
四、ip命常用命令
五、其他
一、网络服务、配置文件
修改网络服务配置文件不会立即生效,重启网络服务或主机后生效且永久有效
1、配置文件
1.1、网络配置文件:/etc/sysconfig/network
[root@TESTHOST ~]# cat /etc/sysconfig/network
NETWORKING=yes #是否启用主机网络功能
HOSTNAME=TESTHOST #设置主机名,不是立即生效,重启主机后永久有效
GATEWAY=10.1.1.1 #设置全局网关
设置主机名命令:hostname HOSTNAME #立即生效,不是永久有效
[root@TESTHOST ~]# hostname
TESTHOST
[root@TESTHOST ~]# hostname PHPSER1
[root@TESTHOST ~]# hostname
PHPSER1
[root@TESTHOST ~]# echo $HOSTNAME
TESTHOST
1.2、网络接口配置文件:/etc/sysconfig/neteork-scripts/ifcfg-INTERFACE_NAME
[root@TESTHOST ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 #表示关联的设备,要与文件名的后半部保持一致
TYPE=Ethernet #网络协议类型为以太网
UUID=c73ea562-20ef-48c6-bdc0-77ea9ab6053b #设备ID号
ONBOOT=yes #开机时是否激活此网络接口
NM_CONTROLLED=no #NetworkManager服务是否启用,一般禁用
BOOTPROTO=none #引导协议,要使用静态地址,使用static或none,dhcp表示使用DHCP服务器获取地址;{static | none | dhcp | bootp}
HWADDR=00:0C:29:89:18:58 #MAC地址
IPADDR=10.1.1.107 #IP地址
PREFIX=8 #同NETMASK子网掩码
GATEWAY=10.1.1.1 #网关地址
DNS1=120.196.165.24 #DNS地址
DEFROUTE=yes #是否使用默认路由
IPV4_FAILURE_FATAL=yes #是否启用IPv4
IPV6INIT=no #是否启用IPv6
NAME="System eth0"
USERCTL=no #是否允许普通用户控制此接口
PEERDNS=no #是否在BOOTPROTO为dhcp时接受由DHCP服务器指定的DNS地址
1.3、主机路由配置文件:/etc/sysconfig/network-scripts/route-eth#,格式如下
# DEST via NEXTHOP
10.0.0.0/8 via 192.168.0.1
或
ADDRESS0=10.0.0.0
NETMASK0=255.0.0.0
GATEWAY0=192.168.0.1
ADDRESS1=
NETMASK1=
GATEWAY1=
1.4、DNS服务器配置文件: /etc/resolv.conf #启用DHCP时,网络接口文件中PEERDNS如果不设置为no,DHCP会自动修改resolv.conf中的配置
# nameserver DNS_IP_1
# nameserver DNS_IP_2
# nameserver DNS_IP_3
nameserver 120.196.165.24
1.5、本地解析配置文件:/etc/hosts #文件修改后,重启主机后生效
#主机地址 主机名称 主机别名(可省)
127.0.0.1 www.test.org TEST.local
1.6、定义网卡设备名配置文件:RHEL5中的/etc/modprobe.conf文件;RHEL6中的/etc/udev/rules.d/70-persistent-net.rules文件
2、网络服务程序
RHEL5:/etc/init.d/network { start | stop | restart | status } #/etc/rc.d/init.d/network为同一文件
RHEL6:常用/etc/init.d/network { start | stop | restart | status } #/etc/rc.d/init.d/network为同一文件
/etc/init.d/NetworkManager { start | stop | restart | status } #此服务默认开启,生产环境中通常禁用该服务,因为虚拟化需要使用桥接,此服务不支持
禁用NetworkManager
[root@TESTHOST ~]# chkconfig NetworkManager off
3、服务相关命令
3.1、查询网卡状态
[root@localhost ~]# service network status #或/etc/init.d/network status
Configured devices: #配置设备
lo eth0 eth1
Currently active devices: #处于活动状态的设备
lo eth0 eth1
3.2、关闭或激活网卡
[root@localhost ~]# ifdown eth0 #关闭eth0,同ifconfig eth0 down
[root@localhost ~]# ifup eth0 #激活eth0,同ifconfig eth0 up
3.3、启动、关闭、重启网络服务
[root@TESTHOST ~]# service network start #启动网络服务,同/etc/init.d/network start
[root@TESTHOST ~]# service network stop #关闭网络服务,同/etc/init.d/network stop
[root@TESTHOST ~]# service network restart #重启网络服务,同/etc/init.d/network restart
二、ifconfig常用命令
ifconfig -a:查看所有网卡配置信息
ifconfig [eth#] 查看网卡配置信息
ifconfig ethX IP/MASK [up | down]:临时配置网卡信息,重启网络服务或主机会失效
[root@TESTHOST ~]# ifconfig eth1 #查看eth1信息
eth1 Link encap:Ethernet HWaddr 00:0C:29:89:18:62
inet addr:10.1.1.108 Bcast:10.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe89:1862/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:269 errors:0 dropped:0 overruns:0 frame:0
TX packets:89 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:18108 (17.6 KiB) TX bytes:7432 (7.2 KiB)
[root@TESTHOST ~]# ifconfig eth1 10.1.1.109/16 #临时配置eth1的信息
[root@TESTHOST ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:89:18:62
inet addr:10.1.1.109 Bcast:10.1.255.255 Mask:255.255.0.0 #此处看到eth1信息已更改
inet6 addr: fe80::20c:29ff:fe89:1862/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:295 errors:0 dropped:0 overruns:0 frame:0
TX packets:100 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:19990 (19.5 KiB) TX bytes:9098 (8.8 KiB)
----------------------------------------------------
ifconfig直接使用可以查看主机上处于活动状态网卡的信息
[root@TESTHOST ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:89:18:58
inet addr:10.1.1.107 Bcast:10.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::20c:29ff:fe89:1858/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:98689 errors:0 dropped:0 overruns:0 frame:0
TX packets:3800 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6246172 (5.9 MiB) TX bytes:292620 (285.7 KiB)
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:16436 Metric:1
RX packets:1362 errors:0 dropped:0 overruns:0 frame:0
TX packets:1362 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:74910 (73.1 KiB) TX bytes:74910 (73.1 KiB)
----------------------------------------------------
ifconfig相关信息
lo #local简称,常指本地回环设备
eth# #ethernet简称,以太网网卡
Link encap #二层网络协议
HWaddr #mac地址
inet addr #IP地址
Bcast #广播地址
Mask #子网掩码
inet6 addr #IPv6地址
Scope #作用范围,glob表示全局,Link表示设备本身
UP #表示启用状态
BROADCAST #表示允许广播
RUNNING #表示运行状态
MULTICAST #表示允许多播
MTU #最大传输单元
Metric #度量
RX packets #接收到报文的个数
TX packets #传送出的报文个数
errors #错误
dropped #丢弃
overruns #溢出
frame #帧数
collisions #冲突
txqueuelen #传输队列长度
RX bytes #收到报文的字节数
TX bytes #传送出报文的字节数
Interrupt #中断号
三、route常用命令
route:命令本身可以查看路由表
route –n:以数字方式显示主机或端口等相关信息
子命令:临时配置路由信息,重启网络服务或主机会失效
add:route add –net | –host DEST gw NEXTHOP,添加路由信息
-host 主机路由
-net 网络路由
-net:0.0.0.0 添加默认路由
del:route del –net | –host DEST [gw NEXTHOP] ,删除路由信息
-host 主机路由
-net 网络路由
----------------------------------------------------
route:查看本地路由表
[root@TESTHOST ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.1.0.0 * 255.255.0.0 U 0 0 0 eth1
link-local * 255.255.0.0 U 1002 0 0 eth0
10.0.0.0 * 255.0.0.0 U 0 0 0 eth0
default 10.1.1.1 0.0.0.0 UG 0 0 0 eth0
路由信息中flag的含义
U 表示该条路由处于启动状态
G 该路由是到一个网关(路由器);如果没有设置该标志,说明目的地是直接相连的,是本地路由
H 该路由是到一个主机,也就是说,目的地址是一个完整的主机地址。如果没有设置该标志,说明该路由是到一个网
络,而目的地址是一个网络地址、一个网络号,或者网络号与子网号的组合
D 该路由是由重定向报文创建的
M 该路由已被重定向报文修改
----------------------------------------------------
route –n:以数字方式显示主机或端口等相关信息
[root@TESTHOST ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.1.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
10.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth0
0.0.0.0 10.1.1.1 0.0.0.0 UG 0 0 0 eth0
----------------------------------------------------
route add –net | –host DEST gw NEXTHOP #添加路由信息,route add default gw NEXTHOP为添加默认路由
[root@TESTHOST ~]# route add -net 10.0.0.0/8 gw 10.1.1.1
[root@TESTHOST ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.1.0.0 * 255.255.0.0 U 0 0 0 eth1
link-local * 255.255.0.0 U 1002 0 0 eth0
10.0.0.0 10.1.1.1 255.0.0.0 UG 0 0 0 eth1
10.0.0.0 * 255.0.0.0 U 0 0 0 eth0
default 10.1.1.1 0.0.0.0 UG 0 0 0 eth0
----------------------------------------------------
route del –net | –host DEST [gw NEXTHOP] #删除路由信息
[root@TESTHOST ~]# route #查看主机路由表
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.1.0.0 * 255.255.0.0 U 0 0 0 eth1
link-local * 255.255.0.0 U 1002 0 0 eth0
10.0.0.0 10.1.1.1 255.0.0.0 UG 0 0 0 eth1
10.0.0.0 * 255.0.0.0 U 0 0 0 eth0
default 10.1.1.1 0.0.0.0 UG 0 0 0 eth0
[root@TESTHOST ~]# route del -net 10.0.0.0/8 #删除路由信息,gw NEXTHOP可省略
[root@TESTHOST ~]# route #再次查看,其中一条路由信息已删除
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.1.0.0 * 255.255.0.0 U 0 0 0 eth1
link-local * 255.255.0.0 U 1002 0 0 eth0
10.0.0.0 * 255.0.0.0 U 0 0 0 eth0
default 10.1.1.1 0.0.0.0 UG 0 0 0 eth0
四、ip命令常用
子命令:
link:网络接口属性
addr:协议地址
route:路由
1、link
show:ip [–s] link show DEV #显示网卡相关属性
set:ip link set DEV{up | down} #设置网卡工作状态,启用或关闭,同ifdown、ifup
ip [-s] link show #显示网卡相关属性
[root@TESTHOST ~]# ip link show #没有加-s选项,显示网卡设备基本信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:89:18:58 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:89:18:62 brd ff:ff:ff:ff:ff:ff
[root@TESTHOST ~]# ip -s link show #加了选项-s,额外显示网卡设备的流量信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
83050 1510 0 0 0 0
TX: bytes packets errors dropped carrier collsns
83050 1510 0 0 0 0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:89:18:58 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
6847173 108424 0 0 0 0
TX: bytes packets errors dropped carrier collsns
292620 3800 0 0 0 0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:89:18:62 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
7398263 115255 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1227674 16952 0 0 0 0
You have new mail in /var/spool/mail/root
2、addr
add:ip addr add ADDRESS dev DEV #添加IP地址信息
del:ip addr del ADDRESS dev DEV #删除IP地址信息
show:ip addr show dev DEV to PREFIX #查看IP地址信息
flush:ip addr flush dev DEV to PREFIX #清除IP地址信息,可批量
add:
ip addr add ADDRESS dev DEV #添加IP地址信息
[root@TESTHOST ~]# ip addr add 10.1.1.109/16 dev eth0
RTNETLINK answers: File exists
[root@TESTHOST ~]# ping 10.1.1.109 #添加成功
PING 10.1.1.109 (10.1.1.109) 56(84) bytes of data.
64 bytes from 10.1.1.109: icmp_seq=1 ttl=64 time=0.018 ms
del:
ip addr del ADDRESS dev DEV #删除IP地址信息
[root@TESTHOST ~]# ip addr del 10.1.1.109/16 dev eth0
[root@TESTHOST ~]# ping 10.1.1.109 #删除后已ping不通
PING 10.1.1.109 (10.1.1.109) 56(84) bytes of data.
From 10.1.1.108 icmp_seq=2 Destination Host Unreachable
show:
ip addr show dev DEV to PREFIX #查看IP地址信息
[root@TESTHOST ~]# ip addr show dev eth0 #查看指定设备ip信息
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:89:18:58 brd ff:ff:ff:ff:ff:ff
inet 10.1.1.107/8 brd 10.255.255.255 scope global eth0
inet 172.16.200.33/16 brd 172.16.255.255 scope global eth0:0
inet 192.168.1.236/8 scope global eth0:1
inet6 fe80::20c:29ff:fe89:1858/64 scope link
valid_lft forever preferred_lft forever
[root@TESTHOST ~]# ip addr show dev eth0 to 192.168/8 #仅查看设备对应ip信息
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.1.236/8 scope global eth0:1
flush:
ip addr flush dev DEV to PREFIX #清除IP地址信息,可批量
[root@TESTHOST ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:89:18:58 brd ff:ff:ff:ff:ff:ff
inet 10.1.1.107/8 brd 10.255.255.255 scope global eth0
inet 172.16.200.33/16 brd 172.16.255.255 scope global eth0:0
inet 192.168.1.236/8 scope global eth0:1
inet 172.16.212.14/16 scope global secondary eth0
inet6 fe80::20c:29ff:fe89:1858/64 scope link
valid_lft forever preferred_lft forever
[root@TESTHOST ~]# ip addr show eth0 #批量删除前后缀为172/16的IP地址信息
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:89:18:58 brd ff:ff:ff:ff:ff:ff
inet 10.1.1.107/8 brd 10.255.255.255 scope global eth0
inet 192.168.1.236/8 scope global eth0:1
inet6 fe80::20c:29ff:fe89:1858/64 scope link
valid_lft forever preferred_lft forever
[root@TESTHOST ~]#
3、route
add:ip route add DEST dev DEV via NEXTHOP #添加路由信息
del:ip route del DEST dev DEV [via NEXTHOP] #删除路由信息
show:ip route show DEST #显示路由信息表
flush:ip route flush DEST #清除路由信息表,可批量
add:
ip route add DEST dev DEV via NEXTHOP #添加路由信息
[root@TESTHOST ~]# ip route add 172.0.0.0/24 dev eth0 via 192.168.16.1
[root@TESTHOST ~]# ip route show 172.0.0.0/24 #显示路由信息表
172.0.0.0/24 via 192.168.16.1 dev eth0
del:
ip route del DEST dev DEV [via NEXTHOP] #删除路由信息
[root@TESTHOST ~]# ip route del 172.0.0.0/24 dev eth0
You have new mail in /var/spool/mail/root
[root@TESTHOST ~]# ip route show 172.0.0.0/24 #路由信息已经删除
[root@TESTHOST ~]#
show:
ip route show DEST #显示路由信息表
[root@TESTHOST ~]# ip route show
10.0.0.0/16 via 172.16.0.1 dev eth0
169.254.0.0/16 dev eth1 scope link metric 1003
192.0.0.0/8 dev eth1 proto kernel scope link src 192.168.1.236
192.0.0.0/8 dev eth0 proto kernel scope link src 192.168.1.236
10.0.0.0/8 dev eth1 proto kernel scope link src 10.1.1.108
10.0.0.0/8 dev eth0 proto kernel scope link src 10.1.1.107
flush:
ip route flush DEST #清除路由信息表,可批量
[root@TESTHOST ~]# ip route show
10.0.0.0/16 via 172.16.0.1 dev eth0
169.254.0.0/16 dev eth1 scope link metric 1003
192.0.0.0/8 dev eth1 proto kernel scope link src 192.168.1.236
192.0.0.0/8 dev eth0 proto kernel scope link src 192.168.1.236
10.0.0.0/8 dev eth1 proto kernel scope link src 10.1.1.108
10.0.0.0/8 dev eth0 proto kernel scope link src 10.1.1.107
[root@TESTHOST ~]# ip route flush 192/8 #清除符合条件的路由信息
[root@TESTHOST ~]# ip route show
10.0.0.0/16 via 172.16.0.1 dev eth0
169.254.0.0/16 dev eth1 scope link metric 1003
10.0.0.0/8 dev eth1 proto kernel scope link src 10.1.1.108
10.0.0.0/8 dev eth0 proto kernel scope link src 10.1.1.107
五、其他
1、给同一网卡配置多个ip时,启用别名
例如给eth0网卡一个别名eth0:0
[root@TESTHOST ~]# ifconfig eth0:0 192.168.1.236/8
[root@TESTHOST ~]# ifconfig eth0:0
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:89:18:58
inet addr:172.16.200.33 Bcast:172.16.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
或
[root@TESTHOST ~]# ip addr add 192.168.1.236/8 dev eth0 label eth0:1 #使用label指定别名
[root@TESTHOST ~]# ifconfig eth0:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:89:18:58
inet addr:192.168.1.236 Bcast:0.0.0.0 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
同样,命令是立即生效,但重启服务或主机即失效;需要配置永久有效需要添加配置文件/etc/sysconfig/network-scripts/ifg-eth0:0
2、网络配置图形界面:
RHEL5:
system-config-network-tui文本图形界面
system-config-network-gui纯图形界面
通常使用setup命令
3.转载陈明乾关于网络配置的博文
转载于:https://blog.51cto.com/64314491/1640448