0


linux 网卡配置

linux网卡可以通过命令和配置文件配置,如果是桌面环境还可以通过图形化界面配置.

1.ifconfig(interfaces config)命令方式

通常需要以root身份登录或使用sudo以便在Linux机器上使用ifconfig工具。依赖于ifconfig命令中使用一些选项属性,ifconfig工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置(用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在)。

1.1命令格式
  1. ifconfig [网络设备] [参数]
1.2命令功能

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

1.3命令参数
  1. up 启动指定网络设备/网卡。
  2. down 关闭指定网络设备/网卡。该参数可以有效地阻止通过指定接口的IP信息流,如果想永久地关闭一个接口,我们还需要从核心路由表中将该接口的路由信息全部删除。
  3. arp 设置指定网卡是否支持ARP协议。 -promisc 设置是否支持网卡的promiscuous模式,如果选择此参数,网卡将接收网络中发给它所有的数据包 -allmulti 设置是否支持多播模式,如果选择此参数,网卡将接收网络中所有的多播数据包 -a 显示全部接口信息 -s 显示摘要信息(类似于 netstat -i)
  4. add 给指定网卡配置IPv6地址
  5. del 删除指定网卡的IPv6地址
  6. <硬件地址> 配置网卡最大的传输单元
  7. mtu<字节数> 设置网卡的最大传输单元 (bytes)
  8. netmask<子网掩码> 设置网卡的子网掩码。掩码可以是有前缀0x的32位十六进制数,也可以是用点分开的4个十进制数。如果不打算将网络分成子网,可以不管这一选项;如果要使用子网,那么请记住,网络中每一个系统必须有相同子网掩码。
  9. tunel 建立隧道
  10. dstaddr 设定一个远端地址,建立点对点通信
  11. -broadcast<地址> 为指定网卡设置广播协议
  12. -pointtopoint<地址> 为网卡设置点对点通讯协议
  13. multicast 为网卡设置组播标志
  14. address 为网卡设置IPv4地址
  15. txqueuelen<长度> 为网卡设置传输列队的长度
1.4使用实例
1.4.1显示网络设备信息(激活状态的)

命令:

  1. ifcofig

输出:

  1. [root@localhost ~]# ifconfig
  2. eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20
  3. inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
  4. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  5. RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
  6. TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
  7. collisions:0 txqueuelen:1000
  8. RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)
  9. lo Link encap:Local Loopback
  10. inet addr:127.0.0.1 Mask:255.0.0.0
  11. UP LOOPBACK RUNNING MTU:16436 Metric:1
  12. RX packets:68 errors:0 dropped:0 overruns:0 frame:0
  13. TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
  14. collisions:0 txqueuelen:0
  15. RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)

说明

  • eth0 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址)是 00:50:56:BF:26:20
  • inet addr 用来表示网卡的IP地址,此网卡的 IP地址是 192.168.120.204,广播地址, Bcast:192.168.120.255,掩码地址Mask:255.255.255.0
  • lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 HTTPD服务器的指定到回坏地址,在浏览器输入 127.0.0.1 就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。
  • 第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址)
  • 第二行:网卡的IP地址、子网、掩码
  • 第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节
  • 第四、五行:接收、发送数据包情况统计
  • 第七行:接收、发送数据字节数统计信息。
1.4.2启动关闭指定网卡

命令
ifconfig eth0 up
ifconfig eth0 down
输出
说明
ifconfig eth0 up 为启动网卡eth0 ;ifconfig eth0 down 为关闭网卡eth0。ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。

1.4.3为网卡配置和删除IPv6地址

命令
ifconfig eth0 add 33ffe:3240:800:1005::2/64
ifconfig eth0 del 33ffe:3240:800:1005::2/64
输出
说明
ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0配置IPv6地址;
ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0删除IPv6地址;
练习的时候,ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。

1.4.4用ifconfig修改MAC地址

命令

  1. ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE

输出

  1. [root@localhost ~]# ifconfig eth0 down //关闭网卡
  2. [root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址
  3. [root@localhost ~]# ifconfig eth0 up //启动网卡
  4. [root@localhost ~]# ifconfig
  5. eth0 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:EE
  6. inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
  7. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  8. RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
  9. TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
  10. collisions:0 txqueuelen:1000
  11. RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)
  12. lo Link encap:Local Loopback
  13. inet addr:127.0.0.1 Mask:255.0.0.0
  14. UP LOOPBACK RUNNING MTU:16436 Metric:1
  15. RX packets:68 errors:0 dropped:0 overruns:0 frame:0
  16. TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
  17. collisions:0 txqueuelen:0
  18. RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)
  19. [root@localhost ~]# ifconfig eth0 hw ether 00:50:56:BF:26:20 //关闭网卡并修改MAC地址
  20. [root@localhost ~]# ifconfig eth0 up //启动网卡
  21. [root@localhost ~]# ifconfig
  22. eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20
  23. inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
  24. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  25. RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
  26. TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
  27. collisions:0 txqueuelen:1000
  28. RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB)
  29. lo Link encap:Local Loopback
  30. inet addr:127.0.0.1 Mask:255.0.0.0
  31. UP LOOPBACK RUNNING MTU:16436 Metric:1
  32. RX packets:68 errors:0 dropped:0 overruns:0 frame:0
  33. TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
  34. collisions:0 txqueuelen:0
  35. RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)
1.4.5配置IP地址

输出:

  1. [root@localhost ~]# ifconfig eth0 192.168.120.56
  2. [root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0
  3. [root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255

说明:
ifconfig eth0 192.168.120.56
给eth0网卡配置IP地:192.168.120.56
ifconfig eth0 192.168.120.56 netmask 255.255.255.0
给eth0网卡配置IP地址:192.168.120.56 ,并加上子掩码:255.255.255.0
ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255
/给eth0网卡配置IP地址:192.168.120.56,加上子掩码:255.255.255.0,加上个广播地址: 192.168.120.255

1.4.6启用和关闭ARP协议

命令
ifconfig eth0 arp
ifconfig eth0 -arp
输出

  1. [root@localhost ~]# ifconfig eth0 arp
  2. [root@localhost ~]# ifconfig eth0 -arp

说明
ifconfig eth0 arp 开启网卡eth0 的arp协议;
ifconfig eth0 -arp 关闭网卡eth0 的arp协议;

1.4.7 设置最大传输单元

命令
ifconfig eth0 mtu 1500
输出

  1. [root@localhost ~]# ifconfig eth0 mtu 1480
  2. [root@localhost ~]# ifconfig
  3. eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F
  4. inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0
  5. UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
  6. RX packets:8712395 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:36631 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:1000
  9. RX bytes:597062089 (569.4 MiB) TX bytes:2643973 (2.5 MiB)
  10. lo Link encap:Local Loopback
  11. inet addr:127.0.0.1 Mask:255.0.0.0
  12. UP LOOPBACK RUNNING MTU:16436 Metric:1
  13. RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
  14. TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
  15. collisions:0 txqueuelen:0
  16. RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)
  17. [root@localhost ~]# ifconfig eth0 mtu 1500
  18. [root@localhost ~]# ifconfig
  19. eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F
  20. inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0
  21. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  22. RX packets:8712548 errors:0 dropped:0 overruns:0 frame:0
  23. TX packets:36685 errors:0 dropped:0 overruns:0 carrier:0
  24. collisions:0 txqueuelen:1000
  25. RX bytes:597072333 (569.4 MiB) TX bytes:2650581 (2.5 MiB)
  26. lo Link encap:Local Loopback
  27. inet addr:127.0.0.1 Mask:255.0.0.0
  28. UP LOOPBACK RUNNING MTU:16436 Metric:1
  29. RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
  30. TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
  31. collisions:0 txqueuelen:0
  32. RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)
  33. [root@localhost ~]#

说明:
设置能通过的最大数据包大小为1500bytes

2.配置文件方式

ubuntu配置文件:

  1. /etc/network/interfaces
  1. auto lo
  2. iface lo inet loopback
  3. auto eth0 #配置静态ip
  4. iface eth0 inet static
  5. address 192.168.1.100
  6. netmask 255.255.255.0
  7. gateway 192.168.1.1

centos配置文件:

  1. /etc/sysconfig/network-scripts/ifcfg-eth0
  1. DEVICE=eth0(默认)
  2. HWADDR=00:0C:29:2E:36:16(默认)
  3. TYPE=Ethernet(默认)
  4. UUID=XXXXXXX(默认)
  5. ONBOOT=yes(默认为no,修改为yes意为每次reboot ifup eth0)
  6. MM_CONTROLLED=yes(默认)
  7. #BOOTPROTO=dhcp(dhcp为自动分配ip地址,我们把他注释了,在下面另外加)
  8. BOOTPROTO=static(新添加)
  9. IPV6INIT=no(新添加)
  10. USERCTL=no(新添加)
  11. IPADDR=192.168.164.100(新添加)
  12. NETMASK=255.255.255.0(新添加)

service network restart重启网卡服务

3.图形界面方式

添加虚拟网卡

一台服务器需要设置多个ip,但又不想添加多块网卡,那就需要设置虚拟网卡.这里介绍几种方式在linux服务器上添加虚拟网卡.
比如向eth0中添加一块虚拟网卡:

1.快速创建删除虚拟网卡
  1. sudo ifconfig eth0: 192.168.10.10 up

以上的命令就可以在eth0网卡上创建一个叫eth0:0的虚拟网卡,他的地址是:192.168.1.63
如果不想要这个虚拟网卡了,可以使用如下命令删除:

  1. sudo ifconfig eth0:0 down

重启服务器或者网络后,虚拟网卡就没有了.

2.修改网卡配置文件

在ubuntu下,网卡的配置文件是

  1. /etc/network/interfaces

,所以我们修改它:

  1. sudo vim /etc/network/interfaces

在这个文件中增加如下内容并保存:

  1. auto eth0:0
  2. iface eth0:0 inet static
  3. address 192.168.10.10
  4. netmask 255.255.255.0
  5. #network 192.168.10.1
  6. #broadcast 192.168.1.255

保存后,我们需要重启网卡(重新加载配置文件)才会生效,使用如下命令重启:

  1. sudo /etc/init.d/networking restart

他的优点是重启服务器或者网卡配置不会丢失。

3.创建tag

前两种方法都有一个特点,创建的网卡可有不同的ip地址,但是Mac地址相同。无法用来创建虚拟机。
添加虚拟网卡tap

  1. tunctl -b

其他配置命令:

显示网桥信息:

  1. brctl show

添加网桥:

  1. brctl addbr virbr0

激活网桥:

  1. ip link set virbr0 up

添加虚拟网卡tap:

  1. tunctl -b

tap0 -------> 执行上面使命就会生成一个tap,后缀从0,1,2依次递增
激活创建的tap:

  1. ip link set tap0 up

将tap0虚拟网卡添加到指定网桥上:

  1. brctl addif br0 tap0

给网桥配制ip地址:

  1. ifconfig virbr1 169.254.251.4 up

将virbr1网桥上绑定的网卡eth5解除:

  1. brctl delif virb1 eth5 ```
  2. 给virbr1网桥添加网卡eth6:`brctl addif virbr1 eth6 `
标签: linux php 网络

本文转载自: https://blog.csdn.net/sjw890821sjw/article/details/141387858
版权归原作者 轻口味 所有, 如有侵权,请联系我们删除。

“linux 网卡配置”的评论:

还没有评论