linux相识相知网络属性配置
Posted follow your heart.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux相识相知网络属性配置相关的知识,希望对你有一定的参考价值。
当我们拥有一个崭新的计算机的时候,第一步恐怕都是迫不及待的下载各种软件,看视频,听音乐等,这里的关键的一点是要有网络。现在的个人计算机大部分都是windows操作系统的,接入网络网络很简单,插上网线也许就能上网了,最麻烦不过找到网络和共享中心手动配置一个ip地址、网关地址和dns服务器就能上网了,当然这都是得益于图形化界面。但是现在很多的服务器都是linux操作系统且没有图形化的界面,这时候我们就需要了解怎样正确的给linux主机配置网络属性了,本次博客就来说说以正确的方式打开linux操作系统的网络属性配置(本次博客基于的发行版和版本:centOS7、centOS6.5)。
接入网络的两种方式
一、手动的指定
1.使用命令
一般是临时的修改,重启后失效,如:ifconfig、route、ip addr等。
2.修改配置文件
修改文件配置,永久有效,但是可能不能立即生效,需要重启服务。
二、动态获取
如果本地网路中存在DHCP服务,可以动态的获取到ip地址、网关地址、DNS服务器地址等。
常用的管理和配置命令
一、ifconfig
1.不加任何选项,查看处于活动状态下的接口,-a选项显示所有接口,包括inactive状态的接口:
[root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:AD inet addr:192.168.172.136 Bcast:192.168.172.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aaad/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2168 errors:0 dropped:0 overruns:0 frame:0 TX packets:404 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:149715 (146.2 KiB) TX bytes:29251 (28.5 KiB) 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:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) [root@localhost ~]# ifconfig -a eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:AD inet addr:192.168.172.136 Bcast:192.168.172.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aaad/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2223 errors:0 dropped:0 overruns:0 frame:0 TX packets:425 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:153927 (150.3 KiB) TX bytes:32473 (31.7 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) 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:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
接口的命名方式:
1.传统的命令方式: 以太网:eth1,eth2...ethn #centos6较常见的命名方式 PPP网络:ppp1,ppp2...pppn 2.可预测的命令方案 如果固件或者Bios为主板上集成的设备提供的索引信息可以用,根据此索引进行命令,例如:eno1,eno2....enon; 如果固件或者BIOS为PCI-E扩展槽上所提供的索引信息可以用,根据此索引进行命令,例如:ens1,ens2...ensn; 如果硬件接口的物理位置信息可用,则根据此信息命令,如enp2s0 如果用户显示定义,也可以根据MAC地址命令,例如enx1232131231 3.命名格式的组成 en:以太网 wl:无线网 ww:广域网 4.名称类型 o<index>:集成设备的设备索引号 s<slot>:扩展槽的索引号 x<MAC>:基于MAC地址的命名 p<bus>s<slot>:基于总线槽的拓扑结构进行命名
2. 配置接口ip地址掩码的两种方式:
ifconfig INTERFACE IP/MASK [up|down] #up和down为可选项 ifconfig INTERFACE IP netmask NETMASK [up|down]
举例:
[root@localhost ~]# ifconfig eth1 192.168.1.1/24 up [root@localhost ~]# [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:AD inet addr:192.168.172.136 Bcast:192.168.172.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aaad/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2392 errors:0 dropped:0 overruns:0 frame:0 TX packets:510 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:168403 (164.4 KiB) TX bytes:43303 (42.2 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:5 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:398 (398.0 b) 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:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
3.开启接口的杂合模式,这里就不先做太多的说明,后面博客中会说明:
[root@localhost ~]# ifconfig eth1 promisc [root@localhost ~]# [root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 #这里多了一个PROMISC RX packets:29 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2985 (2.9 KiB) TX bytes:468 (468.0 b)
4.新增和删除IPv6地址
ifconfig INTERFACE add addr/prefixlen ifconfig INTERFACE del addr/prefixlen
举例:
[root@localhost ~]# ifconfig eth1 add 2001:0:0:12::1/64 #新增一个IPV6地址 [root@localhost ~]# [root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: 2001:0:0:12::1/64 Scope:Global inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:57 errors:0 dropped:0 overruns:0 frame:0 TX packets:10 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:6604 (6.4 KiB) TX bytes:836 (836.0 b) [root@localhost ~]# ifconfig eth1 del 2001:0:0:12::1/64 #删除ipv6地址 [root@localhost ~]# [root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:9F:AA:B7 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe9f:aab7/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:62 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7468 (7.2 KiB) TX bytes:1016 (1016.0 b)
如果在没有配置ipv6地址的情况下,我们会发现每个接口都有一个开头为fe80的ipv6地址,这个地址被称为链路本地的地址,其前64为都是固定的格式,后64是位是基于EUI-64生成的,通俗的来说就是基于MAC地址生成的,链路本地地址只能用户本地的通信,如果想要实现上网的功能,就必须要给接口配置一个全局的地址,也就是全球可聚合单播地址,配置的方式就如上,显示为Global。
可以给接口起别名配置多个IP地址:
[root@localhost ~]# ifconfig ens34:1 192.168.179.130 netmask 255.255.255.0 [root@localhost ~]# [root@localhost ~]# ifconfig ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.172.134 netmask 255.255.255.0 broadcast 192.168.172.255 inet6 fe80::4:ed31:efad:25d0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:db:7f:47 txqueuelen 1000 (Ethernet) RX packets 25157 bytes 12826364 (12.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11651 bytes 2175604 (2.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.179.128 netmask 255.255.255.0 broadcast 192.168.179.255 ether 00:0c:29:db:7f:51 txqueuelen 1000 (Ethernet) RX packets 666 bytes 91944 (89.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 220 bytes 29356 (28.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens34:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.179.130 netmask 255.255.255.0 broadcast 192.168.179.255 ether 00:0c:29:db:7f:51 txqueuelen 1000 (Ethernet)
然后为别名添加配置文件即可,记得DEVICE为别名名称且BOOTPROTO:不支持动态获取地址。
注意:执行ifconfig的时候,命令执行后立即生效的,但是重启后失效!
二、route
1.查看路由条目
route -n #用数字表示,不加-n选项,可能会以主机名和其他的符号来显示地址
举例:
[root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.172.0 * 255.255.255.0 U 0 0 0 eth0 link-local * 255.255.0.0 U 1002 0 0 eth0 default 192.168.172.2 0.0.0.0 UG 0 0 0 eth0 [root@localhost ~]# [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.172.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 1002 0 0 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0
2.添加路由条目
route add [-net|-host] target [netmask Nm] [gw GW] [[dev] If]
举例:
[root@localhost ~]# route add -net 100.100.100.0/24 gw 192.168.1.1 #接口可省略 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 100.100.100.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.172.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 1002 0 0 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0 [root@localhost ~]# route add default gw 192.168.1.1 dev eth1 #使用default参数创建默认路由 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 100.100.100.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.172.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 1002 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth1 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0
3.删除路由
route del [-net|-host] target [gw GW] [netmask Nm] [[dev] If]
举例:
[root@localhost ~]# route del -net 100.100.100.0/24 [root@localhost ~]# route del -net 0.0.0.0/0 gw 192.168.1.1 #如果目的IP地址存在多个,可以指定网关地址,避免误删 [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.172.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 1002 0 0 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0
三、netstat命令
专门用来显示命令
1.显示路由表
netstat -rn -r:显示内核路由表 -n:数字格式
举例:
[root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.172.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 eth0 0.0.0.0 192.168.172.2 0.0.0.0 UG 0 0 0 eth0
2.显示网络连接
netstat [--tcp|-t] [--udp|-u] [--udplite|-U] [--sctp|-S] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--extend|-e[--extend|-e]] [--program|-p] -t:TCP的相关连接 -u:UDP相关连接 -w:raw socket相关连接 -l:处于监听状态的连接 -a:所有状态 -n:以数字格式显示ip和端口 -e:扩展格式 -p:显示相关进程及PID 常用组合: -tan -uan -tnl -unl -tunlp
举例:
[root@localhost ~]# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 64 192.168.172.136:22 192.168.172.1:60645 ESTABLISHED tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN [root@localhost ~]# netstat -uan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:68 0.0.0.0:* [root@localhost ~]# netstat -tnl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN [root@localhost ~]# netstat -unl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:68 0.0.0.0:* [root@localhost ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0以上是关于linux相识相知网络属性配置的主要内容,如果未能解决你的问题,请参考以下文章