Network configuration under Linux - CentOS 6.x

Network configuration under Linux - CentOS 6.x

Text Keywords: Linux, CentOS, network services, eth0, LAN configuration items

First, view the network information

Do we usually see the machine's normal network, the main need to know is that the card is working properly, the relevant network information (IP address, broadcast address, subnet mask, etc.) is. In addition, we need to test whether the current machine connected to the Internet, or is able to communicate properly with the gateway.

1. ifconfig

ifconfig is one of the most commonly used tool to view and set each NIC.

  • Directly use the ifconfig command: Lists all card information currently enabled

Network configuration under Linux - CentOS 6.x

  • Using ifconfig and name card: View of a specific piece of card information

Network configuration under Linux - CentOS 6.x

2. ping

Use the ping command can be used to test network connectivity, you can help us troubleshoot the problem, usually first checks connectivity gateways, followed by an address outside the network, such as: Baidu.

  • A connectivity check with the gateway

Network configuration under Linux - CentOS 6.x

  • A connectivity check with the external network address

Network configuration under Linux - CentOS 6.x

Second, modify the network configuration

1. NIC configuration items

  • DEVICE: The device name
  • TYPE: the type of network (Ethernet, etc.)
  • ONBOOT: whether to automatically enable (yes / no) boot
  • HWADDR: MAC address (physical address)
  • BOOTPROTO: Obtaining the card configuration (dhcp / static / none / other)
  • IPADDR: IP address
  • NETMASK: Subnet Mask
  • GATEWAY: Gateway address
  • BROADCAST: Broadcast address
  • DNS: DNS server address
  • NAME: Name card
  • USERCTL: whether ordinary users can control (yes / no)

    2. Temporary entry into force

    Use ifconfig command can be configured to temporarily modify the network card, a card off to the next reboot, or restart the computer has been effective before.

  • Temporary IP address changes

Network configuration under Linux - CentOS 6.x

  • Temporary bind multiple IP addresses

Network configuration under Linux - CentOS 6.x
Network configuration under Linux - CentOS 6.x

3. permanent

Use setup tool for network configuration on the line modification, or directly modify the network card configuration file can make the configuration permanent, modify the first time, you need to manually re-start.

  • setup tool: Use the root user to enter the setup for network configuration tool

Network configuration under Linux - CentOS 6.x
可以对网卡和DNS进行设置,需要注意的是:网卡配置文件中的DNS设置拥有更高的优先级,如果你希望为所有的网卡都配置相同的DNS服务器地址,那么你应该确保网卡配置文件中没有出现DNS的配置,然后在DNS配置中指定DNS服务器地址。
Network configuration under Linux - CentOS 6.x
此处可以对网卡进行激活或者关闭,也可以移除网卡配置,选中网卡直接回车进入配置。
Network configuration under Linux - CentOS 6.x
显示星号的部分代表开启和启用,通过空格切换。当dhcp为关闭状态时才可手动修改IP地址等配置,否则为锁定状态,修改完成后保存退出。
Network configuration under Linux - CentOS 6.x
在网卡配置文件中的DNS配置会被写入到DNS解析配置中。
Network configuration under Linux - CentOS 6.x

  • 配置文件方式修改:/etc/sysconfig/network-scripts/ifcfg-eth0

使用setup工具修改的方式其实本质上也是修改网卡的配置文件。需要注意的是,如果是手动进行相关信息的配置,一定要使用正确的子网掩码和广播地址,并且IP地址一定要与网关在同一网段。对于VMware虚拟化软件:vmnet0(桥接模式)、vmnet1(仅主机模式)、vmnet8(NAT模式)分别有不同的网关。
Network configuration under Linux - CentOS 6.x
桥接模式:将虚拟机当成一台真实存在的机器,直接连入宿主机所在的网络,局域网内的其他机器也能访问到该虚拟机,则网关地址应与宿主机相同。
仅主机模式:使虚拟机只能和宿主机通信,不能连入外网(除非使用代理转发),则网关地址应和宿主机中vmnet1的网卡地址相同,通常为xxx.xxx.xxx.1,虚拟机可以使用该地址与宿主机通信。
NAT模式:是虚拟机能够和宿主机共享网络,并且也能够与宿主机通信,则网关地址默认被指配为xxx.xxx.xxx.2,宿主机中vmnet8的网卡地址为xxx.xxx.xxx.1,虚拟机可以使用该地址与宿主机通信。
Network configuration under Linux - CentOS 6.x
如果网关没有设置正确,则会直接导致虚拟机无法正常的与外网进行网络通信(尤其在NAT模式下)。方便大家复制,以下给出文本:

DEVICE=eth0
TYPE=Ethernet
UUID=b56a4da9-b82a-4b32-9ee5-b2b08f546e64
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
# 根据对应的虚拟机网卡或连入的路由器网段进行设置
IPADDR=192.168.19.145
NETMASK=255.255.255.0
GATEWAY=192.168.19.2
# DNS1相当于主DNS服务器地址
DNS1=192.168.19.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
USERCTL=no
HWADDR=00:0C:29:98:FE:94
PREFIX=24
# 最后一次连接的时间点:Unix时间戳
LAST_CONNECT=1576595442

三、网络状态控制

当我们需要让网卡的最新配置生效,通常要重启网卡,可以使用如下常用的两种方式实。

1. 服务控制

通过直接重启网络服务的方式,切记:这会波及所有网卡!所以如果是个人学习阶段没有关系,如果是在多网卡的服务器环境请慎用。

  • 命令格式:service {服务名称} {服务操作}
    • service network status:查看网络服务运行状态
    • service network start:启动网络服务
    • service network stop:停止网络服务
    • service network restart:重启网络服务

Network configuration under Linux - CentOS 6.x

2. 网卡控制

If we want to restart a piece of card, you can use ifconfig command, such as: ifconfig eth0 up / down. But I prefer to use another simple commands.

  • Disable the network card eth0: ifdown eth0
  • Enable eth0 card: ifup eth0

Fourth, the master switch and the network DNS configuration

1. The main switch network

  • The relevant configuration file: / etc / sysconfig / network

For CentOS system, there is a main switch network profile. Of course, we generally only modify the system when the host name will be used in this document, which has two configuration items, a network switch, a host name, as follows.
Network configuration under Linux - CentOS 6.x
Only total network switch is opened, to be able to properly use network services. But generally no one would yes to no, know look like, most of the major network does not work properly or NIC profile issues.

2. DNS Configuration

  • DNS configuration file: /etc/resolv.conf

Already mentioned above, the network card configuration file DNS configuration has a higher priority, after the entry into force of the NIC, DNS configuration will be written directly to the DNS configuration file. So we can directly view the contents of resolv.conf is to see what the current DNS server address of the machine to use is that if the card's configuration file does not specify a DNS address, then we can manually edit the file to add, just to wrap address a list can be.
Network configuration under Linux - CentOS 6.x

Guess you like

Origin blog.51cto.com/10984944/2459553