2013年3月3日



网络管理



一、IP地址



1、查看IP地址



[root@desktop7 ~]# ifconfig eth0 |head -n 5


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:81 errors:0 dropped:0 overruns:0 frame:0


          TX packets:104 errors:0 dropped:0 overruns:0 carrier:0



2、修改IP地址



[root@desktop7 ~]# ifconfig eth0 192.168.0.211  


[root@desktop7 ~]# ifconfig eth0 |head -n 5


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet addr:192.168.0.211  Bcast:192.168.0.255  Mask:255.255.255.0


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0



3、修改子网掩码



[root@desktop7 ~]# ifconfig eth0 192.168.0.211 netmask 255.255.0.0


[root@desktop7 ~]# ifconfig eth0 |head -n 3


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet addr:192.168.0.211  Bcast:192.168.255.255  Mask:255.255.0.0


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link



为便于验证,改为另一网段



[root@desktop7 ~]# ifconfig eth0 192.168.0.211 netmask 255.255.255.0


[root@desktop7 ~]# ifconfig eth0 |head -n 3


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet addr:192.168.0.211  Bcast:192.168.0.255  Mask:255.255.255.0


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


[root@desktop7 ~]#



4、关闭网卡



关闭前



[root@desktop7 ~]# ifconfig eth0 |head -n 5


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet addr:192.168.0.211  Bcast:192.168.0.255  Mask:255.255.255.0


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0



关闭网卡



[root@desktop7 ~]# ifconfig eth0 down



关闭后



[root@desktop7 ~]# ifconfig eth0 |head -n 5


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet addr:192.168.0.211  Bcast:192.168.0.255  Mask:255.255.255.0


          BROADCAST MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0


          TX packets:214 errors:0 dropped:0 overruns:0 carrier:0


[root@desktop7 ~]#



5、打开网卡



[root@desktop7 ~]# ifconfig eth0 up



[root@desktop7 ~]# ifconfig eth0 |head -n 5


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet addr:192.168.0.211  Bcast:192.168.0.255  Mask:255.255.255.0


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0


[root@desktop7 ~]#



6、简单方式关闭、打开网卡



(1)关闭网卡



[root@desktop7 ~]# ifdown eth0



[root@desktop7 ~]# ifconfig eth0 |head -n 5


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          BROADCAST MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0


          TX packets:241 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:1000



(2)打开网卡



[root@desktop7 ~]# ifup eth0



[root@desktop7 ~]# ifconfig eth0 |head -5


eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0


          TX packets:257 errors:0 dropped:0 overruns:0 carrier:0


[root@desktop7 ~]#



7、查看本机所有网卡(NIC)信息



[root@desktop7 ~]# ifconfig -a


br0       Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet addr:192.168.0.7  Bcast:192.168.0.255  Mask:255.255.255.0


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0


          TX packets:125 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:0


          RX bytes:37321 (36.4 KiB)  TX bytes:22797 (22.2 KiB)



eth0      Link encap:Ethernet  HWaddr 50:46:5D:71:C2:F1  


          inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:342 errors:0 dropped:0 overruns:0 frame:0


          TX packets:257 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:1000


          RX bytes:42133 (41.1 KiB)  TX bytes:60062 (58.6 KiB)


          Interrupt:233 Base address:0x6000



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:2742 errors:0 dropped:0 overruns:0 frame:0


          TX packets:2742 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:0


          RX bytes:4120944 (3.9 MiB)  TX bytes:4120944 (3.9 MiB)



sit0      Link encap:IPv6-in-IPv4  


          NOARP  MTU:1480  Metric:1


          RX packets:0 errors:0 dropped:0 overruns:0 frame:0


          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:0


          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)



virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00  


          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:0 errors:0 dropped:0 overruns:0 frame:0


          TX packets:20 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:0


          RX bytes:0 (0.0 b)  TX bytes:3617 (3.5 KiB)



vnet0     Link encap:Ethernet  HWaddr FE:54:00:00:00:CF  


          inet6 addr: fe80::fc54:ff:fe00:cf/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:6 errors:0 dropped:0 overruns:0 frame:0


          TX packets:305 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:500


          RX bytes:384 (384.0 b)  TX bytes:47407 (46.2 KiB)



vnet1     Link encap:Ethernet  HWaddr FE:54:00:00:00:6B  


          inet6 addr: fe80::fc54:ff:fe00:6b/64 Scope:Link


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


          RX packets:6 errors:0 dropped:0 overruns:0 frame:0


          TX packets:305 errors:0 dropped:0 overruns:0 carrier:0


          collisions:0 txqueuelen:500


          RX bytes:384 (384.0 b)  TX bytes:47226 (46.1 KiB)



[root@desktop7 ~]#



二、网络服务



1、查看网络状态



[root@desktop7 ~]# service network sdatus


Usage: /etc/init.d/network {start|stop|restart|reload|status}



[root@desktop7 ~]# /etc/init.d/network status


Configured devices:


lo br0 eth0


Currently active devices:


lo eth0 br0 virbr0 vnet0 vnet1


[root@desktop7 ~]#



2、停止网络服务



[root@desktop7 ~]# service network stop


Shutting down interface br0:                               [  OK  ]


Shutting down interface eth0:                              [  OK  ]


Shutting down loopback interface:                          [  OK  ]


Disabling IPv4 packet forwarding:  net.ipv4.ip_forward = 0


                                                           [  OK  ]


查看网络状态



[root@desktop7 ~]# service network status


Configured devices:


lo br0 eth0


Currently active devices:


virbr0 vnet0 vnet1


[root@desktop7 ~]#



3、重启网络服务



[root@desktop7 ~]# service network restart


Shutting down loopback interface:                          [  OK  ]


Bringing up loopback interface:                            [  OK  ]


Bringing up interface eth0:                                [  OK  ]


Bringing up interface br0:  


Determining IP information for br0... done.


                                                           [  OK  ]



查看网络状态



[root@desktop7 ~]# service network status


Configured devices:


lo br0 eth0


Currently active devices:


lo eth0 br0 virbr0 vnet0 vnet1


[root@desktop7 ~]#



三、网关



1、查看网关状态



[root@desktop7 ~]# route


Kernel IP routing table


Destination     Gateway         Genmask         Flags Metric Ref    Use Iface


192.168.0.0     *               255.255.255.0   U     0      0        0 br0


192.168.122.0   *               255.255.255.0   U     0      0        0 virbr0


169.254.0.0     *               255.255.0.0     U     0      0        0 br0


default         192.168.0.254   0.0.0.0         UG    0      0        0 br0



用数字显示



[root@desktop7 ~]# route -n


Kernel IP routing table


Destination     Gateway         Genmask         Flags Metric Ref    Use Iface


192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 br0


192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0


169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 br0


0.0.0.0         192.168.0.254   0.0.0.0         UG    0      0        0 br0


[root@desktop7 ~]#



2、删除多余网关



[root@desktop7 ~]# route del -net 169.254.0.0/16


[root@desktop7 ~]# route del -net 192.168.122.0/24


[root@desktop7 ~]# route del default gw 192.168.0.254



查看状态



[root@desktop7 ~]# route -n


Kernel IP routing table


Destination     Gateway         Genmask         Flags Metric Ref    Use Iface


192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 br0


[root@desktop7 ~]#



3、新增网关



(1)添加默认网关



[root@desktop7 ~]# route add default gw 192.168.0.253



(2)添加172.16.0.0/16网段网关



[root@desktop7 ~]# route add -net 172.16.0.0/16 gw 192.168.0.11



(3)添加10.0.0.0/8网段网关



[root@desktop7 ~]# route add -net 10.0.0.0/8 gw 192.168.0.22



查看状态



[root@desktop7 ~]# route -n


Kernel IP routing table


Destination     Gateway         Genmask         Flags Metric Ref    Use Iface


192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 br0


172.16.0.0      192.168.0.11    255.255.0.0     UG    0      0        0 br0


10.0.0.0        192.168.0.22    255.0.0.0       UG    0      0        0 br0


0.0.0.0         192.168.0.253   0.0.0.0         UG    0      0        0 br0


[root@desktop7 ~]#



(4)最后删除多余网段路由



[root@desktop7 ~]# route del -net 172.16.0.0/16


[root@desktop7 ~]# route del -net 10.0.0.0/8


[root@desktop7 ~]# route del default gw 192.168.0.253



查看状态



[root@desktop7 ~]# route -n


Kernel IP routing table


Destination     Gateway         Genmask         Flags Metric Ref    Use Iface


192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 br0


[root@desktop7 ~]#



四、网关配置文件



[root@desktop7 ~]# vi /etc/sysconfig/network-scripts/route-eth0



GATEWAY0=192.168.0.253


NETMASK0=0.0.0.0


DDRESS0=0.0.0.0



GATEWAY1=192.168.0.254


NETMASK1=255.255.0.0


ADDRESS1=172.16.0.0


             


另外一种书写方式



[root@desktop211 ~]# vim /etc/sysconfig/network-scripts/route-eth0


172.16.0.0/16 via 192.168.0.254


0.0.0.0 via 192.168.0.253



五、DNS配置文件



[root@desktop7 ~]# vi /etc/resolv.conf



; generated by /sbin/dhclient-script


search example.com


nameserver 202.106.0.20


nameserver 8.8.8.8



六、主机名



1、查看主机名



[root@desktop7 ~]# hostname


desktop7.example.com



2、修改主机名



[root@desktop7 ~]# hostname desktop211.example.com



查看修改结果



[root@desktop7 ~]# hostname


desktop211.example.com


[root@desktop7 ~]#


 


注销,主机名生效



[root@desktop211 ~]# hostname


desktop211.example.com


[root@desktop211 ~]#



改回原主机名



[root@desktop211 ~]# hostname desktop7.example.com


[root@desktop211 ~]#



[root@desktop7 ~]# hostname


desktop7.example.com


[root@desktop7 ~]#



3、主机名配置文件



[root@desktop7 ~]# vi /etc/sysconfig/network


NETWORKING=yes


NETWORKING_IPV6=yes


HOSTNAME=desktop7.example.com



注意:未修改配置文件,单纯用命令修改主机名注销后虽然能生效,但重启主机后仍为原主机名



[root@desktop211 ~]# hostname


desktop211.example.com


[root@desktop211 ~]# vi /etc/sysconfig/network


NETWORKING=yes


NETWORKING_IPV6=yes


HOSTNAME=desktop7.example.com



反之,只修改配置文件,不用命令修改主机名,重启主机后新主机名生效



七、IP



IP配置文件



[root@desktop211 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0


DEVICE=eth0


BOOTPROTO=dhcp


ONBOOT=yes


BRIDGE=br0


NETMASK=255.255.255.0


GATEWAY=192.168.0.253


DNS1=202.106.0.20


DNS2=8.8.8.8