Linux的静态IP配置

4,645次阅读
没有评论

Linux 的静态 IP 配置

在 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.

正文完
 
VPSWe
版权声明:本站原创文章,由 VPSWe 2016-05-06发表,共计2809字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码