Linux网络初级配置(Linux Network Configure Basicly)
2010-09-15 16:09:23 阿炯

Configure the Network Manually
You can use ip or ifconfig command to configure IP address and other information.

Task: Display the Current Network Configuration
Type the following command:
$ ip address show
Output:

1: lo:  mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast qlen 100
link/ether 00:19:d1:2a:ba:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.1/24 brd 192.168.2.255 scope global eth0
inet6 fe80::219:d1ff:fe2a:baa8/64 scope link
valid_lft forever preferred_lft forever
3: ra0:  mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:17:9a:0a:f6:44 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.106/24 brd 192.168.1.255 scope global ra0
inet6 fe80::217:9aff:fe0a:f644/64 scope link
valid_lft forever preferred_lft forever
4: ppp0:  mtu 1496 qdisc pfifo_fast qlen 3
link/ppp
inet 10.1.3.103 peer 10.0.31.18/32 scope global ppp0

You can also use ifconfig -a command, enter:
$ ifconfig -a
Output:
eth0      Link encap:Ethernet  HWaddr 00:19:D1:2A:BA:A8
inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
inet6 addr: fe80::219:d1ff:fe2a:baa8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:15819 errors:0 dropped:0 overruns:0 frame:0
TX packets:27876 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:1695948 (1.6 MB)  TX bytes:40399983 (38.5 MB)
Base address:0x1000 Memory:93180000-931a0000

lo        Link encap:Local Loopback
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:16436  Metric:1
RX packets:11943 errors:0 dropped:0 overruns:0 frame:0
TX packets:11943 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:7024449 (6.6 MB)  TX bytes:7024449 (6.6 MB)

ppp0      Link encap:Point-to-Point Protocol
inet addr:10.1.3.103  P-t-P:10.0.31.18  Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1496  Metric:1
RX packets:34922 errors:0 dropped:0 overruns:0 frame:0
TX packets:15764 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:50535608 (48.1 MB)  TX bytes:1256881 (1.1 MB)

ra0       Link encap:Ethernet  HWaddr 00:17:9A:0A:F6:44
inet addr:192.168.1.106  Bcast:192.168.1.255  Mask:255.255.255.0
inet6 addr: fe80::217:9aff:fe0a:f644/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:73809 errors:0 dropped:0 overruns:0 frame:0
TX packets:31332 errors:1 dropped:1 overruns:0 carrier:0
collisions:27 txqueuelen:1000
RX bytes:61373519 (58.5 MB)  TX bytes:5007190 (4.7 MB)
Interrupt:20

The information is grouped by network interfaces. Every interface entry starts with a digit, called the interface index, with the interface name displayed after the interface index. In the above example, there are four interfaces:
* lo : Loopback interface, used to access local services such as proxy or webserver http://127.0.0.1/
* eth0 : The first Ethernet interface connected to network switch or router
* ra0 : The first wireless interface
* ppp0 :The first point-to-point interface, used to connect via VPN or dial up service

Task: Device / Interface Statistics
Type the following command:
$ ip -s link show interface-name
$ ip -s link show eth0
$ ip -s link show ppp0

Output:
4: ppp0:  mtu 1496 qdisc pfifo_fast qlen 3
link/ppp
RX: bytes  packets  errors  dropped overrun mcast
50537336   34946    0       0       0       0
TX: bytes  packets  errors  dropped carrier collsns
1257745    15776    0       0       0       0

Change the Current Network Configuration
You must login as the root to change current network settings.

Task: Assign an IP Address to a Device Interface
In the following example, the command assigns the IP address 192.168.1.10 to the device eth0. The network mask is 24 (255.255.255.0) bits long. The brd + option sets the broadcast address automatically as determined by the network mask.
# ip address add 192.168.1.100/24 brd + dev eth0
You can also use ifconfig command, enter
# ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up

Task: Remove / Delete / Deactivate IP address from a device interface
To remove IP / delete device, enter:
# ip address del 192.168.1.100 dev eth0
OR
# ifconfig eth0 down

Save Network Settings to a Configuration File
To change the current network configuration setting you'll need to edit /etc/network/interfaces file using a text editor such as vi. This is the only way to save device setting to a configuration file so that system can remember changes after a reboot.

Task: Configure a Device Statically
Open /etc/network/interfaces file as the root user:
# vi /etc/network/interfaces
Let us assign static public routable (or private) IP address to eth0, enter:
auto eth0
iface eth0 inet static
address 192.168.2.1
netmask 255.255.255.0

Save and close the file. Where,
* auto eth0 : Identify the physical interfaces such as eth0, eth1 and so on
* iface eth0 inet static : This method used to define ethernet interfaces with statically allocated IPv4 addresses
* address 192.168.2.1 : Static IP address
* netmask 255.255.255.0 : Static netmask

Task: Configure a Device Dynamically with DHCP
Open /etc/network/interfaces file as the root user:
# vi /etc/network/interfaces
Let us configure eth0 using DHCP. When the device is configured by using DHCP, you don’t need to set any options for the network address configuration in the file.
auto eth0
iface eth0 inet dhcp
Save and close the file.

* auto eth0 : Identify the physical interfaces such as eth0, eth1 and so on
* iface eth0 inet dhcp : This method used to define ethernet interfaces with DHCP server allocated IPv4 addresses

Start and Stop Configured Interfaces
To apply changes to a configuration file, you need to stop and restart the corresponding interface
# /etc/init.d/networking stop
# /etc/init.d/networking start
# /etc/init.d/networking restart
You can also use following command to bring down or up the eth0. Disables the device eth0, enter:
# ifdown eth0
Enables eth0 again, enter:
# ifup eth0

------------------------------------
Linux convert DHCP network configuration to static IP configuration
convert DHCP network configuration to static configuration. After initial installation, he wanted to change network settings. Further, his system is w/o GUI system aka X Windows. Here is quick way to accomplish the same:
Your main network configuration file is /etc/network/interfaces

Desired new sample settings:
=> Host IP address 192.168.1.100
=> Netmask: 255.255.255.0
=> Network ID: 192.168.1.0
=> Broadcast IP: 192.168.1.255
=> Gateway/Router IP: 192.168.1.254
=> DNS Server: 192.168.1.254

Open network configuration file
$ sudo vi /etc/network/interfacesOR$ sudo nano /etc/network/interfaces

Find and remove dhcp entry:
iface eth0 inet dhcp

Append new network settings:
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254

Save and close the file. Restart the network:
$ sudo /etc/init.d/networking restart
Task: Define new DNS servers

Open /etc/resolv.conf file
$ sudo vi /etc/resolv.conf

You need to remove old DNS server assigned by DHCP server:
search myisp.com
nameserver 192.168.1.254
nameserver 202.54.1.20

Save and close the file.

Task: Test DNS server

$ host cyberciti.biz
Network command line cheat sheet

You can also use commands to change settings. Please note that these settings are temporary and not the permanent. Use above method to make network changes permanent or GUI tool as described below.

Task: Display network interface information
$ ifconfig

Task: Take down network interface eth0 / take a network interface down

$ sudo ifconfig eth0 downOR $ sudo ifdown eth0

Task: Bring a network interface eth0 up

$ sudo ifconfig eth0 upOR$ sudo ifup eth0

Task: Change IP address and netmask from command line

Activate network interface eth0 with a new IP (192.168.1.50) / netmask:
$ sudo ifconfig eth0 192.168.1.50 netmask 255.255.255.0 up

Task: Display the routing table
$ /sbin/route OR$ /sbin/route -n
Output:
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
localnet        *               255.255.255.0   U     0      0        0 ra0
172.16.114.0    *               255.255.255.0   U     0      0        0 eth0
172.16.236.0    *               255.255.255.0   U     0      0        0 eth1
default         192.168.1.254   0.0.0.0         UG    0      0        0 ra0

Task: Add a new gateway
$ sudo route add default gw 172.16.236.0

Task: Display current active Internet connections (servers and established connection)
$ netstat -nat

Task: Display open ports
$ sudo netstat -tulpOR$ sudo netstat -tulpn

Task: Display network interfaces stats (RX/TX etc)
$ netstat -i

Task: Display output for active/established connections only
$ netstat -e
$ netstat -te
$ netstat -tue

Where,
* -t : TCP connections
* -u : UDP connections
* -e : Established

Task: Test network connectivity
Send ICMP ECHO_REQUEST to network hosts, routers, servers etc with ping command. This verifies connectivity exists between local host and remote network system:
$ ping router
$ ping 192.168.1.254
$ ping cyberciti.biz

See simple Linux system monitoring with ping command and scripts for more information.

Task: Use GUI (Graphical Configuration) network Tool
If you are new, use GUI configuration tool, type the following command at terminal:
$ network-admin &

Above command is Ubuntu's GUI for configuring network connections tool.

A man page is your best friend when you wanted to learn more about particular command or syntax. For example, read detailed information about ifconfig and netstat command:
$ man ifconfig
$ man netstat

Just get a short help with all command options by appending --help option to each command:
$ netstat --help

Find out what command is used for particular task by searching the short descriptions and manual page names for the keyword:
$ man -k 'delete directory'
$ apropos -s 1 remove

Display short descriptions of a command:
$ whatis rm
$ whatis netstat

Linux offers an excellent collection of utilities, which can be use to finding the files and executables, remember you cannot memorize all the commands and files.

------------------------------------
Configure Static Routes In Debian or Red Hat Enterprise Linux
Static routes improves overall performance of your network (especially bandwidth saving). They are also useful in stub networks (i.e. there is only one link to the network). For example, each LAN (located at different offices) is connected to HQ IDC (Internet data center) using single T1/LL/Wan links.

For example under Red Hat/Fedora Linux you can add static router for eth0 network interface by editing /etc/sysconfig/network-scripts/route-eth0 file. Under Debian Linux add static route by editing /etc/network/interface file.

Task: Display Current Routing Table Using ip command
By using the ip command, you can setup and view static route. For example, to display current routing table you can type command:
# ip route show
Sample output:
192.168.2.0/24 dev eth1 proto kernel  scope link  src 192.168.2.1
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.2
default via 192.168.1.254 dev eth0

You can add static route using following command:
ip route add {NETWORK} via {IP} dev {DEVICE}
For example network 192.168.55.0/24 available via 192.168.1.254:
# ip route add 192.168.55.0/24 via 192.168.1.254 dev eth1
Alternatively, you can use old good route command:
# route add -net 192.168.55.0 netmask 255.255.255.0 gw 192.168.1.254 dev eth1
Linux Persistence Routes

The drawback of 'ip' or 'route' command is that, when Linux reboots it will forget static routes. So store them in configuration file. Static routing describes a system that does not implement adaptive routing. In these systems routes through a data network are described by fixed paths (statically). These routes are usually entered into the router by the system administrator

Red Hat (RHEL) / CentOS / Fedora Linux Persistence Static Routing

You need to open /etc/sysconfig/network-scripts/route-eth0 file to define static routes for eth0 interface:
# cat /etc/sysconfig/network-scripts/route-eth0
Sample Output:
GATEWAY0=192.168.1.254
NETMASK0=255.255.255.0
ADDRESS0=192.168.55.0

GATEWAY1=10.164.234.112
NETMASK1= 255.255.255.240
ADDRESS1=10.164.234.132

How do I define static routing for network 10.0.0.0/8 via 10.9.38.65 router?

Open /etc/sysconfig/network-scripts/route-eth0:
# vi /etc/sysconfig/network-scripts/route-eth0
Append following line:
10.0.0.0/8 via 10.9.38.65
Save and close the file. Restart networking:
# service network restart
Verify new routing table:
# route -n

Debian / Ubuntu Linux Persistence Static Routing


Open configuration file /etc/network/interfaces
# cat /etc/network/interfaces
Output:
auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.254
up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1
down route del -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1

Debian / Ubuntu Linux static routing for two interfaces:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.9.38.76
netmask 255.255.255.240
network 10.9.38.64
broadcast 10.9.38.79
### static routing ###
post-up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65
pre-down route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.9.38.65

auto eth1
iface eth1 inet static
address 204.186.149.140
netmask 255.255.255.240
network 204.186.149.128
broadcast 204.186.149.143
gateway 204.186.149.129
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 10.0.80.11 10.0.80.12
dns-search nixcraft.in

Updated for accuracy.

------------------------------------
Setup as DNS Client / Name Server IP Address
Many new Linux user finds it difficult to setup / modify new name server address (NS1 / NS2).

Local name resolution is done via /etc/hosts file. If you have small network, use /etc/hosts file. DNS (domain name service is accountable for associating domain names with ip address, for example domain yahoo.com is easy to remember than IP address 202.66.66.12) provides better name resolution. To configure Linux as DNS client you need to edit or modify /etc/resolv.conf file. This file defines which name servers to use. You want to setup Linux to browse net or run network services like www or smtp; then you need to point out to correct ISP DNS servers:
/etc/resolv.conf file

In Linux and Unix like computer operating systems, the /etc/resolv.conf configuration file contains information that allows a computer connected to the Internet to convert alpha-numeric names into the numeric IP addresses that are required for access to external network resources on the Internet. The process of converting domain names to IP addresses is called "resolving."

The resolv.conf file typically contains the IP addresses of nameservers (DNS name resolvers) that attempt to translate names into addresses for any node available on the network.

Setup DNS Name resolution

Steps to configure Linux as DNS client, first login as a root user (use su command):

Step # 1: Open /etc/resolv.conf file:
# vi /etc/resolv.conf

Step #2: Add your ISP nameserver as follows:
search isp.com
nameserver 202.54.1.110
nameserver 202.54.1.112
nameserver 202.54.1.115

Note Max. three nameserver can be used/defined at a time.

Step # 3:Test setup nslookup or dig command:
$ dig www.nixcraft.com
$ nslookup www.nixcraft.com

------------------------------------
Improve DNS performance for Linux / Windows desktop using DNS caching software
"...My DSL service providers DNS server seems to be little slow, they have two servers it takes little time (some time upto 2 seconds) to resolve a domain name, once domain resolved, browsing speed remains the same, what should I do to improve DNS performance?...."

The answer is use a DNS proxy i.e. Dnsmasq. It is a a lightweight, easy to configure DNS forwarder and optional DHCP server. Dnsmasq is targeted at home networks using NAT and connected to the internet via a modem, cable-modem or ADSL connection but would be a good choice for any small network where low resource use and ease of configuration are important. The main use of the DNS proxy is to increase speed. Generally all computer send their request to ISP's DNS servers. But with DNS proxy request are cached. It stands between your local system and firewall server. Here is our sample network setup, 192.168.1.1-3 are all desktop system, 192.168.1.254 is our Linux firewall server:

Laptop | Desktop --> Linux Server --> ADSL Modem/Router
Firewall
192.168.1.1         192.168.1.254 -> Dynamic or
192.168.1.2                          Static IP assign
192.168.1.3                          by ISP

Login to your Linux firewall server and install Dnsmasq .

Step # 1 : Install Dnsmasq (Debian Linux)
# apt-get install dnsmasq

Fedora/Redhat/Centos user, use yum command to install dnsmasq:
# yum install dnsmasq

RedHat Linux user use rpm/up2date command to install it:
# up2date -i dnsmasq

Step # 2 Configure Dnsmasq
To be frank you don't have to change a single line in /etc/dnsmasq.conf. However you need to setup 127.0.0.1 as dns server name in /etc/resolve.conf file:

# vi /etc/resolve.conf
nameserver 127.0.0.1
nameserver 202.54.10.1
nameserver 202.54.20.1

Replace 202.54.10.1/202.54.20.1 with your actual ISP DNS server IPS. The dnsmasq should read the list of ISP nameservers from the automatically /etc/resolv.conf. You should list 127.0.0.1 as the first nameserver address in /etc/resolv.conf. So local desktop clients always gets cached queries.

Step # 3 Restart/start Dnsmasq
# /etc/init.d/dnsmasq start

Step # 4 Update DNS server IPS for all desktop systems
Point your windows XP or Linux Desktop client to IP of Linux firewall server i.e. 192.168.1.254 (see above network diagram)

It is easy to use Dnsmasq rather than setting up caching BIND server. But hold on it has some cool usage too. You can add domains which you want to force to specific IP address. For example, doubleclick.net displays ugly adds on many sites, just send this server it to our 127.0.0.1 (i.e. your local server ). Just open a file /etc/dnsmasq.conf and add following line to it:
address=/doubleclick.net/127.0.0.1

Restart Dnsmasq and make sure you runs local webserver at 127.0.0.1 with some default page. Read the Dnsmasq man page and docs for more information.


------------------------------------
CentOS 详解网络接口配置文件


[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet #网卡类型
DEVICE=eth0 #网卡接口名称
ONBOOT=yes #系统启动时是否激活 yes|no
BOOTPROTO=static #启用地址协议 -static:静态协议 -bootp:协议 -dhcp:协议 -none:不指定协议[最好指定]
IPADDR=192.168.1.11 #网卡IP地址
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.1.1 #网卡网关地址
DNS1=10.203.104.41 #网卡DNS地址
HWADDR=00:0C:29:13:5D:74 #网卡设备MAC地址
BROADCAST=192.168.1.255 #网卡广播地址

注:在修改文件ifcfg-ethx后还需要重新导入文件才能生效,具体命令如下:
[root@localhost ~]# /etc/init.d/network reload #命令有start | restart | stop | reload

重新导入ifcfg-eth0网络配置文件
[root@localhost ~]# /etc/init.d/network reload

网卡接口关闭与激活
[root@localhost ~]# ifdown eth0 #关闭网络
[root@localhost ~]# ifup eth0 #启动网络

网络服务启动与关闭
方法一:
[root@localhost ~]# service network stop #关闭网络服务
[root@localhost ~]# service network start #启动网络服务
[root@localhost ~]# service network restart #重启网络服务

方法二:
[root@localhost ~]# /etc/init.d/network stop
[root@localhost ~]# /etc/init.d/network start
[root@localhost ~]# /etc/init.d/network restart

网卡状态查询
[root@localhost ~]# service network status

临时配置网卡信息,无需重启。
[root@localhost ~]# ifconfig eth0 10.1.1.10 netmask 255.0.0.0

查看网卡接口信息,默认列出所有的活动接口
[root@localhost ~]# ifconfig

注:使用ifconfig -a将会列出所有的的接口,不管其是否被配置为活动的。

查看当前路由及网关信息
[root@localhost ~]# netstat -r

DNS:
主机名:CentOS
主DNS:61.139.2.69
第二DNS:202.106.0.20
第三DNS:8.8.8.8

配置子网掩码有两种方式:

PREFIX=24  和 NETMASK=255.255.255.0都是正确的子网掩码配置项,但是两者同时存在的时候反而会报错;原因在/etc/sysconfig/network-scripts/network-functions配置文件里面源码的写法问题。

NETMASK和PREFIX配置冲突问题还未找到合理的解释!最好的办法就是只按照其中一种配置就行,不要同时写出。

RedHat6中配置网卡可以进入网卡配置文件更改网卡参数

网卡配置文件位置:/etc/sysconfig/network-scripts/ifcfg-ethxx  #修改ethxx网卡配置,下面以eth0网卡为例
HWADDR=00:0A:29:8D:24:73
TYPE=Ethernet   
BOOTPROTO=static  #启用静态IP地址,不会自动获取ip
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=ae0965e7-78b9-45aa-8ec9-e3a20a85d27
ONBOOT=yes  #开启自动启用网络连接
IPADDR=192.168.21.128  #设置IP地址
PREFIX=24  #设置子网掩码,这种写法只需要写掩码位数
GATEWAY=192.168.21.2  #设置网关
DNS1=8.8.8.8  #设置主DNS
DNS2=8.8.4.4  #设置备DNS

service network restart   #重启网络


CentOS7 网络设置-nmcli/nmtui

CentOS7中配置网络有个强大的图形化配置工具- nmcli和nmtui 。

nmcli:NetworkManageCommandLine Interface   #用命令行方式配置网卡信息

nmtui:NetworkManageTextUser Iterface   #用文本界面的方式配置网卡信息

>方法一、网络设置-nmcli

[root@localhost ~]# nmcli -h

Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }  #用法格式

OPTIONS
  -t[erse]                                   terse output
  -p[retty]                                  pretty output
  -m[ode] tabular|multiline                  output mode
  -f[ields] <field1,field2,...>|all|common   specify fields to output
  -e[scape] yes|no                           escape columns separators in values
  -n[ocheck]                                 don't check nmcli and NetworkManager versions
  -a[sk]                                     ask for missing parameters
  -w[ait] <seconds>                          set timeout waiting for finishing operations  #设置命令行超时退出时间
  -v[ersion]                                 show program version    #显示nmcli的版本信息
  -h[elp]                                    print this help   #获取帮助

OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager   #显示网卡设备信息
  a[gent]         NetworkManager secret agent or polkit agent

[root@localhost ~]# nmcli d   #显示网卡设备信息,查看要配置的网卡名称

[root@localhost ~]# nmcli connection modify ethxx ipv4.method manual  ipv4.address 192.168.4.1/24  ipv4.gateway 192.168.4.254  ipv4.dns 192.168.4.200 connection.autoconnect yes  

#method为manual设为手动,后面才能配ip,一套命令下来可以把获取地址方式、ip地址、掩码、网关、随机自连连接方式都设置好了

[root@localhost ~]# nmcli connection up ethxx   #激活刚才配置的网卡信息

>方法二、网络设置-nmtui

[root@localhost ~]# nmtui   #进入网卡配置文本界面,进入edit界面

配置完成后,要激活连接,重启一下网卡服务,systemctl restart network

注意:ip地址配置的时候是CIDR格式,CIDR的作用就是支持IP地址的无分类域间路由,别忘了/24,默认是/32,会导致ssh+ip怎么连都连不上。

补充:在RedHat的Linux系统中,会发现存在一个本机回送地址:127.0.0.1  

[root@localhost ~]# ls /etc/sysconfig/network-scripts/
ifcfg-lo

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-lo #这是系统回环网卡地址配置信息

DEVICE=lo
IPADDR=127.0.0.1
NETMASK=255.0.0.0
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback

对于回送地址,查了一下相关资料,有以下收获:

回送地址:127.0.0.1。一般用于测试使用。例如:ping 127.0.0.1 来测试本机TCP/IP是否正常。

127.0.0.1是回送地址,指本地机,一般用来测试使用。回送地址(127.x.x.x)是本机回送地址(Loopback Address),即主机IP堆栈内部的IP地址,主要用于网络软件测试以及本地机进程间通信,无论什么程序,一旦使用回送地址发送数据,协议软件立即返回,不进行任何网络传输。大家常用"ping 127.0.0.1"命令在本机上做回路测试,用来验证本机的TCP/IP协议簇是否被正确安装。

但你发现了吗?使用"ping 127.1"这个命令也能得到同样的测试结果,其实"ping 127.1"和"ping 127.0.0.1"这两条命令是一样的,都是在进行回路测试。为什么会这样呢?

这就是Ping命令应用中IP地址的使用技巧。大家都知道,IP地址由32位二进制数字组成,为了方便大家记忆,将每8位二进制数字换算成十进制数字,因此就形成了容易记忆的由四部分十进制数字组成的IP地址(如127.0.0.1)。由于,Windows操作系统具有自动填充".0"的功能,因此我就可将"127.0.0.1"变为"127.1"。但是,这个".0"的省略是有条件限制的,并不能任意省略。在Ping命令的应用中,只能将在IP地址的最后一部分十进制数字前出现的一个或多个".0"省略,如把"ping 127.0.0.1"命令改写成"ping 127.1"。如果这一个或多个".0"没有紧挨着最后一部分的十进制数字,而是在其他位置,则这个".0"不能省略,如"ping 202.0.96.1"就不能写成"ping 202.96.1"。这是因为"ping 202.96.1"返回的结果是"202.96.0.1"的应答信息,而不是"202.0.96.1"的应答信息。

------------------------------------
CentOS 7 ifcfg-ethN 配置详解


DEVICE="ethN"
网卡名称

NM_CONTROLLED="yes"
network mamager的参数 ,是否可以由NNetwork Manager托管

HWADDR=
MAC地址

TYPE=Ethernet
类型

PREFIX=24
子网掩码24位

DEFROUTE=yes
就是default route,是否把这个eth设置为默认路由

ONBOOT=yes
设置为yes,开机自动启用网络连接

IPADDR=
IP地址

BOOTPROTO=none
设置为none禁止DHCP,设置为static启用静态IP地址,设置为dhcp将使用DHCP服务来配置其上的地址信息

NETMASK=255.255.255.0
子网掩码

DNS1=8.8.8.8
第一个dns服务器

DNS2=8.8.4.4 #
第二个dns服务器

BROADCAST
广播

UUID
唯一标识

TYPE=Ethernet
网络类型为:Ethernet

BRIDGE=
设置桥接网卡

GATEWAY=
设置网关

IPV6INIT=no
禁止IPV6

USERCTL=no
是否允许非root用户控制该设备,设置为no,只能用root用户更改

NAME="System eth1"
这个就是个网络连接的名字

MASTER=bond1
指定主的名称

SLAVE
指定了该接口是一个bond的组件的从。

NETWORK
网络地址

ARPCHECK=yes
检测

PEERDNS
是否允许DHCP获得的DNS覆盖本地的DNS

PEERROUTES
是否从DHCP服务器获取用于定义接口的默认网关的信息的路由表条目

IPV6INIT
是否启用IPv6的接口。

IPV4_FAILURE_FATAL=yes
如果ipv4配置失败禁用设备

IPV6_FAILURE_FATAL=yes
如果ipv6配置失败禁用设备

该文章最后由 阿炯 于 2018-10-14 09:02:26 更新,目前是第 2 版。