方法一:使用命令ifconfig
ifconfig 网络设备 IP地址 hw MAC地址 netmask 掩码地址 broadcast 广播地址 [up/down]
//网卡关闭与激活
# ifconfig eth0 down
# ifconfig eth0 up
# ifdown eth0
# ifup eth1
//查看网卡信息
# ifconfig [-a]
# ifconfig eth0
//设置IP信息
# ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
# ifconfig eth0 192.168.10.110 netmask 255.255.255.0 mtu 1412 up
# ifconfig eth1 192.168.10.221 netmask 255.255.255.0 broadcast 192.168.10.255 up
# ifconfig eth1 192.168.10.221 hw ether xx:xx:xx:xx:xx:xx netmask 255.255.255.0 broadcast 192.168.10.255 mtu 1412 up
//添加虚拟网卡
# ifconfig eth0 192.168.10.110 hw ether xx:xx:xx:xx:xx:xx netmask 255.255.255.0 up
方法二:使用命令ip address
//查看ip状态
# ip address show
# ip address show dev eth0
//添加ip地址
# ip address add 192.168.10.110/24 brd + dev eth0
# ip address add 192.168.10.221/24 brd + dev eth0 label eth0:0
//删除ip地址
# ip address del 192.168.10.110/24 brd + dev eth0
//清空ip设置
# ip address flush dev eth0
# ip address show dev eth0
//此状态下ifconfig无法查看到第二IP
# ip address add 192.168.10.110/24 brd + dev eth0
# ip address add 192.168.10.221/24 brd + dev eth0
# ip address show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 100
link/ether 00:80:c8:f8:4a:51 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.110/24 brd 192.168.10.255 scope global eth0
inet 192.168.10.221/24 brd 192.168.10.255 scope global secondary eth0
//此状态下ifconfig可以查看到第二IP
# ip address add 192.168.10.110/24 brd + dev eth0
# ip address add 192.168.10.221/24 brd + dev eth0:0
# ip address show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 100
link/ether 00:80:c8:f8:4a:51 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.110/24 brd 192.168.10.255 scope global eth0
inet 192.168.10.221/24 brd 192.168.10.255 scope global secondary eth0:0
注意问题:
ifconfig和ip address添加的IP重启后都会失效,可通过修改/etc/sysconfig/network-scripts/ifcfg-eth0,/etc/sysconfig/network-scripts/ifcfg-eth0:0设置固定IP。
参考链接:
http://linux-ip.net/html/tools-ifconfig.html
http://linux-ip.net/html/tools-ip-address.html