Linux配置网络

Linux配置网络

在windows下面配置网络相对简单,因为有可视化的界面。在linux下面稍微麻烦,因为主要靠命令行。

整个设置过程包括:

  • 设置机器名(hostname)
  • 设置IP,子网掩码,网关
  • 设置DNS

 

设置机器名(hostname)

Debian and Ubuntu

/etc/hostname

CentOS/Fedora

/etc/sysconfig/network

 

设置DNS

/etc/resolv.conf

添加如下内容:

nameserver 8.8.8.8

 

设置IP,子网掩码,网关

这里说的是静态IP

Debian & Ubuntu

/etc/network/interfaces

# The loopback interface
auto lo
iface lo inet loopback

# Configuration for eth0
# We no longer need to use aliases (eg. eth0:0 eth0:1 eth0:2)
# This line ensures that the interface will be brought up during boot
auto eth0
allow-hotplug eth0

# The address and gateway are necessary.
# The netmask is taken automatically from the block.
# Example: /24 is considered to be a public IP address: netmask 255.255.255.0
iface eth0 inet static
    address 198.51.100.5/24
    gateway 198.51.100.1

# This is a second public IP address
iface eth0 inet static
    address 192.0.2.6/24

# This is a private IP address. Private IPs do not have a gateway (they are not publicly routable).
# All you need to specify is the address and the block. The netmask is taken from the block.
# Example: /17 is considered to be a private IP address: netmask 255.255.128.0
iface eth0 inet static
address 192.168.133.234/17

然后重启网络

ifdown -a && ifup -a

 

CentOS 7 & Fedora 20

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

# Configuration for eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes

# Adding a 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

# Specifying the gateway
GATEWAY0=198.51.100.1

# Adding a second public IP address.
IPADDR1=192.0.2.6
PREFIX1=24

# Adding a private IP address.
IPADDR2=192.168.133.234
PREFIX2=17
-----------------------------------------------------------------------------
启动刚才配置的网络
nmcli con up "System eth0"

或者

sudo service network restart

 

CentOS 6.5

/etc/sysconfig/network-scripts/ifcfg-eth[x]

这里的x可以是0,1,2等

 

# 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=198.51.100.5
NETMASK=255.255.255.0
GATEWAY=198.51.100.1

————————————————————————

重启网络
service network restart

 

OpenSUSE

/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='198.51.100.5'
NETMASK='255.255.255.0'

# eth0:0
# This is a second public IP address.
IPADDR1='192.0.2.6'
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'

————————————————————————————————-

重启网络
systemctl reload network

 

 

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章转载自:IT夜班车,否则按侵权处理.

    分享到:

Leave a Reply

Your email address will not be published. Required fields are marked *