Linux下配置IP地址的幾種方式--------CentOS 6.X

Linux配置IP地址

Linux下一切皆文件,要想配置的參數永久有效基本上要修改配置文件。以下主要講怎麼使用ifconfig,route,ip等命令配置IP地址等相關參數並配置相關配置文件使其永久有效。主要使用CentOS 6系列

重啓網絡服務

從CentOS 6系列開始系統默認使用 /etc/init.d/NetworkManager腳本管理網絡服務。但也可使用/etc/init.d/network來管理。NetworkManager不支持網絡橋接等高級功能。以下使用實例

NetworkManager腳本

/etc/init.d/NetworkManager {start|stop|status|restart}

或者

service NetworkManager{start|stop|status|restar}

[root@www ~]# service NetworkManager start

Setting network parameters...                           [  OK  ]

Starting NetworkManager daemon:

[root@www ~]# service NetworkManager stop

Stopping NetworkManager daemon:                         [  OK  ]

[root@www ~]# service NetworkManager restart

Stopping NetworkManager daemon:                         [  OK  ]

Setting network parameters...                           [  OK  ]

Starting NetworkManager daemon:                         [  OK  ]

 

network腳本

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

或者

Service network {start|stop|status|restart|reload}

[root@www ~]# service network stop

Shutting down loopback interface:                          [  OK  ]

 

[root@www ~]# service network start

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0:                                [  OK  ]

[root@www ~]# service network restart

Shutting down interface eth0:                              [  OK  ]

Shutting down loopback interface:                          [  OK  ]

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0:                                [  OK  ]

 

[root@www ~]# service network status

Configureddevices:

lo eth0

Currently activedevices:

lo eth0 eth1

 

[root@www ~]# service network reload

Shutting downinterface eth0:                             [  OK  ]

Shutting downloopback interface:                         [  OK  ]

Bringing uploopback interface:                           [  OK  ]

Bringing upinterface eth0:                               [  OK  ]

ifconfig

ifconfig是最常用的查看及配置IP的命令,使用十分簡單。但只是當時有效重啓網絡服務或者重啓計算機即失效

ifconfig 查看啓用了的網卡

eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:B2  

         inet addr:192.168.43.2  Bcast:192.168.43.255  Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

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

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

         collisions:0 txqueuelen:1000

         RX bytes:14976 (14.6 KiB)  TXbytes:8234 (8.0 KiB)

 

lo        Link encap:Local Loopback  

         inetaddr:127.0.0.1  Mask:255.0.0.0

         inet6 addr: ::1/128 Scope:Host

         UP LOOPBACK RUNNING MTU:16436  Metric:1

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

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

         collisions:0 txqueuelen:0

         RX bytes:1560 (1.5 KiB)  TXbytes:1560 (1.5 KiB)

 

ifconfig - a 查看系統所有網卡

eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:B2 

          inet addr:192.168.43.2  Bcast:192.168.43.255  Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

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

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

         collisions:0 txqueuelen:1000

         RX bytes:75777 (74.0 KiB)  TXbytes:66732 (65.1 KiB)

 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:BC 

          BROADCAST MULTICAST  MTU:1500 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:1000

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

 

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

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

         collisions:0 txqueuelen:0

         RX bytes:1560 (1.5 KiB)  TXbytes:1560 (1.5 KiB)

ifconfig eth*查看單個網卡

[root@www ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:B2 

          inet addr:192.168.43.2  Bcast:192.168.43.255  Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

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

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

         collisions:0 txqueuelen:1000

         RX bytes:108690 (106.1 KiB)  TXbytes:99586 (97.2 KiB)

ifconfig配置臨時使用的IP地址

ifconfig是紅帽系列系統上的老牌管理命令道7系列這個命令將被廢棄改用ip命令配置IP地址這個命令配置管理IP地址簡單方便。

ifconfig eth* IP/MASK位數

[root@www ~]# ifconfig eth1 192.168.1.1/24

[root@www ~]# ifconfig eth1

eth1      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:BC 

          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fe4f:66bc/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

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

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

         collisions:0 txqueuelen:1000

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

ifconfig eth*  [up|down] 啓用或者禁用網卡

[root@www ~]# ifconfig lo down

[root@www ~]# ifconfig eth1 down

[root@www ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:66:B2 

          inet addr:192.168.43.2  Bcast:192.168.43.255  Mask:255.255.255.0

         inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link

         UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1

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

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

         collisions:0 txqueuelen:1000

         RX bytes:157057 (153.3 KiB)  TXbytes:137398 (134.1 KiB)

route命令配置路由和gateway。

route命令可以查看並配置路由和gateway

route 查看計算機上的路由條目

[root@www ~]# route

Kernel IP routing table

Destination     Gateway         Genmask      Flags Metric Ref    Use Iface

192.168.43.0    *               255.255.255.0   U    0      0        0 eth0

link-local       *               255.255.0.0     U    1002   0        0 eth0

default         zhaopeng        0.0.0.0         UG     0      0       0 eth0

U代表UP 


G代表gateway網關路由


[root@www ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask     Flags Metric Ref    Use Iface

192.168.43.0    0.0.0.0         255.255.255.0   U    0      0       0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U    1002   0        0 eth0

0.0.0.0         192.168.43.254  0.0.0.0         UG   0      0        0 eth0

route 添加路由條目

route add –net 目標網絡/MASK位數 gw 本地網關地址

[root@CentOS ~]# route add -net 10.0.0.0/8 gw 192.168.43.1

[root@CentOS ~]# route

Kernel IP routing table

Destination     Gateway        Genmask      Flags Metric  Ref    Use Iface

192.168.43.0    *              255.255.255.0   U    0      0        0 eth0

10.0.0.0        192.168.43.1    255.0.0.0       UG   0      0        0 eth0

route 刪除路由

route del –net Destination網絡/MASK位數

[root@CentOS ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.43.0    0.0.0.0         255.255.255.0   U    0      0        0 eth0

10.0.0.0        192.168.43.1    255.0.0.0       UG   0      0        0 eth0

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

[root@CentOS ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.43.0    0.0.0.0         255.255.255.0   U    0      0        0 eth0

添加及刪除默認路由

添加默認路由和刪除默認路由都有兩種方法。

route add default gw 網關地址     或者         routeadd –net 0.0.0.0 gw 網關地址

route deldefault                                  route del –net 0.0.0.0

 

[root@CentOS ~]# route add default gw 192.168.43.1

[root@CentOS ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.43.0    *               255.255.255.0   U    0      0        0eth0

default         192.168.43.1    0.0.0.0         UG   0      0        0 eth0

[root@CentOS ~]# route del -net 0.0.0.0

[root@CentOS ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.43.0    *               255.255.255.0   U    0      0        0 eth0

配置永久的IP和路由

ifconfig route配置的都是臨時的只有寫在配置文件的參數纔會永久有效不會立即生效重啓主機或服務有效

IP

ip地址的配置文件vim /etc/sysconfig/network-scripts/ifcfg-eth*

DEVICE=eth0 設備名稱一定要與eth*的名字匹配

HWADDR=00:0C:29:57:5C:BB 網卡的MAC地址

TYPE=Ethernet 類型

ONBOOT=yes 開機是否激活此接口

NM_CONTROLLED=yes

BOOTPROTO=static 引導協議 3個選項{static|none|dhcp}staticnone表示使用靜態地址

IPADDR=192.168.1.112 IP地址

NETAMSK=255.255.255.0 子網掩碼

GETWAY=192.168.1.1 網關地址

DNS1=61.128.128.68 DNS服務器地址

route路由

路由的配置文件vim /etc/sysconfig/network-scripts/route-eth*

Destination/mask位數 via nexthop

192.168.1.0/24        via    192.168.1.1

添加默認路由

default     via  192.168.1.1

0.0.0.0/0 via    192.168.1.1

DNS指向

[root@CentOS ~]# cat /etc/resolv.conf DNS配置文件但是建議直接寫在/etc/sysconfig/network-scripts/ifcfg-eth*文件中

# Generated by NetworkManager

# No nameservers found; try putting DNSservers 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.com

nameserver 61.128.128.68 :命名規則

 

setup配置ip dns等信息

直接輸入setup命令


wKiom1QgIDSCsGEgAADwQIB86z4961.jpg


 wKiom1QgHxTyRbxLAACp_GewDkQ224.jpg


wKiom1QgIHCj4o8zAAEilRY8LO4383.jpg


IP命令的使用

重啓計算機就將失效,重啓網絡服務不會失效。

ip link

ip link show 查看網絡接口信息

  • ip link show [dev]

[root@CentOS ~]# ip link show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 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 qlen1000

   link/ether 00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ff

[root@CentOS ~]# ip link show eth0

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

   link/ether 00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ff

  • ip –s link show [dev] 查看接口收發了多少包

 

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

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

    link/ether00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ff

    RX: bytes  packets errors  dropped overrun mcast  

   185812     2152     0      0       0       0     

   TX: bytes  packets  errors dropped carrier collsns

    128240    1187     0       0      0       0      

  • ip link set [dev] [up|down]

[root@www ~]# ip link set eth1 down

[root@www ~]# ip link show

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 qlen1000

    link/ether 00:0c:29:4f:66:b2 brdff:ff:ff:ff:ff:ff

3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdiscpfifo_fast state DOWN qlen 1000

    link/ether 00:0c:29:4f:66:bcbrd ff:ff:ff:ff:ff:ff

ip addr

  • ip aadr show [dev] 查看ip地址

  • ip addr add IP/mask位數 dev eth*  添加ip地址

[root@www ~]# ip addr add10.1.1.1/8 dev eth1

[root@www ~]# ip addr show eth1

3: eth1: <BROADCAST,MULTICAST> mtu1500 qdisc pfifo_fast state DOWN qlen 1000

   link/ether 00:0c:29:4f:66:bc brd ff:ff:ff:ff:ff:ff

inet 10.1.1.1/8 scope global eth1

  • ip addr del address dev eth*

[root@www ~]# ip addr del 10.1.1.1/8 deveth1

  • ip addr flush [dev] to ip/mask位數

 

[root@www ~]# ip addr show eth1

3: eth1: <BROADCAST,MULTICAST> mtu1500 qdisc pfifo_fast state DOWN qlen 1000

   link/ether 00:0c:29:4f:66:bc brd ff:ff:ff:ff:ff:ff

    inet 192.168.100.1/24 scope global eth1

    inet 10.2.2.2/8 scope global eth1

[root@www ~]# ip addr flush eth1 to 10/8

[root@www ~]# ip addr show eth1

3: eth1: <BROADCAST,MULTICAST> mtu1500 qdisc pfifo_fast state DOWN qlen 1000

   link/ether 00:0c:29:4f:66:bc brd ff:ff:ff:ff:ff:ff

    inet 192.168.100.1/24scope global eth1

ip route

ip route add to destination網絡/mask位數 dev eth*via 網關地址

[root@CentOS ~]# ip route add to 192.168.100.0/24 dev eth0 via 192.168.43.1

[root@CentOS ~]# ip route show

192.168.100.0/24 via 192.168.43.1 dev eth0

192.168.43.0/24 dev eth0  proto kernel scope link  src 192.168.43.3

總結:

ifconfig和route配置十分簡單配置很容易功能比較單一,ip命令是屬於iproute2軟件包中的一個強大的網絡配置工具功能十分強悍建議學習使用ip命令。

下載此文檔的下載地址:http://down.51cto.com/data/1875820


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章