linux network configuration file

Linux network related configuration files

Linux systems are generally divided into two categories: 1. RedHat series: Redhat, Centos, Fedora, etc.; 2. Debian series: Debian, Ubuntu , etc.

In the Linux system, the TCP/IP network is configured through several text files, and these files need to be configured to connect to the network. The following is a summary of the basic TCP/IP network configuration files in the two types of Linux systems.

An example of the Ubuntu system in the first type of Debian

The network configuration files of the Ubuntu system include interfaces, resolv.conf, etc.

1. Network interface configuration file: /etc/network/interfaces

1. The content is as follows:

# The loopback network interface

auto lo #The network card is automatically mounted when it is powered on

iface lo inet loopback

auto  eth0 #The network card is automatically mounted when it is powered on and connected to the network

iface eth0 inet  static  #static means using fixed ip, dhcp means using dynamic ip

address 10.1.101.227 #Set  ip address

netmask 255.255.255.0 #Set the  subnet mask

gateway 10.1.101.254 #set  gateway

dns-nameservers 10.1.101.51

 Modify the interfaces to make the network settings take effect with the following command : /etc/init.d/networking restart #Restart the  network

or ifdown eth0 && ifup eth0

2. Set the second ip address (virtual IP address)

Edit the file /etc/network/interfaces

auto eth0:1
iface eth0:1 inet static
address 192.168.1.60
netmask 255.255.255.0
network x.x.x.x
broadcast x.x.x.x
gateway x.x.x.x 

2. DNS configuration file: /etc/resolv.conf

First, you can add some host names and IP addresses corresponding to these host names in /etc/hosts, which is a simple static query using the machine.

To access the DNS server for queries, the /etc/resolv.conf file needs to be set up.

Set the IP address and DNS domain name of the DNS server through this file, which is the configuration file used by the domain name resolver .

(Domain name resolver: resolver, a library that resolves IP addresses based on hostnames)

domainname domain name

search domainname.com #Indicates    that when a host that does not include a full domain name is provided, add domainname.com suffix
nameserver xxxx #Set the preferred DNS, use the host specified by this address as the domain name server when resolving the domain name, and search in the order in which nameserver appears .

nameserver xxxx#Set up alternate DNS

sortlist    # sort the returned domain names

Make the network settings take effect : /etc/init.d/networking restart #Restart the  network

3. /etc/hosts file

包含(本地网络中)已知主机的一个列表。如果系统的IP不是动态获取,就可以使用它。对于简单的主机名解析(点分表示法),在请求DNS或NIS网络之前,/etc/hosts.conf通常会告诉解析程序先查看这里。

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

四、/etc/host.conf

当系统中同时存在DNS域名解析和/etc/hosts主机表机制时,由该/etc/host.conf确定主机名解释顺序。

order hosts,bind #名称解释顺序,order是关键字,定义先用本机hosts主机表进行名称解释,如果不能解释,再搜索bind名称服务器(DNS)。

multi on #允许主机有多个IP地址

nospoof on #禁止IP地址欺骗

五、/etc/services

Internet网络服务文件,将网络服务名转换为端口号/协议。由inetd、telnet、tcpdump和一些其他程序读取。文件中每一行对应一种服务,它由4个字段组成,中间用TAB或者空格分隔,分别表示 “服务名称”、“使用端口”、“协议名称”以及“别名”。

服务            端口/端口类型           别名

$ cat /etc/services  |more

tcpmux          1/tcp                           # TCP port service multiplexer
echo            7/tcp
echo            7/udp
discard         9/tcp           sink null
discard         9/udp           sink null
systat          11/tcp          users
daytime         13/tcp
daytime         13/udp
netstat         15/tcp
qotd            17/tcp          quote
msp             18/tcp                          # message send protocol

六、主机名称配置文件/bin/hostname

用命令hostname newname来设置新主机名

系统启动时,它会从/etc/hostname来读取主机的名称

#假设修改主机名为controller
hostname controller
echo "controller" > /etc/hostname

七、网络相关脚本和命令

/etc/init.d/networking

系统启动时的初始化脚本,当系统以某个级别启动时,它负责初始化所有一级配置的网络接口。

ifconfig命令

不带任何参数的ifconfig命令查看当前系统网络配置情况:
eth0:表示网卡代号

lo:linux内部的网络回环地址,用于模拟网络行为

HWaddr:网卡的硬件地址,也就是MAC地址

inet addr:网卡的Ip地址

Bcast:广播地址

Mask:子网掩码

MTU:最大传输单元(Maximum Trassmission Unit),标识网络接口的可传输的最大封包,此值设定错误可能引起网络故障。

PX:网络从启动到现在为止数据接收情况

TX:网络从启动到现在为止数据发送情况

collisions:网络信号发生冲突的情况

防火墙相关命令

查看防火墙状态:sudo ufw status

启用防火墙:

sudo ufw enable #开启了防火墙,并在系统启动时自动开启

sudo ufw default deny #关闭所有外部对本机的访问,但本机访问外部正常。

sudo ufw disable   #禁用防火墙,默认是禁用的

打开某个端口

sudo ufw allow smtp #允许所有的外部IP访问本机的25/tcp (smtp)端口

sudo ufw allow 22/tcp  #允许所有的外部IP访问本机的22/tcp (ssh)端口

sudo ufw allow 53  #允许外部访问53端口(tcp/udp)

sudo ufw allow from 192.168.1.100  #允许此IP访问所有的本机端口

sudo ufw allow proto udp 192.168.0.1 port 53 to 192.168.0.2 port 53

禁用或关闭某个端口

sudo ufw delete allow 53 #禁用 53 端口

sudo ufw delete allow 80/tcp #禁用 80 端口

sudo ufw deny smtp  #禁止外部访问smtp服务

sudo ufw delete allow smtp #删除 smtp 端口的许可

sudo ufw delete allow from 192.168.254.254 #禁止某特定 IP

第二类redhat中redhat为例

redhat系统的网络配置

一、修改IP地址

1、ip配置文件

修改对应网卡的IP地址配置文件/etc/sysconfig/network-scripts/ifcfg-<interface-name>文件

在Redhat中,系统网络设置的配置文件保存在/etc/sysconfig/network-scripts目录下。ifcfg-eth0代表第一块网卡的配置信息,ifcfg-eth1代表第二块网卡的配置信息。在启动时,系统通过读取这个配置文件决定某个网卡是否启动和如何配置。

若希望手工修改网络地址或增加新的网络连接,可以通过修改对应的ifcfg-<interface-name>或创建新的文件来实现。

下面是/etc/sysconfig/network-scripts/ifcfg-{interface-name}的配置信息含义

DEVICE={name}   #{name}表示网卡对应物理设备的名字,eth0是第一块网卡,双网卡或更多依次为eth1,eth*

TYPE=Ethernet  #网络类型,Ethernet代表以太网

ONBOOT=yes/no   #系统启动引导时是否激活该网络接口,设为yes,即激活此设备

NM_CONTROLLED=yes

BOOTPROTO=none/static/bootp/dhcp   #设置网卡获得ip地址的方式,可能选项为none,static,bootp,dhcp,分别对应不适用协议,静态指定ip地址,通过dhcp协议获得ip地址,通过bootp协议获得ip地址

IPADDR={address}   #如果设置网卡获得ip地址的方式为静态指定,{addres}就代表赋给该网卡的ip地址
NETMASK={mask}  #{mask}表示网卡对应的网络掩码

NETWORK={address} #{address}表示网卡对应的网络地址,(可以不要)
GATEWAY={address} #{address}默认网关
DNS1=10.1.101.51     #DNS服务配置

BROADCAST={address}  #{address}对应的子网广播地址,192.168.0.255(可以不要)

MACADDR={MAC-address}   #{MAC-address}表示指定一个MAC地址

USERCTL=yes/no    # 是否允许非root用户控制该设备

HWADDR=50:e5:49:df:b0:8b  #对应的网卡物理地址

IPV6INIT=no  #IPV6

IPV6_AUTOCONF=no

2、单网卡绑定2个IP

只需要在/etc/sysconfig/network-scripts目录里面创建一个名为ifcfg-eth0:0的文件,

内容样例为:

DEVICE="eth0:0"
IPADDR="x.x.x.x"
NETMASK="255.255.255.0"

如果需要再多绑定一个IP地址,只需要把文件名和文件内的DEVICE中的eth0:0加一即可。

linux最多可支持255个IP别名。

二、修改主机名

临时修改:hostname xxx

永久修改:/etc/sysconfig/network

[root@localhost ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain #修改localhost.localdomain为xxx

localhost.localdomain中点的前面是主机名,后面是域名。如果没有点就代表是主机名。

三、修改网关

网关配置文件为/etc/sysconfig/network文件

网关配置也可以在/etc/sysconfig/network-scripts/ifcfg-eth0中配置。

network用来指定服务器上的网络配置信息,该文件配置项含义如下:

NETWORK=yes #表示系统是否使用网络,一般设置为yes,如果设置为no,则不能使用网络,而且很多系统服务程序将无法启动
RORWARD_IPV4=yes
HOSTNAME={hostname}  #{hostname}表示服务器的主机名,这里的主机名要和/etc/hosts中设置的主机名对应
GAREWAY={address}  #{address}设置本机连接的网关的IP地址,例如,网关10.0.0.2
GATEWAYDEV={device}  #{device}表示网关的设备名,如:eth0
NETWORK=yes/no     #网络是否被配置
FORWARD_IPV4=yes/no     #是否开启IP转发功能

四、修改DNS

DNS配置文件为/etc/resolv.conf文件

DNS配置也可以在/etc/sysconfig/network-scripts/ifcfg-eth0中配置。

resolv.conf文件用来配置DNS客户端,它包含了DNS服务器地址和域名搜索配置。同Ubuntu的配置。目前最多支持三个DNS服务器。

nameserver 8.8.8.8 #google域名服务器

nameserver 8.8.8.4 #google域名服务器

五、/etc/hosts文件

在机器启动时,在可用查询DNS以前,机器需要查询一些主机名到IP地址的匹配。这些信息存放在/etc/hosts文件中。

在没有域名服务器的情况下,系统上所有的网络程序都通过查询该文件来解析对应于某个主机名的IP地址。

[root@lxy-nfs network-scripts]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.101.117 lxy-nfs  lxy-nfs.engine.com

最左边是一系列主机IP信息,中间是一些列主机名。任何后面的列都是该主机的别名。

六、重启网络配置方法

1、#service network restart

2、#/etc/init.d/network restart

3、#ifdown eth0

#ifup eth0

4、#ifconfig eth0 down

#ifconfig eth0 up

七、相关命令

修改IP地址

1、即时生效:

#ifconfig eth0 192.168.0.2 netmask 255.255.255.0

2、重启系统生效:

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

修改网关Default Gateway

1、即时生效

#route add default gw 192.168.0.2 netmask 255.255.255.0

2、重启系统生效:

修改/etc/sysconfig/network

修改DNS

修改/etc/resolv.conf修改后可即时生效,启动同样有效

修改host name

1、即时生效

hostname newname

2、重启系统生效

修改/etc/sysconfig/network

启动和关闭防火墙

1、即时生效:

#service iptables start

#service iptables stop

2、重启系统生效:

#service iptables on

#service iptables off

 总结网络配置:配置IP,网关,DNS,然后重启网络。

开放指定端口

1、用命令

#开放端口:8080

/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT 

#重启防火墙以便改动生效:(或者直接重启系统)

/etc/init.d/iptables restart

#将更改进行保存
/etc/rc.d/init.d/iptables save

 2、直接修改iptables

直接在/etc/sysconfig/iptables中增加一行:

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324154505&siteId=291194637
Recommended