linux环境下单网卡配置多个IP地址

喜欢ヅ旅行 2022-08-26 12:20 724阅读 0赞

1.使用命令进行配置

  1. 这就很简单了,首先查看一下当前机器的IP地址,命令如下:
  2. #ifconfig
  3. eth0 Link encap:Ethernet HWaddr 00:19:D1:24:2A:EC
  4. inet addr:192.168.1.55 Bcast:192.168.3.255 Mask:255.255.252.0
  5. inet6 addr: fe80::219:d1ff:fe24:2aec/64 Scope:Link
  6. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  7. 注意看其中的红色部分,可以看到我们本机的IP地址为1.55,那么我们来配一个1.56(注意不要和局域网内的地址冲突哦),使用命令如下:
  8. #ifconfig eth0:0 192.168.1.56 netmask 255.255.252.0
  9. #ifconfig
  10. eth0 Link encap:Ethernet HWaddr 00:19:D1:24:2A:EC
  11. inet addr:192.168.1.55 Bcast:192.168.3.255 Mask:255.255.252.0
  12. inet6 addr: fe80::219:d1ff:fe24:2aec/64 Scope:Link
  13. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  14. eth0:0 Link encap:Ethernet HWaddr 00:19:D1:24:2A:EC
  15. inet addr:192.168.1.56 Bcast:192.168.3.255 Mask:255.255.252.0
  16. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  17. #ping 192.168.1.56
  18. PING 192.168.1.56 (192.168.1.56) 56(84) bytes of data.
  19. 64 bytes from 192.168.1.56: icmp_seq=1 ttl=64 time=0.045 ms
  20. 64 bytes from 192.168.1.56: icmp_seq=2 ttl=64 time=0.045 ms
  21. 64 bytes from 192.168.1.56: icmp_seq=3 ttl=64 time=0.043 ms
  22. #ping 192.168.1.55
  23. PING 192.168.1.55 (192.168.1.55) 56(84) bytes of data.
  24. 64 bytes from 192.168.1.55: icmp_seq=1 ttl=64 time=0.030 ms
  25. 64 bytes from 192.168.1.55: icmp_seq=2 ttl=64 time=0.022 ms
  26. 64 bytes from 192.168.1.55: icmp_seq=3 ttl=64 time=0.022 ms
  27. 这样我们就很方便的配置了另外一个IP地址1.56

2.使用配置文件进行配置

  1. 网卡IP配置的文件在/etc/sysconfig/network-scripts/下,文件分别为ehtxethx:x,执行命令如下:
  2. #cd /etc/sysconfig/network-scripts/
  3. #cp ifcfg-eth0 ifcfg-eth0:1
  4. 这样我们就简单的copy了一份当前网络的配置文件,然后我们打开这个复制后的文件,改其中的IP地址为我们想要的IP地址,如192.168.1.57即可!
  5. #vi ifcfg-eth0:1
  6. DEVICE=eth0
  7. BOOTPROTO=none
  8. HWADDR=00:19:D1:24:2A:EC
  9. ONBOOT=yes
  10. DHCP_HOSTNAME=zhongqg.localdomain
  11. IPADDR=192.168.1.55
  12. NETMASK=255.255.252.0
  13. GATEWAY=192.168.0.1
  14. TYPE=Ethernet
  15. USERCTL=no
  16. IPV6INIT=no
  17. PEERDNS=yes
  18. 然后修改其中的IPADDR部分为192.168.1.57,然后保存退出并启动该配置文件!
  19. #ifup eth0:1

参考:

1 http://hi.baidu.com/qin_juan/item/f9398930b51f739bb80c03bf

2 Linux下配置ip地址

即时生效:
ifconfig eth0 192.168.1.102 netmask 255.255.255.0
启动生效:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
加入
IPADDR=192.168.1.102
NETMASK=255.255.255.0

修改default gateway
即时生效:
route add default gw 192.168.1.1
启动生效:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
加入
GATEWAY=192.168.1.1
最后结果如下:
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.1.102
NETMASK=255.255.255.0
GETEWAY=192.168.1.1
ONBOOT=yes
TYPE=Ethernet
注:BOOTPROTO只有在static(静态)模式下才可以使用设置的IP信息
修改dns
vim /etc/resolv.conf
修改后可即时生效,启动同样有效

形式
加入nameserver= ***.***.***.***
最多可有三个,作为上一个失败后的候选

修改host name
即时生效:
hostname fc2
启动生效:
vim /etc/sysconfig/network
形式如下:
NETWORKING=yes
HOSTNAME=rh-linux.fc.org
##########################################################
首先,先了解传统的网络配置命令:

  1. 使用ifconfig命令配置并查看网络接口情况
    示例1: 配置eth0的IP,同时激活设备:
    ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up
    示例2: 配置eth0别名设备 eth0:1 的IP,并添加路由
    ifconfig eth0:1 192.168.4.2
    route add –host 192.168.4.2 dev eth0:1
    示例3:激活(禁用)设备
    ifconfig eth0:1 up(down)
    示例4:查看所有(指定)网络接口配置
    ifconfig (eth0)
    —————————
  2. 使用route 命令配置路由表
    示例1:添加到主机路由
    route add –host 192.168.4.2 dev eth0:1
    route add –host 192.168.4.1 gw 192.168.4.250
    示例2:添加到网络的路由
    route add –net IP netmask MASK eth0
    route add –net IP netmask MASK gw IP
    route add –net IP/24 eth1
    示例3:添加默认网关
    route add default gw IP
    示例4:删除路由
    route del –host 192.168.4.1 dev eth0:1
    示例5:查看路由信息
    route 或  route -n  (-n 表示不解析名字,列出速度会比route 快)
    —————————
    3.ARP 管理命令
    示例1:查看ARP缓存
    arp
    示例2: 添加
    arp –s IP MAC
    示例3: 删除
    arp –d IP
    —————————
    4. ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具。例如:ifconfig、route等,
    上面的示例完全可以用下面的ip命令实现,而且ip命令可以实现更多的功能.下面介绍一些示例:

4.0 ip命令的语法
ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]

4.1 ip link set–改变设备的属性. 缩写:set、s

示例1:up/down 起动/关闭设备。
ip link set dev eth0 up
这个等于传统的
ifconfig eth0 up
ifconfig eth0 down
示例2:改变设备传输队列的长度。
参数:txqueuelen NUMBER或者txqlen NUMBER
ip link set dev eth0 txqueuelen 100
示例3:改变网络设备MTU(最大传输单元)的值。
ip link set dev eth0 mtu 1500
示例4: 修改网络设备的MAC地址。
参数: address LLADDRESS
ip link set dev eth0 address 00:01:4f:00:15:f1

4.2 ip link show–显示设备属性.  缩写:show、list、lst、sh、ls、l
-s选项出现两次或者更多次,ip会输出更为详细的错误信息统计。
示例:
ip -s -s link ls eth0
这个命令等于传统的
ifconfig eth0
—————————
5.1 ip address add–添加一个新的协议地址. 缩写:add、a
示例1:为每个地址设置一个字符串作为标签。为了和Linux-2.0的网络别名兼容,这个字符串必须以设备名开头,接着一个冒号,
ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0
等价于
ifconfig eth0:5 192.168.4.1/28
ip addr add local 192.168.4.1/28 dev eth0
示例2:
在以太网接口eth0上增加一个地址192.168.20.0,掩码长度为24位(155.155.155.0),标准广播地址,标签为eth0:Alias:
ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1
这个命令等于传统的: 
ifconfig eth1:1 192.168.4.2/24

5.2 ip address delete–删除一个协议地址. 缩写:delete、del、d
ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1

5.3 ip address show–显示协议地址. 缩写:show、list、lst、sh、ls、l
ip addr ls eth0

5.4.ip address flush–清除协议地址. 缩写:flush、f
示例1 : 删除属于私网10.0.0.0/8的所有地址:
ip -s -s a f to 10/8
示例2 : 取消所有以太网卡的IP地址
ip -4 addr flush label “eth0″
—————————

  1. ip neighbour–neighbour/arp表管理命令
    缩写 neighbour、neighbor、neigh、n
    命令 add、change、replace、delete、fulsh、show(或者list)

6.1 ip neighbour add — 添加一个新的邻接条目
ip neighbour change–修改一个现有的条目
ip neighbour replace–替换一个已有的条目
缩写:add、a;change、chg;replace、repl
示例1: 在设备eth0上,为地址10.0.0.3添加一个permanent ARP条目:
ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm
示例2:把状态改为reachable
ip neigh chg 10.0.0.3 dev eth0 nud reachable

6.2.ip neighbour delete–删除一个邻接条目
示例1:删除设备eth0上的一个ARP条目10.0.0.3
ip neigh del 10.0.0.3 dev eth0
6.3.ip neighbour show–显示网络邻居的信息. 缩写:show、list、sh、ls
示例1: ip -s n ls 193.233.7.254
193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud
reachable
6.4.ip neighbour flush–清除邻接条目. 缩写:flush、f
示例1: (-s 可以显示详细信息)
ip -s -s n f 193.233.7.254
—————————

  1. 路由表管理
    7.1.缩写 route、ro、r
    7.5.路由表
    从Linux-2.2开始,内核把路由归纳到许多路由表中,这些表都进行了编号,编号数字的范围是1到255。另外,
    为了方便,还可以在/etc/iproute2/rt_tables中为路由表命名。
    默认情况下,所有的路由都会被插入到表main(编号254)中。在进行路由查询时,内核只使用路由表main。

7.6.ip route add — 添加新路由
ip route change — 修改路由
ip route replace — 替换已有的路由
缩写:add、a;change、chg;replace、repl
示例1: 设置到网络10.0.0/24的路由经过网关193.233.7.65
ip route add 10.0.0/24 via 193.233.7.65

示例2: 修改到网络10.0.0/24的直接路由,使其经过设备dummy
ip route chg 10.0.0/24 dev dummy

示例3:
实现链路负载平衡.加入缺省多路径路由,让ppp0和ppp1分担负载(注意:scope值并非必需,它只不过是告诉内核,这个路由要经过网关而不是直连的。实际上,如果你知道远程端点的地址,使用via参数来设置就更好了)。
ip route add default scope global nexthop dev ppp0 nexthop dev ppp1
ip route replace default scope global nexthop dev ppp0 nexthop dev ppp1
示例4:
设置NAT路由。在转发来自192.203.80.144的数据包之前,先进行网络地址转换,把这个地址转换为193.233.7.83
ip route add nat 192.203.80.142 via 193.233.7.83

示例5: 实现数据包级负载平衡,允许把数据包随机从多个路由发出。weight
可以设置权重.
ip route replace default equalize nexthop via 211.139.218.145 dev eth0
weight 1 nexthop via 211.139.218.145 dev eth1 weight 1

7.7.ip route delete– 删除路由
缩写:delete、del、d
示例1:删除上一节命令加入的多路径路由
ip route del default scope global nexthop dev ppp0 nexthop dev ppp1

7.8.ip route show — 列出路由
缩写:show、list、sh、ls、l

示例1: 计算使用gated/bgp协议的路由个数
ip route ls proto gated/bgp |wc
1413  9891  79010

示例2:
计算路由缓存里面的条数,由于被缓存路由的属性可能大于一行,以此需要使用-o选项
ip -o route ls cloned |wc
159  2543  18707
示例3: 列出路由表TABLEID里面的路由。缺省设置是table
main。TABLEID或者是一个真正的路由表ID或者是/etc/iproute2/rt_tables文件定义的字符串,
或者是以下的特殊值:
all — 列出所有表的路由;
cache — 列出路由缓存的内容。
ip ro ls 193.233.7.82 tab cache
示例4: 列出某个路由表的内容
ip route ls table fddi153

示例5: 列出默认路由表的内容
ip route ls
这个命令等于传统的: route

7.9.ip route flush — 擦除路由表
示例1: 删除路由表main中的所有网关路由(示例:在路由监控程序挂掉之后):
ip -4 ro flush scope global type unicast
示例2:清除所有被克隆出来的IPv6路由:
ip -6 -s -s ro flush cache
示例3: 在gated程序挂掉之后,清除所有的BGP路由:
ip -s ro f proto gated/bgp
示例4: 清除所有ipv4路由cache
ip route flush cache
*** IPv4 routing cache is flushed.

7.10 ip route get — 获得单个路由 .缩写:get、g
使用这个命令可以获得到达目的地址的一个路由以及它的确切内容。
ip route get命令和ip route show命令执行的操作是不同的。ip route
show命令只是显示现有的路由,而ip route get命令在必要时会派生出新的路由。
示例1: 搜索到193.233.7.82的路由
ip route get 193.233.7.82
193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac cache mtu 1500 rtt
300
示例2:
搜索目的地址是193.233.7.82,来自193.233.7.82,从eth0设备到达的路由(这条命令会产生一条非常有意思的路由,这是一条到193.233.7.82的回环路由)
ip r g 193.233.7.82 from 193.233.7.82 iif eth0
193.233.7.82 from 193.233.7.82 dev eth0 src 193.233.7.65 realms
inr.ac/inr.ac
cache

示例1: 通过路由表inr.ruhep路由来自源地址为192.203.80/24的数据包
ip ru add from 192.203.80/24 table inr.ruhep prio 220

示例2:把源地址为193.233.7.83的数据报的源地址转换为192.203.80.144,并通过表1进行路由
ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320

示例3:删除无用的缺省规则
ip ru del prio 32767

8.7. ip rule show — 列出路由规则
缩写:show、list、sh、ls、l
示例1: 
ip ru ls
0:   from all lookup local
32762: from 192.168.4.89 lookup fddi153
32764: from 192.168.4.88 lookup fddi153
32766: from all lookup main
32767: from all lookup 253
—————————
9. ip maddress — 多播地址管理
缩写:show、list、sh、ls、l
9.3.ip maddress show — 列出多播地址
示例1:
ip maddr ls dummy

9.4. ip maddress add — 加入多播地址
ip maddress delete — 删除多播地址
缩写:add、a;delete、del、d
使用这两个命令,我们可以添加/删除在网络接口上监听的链路层多播地址。这个命令只能管理链路层地址。

示例1: 增加    ip maddr add 33:33:00:00:00:01 dev dummy
示例2: 查看   
ip -O maddr ls dummy
2: dummy
link 33:33:00:00:00:01 users 2 static
link 01:00:5e:00:00:01
示例3: 删除  
ip maddr del 33:33:00:00:00:01 dev dummy
—————————
10.ip mroute — 多播路由缓存管理

10.4. ip mroute show — 列出多播路由缓存条目
缩写:show、list、sh、ls、l

示例1:查看  ip mroute ls
(193.232.127.6, 224.0.1.39)   Iif: unresolved
(193.232.244.34, 224.0.1.40)   Iif: unresolved
(193.233.7.65, 224.66.66.66)   Iif: eth0    Oifs: pimreg
示例2:查看  ip -s mr ls 224.66/16
(193.233.7.65, 224.66.66.66)   Iif: eth0    Oifs: pimreg
9383 packets, 300256 bytes
—————————

  1. ip tunnel — 通道配置
    缩写 tunnel、tunl

11.4.ip tunnel add — 添加新的通道
ip tunnel change — 修改现有的通道
ip tunnel delete — 删除一个通道
缩写:add、a;change、chg;delete、del、d
示例1:建立一个点对点通道,最大TTL是32
ip tunnel add Cisco mode sit remote 192.31.7.104 local 192.203.80.1 ttl 32

11.4.ip tunnel show — 列出现有的通道
缩写:show、list、sh、ls、l
示例1:  ip -s tunl ls Cisco
—————————

  1. ip monitor和rtmon — 状态监视
    ip命令可以用于连续地监视设备、地址和路由的状态。这个命令选项的格式有点不同,命令选项的名字叫做monitor,接着是操作对象:
    ip monitor [ file FILE ] [ all | OBJECT-LIST ]
    示例1: 
    rtmon file /var/log/rtmon.log
    示例2: 
    ip monitor file /var/log/rtmon.log r

ifcfg-eth0 配置

文件:/etc/sysconfig/network-scripts/ifcfg-eth0

以下各值常见于所有的基本配置文件中:
* DEVICE=name,这里name是物理设备的名字(动态分配的PPP设备应当除外,
它的名字是“逻辑名”。
* IPADDR=addr, 这里addr是IP地址。
* NETMASK=mask, 这里mask是网络掩码。
* NETWORK=addr, 这里addr是网络地址。
* BROADCAST=addr, 这里addr是广播地址。
* GATEWAY=addr, 这里addr是网关地址。
* ONBOOT=answer, 这里answer取下列值之一:
o yes — 该设备将在boot时被激活。
o no — 该设备不在boot时激活。
* USERCTL=answer, 这里answer取下列值之一:
o yes —非root用户可以控制该设备。
o no — 非root用户不允许控制该设备。
* BOOTPROTO=proto, 这里proto取下列值之一:
o none — 不使用boot时协议。
o bootp — 使用bootp协议。
o dhcp —使用dhcp协议。

终端:查询IP地址: ifconfig -a
修改局域网IP:
1.以 root 登录
2.修改配置文件
/etc/sysconfig/network-scripts/ifcfg-eth0

文件内容如下:
\DEVICE=eth0
HWADDR=00:0C:29:A2:8C:B2
ONBOOT=yes
TYPE=Ethernet
NETMASK=255.255.255.0
IPADDR=192.168.1.11 -> 修改为 192.168.1.12
GATEWAY=192.168.1.1

reboot

ifconfig eth0 新ip 然后编辑/etc/sysconfig/network-scripts/ifcfg-eth0,修改ip

ifconfig eth0 新IP

然后编辑/etc/sysconfig/network-scrIPts/ifcfg-eth0,修改IP

一、修改IP地址

[aeolus@db1 network-scrIPts]$ vi ifcfg-eth0

DEVICE=eth0

ONBOOT=yes

BOOTPROTO=static

IPADDR=219.136.241.211

NETMASK=255.255.255.128

GATEWAY=219.136.241.254

二、修改网关

vi /etc/sysconfig/network

NETWORKING=yes

HOSTNAME=Aaron

GATEWAY=192.168.1.1

三、修改DNS

[aeolus@db1 etc]$ vi resolv.conf

nameserver 202.96.128.68

nameserver 219.136.241.206

四、重新启动网络配置

/etc/init.d/network restart

修改IP地址

即时生效:

# ifconfig eth0 192.168.0.20 netmask 255.255.255.0

启动生效:

修改/etc/sysconfig/network-scrIPts/ifcfg-eth0

修改default gateway

即时生效:

# route add default gw 192.168.0.254

启动生效:

修改/etc/sysconfig/network-scrIPts/ifcfg-eth0

修改DNS

修改/etc/resolv.conf

修改后可即时生效,启动同样有效

修改host name

即时生效:

# hostname fc2

启动生效:

修改/etc/sysconfig/network

linux 下 ifcfg-eth0 配置

  1. 网络接口配置文件
  2. [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
  3. # Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
  4. TYPE=Ethernet #网卡类型
  5. DEVICE=eth0 #网卡接口名称
  6. ONBOOT=yes #系统启动时是否自动加载
  7. BOOTPROTO=static #启用地址协议 --static:静态协议 --bootp协议 --dhcp协议
  8. IPADDR=192.168.1.11 #网卡IP地址
  9. NETMASK=255.255.255.0 #网卡网络地址
  10. GATEWAY=192.168.1.1 #网卡网关地址
  11. DNS1=10.203.104.41 #网卡DNS地址
  12. HWADDR=00:0C:29:13:5D:74 #网卡设备MAC地址
  13. BROADCAST=192.168.1.255 #网卡广播地址
  14. 重新导入ifcfg-eth0网络配置文件
  15. [root@localhost ~]# /etc/init.d/network reload
  16. Shutting down interface eth0: [ OK ]
  17. Shutting down loopback interface: [ OK ]
  18. Bringing up loopback interface: [ OK ]
  19. Bringing up interface eth0: [ OK ]
  20. 网卡接口关闭与激活
  21. [root@localhost ~]# ifdown eth0 #关闭网络
  22. [root@localhost ~]# ifup eth0 #启动网络
  23. 网络服务启动与关闭
  24. 方法一:
  25. [root@localhost ~]# service network stop #关闭网络服务
  26. [root@localhost ~]# service network start #启动网络服务
  27. [root@localhost ~]# service network restart #重启网络服务
  28. 方法二:
  29. [root@localhost ~]# /etc/init.d/network stop
  30. [root@localhost ~]# /etc/init.d/network start
  31. [root@localhost ~]# /etc/init.d/network restart
  32. 网卡状态查询
  33. [root@localhost ~]# service network status
  34. Configured devices:
  35. lo eth0
  36. Currently active devices:
  37. lo eth0
  38. 临时配置网卡信息,无需重启。
  39. [root@localhost ~]# ifconfig eth0 10.1.1.10 netmask 255.0.0.0
  40. 查看网卡接口信息,默认列出所有接口
  41. [root@localhost ~]# ifconfig
  42. eth0 Link encap:Ethernet HWaddr 00:0C:29:13:5D:74
  43. inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0
  44. inet6 addr: fe80::20c:29ff:fe13:5d74/64 Scope:Link
  45. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  46. RX packets:413 errors:0 dropped:0 overruns:0 frame:0
  47. TX packets:572 errors:0 dropped:0 overruns:0 carrier:0
  48. collisions:0 txqueuelen:1000
  49. RX bytes:47701 (46.5 KiB) TX bytes:64842 (63.3 KiB)
  50. Base address:0x2000 Memory:d8920000-d8940000
  51. lo Link encap:Local Loopback
  52. inet addr:127.0.0.1 Mask:255.0.0.0
  53. inet6 addr: ::1/128 Scope:Host
  54. UP LOOPBACK RUNNING MTU:16436 Metric:1
  55. RX packets:407 errors:0 dropped:0 overruns:0 frame:0
  56. TX packets:407 errors:0 dropped:0 overruns:0 carrier:0
  57. collisions:0 txqueuelen:0
  58. RX bytes:70759 (69.1 KiB) TX bytes:70759 (69.1 KiB)
  59. 查看当前路由及网关信息
  60. [root@localhost ~]# netstat -r
  61. Kernel IP routing table
  62. Destination Gateway Genmask Flags MSS Window irtt Iface
  63. 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
  64. 169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
  65. default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
  66. DNS
  67. 主机名:CentOS
  68. DNS202.106.46.151
  69. 第二DNS:202.106.0.20
  70. 第三DNS:8.8.8.8
  71. 网络配置:
  72. eth0
  73. 静态ip192.168.1.106
  74. 子网掩码 255.255.255.0
  75. 默认网关 IP 192.168.1.1
  76. DEVICE=eth0
  77. IPADDR=192.168.1.106
  78. NETMASK=255.255.255.0
  79. BROADCAST=192.168.1.255
  80. ONBOOT=yes
  81. BOOTPROTO=none
  82. GATEWAY=192.168.1.1
  83. TYPE=Ethernet
  84. "/etc/sysconfig/network-scripts/ifcfg-eth0" 11L, 187C

发表评论

表情:
评论列表 (有 0 条评论,724人围观)

还没有评论,来说两句吧...

相关阅读