linux中接口设置方法

一、           设置接口ip地址

1.      ipv4地址

1)        查看接口配置

命令:

ifconfig            查看所有接口的配置

ifconfig  [接口名]    查看指定接口的配置

例子:

[root@TESTBED1 ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:03:0F:FF:DD:20 

          inet addr:194.168.111.1  Bcast:194.168.111.255  Mask:255.255.255.0

          inet6 addr: fe80::203:fff:feff:dd20/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:448747 (438.2 KiB)  TX bytes:421026 (411.1 KiB)

          Interrupt:11 Base address:0xe000

 

eth1      Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:2.0.0.2  Bcast:2.0.0.255  Mask:255.255.255.0

          inet6                                         

          RX bytes:15897 (15.5 KiB)  TX bytes:69071 (67.4 KiB)

          Interrupt:10

2)        配置接口地址

命令:

ifconfig  [接口名]  [ip地址] 设定接口地址,掩码根据ip地址自动计算出来的。

ifconfig  [接口名]  [ip地址]  netmask  [掩码] 设定接口的地址和掩码

例子:

[root@TESTBED1 ~]# ifconfig eth1 3.0.0.1

[root@TESTBED1 ~]# ifconfig eth1

eth1      Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:3.0.0.1  Bcast:3.255.255.255  Mask:255.0.0.0

          inet6 addr: fe80::21e:58ff:fe46:3d44/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:15897 (15.5 KiB)  TX bytes:73080 (71.3 KiB)

          Interrupt:10

 

[root@TESTBED1 ~]# ifconfig eth1 3.0.0.1 netmask 255.255.255.0

[root@TESTBED1 ~]# ifconfig eth1

eth1      Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:3.0.0.1  Bcast:3.0.0.255  Mask:255.255.255.0

          inet6 addr: fe80::21e:58ff:fe46:3d44/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:15897 (15.5 KiB)  TX bytes:76883 (75.0 KiB)

          Interrupt:10

3)        配置接口多个地址

命令:

ifconfig  [接口名]:[子接口数字]  [ip地址]  netmask  [掩码]

说明:

相当于配置多个子接口,每个子接口一个ip地址。

例子:

[root@TESTBED1 ~]# ifconfig eth1:1 1.0.0.1 netmask 255.255.254.0

[root@TESTBED1 ~]# ifconfig eth1:1

eth1:1    Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:1.0.0.1  Bcast:1.0.1.255  Mask:255.255.254.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          Interrupt:10

4)        删除接口地址

命令:

ifconfig  [接口名]  down

说明:

该命令只是将接口协议down掉,物理连接并未down掉。执行这条命令后,该接口的物理连接指示灯仍然是亮的。

例子:

[root@TESTBED1 ~]# ifconfig eth1:1 down

[root@TESTBED1 ~]# ifconfig eth1:1

eth1:1    Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          Interrupt:10

2.      ipv6地址

1)        增加接口地址

命令:

ifconfig  [接口名]  add  [ipv6地址/掩码]

说明:

Ipv6的接口可以配置多个地址,这里就不需要像ipv4一样配置多个接口了,增加接口地址只需要再次执行该命令。命令中,掩码值不填默认为0;该命令同时也支持地址为ipv4的地址,如果你输入ifconfig eth1 add 6.0.0.1,和你输入ifconfig eth1:0  6.0.0.1执行的效果是一样的。

例子:

[root@TESTBED1 ~]# ifconfig eth1 add 6::1

[root@TESTBED1 ~]# ifconfig eth1 add 3::1/96

[root@TESTBED1 ~]# ifconfig eth1

eth1      Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:3.0.0.1  Bcast:255.255.255.159  Mask:255.255.255.0

          inet6 addr: 3::1/96 Scope:Global

          inet6 addr: 6::1/0 Scope:Global

          inet6 addr: fe80::21e:58ff:fe46:3d44/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:16077 (15.7 KiB)  TX bytes:103526 (101.0 KiB)

          Interrupt:10

2)        删除接口地址

命令:

ifconfig  del  [ipv6地址/掩码]

说明:

掩码值默认为0

例子:

[root@TESTBED1 ~]# ifconfig eth1 del 3::1/96

[root@TESTBED1 ~]# ifconfig eth1

eth1      Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:3.0.0.1  Bcast:255.255.255.159  Mask:255.255.255.0

          inet6 addr: 6::1/0 Scope:Global

          inet6 addr: fe80::21e:58ff:fe46:3d44/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:16077 (15.7 KiB)  TX bytes:105894 (103.4 KiB)

          Interrupt:10

[root@TESTBED1 ~]# ifconfig eth1 del 6::1

[root@TESTBED1 ~]# ifconfig eth1

eth1      Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:3.0.0.1  Bcast:255.255.255.159  Mask:255.255.255.0

          inet6 addr: fe80::21e:58ff:fe46:3d44/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:16077 (15.7 KiB)  TX bytes:107448 (104.9 KiB)

          Interrupt:10

3.      配置vlan接口(接口发报文都带tag

1)        配置地址:

命令:

vconfig  add  [接口名]  [vlan]

ifconfig  [接口名].[vlan]  [ip地址]

说明:

第一个命令增加一个vlan子接口,第二个命令配置该子接口的地址,配置好后该子接口发出的报文都会带上配置的tag

例子:

配置接口属于vlan2tagip地址为3.0.0.2

[root@TESTBED1 ~]# vconfig add eth1 2

Added VLAN with VID == 2 to IF -:eth1:-

[root@TESTBED1 ~]# ifconfig eth1.2 3.0.0.2 netmask 255.255.0.0

[root@TESTBED1 ~]# ifconfig eth1.2

eth1.2    Link encap:Ethernet  HWaddr 00:1E:58:46:3D:44 

          inet addr:3.0.0.2  Bcast:3.0.255.255  Mask:255.255.0.0

          inet6 addr: fe80::21e:58ff:fe46:3d44/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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

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

          collisions:0 txqueuelen:0

          RX bytes:0 (0.0 b)  TX bytes:4580 (4.4 KiB)

2)        删除地址:

命令:

vconfig rem [接口名].[vlan]

例子:

[root@TESTBED1 ~]# vconfig rem eth1.2

Removed VLAN -:eth1.2:-

[root@TESTBED1 ~]# ifconfig eth1.2

eth1.2: error fetching interface information: Device not found

二、           配置路由

1.      添加到主机路由

命令:

route  add  –host  [ip地址]  dev  [接口名]

或者

route  add  –host  [ip地址]   gw  [下一跳地址]

例子:

[root@TESTBED1 ~]# route add -host 100.0.0.1 dev eth1

[root@TESTBED1 ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

100.0.0.1       *               255.255.255.255 UH    0      0        0 eth1

194.168.111.0   *               255.255.255.0   U     0      0        0 eth0

link-local      *               255.255.0.0     U     0      0        0 eth1

1.0.0.0         *               255.0.0.0       U     0      0        0 eth1

 

[root@TESTBED1 ~]# route add -host 100.0.0.1 gw 1.0.0.2

[root@TESTBED1 ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

100.0.0.1       1.0.0.2         255.255.255.255 UGH   0      0        0 eth1

100.0.0.1       *               255.255.255.255 UH    0      0        0 eth1

194.168.111.0   *               255.255.255.0   U     0      0        0 eth0

link-local      *               255.255.0.0     U     0      0        0 eth1

1.0.0.0         *               255.0.0.0       U     0      0        0 eth1

[root@TESTBED1 ~]# 

2.      添加到网络的路由

命令:

route  add  -net  [网段]  netmask  [掩码]  gw  [下一条地址]

或者:

route  add  -net  [网段]  netmask  [掩码]  dev  [接口名]

或者:

route  add  -net  [网段]/[掩码位数]  dev  [接口名]

例子:

[root@TESTBED1 ~]# route add -net 200.1.1.0 netmask 255.255.255.0 gw 1.0.0.2 

[root@TESTBED1 ~]# route add -net 200.1.1.0 netmask 255.255.255.0  eth1     

[root@TESTBED1 ~]# route add -net 201.1.1.0/24 dev eth1

[root@TESTBED1 ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

200.1.1.0       *               255.255.255.0   U     0      0        0 eth1

200.1.1.0       1.0.0.2         255.255.255.0   UG    0      0        0 eth1

201.1.1.0       *               255.255.255.0   U     0      0        0 eth1

194.168.111.0   *               255.255.255.0   U     0      0        0 eth0

link-local      *               255.255.0.0     U     0      0        0 eth1

1.0.0.0         *               255.0.0.0       U     0      0        0 eth1 

3.      添加默认网关

命令:

route  add  default  gw  [下一跳地址]

例子:

[root@TESTBED1 ~]# route add default gw 1.0.0.3

[root@TESTBED1 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

194.168.111.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0

1.0.0.0         0.0.0.0         255.0.0.0       U     0      0        0 eth1

0.0.0.0         1.0.0.3         0.0.0.0         UG    0      0        0 eth1

4.      删除路由

命令:

route  del  [增加路由时添加的选项]

例子:

删除配置的默认路由

[root@TESTBED1 ~]# route del default gw 1.0.0.3    

[root@TESTBED1 ~]# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

194.168.111.0   *               255.255.255.0   U     0      0        0 eth0

1.0.0.0         *               255.0.0.0       U     0      0        0 eth1

5.      增加ipv6路由

命令:

route  -A  inet  add  [ipv6地址/掩码]  gw  [下一跳地址]

6.      删除ipv6路由

route  -A  inet  del  [ipv6地址/掩码]  gw  [下一跳地址]

Logo

更多推荐