linux网络配置文件

修改配置文件

 

示例:设置一个静态的ip固定的虚拟机网络配置文件(静态ipVMvare虚拟机使用桥接模式)

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eno16777736
ONBOOT=yes
IPADDR=192.168.4.111
NETMASK=255.255.255.0
GATEWAY=192.168.4.1
DNS1=114.114.114.114

一般情况下改成如下就行:(动态ipVMvare虚拟机使用NAT模式)

TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eno16777736
ONBOOT=yes

 重启命令1:

systemctl restart network

 重启命令2:

service network restart

 

如果使用的是VMvare重启之后需要设置VMvare的网络连接为桥接模式

 

 

 

 

名称不一致:

通过查找

cd /etc/udev/rules.d

ls

目录下的文件,然后编辑里边对应的文件

 

 

 

虚拟机里边装KVM,配置文件对应的路径:

/etc/libvirt/qemu/XXX.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit cepo-JK1
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>cepo-JK1</name>
  <uuid>6661f0f8-f727-4e8b-9f6b-946b8601a6ee</uuid>
  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>4194304</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Haswell-noTSX-IBRS</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/kvm/cepo-JK1.img'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdb' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='1'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:49:00:b7'/>
      <source bridge='br0'/>
      <model type='rtl8139'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <interface type='direct'>
      <mac address='52:54:00:c7:67:0a'/>
      <source dev='eno2' mode='passthrough'/>
      <model type='rtl8139'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
</domain>

 

 

 

 


 

1、ip、MASK、GW、DNS配置文件的位置:

  /etc/sysconfig/network-scripts/ifcfg-IFACE

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet   接口类型,常见的有ETHERNET(以太网)、Bridge(桥接接口)
BOOTPROTO=dhcp  激活此接口使用什么协议来配置接口属性:dhcp,boot,static,none
DEFROUTE=yes   
PEERDNS=yes  如果BOOTPROTO的值为“dhcp”,是否允许dhcp
DNS1:第一DNS服务器指向;若/etc/resolve的配置文件也有,则此处优先
DNS2:备用DNS服务器指向;
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes  是否初始化IPV6
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=0ea5507b-feab-4d8a-b582-e1874ca0bb97  此设备的唯一标识
DEVICE=ens33  此配置文件对应的设备名称
ONBOOT=yes  操作系统启动时,在系统引导过程中,是否激活此接口
IPADDR: 指明IP地址;
NETMASK:子网掩码;CentOS 7支持使用PREFIX以长度方式指明子网掩码;
GATEWAY:默认网关;
USERCTL:是否允许普通用户控制此设备;

 eg:

# vim ifcfg-eth0
    1、设置:BOOTPROTO=static
    2、增加:
        IPADDR=172.16.100.12
    NETMASK=255.255.0.0
        GATEWAY=172.16.0.1
    DNS1=172.16.0.1    
# route -n 执行命令,查看发现此时还没有生效,重启后后效
# ifconfig eth0 down ifconfig eth0 up  禁用网卡就,再重启,不建议,建议重启网络
# service network restart 重启

说明:# service network restart相当于 # /etc/rc.d/init/d/network restart 

          service命令调用上面这个脚本并传递参数restart

  2、配置路由

    配置文件位置: /etc/sysyconfig/network-scripts/route-IFACE

    支持两种配置方式,当不可混用

      (1)、每行一个路由条目:

          TARGET VIA GW

      (2)每三行一个路由条目:

           ADDRESS#=TARGET(#:0.1.2.3....)
                           NETMASK#=MASK :目标地址对应的掩码
                         GATEWAY#=NEXTHOP :指明网关 

    eg:

      # 给eno33554984配置路由文件:

[root@yph7 network-scripts]# vim route-eno33554984
[root@yph7 network-scripts]# cat route-eno33554984 
10.0.0.0/8 via 192.168.0.1
或者以下面的格式:
ADDRESS0=20.0.0.0
NETMASK0=255.0.0.0
GATEWAY0=192.168.10.2

给网卡接口配置多地址

    ip addr之外,ifconfig或配置文件都可以;

    1、ifconfig

        格式:

          # ifconfig IFACE_LABEL IPADDR/NETMASK

            说明:IFACE_LABEL:类似于eth0:0,eth0:1

        eg:

[root@localhost ~]# ifconfig ens33:0 172.22.132.26
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.22.133  netmask 255.255.255.0  broadcast 192.168.22.255
        inet6 fe80::e508:e81e:801f:4073  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d3:73:49  txqueuelen 1000  (Ethernet)
        RX packets 5235  bytes 481513 (470.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4234  bytes 488692 (477.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.132.26  netmask 255.255.0.0  broadcast 172.22.255.255
        ether 00:0c:29:d3:73:49  txqueuelen 1000  (Ethernet)

 2、ip

      格式:# ip addr add IP dev DEVICE label IFACE_LABEL

    3、给网卡别名添加配置文件

      在上面讲的设置配置文件的基础上只需要修改两个地方:

        DEVICE=“ens33:0”

        IPADDR=.....

        注意网卡别名不支持动态获取地址,所以可以用的是:static,none

 

Linux网络属性配置的tui工具

  • tui:text user interface,文本用户接口工具
  • 命令工具:# system-config-network-tui

    注意:保存之后会直接修改配置文件,不会立即有效,要重启网络服务,或者使用setup找到

 

配置当前主机的主机名

  1、命令:hostname

  2、修改配置文件

      /etc/sysconfig/nerwork

    eg:

[root@localhost ~]# cat /etc/sysconfig/network
# Created by anaconda

HOSTNAME=CentOS7
[root@localhost ~]# hostname
localhost.localdomain  重启后会改变为CentOS7
[root@localhost ~]# 

nmcli命令

   1、CentOS7上ip配置的拎一个命令

   2、格式:

     nmcli  [ OPTIONS ] OBJECT { COMMAND | help }

      OBJECT:

        1、device :查看和管理网络接口

            COMMAND = {status | show | connect | disconnect | delete | wifi | wimax }

        2、connection :开启、禁止管理网络连接           

COMMAND = { show| up | down | add | edit | modify | delete | reload | load }

         注意:connection可以缩写为conn

    eg:

      # 显示设备

[root@localhost ~]# nmcli device show
GENERAL.设备:                           virbr0
GENERAL.类型:                           bridge
GENERAL.硬盘:                           52:54:00:84:35:64
GENERAL.MTU:                            1500
GENERAL.状态:                           100 (连接的)
GENERAL.CONNECTION:                     virbr0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
IP4.地址[1]:                            192.168.122.1/24
IP4.网关:                               
IP6.网关:

# 显示设备状态

[root@localhost ~]# nmcli device status
设备        类型      状态    CONNECTION 
virbr0      bridge    连接的  virbr0     
ens33       ethernet  连接的  ens33      
lo          loopback  未管理  --         
virbr0-nic  tun       未管理  -- 

3、如何修改IP地址等属性

      格式: # nmcli  conn modify  IFACE  [+|-]setting.property  value

         说明:modify支持修改的参数如下:           

              ipv4.address

              ipv4.gateway

              ipv4.dns1

              ipv4.method

              manual

      注意:这个命令还不成熟,添加地址后,先down后up才能生效

      eg:

[root@yph6 network-scripts]# nmcli conn modify eno33554984 +ipv4.address 192.168.200.10/24
[root@yph6 network-scripts]# nmcli conn down eno33554984
[root@yph6 network-scripts]# nmcli conn up eno33554984
[root@yph6 network-scripts]# nmcli conn show

[root@yph6 network-scripts]# nmcli conn modify eno33554984 -ipv4.address 192.168.200.10/24
[root@yph6 network-scripts]# nmcli conn down eno33554984
[root@yph6 network-scripts]# nmcli conn up eno33554984
[root@yph6 network-scripts]# nmcli conn show

补充: 

    网络服务:

        network和NetworkManager

          centos6中最好用network

          centos7用NetworkManager

 

    管理网络服务

      CentOS 6:  service SERVICE {start|stop|restart|status(查看状态)}

      CentOS 7:systemctl {start|stop|restart|status} SERVICE[.service]

 

    配置文件修改之后,需要重启网络服务才能让配置文件生效哦;

      CentOS 6:# service  network  restart

      CentOS 7:# systemctl  restart  network.service

       centos7也可与6的用法一样,只是不太正规。

 

转自:https://www.cnblogs.com/FernLi/p/7040711.html