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