一、网络配置

linux 的网络是属于内核的,所以可以在linux主机上设置多个网段的ip地址,在ping操作的时候都会响应。

1、网络接口类型:

    lo:本地回环

    

    以太网网卡:ethx

        centos6.0以上版本修改以太网卡名称:

[root@localhost ~]# more /etc/udev/rules.d/70-persistent-net.rules 


# This file was automatically generated by the /lib/udev/write_net_rules


# program, run by the persistent-net-generator.rules rules file.


#


# You can modify it, as long as you keep each rule on a single


# line, and change only the value of the NAME= key.




# PCI device 0x8086:0x100f (e1000)


SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:43:67:3


5", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"


    配置网卡命令:

        ifconfig 

            -a:显示所有网卡信息

    激活、启动网卡

[root@localhost ~]# ifconfig eth1 down

    配置网卡ip地址

[root@localhost ~]# ifconfig eth0 add 10.40.0.139 255.255.255.0

    

    配置路由

        route

            add:添加路由

                -host:添加主机路由,到具体主机

                -net:添加网络路由,到一个网络

[root@localhost ~]# route add -net 10.40.0.0/24 gw 10.40.0.200

        添加默认路由

[root@localhost ~]# route add default gw 10.40.0.200

            del:删除路由

[root@localhost ~]# route del -net 10.40.0.0/24 gw 10.40.0.200

      

网络配置文件:

/etc/sysconfig/network

网络接口配置文件

/etc/sysconfig/network-scripts/ifcfg-ethx

/etc/sysconfig/network-scripts/ifcfg-ethx

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0  DEVICE=eth0                      ##名称要和文件名后缀相同 HWADDR=00:0C:29:B2:45:39         ##mac地址 TYPE=Ethernet                    ##网络类型 UUID=da6b9574-62a5-4e25-9f9c-0628e6fae5a9 ONBOOT=yes                        ##开机激活 NM_CONTROLLED=yes BOOTPROTO=static                  ##设置ip地址分配方式,静态使用static或者none IPADDR=10.40.0.230 NETMASK=255.255.255.0 GATEWAY=10.40.0.200 DNS1=114.114.114.114

usectl=yes                ##是否允许普通用户控制此接口

peerdns=yes            ##是否允许动态接收dhcp服务器分配的DNS地址

配置子接口方法:

方法一:

[root@localhost ~]# ifconfig eth0:0 192.168.1.2/24

[root@localhost ~]# ifconfig eth0:0


eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:43:67:35  


          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0


          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1


方法二:

    创建配置文件ifcfg-eth0:0,可以复制一份ifcfg-eth0,并修改完成。

    

2、路由表配置文件:

/etc/sysconfig/network-scripts/route-ethx

配置文件格式:

格式一:

    dest via nexthop

    

格式二:

    ADDRESS0=

    NETMASK0=

    GATEWAY0=

创建并重启network服务

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

[root@localhost ~]# service network restart

查看配置文件及路由表

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

8.8.8.8 via 10.40.0.200

ADDRESS0=192.168.1.0 ##0表示组名称,创建另一组使用1

NETMASK0=255.255.255.0

GATEWAY=10.40.0.208

[root@localhost ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

10.40.0.0       *               255.255.255.0   U     1      0        0 eth0

default         10.40.0.200     0.0.0.0         UG    0      0        0 eth0

3、DNS服务器指定方法:

    配置文件:/etc/resolv.conf

        nameserver DNS_IP_1

        nameserver  DNS_IP_2

[root@localhost ~]# more /etc/resolv.conf 


# Generated by NetworkManager


nameserver 8.8.8.8


注意:如果不允许dhcp服务器修改此文件的dns服务器地址,需要设置PEERDNS=no

4、指定解析本机服务器或者非公网注册的服务器,类似windows的hosts。

    配置文件:/etc/hosts

        主机ip 主机名 主机别名(可省略)

[root@localhost ~]# more /etc/hosts


127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4


::1         localhost localhost.localdomain localhost6 localhost6.localdom

测试:

[root@localhost ~]# ping gongbing.com.cn

PING gongbing.com.cn (10.40.0.230) 56(84) bytes of data.

64 bytes from gongbing.com.cn (10.40.0.230): icmp_seq=1 ttl=64 time=0.029 ms

5、修改主机名

配置文件:/etc/sysconfig/network,需要重启

NETWORKING=yes ##是否启用网络主机功能

HOSTNAME=gongbing.virtual

NETWORKING_IPV6= ##设置是否启用ipv6,没有实质意义。

立即生效:

[root@localhost ~]# hostname gongbing.virtual

6、网络配置命令 ip-替代ifconfig

    link:配置物理网络接口属性

        show:显示网络接口信息

        -s:显示统计信息

[root@localhost ~]# ip -s link  show eth0

2: eth0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:b2:45:39 brd ff:ff:ff:ff:ff:ff

    RX: bytes  packets  errors  dropped overrun mcast   

    5790639221 12215034 0       0       0       0       

    TX: bytes  packets  errors  dropped carrier collsns 

    15619359   124582   0       0       0       0       

        set:设置设备激活、mtu、混杂模式、等。

[root@localhost ~]# ip link set eth1 down ##关闭接口

    addr:网络协议地址

        add:添加

[root@localhost ~]# ip addr add dev eth0 label eth0:2 192.11.1.1/24 

        show:查看

[root@localhost ~]# ip link show 

2: eth0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:b2:45:39 brd ff:ff:ff:ff:ff:ff

    inet 10.40.0.230/24 brd 10.40.0.255 scope global eth0

    inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0:0

    inet 192.1.1.1/24 scope global eth0

    inet6 fe80::20c:29ff:feb2:4539/64 scope link 

       valid_lft forever preferred_lft forever

        del:删除

[root@localhost ~]# ip addr del 192.168.1.2/24 dev eth0:0

        flush:一次删除多个地址,指定网络地址及子网掩码,如192.1.1/24

[root@localhost ~]# ip addr flush to 192.1.1/24  dev eth0

        route:路由

            add:添加路由

            show:显示路由表

            flush:清空符合某一条件的路由表