Linux 网卡配置

摘要:
在Linux的不同发行版中编辑和删除网络配置的方法彼此相似。下面介绍RHEL6系列、SUSE11和Ubuntu 12.04的配置。CentOS和RHELetthtool peth010操作后,查看哪个网卡的LED灯闪烁,eth0对应哪个网卡/etc/sysconfig/networkNETWORKING=yes#系统启动时是否启动网络HOSTNAME=z
  • 编辑
  • 删除

    Linux中不同distribution中配置网络的方法大同小异。以下介绍RHEL6系列,SUSE11, Ubuntu12.04的配置

    CentOS,RHEL

    ethtool -p eth0 10

    操作完毕后,看哪块网卡的led灯在闪,eth0就对应着哪块网卡

    /etc/sysconfig/network

  1. NETWORKING=yes            # 系统启动时是否启动网络  
  2. HOSTNAME=zhubinqiangPC    # 定义主机名  

    NETWORKING=yes # 系统启动时是否启动网络

    HOSTNAME=zhubinqiangPC # 定义主机名

     

    /etc/udev/rules.d/70-persistent-net.rules 得到网卡的MAC地址

  3. SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:58:5f:4b", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"  

    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:58:5f:4b", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

     

    /etc/sysconfig/network-scripts/ifcfg-eth0 配置网卡

    配置项有很多个,以下只列出部分重要的。具体可以参考 /usr/share/doc/initscripts-*/sysconfig.txt

  4. DEVICE=eth0                 # 网卡代号,必须与ifcfg-eth0相对于  
  5. HWADDR=00:0c:29:58:5f:4b    # MAC 地址, 如果只有一个网卡,可以省略  
  6. TYPE=Ethernet               # 网络接口格式 这里是以太网格式  
  7. UUID=371ea884-5bb7-4e03-9913-127fd7e787ae    # nmcli con 可得到 未知什么意思  
  8. ONBOOT=yes                  # 开机启动  
  9. NM_CONTROLLED=yes           # controlled by NetworkManager  
  10. BOOTPROTO=static            # static  none 功能相同,表示手动配置, dhcp表示动态获取IP  
  11. IPADDR=192.168.1.211        # IP 地址  
  12. NETMASK=255.255.255.0       # 子网掩码  
  13. GATEWAY=192.168.1.1         # 默认网关,如果有多个网卡配置文件 只须配置一个即可  
  14. USERCTL=no                  # 是否允许非root用户控制该设备  
  15. PEERDNS=yes                 # yes表示由DHCP来获取DNS no表示 /etc/resolv.conf 来控制  
  16. IPV6INIT=no                 # 是否允许IPV6  

    DEVICE=eth0 # 网卡代号,必须与ifcfg-eth0相对于

    HWADDR=00:0c:29:58:5f:4b # MAC 地址,如果只有一个网卡,可以省略

    TYPE=Ethernet # 网络接口格式这里是以太网格式

    UUID=371ea884-5bb7-4e03-9913-127fd7e787ae # nmcli con 可得到未知什么意思

    ONBOOT=yes # 开机启动

    NM_CONTROLLED=yes # controlled by NetworkManager

    BOOTPROTO=static # static none 功能相同,表示手动配置, dhcp表示动态获取IP

    IPADDR=192.168.1.211 # IP 地址

    NETMASK=255.255.255.0 # 子网掩码

    GATEWAY=192.168.1.1 # 默认网关,如果有多个网卡配置文件只须配置一个即可

    USERCTL=no # 是否允许非root用户控制该设备

    PEERDNS=yes # yes表示由DHCP来获取DNS no表示 /etc/resolv.conf 来控制

    IPV6INIT=no # 是否允许IPV6

    修改完配置文件要重启网络

  17. /etc/init.d/network restart  

    /etc/init.d/network restart

     

    通过ifconfig来配置网络

    查看所有网卡

  18. ifconfig -a  

    ifconfig -a

     

    此配置在重启网络后会失效,恢复为ifcfg-eth0为主的配置

  19. ifconfig eth0 192.168.0.211 netmask 255.255.255.0  
  20. ifconfig eth0 192.168.0.211/24  

    ifconfig eth0 192.168.0.211 netmask 255.255.255.0

    ifconfig eth0 192.168.0.211/24

     

    修改网卡MAC地址

    [ifconfig eth0 hw ether 00:0C:29:58:5F:4C  

    ifconfig eth0 hw ether 00:0C:29:58:5F:4C

    开启和禁用网卡

  21. ifconfig eth0 up  
  22. ifconfig eth0 down  

    ifconfig eth0 up

    ifconfig eth0 down

     

    ifup和ifdown来激活与关闭网卡

    用ifup来激活 ifcfg-eth0文件要配置正确, 实际是通过/sbin/ifup 来调用相关脚本。

    注意  一旦用ifconfig 来激活 就不能用ifdown来关闭。 一定要ifconfig才行。因为ifdown会比较当前网络与ifcfg-eth0是否相同,如果不同放弃ifdown的操作

  23. ifup eth0          #激活  
  24. ifdown eth0     #关闭  
  25.     
  26. ifconfig eth0 up  #激活  
  27. ifconfig eth0 down   #关闭  

    ifup eth0 #激活

    ifdown eth0 #关闭

     

    ifconfig eth0 up #激活

    ifconfig eth0 down #关闭

     

    /etc/resolv.conf 定义DNS服务器

  28. # Generated by NetworkManager  
  29. domain localdomain             
  30. search localdomain          # 查找计算机名是 默认属于该域  
  31. nameserver 192.168.157.2    # DNS 服务器地址, 可以配置多个  

    # Generated by NetworkManager

    domain localdomain

    search localdomain # 查找计算机名是默认属于该域

    nameserver 192.168.157.2 # DNS 服务器地址,可以配置多个

     

    通过DHCP获取IP

    通过修改ifcfg-ethX的BOOTPROTO=dhcp并重启网络  或者以下方法

  32. dhclient eth0  

    dhclient eth0

     

    /etc/hosts 配置

  33. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  
  34. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6  

    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

     

     

     

    修改hostname

    1.修改/etc/sysconfig/network

    2.修改/etc/hosts

     

    Ubuntu12.04

    参考官方文档 https://help.ubuntu.com/12.04/serverguide/network-configuration.html

    通过lshw 得到MAC地址

  35. lshw -c network  

    lshw -c network

    *-network
                    description: Ethernet interface
                    product: 79c970 [PCnet32 LANCE]
                    vendor: Hynix Semiconductor (Hyundai Electronics)
                    physical id: 1
                    bus info: pci@0000:02:01.0
                    logical name: eth0
                    version: 10
                    serial: 00:0c:29:77:82:a2
                    32 bits
                    clock: 33MHz
                    capabilities: bus_master rom ethernet physical logical
                    configuration: broadcast=yes driver=pcnet32 driverversion=1.35 ip=192.168.157.183 latency=64 link=yes maxlatency=255 mingnt=6 multicast=yes
                    resources: irq:19 ioport:2000(size=128) memory:d8400000-d840ffff

     

    /etc/network/interfaces 配置网卡

  36. auto lo  
  37. iface lo inet loopback  
  38. auto eth0  
  39. iface eth0 inet static       # 如果是dhcp 则把static改为dhcp  
  40.     hwaddress ether 00:13:20:f9:f4:d3   #MAC  
  41.     address 192.168.1.10     # IP 地址  
  42.     netmask 255.255.255.0    # 子网掩码  
  43.     gateway 192.168.1.1      # 网关  

    auto lo

    iface lo inet loopback

    auto eth0

    iface eth0 inet static # 如果是dhcp 则把static改为dhcp

    hwaddress ether 00:13:20:f9:f4:d3 #MAC

    address 192.168.1.10 # IP 地址

    netmask 255.255.255.0 # 子网掩码

    gateway 192.168.1.1 # 网关

     

    重启网络 生效配置

  44. /etc/init.d/networking restart  

    /etc/init.d/networking restart

     

    /etc/hosts 配置

  45. 127.0.0.1   localhost  
  46. 127.0.1.1   zbq-pc  
  47. 192.168.157.136 zbq-pc  
  48. 192.168.157.151 itms-server  
  49.     
  50. # The following lines are desirable for IPv6 capable hosts  
  51. ::1     ip6-localhost ip6-loopback  
  52. fe00::0 ip6-localnet  
  53. ff00::0 ip6-mcastprefix  
  54. ff02::1 ip6-allnodes  
  55. ff02::2 ip6-allrouters  

    127.0.0.1 localhost

    127.0.1.1 zbq-pc

    192.168.157.136 zbq-pc

    192.168.157.151 itms-server

     

    # The following lines are desirable for IPv6 capable hosts

    ::1 ip6-localhost ip6-loopback

    fe00::0 ip6-localnet

    ff00::0 ip6-mcastprefix

    ff02::1 ip6-allnodes

    ff02::2 ip6-allrouters

     

    hostname 修改 /etc/hostname

     

    SUSE11

    cat /etc/udev/rules.d/70-persistent-net.rules 查看MAC 

    修改 /etc/sysconfig/network/ifcfg-eth0

  56. BOOTPROTO='dhcp'  
  57. BROADCAST=''  
  58. ETHTOOL_OPTIONS=''  
  59. IPADDR=''  
  60. MTU=''  
  61. NAME='82545EM Gigabit Ethernet Controller (Copper)'  
  62. NETMASK=''  
  63. NETWORK=''  
  64. REMOTE_IPADDR=''  
  65. STARTMODE='auto'  
  66. USERCONTROL='no'  

    BOOTPROTO='dhcp'

    BROADCAST=''

    ETHTOOL_OPTIONS=''

    IPADDR=''

    MTU=''

    NAME='82545EM Gigabit Ethernet Controller (Copper)'

    NETMASK=''

    NETWORK=''

    REMOTE_IPADDR=''

    STARTMODE='auto'

    USERCONTROL='no'

    重启网络

  67. /etc/init.d/network restart  

    /etc/init.d/network restart

     

    修改hostname:/etc/HOSTNAME

     

     

     

    查看Linux下网卡连接状态(up还是down)?  

    2011-09-16 11:28:22|  分类:linux |  标签: |举报 |字号大中小 订阅

             

    Linux 网卡配置第1张

    用微信  "扫一扫"

    将文章分享到朋友圈。

       

    Linux 网卡配置第2张

    用易信  "扫一扫"

    将文章分享到朋友圈。

       

      下载LOFTER我的照片书  |

    1种方法

        [root@localhost /]# ifconfig -a
        eth0   Link encap:Ethernet HWaddr 00:09:6B:09:08:FC
              inet addr:192.168.10.1 Bcast:192.168.10.255 Mask:255.255.255.0
              inet6 addr: fe80::209:6bff:fe09:8fc/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
              RX packets:106732953 errors:0 dropped:0 overruns:0 frame:0
              TX packets:104379788 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:100
              RX bytes:2351331877 (2.1 GiB) TX bytes:391707945 (373.5 MiB)
              Base address:0x2500 Memory:fbfe0000-fc000000
       eth1    Link encap:Ethernet HWaddr 00:09:6B:09:08:FD
              BROADCAST MULTICAST MTU:1500 Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
              Base address:0x2540 Memory:fbfc0000-fbfe0000

     

    [root@localhost /root]# ifconfig
    eth0      Link encap:Ethernet HWaddr 00:19:DB:41:08:1B
              inet addr:211.100.10.235 Bcast:211.100.10.235 Mask:255.255.255.255
              UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
              RX packets:178812 errors:0 dropped:0 overruns:0 frame:0
              TX packets:153415 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:100
              RX bytes:33094019 (31.5 MiB) TX bytes:71911163 (68.5 MiB)
              Base address:0x8f00 Memory:fd6e0000-fd700000

    eth1      Link encap:Ethernet HWaddr 00:19:DB:41:08:1C
              inet addr:192.168.1.115 Bcast:192.168.1.255 Mask:255.255.255.0
              UP BROADCAST MULTICAST MTU:1500 Metric:1
              RX packets:13805 errors:0 dropped:0 overruns:0 frame:0
              TX packets:11356 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:1520439 (1.4 MiB) TX bytes:5406527 (5.1 MiB)
              Base address:0x7f00 Memory:fd2e0000-fd300000

    eth2      Link encap:Ethernet HWaddr 00:19:DB:41:08:1D
              inet addr:202.194.99.62 Bcast:202.194.99.62 Mask:255.255.255.255
              UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
              RX packets:158024 errors:0 dropped:0 overruns:0 frame:0
              TX packets:182280 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:74748049 (71.2 MiB) TX bytes:33535454 (31.9 MiB)
              Base address:0xdf00 Memory:fdee0000-fdf00000

    lo        Link encap:Local Loopback
              inet addr:127.0.0.1 Mask:255.0.0.0
              UP LOOPBACK RUNNING MTU:16436 Metric:1
              RX packets:99 errors:0 dropped:0 overruns:0 frame:0
              TX packets:99 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:12978 (12.6 kiB) TX bytes:12978 (12.6 kiB)

    第二种方法,通过mii-tool指令


           [root@localhost root]# mii-tool
             eth0: negotiated 100baseTx-FD, link ok
            eth1: no link
          

           [root@localhost root]# mii-tool -v
            eth0: negotiated 100baseTx-FD, link ok
             product info: vendor 00:50:43, model 2 rev 3
              basic mode:   autonegotiation enabled
              basic status: autonegotiation complete, link ok
             capabilities: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
              advertising: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control
             link partner: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
            eth1: no link
              product info: vendor 00:50:43, model 2 rev 3
              basic mode:   autonegotiation enabled
               basic status: no link
              capabilities: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD
              advertising: 100baseTx-FD 100baseTx-HD 10baseT-FD 10baseT-HD flow-control
          

          [root@localhost root]# mii-tool -w
           21:20:33 eth0: negotiated 100baseTx-FD, link ok
            21:20:33 eth1: no link

    第三种方法

    ethtool eth0

    Settings for eth0:
            Link detected: yes

     

免责声明:文章转载自《Linux 网卡配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇IOS-基础知识phpexcel设置所有单元格的默认对齐方式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WCF服务最近经常死掉

系统上线后WCF服务最近经常死掉的原因分析总结  前言     最近系统上线完修改完各种bug之后,功能上还算是比较稳定,由于最近用户数的增加,不知为何经常出现无法登录、页面出现错误等异常,后来发现是由于WCF服务时不时的就死掉了。后来就开始分析问题。得到的初步解决方案如下:   1、在Web端调用WCF服务使用后,未释放未关闭导致新的链接无法访问  ...

【IIS】设置URL重定向

名字解释:   URL转发分为隐含转发和非隐含转发,隐含转发就是指当前域名转发后,仍然显示当前域名,而非隐含转发者指当前域名转发后,显示被转发的地址。 前提:   你有一个A网站:地址是:192.168.1.223:8700                       B网站:地址是 www.baidu.com 现在要在输入A网站地址后,直接跳转到B网站中...

[WorldWind学习]8.Cache对象

MainApplication的构造函数599行实例化了worldWindow的Cache属性。 // set Upper and Lower limits for Cache size control, in bytes long CacheUpperLimit = (long)Settings.CacheSizeMega...

PHP mysqli获取数据表以及表结构

<?php$mysqli = new mysqli('localhost','root','','le');$result = $mysqli->query('SHOW TABLES');//执行查询语句//输出此数据库中表结构$tables = array();while($arr = $result->fetch_assoc()){...

【STM32H7教程】第13章 STM32H7启动过程详解

完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第13章       STM32H7启动过程详解 本章教程主要跟大家讲STM32H7的启动过程,这里的启动过程是指从CPU上电复位执行第1条指令开始(汇编文件)到进入C程序main()函数入口之间的部分。启动过程相对来说...

QTablewidget通过代理实现限制输入

QTablewidget代理 之前做过一个QTablewidget想要限制某些单元格只能输入IP,刚开始采用在单元格中添加QLineEdit控件的方法,效果差强人意。后来发现通过QItemDelegate可以很方便实现需求。 QItemDelegate  1 #pragma once 2 #include <QWidget> 3 #inc...