ip、ifconfig命令与IP

ifconfig命令应该是属于linux下的入门命令了。不过,最近在某技术群里看到有人提到在某公司面试时,被一道关于ifconfig用法的问题给难住而被拒之门外的 。该问题的要求就是在linux下不重新情况下,如何临时增加一个IP及临时删除一个IP 。该问题除了可以通过ifconfig命令完成外,也可以通过ip命令完成,不过两者是有区别的。

一、ifconfig与IP

1、ifconfig添加或修改原IP

ifconfig eth0 192.168.10.199  
ifconfig eth0 192.168.10.199 netmask 255.255.255.0 up
ifconfig eth0:1 192.168.10.198 netmask 255.255.255.0 up

注:以上两台效果是一样的,上面一种写法是下面一种写法的减缩版。如果eth0上之前已经配置这IP,该配置会将原来的IP清掉,换成上面配置的IP,但在远程ssh时最好不要使用该方法,因为网络环境不同。一旦更改不生效,就要跑到机房再进行配置。

2、禁用启用网卡

ifconfig eth0 down
ifconfig eth0 up

该用法,是不是和ifup eth0、ifdown eth0:1很像?

注:当一块网卡上配置多个IP时,如eth0、eth0:1时,如果禁掉eth0:1时,eth0上的网卡配置依然生效。但禁掉直接物理网卡口时(即eth0)时,其后面配置的IP (eth0:1等)都将被删除掉。另外,ifconfig 还可以用于设置mtu和设置网卡的混杂模式:

ifconfig eth0 mtu 1472
利用netstat -i查看
eth0设置成混杂模式
ifconfig eth0 promisc
取消混杂
ifconfig eth0 -promisc 

3、修改网卡mac地址:

ifconfig eth0 hw ether xxxxxxxxxxxx

ifconfig查看的信息里,经常被我们忽视的第三行非常有用,如在没有mii-tool工具时,可以通过其查看网卡连接状态。

UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节

二、ip命令与IP

ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig、route等,使用权限为超级用户。

1、ip命令添加一个IP地址:

[root@localhost ~]# ip addr add 192.168.10.198/24 dev eth0:1
[root@localhost ~]# ip addr add 192.168.10.199/24 dev eth0
[root@localhost ~]# ip -f inet addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    inet 192.168.10.26/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.198/24 scope global secondary eth0
    inet 192.168.10.199/24 scope global secondary eth0
[root@localhost ~]# ip addr add 192.168.10.200/24 dev eth0:3
[root@localhost ~]# ip -f inet addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    inet 192.168.10.26/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.198/24 scope global secondary eth0
    inet 192.168.10.199/24 scope global secondary eth0
    inet 192.168.10.200/24 scope global secondary eth0

当然,上面的增加地址的写法,我们也可以使用以下两种方式增加,不过由于没有上面的写法容易记,我平时很少会用下面的方式增加:

ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0
ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1

由上面的操作命令不难看出,随便我们怎么去添加IP,后面的设备名无论是eth0、eth0:1、eth0:100也好,其都不会将原网卡上绑定的地址给清掉。其通过ip addr show 显示的出的结果都是secondary eth0 。

注:ip addr命令增加的IP ,不能通过ifconfig查看到,也不能通过ifconfig eth0:1 down 或ifdown eth0:1 这样的方式停掉。

2、ip命令删除一个IP

[root@localhost ~]# ip addr del 192.168.10.200
Not enough information: "dev" argument is required.
[root@localhost ~]# ip addr del 192.168.10.200 dev eth0
Warning: Executing wildcard deletion to stay compatible with old scripts.
         Explicitly specify the prefix length (192.168.10.200/32) to avoid this warning.
         This special behaviour is likely to disappear in further releases,
         fix your scripts!
[root@localhost ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 40:61:86:98:95:05 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.26/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.198/24 scope global secondary eth0
    inet 192.168.10.199/24 scope global secondary eth0
    inet6 fe80::4261:86ff:fe98:9505/64 scope link
       valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
    link/sit 0.0.0.0 brd 0.0.0.0
[root@localhost ~]# ip addr del 192.168.10.199/24 dev eth0 

在不加掩码删除时,其会提示警告,但还是可以将其地址删掉。ip命令的用法比较多,就不一一列举了。

三、路由配置

增加路由

route add -net 192.168.6.0/24 gw 192.168.101.254
route add default gw 192.168.101.254

查看路由

ip route list
route n
netstat r

四、总结

以上的ifconfig和ip命令配置的信息,重启都会清除,想要永久生效,还是配置相关的配置文件。不过掌握命令配置方法很重要,在LVS+keepalive等架构上,浮动IP的变动,很多都是通过ip命令来完成的。

ifconfig和ip的底层实现上的区别可以查看http://blog.csdn.net/dog250/article/details/5303542 。



猜你喜欢

转载自blog.csdn.net/shangyuanlang/article/details/80767868