Linux ifconfig命令详解


ifconfig(interfaces config)。通常需要以root身份登录或使用sudo来使用ifconfig工具

ifconfig 命令用来查看和配置网络设备。当网络环境发生改变时可通过此命令对网络进行相应的配置。

备注:用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。

ifconfig常见命令参数

Usage:
ifconfig [-a] [-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] ...

常用的命令展示

查看当前系统有几个网卡

[root@localhost ~]# ifconfig |grep eth* | awk -F '[ ]+' '{print $1}'

Linux ifconfig命令详解_重启

启动关闭指定网卡

ifconfig eth0 up        # 启动
ifcfg etho up           # 启动
ifconfig eth0 down # 关闭
ifcfg eth0 down         # 关闭
ifconfig eth0 reload # 重启
说明: ifconfig eth0 up 为启动网卡eth0 ;ifconfig eth0 down 为关闭网卡eth0。
ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。

Linux ifconfig命令详解_Linux命令_02

为网卡配置和删除IPv6地址【临时生效,永久生效需要更改配置文件】

eth2网卡配置文件: /etc/sysconfig/network-scripts/ifcfg-eth2

ifconfig eth0 add 33ffe:3240:800:1005::2/64             # 为网卡eth0配置IPv6地址 

ifconfig eth0 del 33ffe:3240:800:1005::2/64 # 为网卡eth0删除IPv6地址

为网卡配置和删除IPv4地址【临时生效,永久生效需要更改配置文件】

eth2网卡配置文件: /etc/sysconfig/network-scripts/ifcfg-eth2

[root@localhost ~]# ifconfig eth0 192.168.25.166 netmask 255.255.255.0 up
[root@localhost ~]# ifconfig eth0 192.168.25.166/24 up       【效果同上】
[root@localhost ~]# ip addr add 192.168.25.166/24  dev eth0  【效果同上】

Linux ifconfig命令详解_#if_03

[root@localhost ~]# ifconfig eth0:ws 192.168.25.166 netmask 255.255.255.0 up
[root@localhost ~]# ifconfig eth0:ws 192.168.25.166/24 up       【效果同上】
[root@localhost ~]# ip addr add 192.168.25.166/24  dev eth0:ws  【效果同上】

Linux ifconfig命令详解_重启_04

[root@localhost ~]#ifconfig eth0:ws  192.168.25.166 netmask 255.255.255.0 down
[root@localhost ~]#ifconfig eth0:ws  192.168.25.166/24 dwon     【效果同上】
[root@localhost ~]# ip addr del 192.168.25.166/24  dev eth0:ws  【效果同上】

Linux ifconfig命令详解_Linux命令_05

用ifconfig修改MAC地址

ifconfig eth0 down //关闭网卡
ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址
ifconfig eth0 up    //启动网卡

启用和关闭ARP协议

ifconfig eth0:ws arp 

ifconfig eth0:ws -arp

设置最大传输单元

ifconfig eth0 mtu 1500


网卡配置文件

网卡eth2对应: /etc/sysconfig/network-scripts/ifcfg-eth2

网卡eth0对应: /etc/sysconfig/network-scripts/ifcfg-eth0

根据网卡名称找对应的文件名称即可

[root@localhost omd]# cat /etc/sysconfig/network-scripts/ifcfg-eth2 
HWADDR=00:0c:29:E4:35:5D
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.25.133
PREFIX=24
GATEWAY=192.168.25.2
DNS1=192.168.25.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="eth2"
UUID=6e6f9829-0737-4943-ab21-61d6173ba8c4
ONBOOT=yes
LAST_CONNECT=1438160743
DEVICE=eth2
USERCTL=no

网卡的硬件信息

网卡出现乱序,多是因为Mac和网卡名称不一致导致,需要更改此网卡的硬件信息

[root@localhost ~]# cat /etc/udev/rules.d/70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x1022:0x2000 (vmxnet)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:cc:16:f0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x1022:0x2000 (vmxnet)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:e4:35:5d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"


作者:​​小a玖拾柒​​​ ​

-------------------------------------------

个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~

本文版权归作者【​​小a玖拾柒​​​】,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!