所有的鸟儿他们都知道, 他们的巢应该筑在什么地方, 鸟儿知道自己该在什么地方筑巢, 那就意味着他们了解他们自己的使命。 我们身为万物之灵的人类, 怎么会不知道,连鸟儿都知道的道理呢?
Linux Static IP Configuration
Linux Static IP Configuration

Linux Static IP Configuration

 Published: Thursday, July 16th, 2009 by Phil Paradis

By default, Linodes use DHCP to acquire their IP address, routing and DNS information. However, DHCP will only assign one IP to your Linode, so if you have multiple IPs, you’ll need to use a static configuration. Even if you only have one IP, you can still do a static assignment, but it’s not required in most cases.

 Contents

Obtain Networking Info

Before you edit any files, you’ll need to obtain some information. Log into the Linode Manager and click the "Remote Access" tab. You’ll find your IP addresses (both public and private, if you have a private IP assigned), gateways, netmasks and DNS resolvers.

Keep this information handy, because you’ll need to refer to it as you configure your Linode’s network settings. Since Linodes only have one virtual ethernet interface (eth0), you’ll need to assign additional IPs to aliases on that interface. This means you’ll append a colon and a number to the interface name. For these examples, the aliases are numbered in the order they were given, but most outbound connections will originate from the IP assigned to the eth0 interface. If you need server daemons to bind to a particular IP address, you’ll need to specify the correct IP in their configuration files.

Please note that although your VPS may have multiple IP addresses assigned to it, you should only specify a default gateway for one IP. This gateway should be the one that corresponds to the IP address you are setting it on. For example, if you are setting the default gateway for "12.34.56.78" you should use "12.34.56.1" for the gateway.

A default gateway should not be specified for private IP addresses. Additionally, the subnet mask for private IP addresses should be set to "255.255.128.0" (not "255.255.255.0").

Important: You must reboot your Linode after adding an IP address to it before the changes will take effect.

Hostname and FQDN Settings

General Information

If you haven’t already done so, set your system’s hostname and FQDN (fully qualified domain name). Your hostname should be something unique; some people name their systems after planets, others after philosophers, etc. Please note that the system’s hostname has no relationship to websites or email services hosted on it, aside from providing a name for the system itself. Thus, your hostname should not be "www" or anything else too generic.

Debian and Ubuntu

Issue the following commands to set the hostname, replacing "plato" with the hostname of your choice:

echo "plato" > /etc/hostname
hostname -F /etc/hostname

If it exists, edit the file /etc/default/dhcpcd to comment out the "SET_HOSTNAME" directive:

File excerpt:/etc/default/dhcpcd

#SET_HOSTNAME='yes'

Proceed to the section entitled "Update /etc/hosts" to continue.

CentOS/Fedora

Issue the following commands to set the hostname, replacing "plato" with the hostname of your choice:

echo "HOSTNAME=plato" >> /etc/sysconfig/network
hostname "plato"

Proceed to the section entitled "Update /etc/hosts" to continue.

Slackware

Issue the following commands to set the hostname, replacing "plato" with the hostname of your choice:

echo "plato" > /etc/HOSTNAME
hostname -F /etc/HOSTNAME

Proceed to the section entitled "Update /etc/hosts" to continue.

Gentoo

Issue the following commands to set the hostname, replacing "plato" with the hostname of your choice:

echo "HOSTNAME=\"plato\"" > /etc/conf.d/hostname
/etc/init.d/hostname restart

Proceed to the section entitled "Update /etc/hosts" to continue.

Arch

Issue the following commands to set the hostname, replacing "plato" with the hostname of your choice:

echo "HOSTNAME=\"plato\"" > /etc/conf.d/hostname
hostname plato

Proceed to the section entitled "Update /etc/hosts" to continue.

Update /etc/hosts

Next, edit your /etc/hosts file to resemble the following example, replacing "plato" with your chosen hostname, "example.com" with your system’s domain name, and "12.34.56.78" with your system’s IP address. As with the hostname, the domain name part of your FQDN does not necesarily need to have any relationship to websites or other services hosted on the server (although it may if you wish). As an example, you might host "www.something.com" on your server, but the system’s FQDN might be "mars.somethingelse.com."

File:/etc/hosts

127.0.0.1        localhost.localdomain    localhost
12.34.56.78      plato.example.com        plato

The value you assign as your system’s FQDN should have an "A" record in DNS pointing to your Linode’s IP address. For more information on configuring DNS, please see our guide on configuring DNS with the Linode Manager.

DNS Resolver Settings

If you’ve migrated to a new location, you may need to edit your /etc/resolv.conf file so that your Linode can resolve DNS queries. Your nameservers are listed under the "Remote Access" tab. The search and domain lines are optional, but you should definitely include the "options rotate" line.

In the example below, change the IP addresses to reflect the values shown under the "Remote Access" tab of the Linode Manager.

File:/etc/resolv.conf

domain members.linode.com
search members.linode.com
nameserver 98.76.54.32
nameserver 76.54.32.10
options rotate

Static IP Configuration

Debian & Ubuntu

Since Ubuntu is based on Debian, their configuration is the same. The relevant file to edit is /etc/network/interfaces – the file syntax is fairly straightforward, but you can read the comments in the file for more details about what each line does.

In the example below, change the IP addresses to reflect the values shown under the "Remote Access" tab of the Linode Manager.

File:/etc/network/interfaces

# The loopback interface
auto lo
iface lo inet loopback

# Configuration for eth0 and aliases

# This line ensures that the interface will be brought up during boot.
auto eth0 eth0:0 eth0:1

# eth0 - This is the main IP address that will be used for most outbound connections.
# The address, netmask and gateway are all necessary.
iface eth0 inet static
 address 12.34.56.78
 netmask 255.255.255.0
 gateway 12.34.56.1

# eth0:0
# This is a second public IP address.
iface eth0:0 inet static
 address 34.56.78.90
 netmask 255.255.255.0

# eth0:1 - Private IPs have no gateway (they are not publicly routable) so all you need to
# specify is the address and netmask.
iface eth0:1 inet static
 address 192.168.133.234
 netmask 255.255.128.0

Restart networking:

/etc/init.d/networking restart

From the Linode, ping each of the default gateways listed on the "Remote Access" tab of the Linode Manager:

ping 12.34.56.1
ping 98.76.54.1

Once you have confirmed that your networking settings have been correctly configured, issue the following command to uninstall the DHCP client, as it is no longer required.

apt-get remove isc-dhcp-client dhcp3-client dhcpcd

CentOS & Fedora

Note: CentOS & Fedora may include two services to configure networking, and having the wrong one enabled may cause networking to not come up after making these changes. You can check your configuration using these instructions.

CentOS keeps the information for each interface in a separate file named /etc/sysconfig/network-scripts/ifcfg-<interface_alias_name> so you’ll need to create one foreth0 and one for each alias.

In the example below, change the IP addresses to reflect the values shown under the "Remote Access" tab of the Linode Manager.

File:/etc/sysconfig/network-scripts/ifcfg-eth0

# Configuration for eth0
DEVICE=eth0
BOOTPROTO=none

# This line ensures that the interface will be brought up during boot.
ONBOOT=yes

# eth0 - This is the main IP address that will be used for most outbound connections.
# The address, netmask and gateway are all necessary.
IPADDR=12.34.56.78
NETMASK=255.255.255.0
GATEWAY=12.34.56.1

File:/etc/sysconfig/network-scripts/ifcfg-eth0:0

# Configuration for eth0:0
DEVICE=eth0:0
BOOTPROTO=none

# This line ensures that the interface will be brought up during boot.
ONBOOT=yes

# eth0:0
IPADDR=34.56.78.90
NETMASK=255.255.255.0

File:/etc/sysconfig/network-scripts/ifcfg-eth0:1

# Configuration for eth0:1
DEVICE=eth0:1
BOOTPROTO=none

# This line ensures that the interface will be brought up during boot.
ONBOOT=yes

# eth0:1 - Private IPs have no gateway (they are not publicly routable) so all you need to
# specify is the address and netmask.
IPADDR=192.168.133.234
NETMASK=255.255.128.0

Restart networking:

service network restart

From the Linode, ping each of the default gateways listed on the "Remote Access" tab of the Linode Manager:

ping 12.34.56.1
ping 98.76.54.1

Arch Linux

In the example below, change the IP addresses to reflect the values shown under the "Remote Access" tab of the Linode Manager. All other details should remain the same. The configuration of networking states occurs in the /etc/rc.conf file. Add a hash sign (e.g. #) to the beginning of the existing INTERFACES and eth0 lines to convert them to comments, then add the following lines to your rc.conf file.

File excerpt:/etc/rc.conf

eth0="eth0 12.34.56.78 netmask 255.255.255.0"
eth0_0="eth0:0 21.43.65.87 netmask 255.255.255.0"
eth0_1="eth0:1 192.168.134.241 netmask 255.255.128.0"

INTERFACES=(eth0 eth0_0 eth0_1)

gateway="default gw 12.34.56.1"
ROUTES=(gateway)

In this example, two static public IP addresses are configured on aliases of eth0eth0:0, and eth0:1. Ensure that every interface configuration is included in the INTERFACES list. When you have configured these, issue the following command:

/etc/rc.d/network restart

Due to the way that Arch Linux handles network interfaces, this command will report that the "Stopping the Network Interfaces" operation has failed. When you issue the commandifconfig, you will be able to see the properly configured interfaces.

From the Linode, ping each of the default gateways listed on the "Remote Access" tab of the Linode Manager:

ping 12.34.56.1
ping 98.76.54.1

Gentoo

In the example below, change the IP addresses to reflect the values shown under the "Remote Access" tab of the Linode Manager. You’ll be setting up a static public IP on eth0 and a static private IP on eth0:1. We recommend logging into Lish, since you’ll be restarting the network.

First, open /etc/conf.d/net and comment out your existing config_ethX and routes_ethX lines:

#config_eth0=( "12.34.56.XXX netmask 255.255.255.0" )
#routes_eth0=( "default gw 12.34.56.1" )
#config_eth1=( "192.168.133.XXX netmask 255.255.128.0" )

Using the comments in /etc/conf.d/net.example as your guide, create a new config_eth0 entry that contains both of your addresses in CIDR notation:

config_eth0=(
        "12.34.56.XXX/24"
        "192.168.133.XXX/17"
)

Tip: You can use the free whatmask online tool to get the correct CIDR entry for your netmask.

Now add a new routes_eth0 entry:

routes_eth0=( "default via 12.34.56.1" )

Full example of working /etc/conf.d/net:

config_eth0=(
        "12.34.56.XXX/24"
        "192.168.133.XXX/17"
)

routes_eth0=( "default via 12.34.56.1" )

dns_servers="34.56.78.90 34.56.78.91 34.56.78.92"
dns_search="example.com members.linode.com"
dns_domain="example.com"

Stop your existing eth1 interface and remove it from startup, if applicable:

/etc/init.d/net.eth1 stop
rc-update del net.eth1 default
rm /etc/init.d/net.eth1

Restart networking:

/etc/init.d/net.eth0 restart

From the Linode, ping each of the default gateways listed on the "Remote Access" tab of the Linode Manager:

ping 12.34.56.1
ping 98.76.54.1

OpenSuse

In the example below, change the IP addresses to reflect the values shown under the "Remote Access" tab of the Linode Manager.

File:/etc/sysconfig/network/ifcfg-eth0

# Configuration for eth0
BOOTPROTO='static'

# This line ensures that the interface will be brought up during boot.
STARTMODE='onboot'

# eth0 - This is the main IP address that will be used for most outbound connections.
# The address, netmask and gateway are all necessary. The metric is not necessary but
# ensures you always talk to the same gateway if you have multiple public IPs from
# different subnets.
IPADDR='12.34.56.78'
NETMASK='255.255.255.0'

# eth0:0
# This is a second public IP address.
IPADDR1='34.56.78.90'
NETMASK1='255.255.255.0'
LABEL1='0'

# eth0:1 - Private IP
# Private IP addresses do not have a gateway.
IPADDR2='192.168.133.234'
NETMASK2='255.255.128.0'
LABEL2='1'

Create the following file if necessary:

File/etc/sysconfig/network/routes

# Destination   Gateway                 Netmask                 Device
66.246.75.0     0.0.0.0                 255.255.255.0           eth0
0.0.0.0         12.34.56.1              0.0.0.0                 eth0

Double-check that your /etc/resolv.conf exists and is correct.

Restart networking:

/etc/init.d/network restart

From the Linode, ping each of the default gateways listed on the "Remote Access" tab of the Linode Manager:

ping 12.34.56.1
ping 98.76.54.1
 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注