Linux网络配置详解

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:

1.前言

对于LINUX而言,如果我们想对其进行网络配置的话,那么主要涉及到如下方面的配置:

IP,子网掩码,网关,主机名,DNS服务器地址,路由信息。

那么下面,将对这些方面进行操作配置。如果大家对网络的有关知识,不太清楚的,可以参考我的博客:http://zhangfengzhe.blog.51cto.com/8855103/1438163 【这篇博客将快速让大家了解一些概念】


2.关于ifconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost ~] # ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:6A:2D:34  
           inet addr:192.168.204.88  Bcast:192.168.204.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe6a:2d34 /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:69 errors:0 dropped:0 overruns:0 frame:0
           TX packets:109 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:7622 (7.4 KiB)  TX bytes:14522 (14.1 KiB)
           Interrupt:67 Base address:0x2000 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:6A:2D:3E  
           inet addr:192.168.1.116  Bcast:192.168.1.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe6a:2d3e /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:155 errors:0 dropped:0 overruns:0 frame:0
           TX packets:57 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:13616 (13.2 KiB)  TX bytes:9968 (9.7 KiB)
           Interrupt:67 Base address:0x2080 
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:12 errors:0 dropped:0 overruns:0 frame:0
           TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0 
           RX bytes:908 (908.0 b)  TX bytes:908 (908.0 b)

说明:

  • 注意ifconfig是一个非常老旧的configure a network interface命令。它会显示当前处于UP状态下的网络接口信息。

  • eth0,eth1,lo这些是什么呢?

    【lo,即loop back,本地回环设备,说白了,就是server and client in one PC,自己和自己通信而已。ethX即以太网网卡。除了上面的,还有点对点连接pppX】

  • ifconfig -a查看所有的网络接口信息;ifconfig eth1查看指定网卡的信息;


3.命令配置IP并指定子网掩码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost ~] # ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:6A:2D:3E  
           inet addr:192.168.1.116  Bcast:192.168.1.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe6a:2d3e /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:350 errors:0 dropped:0 overruns:0 frame:0
           TX packets:57 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:30883 (30.1 KiB)  TX bytes:9968 (9.7 KiB)
           Interrupt:67 Base address:0x2080 
[root@localhost ~] # ifconfig eth1 192.168.1.130/24 up
[root@localhost ~] # ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:6A:2D:3E  
           inet addr:192.168.1.130  Bcast:192.168.1.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe6a:2d3e /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:383 errors:0 dropped:0 overruns:0 frame:0
           TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:33823 (33.0 KiB)  TX bytes:12966 (12.6 KiB)
           Interrupt:67 Base address:0x2080 
[root@localhost ~] #


说明:

注意 ifconfig eth1 192.168.1.130/24 up,这个24其实指定的就是子网掩码信息,一旦指定子网掩码

便会为我们自动计算广播地址。up表示启用这个网络接口。


利用ifconfig进行配置,会立即生效的,但是一旦重启网络服务或者主机,便会失效。


4.重启网络服务的方法

对于REDHAT 5而言,如下:

1
2
3
4
5
6
7
8
9
[root@localhost ~] # /etc/init.d/network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down interface eth1:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface eth1:  
Determining IP information  for  eth1...  done .
                                                            [  OK  ]

说明:

显然,这个脚本应该还有start,stop,status等。


5.命令配置路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~] # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
192.168.204.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
default         localhost       0.0.0.0         UG    0      0        0 eth1
[root@localhost ~] # route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.204.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth1
[root@localhost ~] #

说明:

  • route命令可以显示和配置路由信息,选项-n用于数字化显示。

  • 路由信息大致上就是到达哪一个网络或者主机需要经过哪一个网关,通过哪一个网络接口。也就是说,分为网络路由和主机路由。在Flags中的G标示的就是网路路由,U标示UP启用状态。

实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost init.d] # route add -net 10.0.0.0/8  gw 192.168.1.1
[root@localhost init.d] # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
192.168.204.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
10.0.0.0        localhost       255.0.0.0       UG    0      0        0 eth1
default         localhost       0.0.0.0         UG    0      0        0 eth1
[root@localhost ~] # route del -net 10.0.0.0/8 gw 192.168.1.1
[root@localhost ~] # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
192.168.204.0   *               255.255.255.0   U     0      0        0 eth0
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
default         localhost       0.0.0.0         UG    0      0        0 eth1

很显然,一旦重启网络服务或者主机的话,必然失效。


6.在文件中进行网络配置

如果我们想IP,NETMASK,GATEWAY这些信息在重启后有效的话,可以选择在/etc/sysconfig/network-scripts/ifcfg-ethXXX进行配置


对于ifcfg-ethXXX而言:

其实就是一个KEY=VALUE的形式,比如:

DEVICE=eth0     【关联的设备,一定要和文件名称的后半部分一致。 】

HWADDR=00:0c:29:1c:95:d2  【硬件地址,不可修改】

ONBOOT=yes   【是否开机时自动启动此设备】

NETMASK=255.255.255.0 【子网掩码】

IPADDR=192.168.152.2   【IP地址】

TYPE=Ethernet       【网络接口类型】

BOOTPROTO=static   【静态地址类型】


需要注意的是,

BOOTPROTO还可以取值DHCP,即由DHCP服务器分配地址。

USERCTL={yes|no} :是否允许普通用户控制此接口

PEERDNS={yes|no} :在BOOTPROTO为DHCP时,是否接受DHCP服务器指定的DNS地址。




如果我们想路由信息在重启后有效的话,可以选择在/etc/sysconfig/network-scripts/route-ethXXX进行配置


目标主机   via 网关

目标网络   via 网关

【需要加入子网掩码,并且网关与你的ethXXX应处于同一网络,也就说,是从ethXXX发出去的】


还可以采用另一种格式:

ADDRESS0=

NETMASK0=

GATEWAY0=


ADDRESS1=

NETMASK1=

GATEWAY1=


7.DNS配置

关于DNS的配置方式,只有一种,那就是编辑配置文件。

[root@localhost ~]# cat /etc/resolv.conf

nameserver 202.106.46.151

nameserver 192.168.1.1


注意最多只能有3个,也就是说:

nameserver DNS_IP_1

nameserver DNS_IP_2

nameserver DNS_IP_3



指定本地解析,类似于WINDOWS的HOSTS文件。

/etc/hosts

主机IP  主机名称  别名


8.主机名配置

配置主机名:

hostname HOSTNAME

同上,可以生效,不能永久有效。


可以编辑

/etc/sysconfig/network文件,里面有hostname这一行。

[root@localhost ~]# cat /etc/sysconfig/network

NETWORKING=yes   ===》是否启用本机的网络功能,是网络功能的总开关。

NETWORKING_IPV6=no

HOSTNAME=localhost.localdomain


9.ip命令介绍

可能很多同学没有听说过ip命令。iproute2是一个非常强大的软件包,它提供了一个命令,就叫ip,非常强大。下面附带一些实例供大家参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@localhost ~] # ip link show   ------------》相当于ifconfig -a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
     link /ether  00:0c:29:6a:2d:34 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
     link /ether  00:0c:29:6a:2d:3e brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop 
     link /sit  0.0.0.0 brd 0.0.0.0
[root@localhost ~] # ip -s link show  ------------>s选项会显示一些额外的统计信息。
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
     link /loopback  00:00:00:00:00:00 brd 00:00:00:00:00:00
     RX: bytes  packets  errors  dropped overrun mcast   
     5965       36       0       0       0       0      
     TX: bytes  packets  errors  dropped carrier collsns 
     5965       36       0       0       0       0    
[root@localhost ~] # ifconfig eth0:0 192.168.152.3/24
[root@localhost ~] # ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1C:95:D2  
           inet addr:192.168.152.2  Bcast:192.168.152.255  Mask:255.255.255.0
           inet6 addr: fe80::20c:29ff:fe1c:95d2 /64  Scope:Link
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:196 errors:0 dropped:0 overruns:0 frame:0
           TX packets:394 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000 
           RX bytes:21710 (21.2 KiB)  TX bytes:48480 (47.3 KiB)
           Interrupt:67 Base address:0x2024 
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:1C:95:D2  
           inet addr:192.168.152.3  Bcast:192.168.152.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           Interrupt:67 Base address:0x2024 
[root@localhost network-scripts] # ip addr del 192.168.152.3/24 dev eth0
[root@localhost network-scripts] # ip addr show to 192.168.152.2/24
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
     inet 192.168.152.2 /24  brd 192.168.152.255 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
     inet 192.168.152.129 /24  brd 192.168.152.255 scope global eth1
     inet 192.168.152.4 /24  scope global secondary eth1
[root@localhost network-scripts] # ip addr flush to 192.168.152.129/24 dev eth1


说明:

ip link set DEVICE { up | down | arp { on | off } |

               promisc { on | off } |

               allmulti { on | off } |

               dynamic { on | off } |

               multicast { on | off } |

               txqueuelen PACKETS |

               name NEWNAME |

               address LLADDR | broadcast LLADDR |

               mtu MTU }




本文转自zfz_linux_boy 51CTO博客,原文链接:http://blog.51cto.com/zhangfengzhe/1439749,如需转载请自行联系原作者


相关文章
|
27天前
|
安全 Linux 网络安全
Web安全-Linux网络协议
Web安全-Linux网络协议
52 4
|
27天前
|
机器学习/深度学习 安全 网络协议
Web安全-Linux网络命令
Web安全-Linux网络命令
19 1
|
2月前
|
存储 Linux Shell
在Linux中,如何使用脚本,实现判断 192.168.1.0/24 网络里,当前在线的 IP 有哪些?能ping 通则 认为在线。
在Linux中,如何使用脚本,实现判断 192.168.1.0/24 网络里,当前在线的 IP 有哪些?能ping 通则 认为在线。
|
2月前
|
监控 网络协议 Linux
在Linux中,如何实时抓取并显示当前系统中tcp 80 端口的网络数据信息?
在Linux中,如何实时抓取并显示当前系统中tcp 80 端口的网络数据信息?
|
2月前
|
监控 安全 Linux
在Linux中,如何进行网络资源的优先级管理?
在Linux中,如何进行网络资源的优先级管理?
|
23天前
|
网络协议 Linux
Linux 网络配置
了解基本命令与权限后,如何让Linux系统联网?可通过编辑`/etc/sysconfig/network-scripts/`下的`ifcfg-ethX`文件配置网卡,其中`ethX`代表第X块网卡。对于DHCP自动获取或静态IP,需设置`BOOTPROTO`参数,并指定IP、子网掩码和网关等。配置完成后,运行`/etc/init.d/network restart`重启网络。DNS可在`/etc/resolv.conf`中设置,添加`nameserver`行即可,无需重启网卡。配置好后,可用`ifconfig`查看IP信息,并通过远程工具如SecureCRT连接服务器。
45 0
|
1月前
|
域名解析 负载均衡 网络协议
Linux网络接口配置不当所带来的影响
总而言之,Linux网络接口的恰当配置是保证网络稳定性、性能和安全性的基础。通过遵循最佳实践和定期维护,可以最大程度地减少配置错误带来的负面影响。
68 0
|
2月前
|
存储 Linux 网络安全
【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
|
2月前
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
|
2月前
|
网络协议 Linux Shell
【Azure 应用服务】App Service For Linux 中安装paping, 用于验证从App Service向外请求的网络连通性
【Azure 应用服务】App Service For Linux 中安装paping, 用于验证从App Service向外请求的网络连通性
下一篇
无影云桌面