Linux Configuration of IPv4 or IPv6 Addresses
Configuration
Configuration Introduction
- • View Network
ifconfig
- • Network Interface Introduction
- •
<span>eth0</span>: Local network interface (CentOS7 uses ens33) - •
<span>lo</span>: Internal network interface, manages internal IP, which is the 127.0.0.1 address - •
<span>virbr0</span>: Virtual network interface - • Configuration File
<span>ifcfg-<interface></span> - •
<span>ifcfg-eth0</span>: Configuration file for the interface<span>eth0</span>(both<span>ipv4</span>and<span>ipv6</span>are configured in this file) - •
<span>ifcfg-lo</span>: Configuration file for the interface<span>lo</span>Tips: Configuration files are located in the directory<span>/etc/sysconfig/network-scripts/</span>;
Automatically Obtain Address
IPv4 is set to automatically obtain an address by default, no configuration is needed; the following is the configuration to enable automatic address acquisition for IPv6
- 1. Modify the
<span>/etc/sysconfig/network</span>file| | | | --- | --- | | | # Enable network IPv4 | | | NETWORKING=yes | | | | | | # Enable network IPv6, add if not present, some machines have it enabled by default | | | NETWORKING_IPV6=yes | | | | | | # Hostname, takes effect after restart | | | HOSTNAME=localhost.localdomain | | | | - 2. Modify the
<span>/etc/sysconfig/network-script/ifcfg-eth0</span>file| | | | --- | --- | | | # Enable ipv6 address on boot | | | IPV6INIT=yes | - 3. Restart the network interface and test
Static Address
A static address is configured by adding static IP settings in the
<span>ifcfg-<interface></span>file
- • Edit the file
vim /etc/sysconfig/network-scripts/ifcfg-eth0
- • Add IPv4 configuration
| | | | --- | --- | | | DNS1=192.168.0.1 | | | IPADDR=192.168.1.188 | | | PREFIX=24 |Tips: To configure a static IP, modify
<span>BOOTPROTO=static</span> - • Add IPv6 configuration
| | | | --- | --- | | | IPV6INIT=yes | | | IPV6_AUTOCONF=no | | | IPV6_FAILURE_FATAL=no | | | IPV6ADDR=2001:250:250:250:250:250:250:222/64 | | | IPV6_DEFAULTGW=2001:250:250:250::1 | - • Common configuration for
<span>ifcfg-ethX</span>file| | | | --- | --- | | | # Type | | | TYPE=Ethernet | | | # Associated interface name, must match interface | | | DEVICE=eth0 | | | # Name of the network connection | | | NAME=eth0 | | | # Unique identifier | | | UUID=b4701c26-8ea8-46a5-b738-1d4d0ca5b5a9 | | | # Automatically connect, whether to activate this interface on startup or restart | | | ONBOOT=yes | | | # Boot protocol, indicates how to obtain IP | | | ### static | none: use static method | | | ### dhcp: use DHCP protocol | | | BOOTPROTO=static | | | | | | ## Configuration information IPv4 configuration | | | # DNS server | | | DNS1=192.168.0.1 | | | # IP address | | | IPADDR=192.168.1.188 | | | # CentOS subnet mask length: 24 --> 255.255.255.0 | | | # NETMASK=255.255.255.0 | | | PREFIX=24 | | | # Default gateway | | | GATEWAY=192.168.1.1 | | | # IP2, IP3 ... | | | IPADDR2=192.168.2.23 | | | PREFIX2=24 | | | GATEWAY2=192.168.2.1 | | | # Disable device if ipv4 configuration fails | | | IPV4_FAILURE_FATAL=no | | | # Whether to set this interface as the default route for ipv4 | | | DEFROUTE=yes | | | | | | # Whether to use IPV6 address: yes to use; no to disable | | | IPV6INIT=yes | | | # Whether to connect automatically yes for automatic, no for manual | | | IPV6_AUTOCONF=yes | | | # Whether to set this interface as the default route for ipv6 | | | IPV6_DEFROUTE=yes | | | # Disable device if ipv6 configuration fails | | | IPV6_FAILURE_FATAL=no | | | IPV6_ADDR_GEN_MODE="stable-privacy" | | | | | | # Address ipv6 configuration information, if not using ipv6, this can be omitted | | | IPV6_PEERDNS=yes | | | IPV6_PEERROUTES=yes | | | IPV6_PRIVACY=no |
Tips:
- • TYPE, BOOTPROTO, NAME, DEVICE, ONBOOT, IPV6INIT must exist;
- • DNS1, IPADDR, GATEWAY, PREFIX/NETMASK are required for static IP configuration;
Temporary Address
A temporary address refers to a configuration that becomes invalid after a system reboot or network interface restart;
Common Network Configuration Commands
Configure IPv4
- 1. ifconfig configuration
- • Configure temporary ipv4 address
- 1. ifconfig eth0 192.168.5.18 [up|down]
- 2. ip addr add 192.168.5.18/24 dev eth0
route add -host 192.168.5.18 gw 192.168.5.1 dev eth0
- 1. ip route add default via 192.168.5.1
Configure IPv6
- 1. Check ipv6 module
- • Check if the ipv6 module is loaded
lsmod | grep ipv6
- • If not loaded, execute this command to load it
modprobe ipv6
- • Configure temporary ipv6 address
ifconfig eth0 inet6 add IPV6ADDR
- • Configure ipv6 gateway route [add|del] [-net|-host] [network or host][netmask mask] [gw default-ip] [dev interface name] route [add|del] default [gw nexthop]
route -A inet6 add default gw IPV6GATEWAY dev ethX
- • Example
| | | | --- | --- | | | ifconfig eth0 inet6 add 2001:250:250:250:250:250:250:222/64 | | | route -A inet6 add default gw 2001:250:250:250::1 dev eth0 |
Restart Network Interface
- • CentOS 7
systemctl restart network
- • CentOS 6
service network restart
Testing
- • ping
ping | ping6 [-I interface] address
- • IPv4 Test
<span>ping [-I eth0] address</span> - • ping 192.168.5.18
- • IPv6 Test
<span>ping6 [-I eth0] address</span>Tips: You can also use the command
<span>ifconfig</span>to view IPV6 address information; the system will automatically assign a local link address starting with “fe80:” as well as a globally unique IPv6 address that we manually configure. - • ping6 2001:250:250:250:250:250:250:222
Link: https://www.cnblogs.com/librarookie/p/16288388.html
(Copyright belongs to the original author, please delete if infringing)