Linux的静态IP配置

linux

在Internet上有千百万台主机,为了区分这些主机,人们给每台主机都分配了一个专门的地址,称为IP地址。通过IP地址就可以访问到每一台主机。IP地址由4部分数字组成,每部分数字对应于8位二进制数字,各部分之间用小数点分开。如某一台主机的IP地址为:211.152.65.112 ,Internet IP地址由NIC(Internet Network Information Center)统一负责全球地址的规划、管理;同时由Inter NIC、APNIC、RIPE三大网络信息中心具体负责美国及其它地区的IP地址分配。

固定IP(即静态IP):固定IP地址是长期固定分配给一台计算机使用的IP地址,一般是特殊的服务器才拥有固定IP地址。 一般来说,采用专线上网的计算机才拥有固定的 Internet IP 地址而且需要比较昂贵的费用。
动态IP:通过 Modem、ISDN、ADSL、有线宽频、小区宽频等方式上网的计算机,每次上网所分配到的IP地址都不相同,而这是由ISP动态分配暂时的一个IP地址,这就是动态 IP 地址。因为 IP 地址资源很宝贵,大部分用户都是通过动态 IP 地址上网的。普通人一般不需要去了解动态IP地址,这些都是计算机系统自动完成的。
公有地址(Public address)由Inter NIC(Internet Network Information Center 因特网信息中心)负责。这些IP地址分配给注册并向Inter NIC提出申请的组织机构。通过它直接访问因特网。
私有地址(Private address)属于非注册地址,专门为组织机构内部使用。

本篇文章简单介绍下Linux下怎么配置静态IP地址!

CentOS 7 / Fedora 22+

Edit the interface’s config file:

/etc/sysconfig/network-scripts/ifcfg-eth0
. . .

GATEWAY=198.51.100.1

# Your primary public IP address.
# The netmask is taken from the PREFIX (where 24 is Public IP, 17 is Private IP).
IPADDR0=198.51.100.5
PREFIX0="24"

# To add a second public IP address:
IPADDR1=198.51.100.10
PREFIX1="24"

# To add a private IP address:
IPADDR2=192.0.2.6
PREFIX2="17"

CentOS 6

CentOS 6 needs interface aliases specified in its config files. This means that additional IPs are assigned to an alias you create for eth0 (for example, eth0:1, etho0:2, etc.). CentOS 6 keeps each interface configuration in a separate file at /etc/sysconfig/network-scripts/ifcfg-<interface_alias_name>so you’ll need to create one for eth0, and one for each alias you would like.

/etc/sysconfig/network-scripts/ifcfg-eth0
# eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
    
# Your primary public IP address.
IPADDR=198.51.100.5
NETMASK=255.255.255.0
GATEWAY=198.51.100.1
/etc/sysconfig/network-scripts/ifcfg-eth0:1
# eth0:1
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes

# To add a second public IP address:
IPADDR=198.51.100.10
NETMASK=255.255.255.0

Debian / Ubuntu

Edit the interface’s config file:

/etc/network/interfaces
. . .

# Your primary public IP address.
iface eth0 inet static
    address 198.51.100.5/24
    gateway 198.51.100.1

# To add a second public IP address:
iface eth0 inet static
    address 198.51.100.10/24

# To add a private IP address:
iface eth0 inet static
    address 192.0.2.6/17

Gentoo

Networking in Gentoo utilizes the netifrc utility. Addresses are specified in the config_eth0 line and separated by spaces.

/etc/conf.d/net
config_eth0="198.51.100.5/24 198.51.100.10/24 192.0.2.6/17"
routes_eth0="default gw 198.51.100.1"
. . .

OpenSUSE

  1. Edit the interface’s config file:
    /etc/sysconfig/network/ifcfg-eth0
    . . .
    
    # Your primary public IP address.
    IPADDR='198.51.100.5'
    NETMASK='255.255.255.0'
    GATEWAY="198.51.100.1"
    
    # Add a second public IP address:
    IPADDR1='198.51.100.10'
    NETMASK1='255.255.255.0'
    LABEL1='1'
    
    # Add a private IP address:
    IPADDR2='192.0.2.6'
    NETMASK2='255.255.128.0'
    LABEL2='2'
  2. You will also need to add your gateway to the network routes file:
    /etc/sysconfig/network/routes
    # Destination   Gateway                 Netmask                 Device
    default         198.51.100.1            255.255.255.0           eth0

DNS Resolver Settings

File excerpt: /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

For more info on resolv.conf, see its manual page.

发表回复

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

退出移动版