linux下配置IP和路由信息

IP配置:

ifconfig命令:

linux-rd4x:/ # ifconfig --help
Usage:
  ifconfig [-a] [-i] [-v] [-s] <interface> [[<AF>] <address>]
  [add <address>[/<prefixlen>]]
  [del <address>[/<prefixlen>]]
  [[-]broadcast [<address>]]  [[-]pointopoint [<address>]]
  [netmask <address>]  [dstaddr <address>]  [tunnel <address>]
  [outfill <NN>] [keepalive <NN>]
  [hw <HW> <address>]  [metric <NN>]  [mtu <NN>]
  [[-]trailers]  [[-]arp]  [[-]allmulti]
  [multicast]  [[-]promisc]
  [mem_start <NN>]  [io_addr <NN>]  [irq <NN>]  [media <type>]
  [txqueuelen <NN>]
  [[-]dynamic]
  [up|down] ...

对单个网卡配置IP、子网掩码等信息:

linux-rd4x:/ # ifconfig eth0 10.92.20.54 netmask 255.255.0.0
linux-rd4x:/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:00:51:55  
          inet addr:10.92.20.54  Bcast:10.92.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe00:5155/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1469249 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1533403 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:169906332 (162.0 Mb)  TX bytes:1823670850 (1739.1 Mb)
          Interrupt:19 Base address:0x2000 

配置IP后,会默认增加一条网关为0.0.0.0的路由信息:

linux-rd4x:/ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.92.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
删除IP后,此默认添加的路由信息也同时删除;

注:单独删除路由信息,不会影响IP的配置信息





路由配置:

route命令

linux-rd4x:/ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
10.92.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         10.92.1.254     0.0.0.0         UG    0      0        0 eth0

路由增加:

linux-rd4x:/ # route add -net 8.8.8.8 netmask 255.255.255.0 dev eth0
route: netmask doesn't match route address
Usage: route [-nNvee] [-FC] [<AF>]           List kernel routing tables
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

       route {-h|--help} [<AF>]              Detailed usage syntax for specified AF.
       route {-V|--version}                  Display version/author and exit.

        -v, --verbose            be verbose
        -n, --numeric            don't resolve names
        -e, --extend             display other/more information
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

随意添加一条路由信息时,报错,经过尝试,Destination IP必须和掩码相对应

即IP:8.8.8.8 & 255.255.255.0(逻辑与操作) 才是真正的DestinationIP,否则会报错。

linux-rd4x:/ # route add -net 8.8.8.8 netmask 255.255.255.0 dev eth0
route: netmask doesn't match route address
Usage: route [-nNvee] [-FC] [<AF>]           List kernel routing tables
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

       route {-h|--help} [<AF>]              Detailed usage syntax for specified AF.
       route {-V|--version}                  Display version/author and exit.

        -v, --verbose            be verbose
        -n, --numeric            don't resolve names
        -e, --extend             display other/more information
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

linux-rd4x:/ # route add -net 8.8.8.0 netmask 255.255.255.0 dev eth0 
linux-rd4x:/ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
8.8.8.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     0      0        0 eth0
10.92.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         10.92.1.254     0.0.0.0         UG    0      0        0 eth0
设置路由信息时,若不指定网关,则默认为0.0.0.0



添加带网关的路由时,网关同样不可以随意设置,经过尝试,貌似网关必须在已有的Destination IP和掩码二者决定的范围内的某个IP(参考下面关于IP、掩码和网关的知识),否则出错

下面在添加一条9.9.9.0  255.255.255.0的路由后,路由网关 9.9.9.9则可以配置成功:

linux-rd4x:/ # route add -net 8.8.8.0 netmask 255.255.255.0 gw 9.9.9.9
SIOCADDRT: No such process

linux-rd4x:/ # route add -net 9.9.9.0 netmask 255.255.255.0 dev eth0    
linux-rd4x:/ # route add -net 8.8.8.0 netmask 255.255.255.0 gw 9.9.9.9
linux-rd4x:/ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
9.9.9.0         0.0.0.0         255.255.255.0   U     0      0        0 eth0
8.8.8.0         9.9.9.9         255.255.255.0   UG    0      0        0 eth0
8.8.8.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     0      0        0 eth0
10.92.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         10.92.1.254     0.0.0.0         UG    0      0        0 eth0


增加默认路由:

linux-rd4x:/ # route add default gw 8.8.8.8
linux-rd4x:/ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
8.8.8.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     0      0        0 eth0
10.92.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         8.8.8.8         0.0.0.0         UG    0      0        0 eth0
0.0.0.0         10.92.1.254     0.0.0.0         UG    0      0        0 eth0

删除路由信息:

删除有依赖的路由9.9.9.9  255.255.255.0,尝试发现可以删除,破坏了之前的依赖关系,说明Linux下的检测也并不是十分严谨罢了

linux-rd4x:/ # route del -net 9.9.9.0 netmask 255.255.255.0
linux-rd4x:/ # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
8.8.8.0         9.9.9.9         255.255.255.0   UG    0      0        0 eth0
8.8.8.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     0      0        0 eth0
10.92.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         10.92.1.254     0.0.0.0         UG    0      0        0 eth0




下面转一些IP地址基础知识:

局域网中可用的IP地址:

  在一个局域网中,有两个IP地址比较特殊,一个是网络号,一个是广播地址。网络号是用于三层寻址的地址,它代表了整个网络本身;另一个是广播地址,它代表了网络全部的主机。网络号是网段中的第一个地址,广播地址是网段中的最后一个地址,这两个地址是不能配置在计算机主机上的。  例如在192.168.0.0,255.255.255.0这样的网段中,网络号是192.168.0.0/24,广播地址是192.168.0.255。因此,在一个局域网中,能配置在计算机中的地址比网段内的地址要少两个(网络号、广播地址),这些地址称之为主机地址。在上面的例子中,主机地址就只有192.168.0.1至192.168.0.254可以配置在计算机上了。


子网的计算:

        一个主机的IP地址是202.112.14.137,掩码是255.255.255.224,要求计算这个主机所在网络的网络地址和广播地址。

        常规办法是把这个主机地址和子网掩码都换算成二进制数,两者进行逻辑与运算后即可得到网络地址。

        另一个方法:255.255.255.224的掩码所容纳的IP地址有256-224=32个(包括网络地址和广播地址),那么具有这种掩码的网络地址一定是32的倍数。

        而网络地址是子网IP地址的开始,广播地址是结束,可使用的主机地址在这个范围内,因此略小于137而又是32的倍数的只有128,所以得出网络地址是202.112.14.128。而广播地址就是下一个网络的网络地址减1。而下一个32的倍数是160,因此可以得到广播地址为202.112.14.159。


        根据每个网络的主机数量进行子网地址的规划和计算子网掩码

        这也可按上述原则进行计算。比如一个子网有10台主机,那么对于这个子网就需要10+1+1+1=13个IP地址。(注意加的第一个1是指这个网络连接时所需的网关地址,接着的两个1分别是指网络地址和广播地址。)13小于16(16等于2的4次方),所以主机位为4位。而256-16=240,所以该子网掩码为255.255.255.240。

  如果一个子网有14台主机,不少同学常犯的错误是:依然分配具有16个地址空间的子网,而忘记了给网关分配地址。这样就错误了,因为14+1+1+1=17 ,大于16,所以我们只能分配具有32个地址(32等于2的5次方)空间的子网。这时子网掩码为:255.255.255.224。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值