How to Set IP addresses in Linux

Source: Internet
Author: User
Static Method
Note: All operations use the root user.
Modify IP Address:
Edit file/etc/sysconfig/network-scripts/ifcfg-eth0
Device = eth0 // device name, do not modify
Bootproto = static // do not modify
Broadcast = 10.10.22.255 // broadcast address, which is generally the last IP address of the network segment
Ipaddr = 10.10.22.145 // ip address
Netmask = 255.255.255.0 // Subnet Mask
Network = 10.10.22.0 // CIDR block address
Onboot = Yes // do not modify
Type = Ethernet // do not modify
Note: The ifcfg-eth0 is the first Nic, The ifcfg-eth1 is the second Nic, and so on

Add IP Address:
You can recompile a file named/etc/sysconfig/network-scripts/ifcfg-eth0: 0
You can also add it directly in/etc/sysconfig/network-scripts/ifcfg-eth0 (but the device eth0 must have)
Device = eth0: 0 // device name, do not modify
Bootproto = static // do not modify
Broadcast = 10.10.33.255 // broadcast address, which is generally the last IP address of the network segment
Ipaddr = 10.10.33.145 // ip address
Netmask = 255.255.255.0 // Subnet Mask
Network = 10.10.33.0 // network segment address
Onboot = Yes // do not modify
Type = Ethernet // do not modify

And so on. If you add another IP address, add the following Configuration:
Device = eth0: 1 // device name, do not modify
Bootproto = static // do not modify
Broadcast = 10.10.44.255 // broadcast address, which is generally the last IP address of the network segment
Ipaddr = 10.10.44.145 // ip address
Netmask = 255.255.255.0 // Subnet Mask
Network = 10.10.44.0 // CIDR block address
Onboot = Yes // do not modify
Type = Ethernet // do not modify

Similarly, if you are on the second Nic, you need to add:
Device = eth1: 0 // device name, do not modify
Bootproto = static // do not modify
Broadcast = 10.10.33.255 // broadcast address, which is generally the last IP address of the network segment
Ipaddr = 10.10.33.145 // ip address
Netmask = 255.255.255.0 // Subnet Mask
Network = 10.10.33.0 // network segment address
Onboot = Yes // do not modify
Type = Ethernet // do not modify

Add the Default Gateway:
Note that only one machine can have one default gateway; otherwise, a specific routing method should be provided.
Add one to the related device configuration. For example, in the above configuration, add a default gateway to the first IP address of the first NIC:
Device = eth0
Bootproto = static
Broadcast = 10.10.22.255
Ipaddr = 10.10.22.145
Netmask = 255.255.255.0
Network = 10.10.22.0
Onboot = Yes
Type = Ethernet
Gateway = 10.10.22.3

After the modification, You need to restart the machine or network to make the modification take effect.
The method to restart the machine is: reboot
The method to restart the network is: Service Network restart

The preceding method adds IP addresses statically, that is, it is still effective after the machine is restarted.

Dynamic Method
Note: All operations use the root user.
Modify IP Address:
Ifconfig eth0 10.10.22.145
Change the IP address of the first ENI to 10.10.22.145.

Add IP Address:
Ifconfig eth0 add 10.10.33.145 // Add an IP address
Ifconfig eth0: 0 broadcast 10.10.33.255 // modify the broadcast address of the added IP Address

Add another IP Address:
Ifconfig eth0: 0 add 10.10.44.145
Ifconfig eth0: 0: 1 Broadcast 10.10.44.255 // modify the broadcast address of the added IP Address
Do not perform the following operations:
Ifconfig eth0 add 10.10.44.145
In this way, the added ip10.33.145 is changed to 10.10.44.145.

Add another IP Address:
Ifconfig eth0: 0: 1 add 10.10.55.145
Ifconfig eth0: 0: 1: 1 Broadcast 10.10.55.255 // modify the broadcast address of the added IP Address

And so on
After adding the above, it takes effect immediately, but the machine becomes invalid after it is restarted.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.