设为首页 收藏本站
查看: 659|回复: 0

[经验分享] LINUX网络管理之Centos6&&Centos7

[复制链接]

尚未签到

发表于 2018-4-28 12:53:48 | 显示全部楼层 |阅读模式
  一、网络的意义
  时至今日,互联网已经成了人类发展中最重要的资源,在人类还没有足够充分的准备来迎接网络时代的来临时,它就已经成了我们生活中与一切行动紧密相关的内容。我们已无法离开网络,就象生活少不了吃饭一样。虽然没有网络时代的人也存在了上万年,创造出了种种伟大的文明和辉煌成就,但是人的欲望和智慧总是相依相伴的,欲望一旦促使智慧得到开启后,会变得一发不可收拾,无法再回头。
  随着网络的发展和壮大。各互联网公司对网络的安全,可靠要求也越来越大。网络管理已经成为我们必须熟悉掌握的一项基本技能。

  

  二、Centos 6的网络管理(以太网)
          centos 6网络接口的命名方式:eth[0,1,2],centos 6的网路接口命名根据mac地址来识别,第一个识别到的网卡命名为eth0,第二个识别的为eth1,以此类推。这种情况可能会导致当你在eth0配置好了网络配置文件后。当eth0这块网卡坏掉了。我们买了一块新的网卡还是插回原来的槽中,但是网卡名字已经更改为eth1。这会导致之前的网络配置文件无法使用。这个情况在centos 7上得到了改善,这个我们第三章再做说明。
  

  ifconfig命令(立即生效,但是重启系统后即失效)
  ifconfig [interface]
  # ifconfig -a
[root@localhost ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 08:00:27:78:CB:FC  
          inet addr:192.168.0.114  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe78:cbfc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:189 errors:0 dropped:0 overruns:0 frame:0
          TX packets:308 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:20800 (20.3 KiB)  TX bytes:53808 (52.5 KiB)
          Base address:0xd010 Memory:f0000000-f0020000
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:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:560 (560.0 b)  TX bytes:560 (560.0 b)
#显示所有网卡信息  # ifconfig IFACE [up|down]
#禁用或启用指定的网卡eth0
[root@localhost ~]# ifconfig eth0 down
[root@localhost ~]# ifconfig eth0 up  ifconfig interface [aftype] options | address ...
  # ifconfig IFACE IP/mask [up]
#为eth0配置IP地址,后面直接指定掩码位数
[root@localhost ~]# ifconfig eth0 192.168.1.1/24  # ifconfig IFACE IP netmask MASK
#为eth0配置IP地址,后面直接输入netmask+掩码
[root@localhost ~]# ifconfig eth0 192.168.1.1 netmask 255.255.255.0  

  route命令(路由管理命令)
  查看:route -n
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0  添加:route add
  route add  [-net|-host]  target [netmask Nm] [gw Gw] [[dev] If]
#指定去往192.168.0.88的目标主机网关为192.168.0.1,经由eth0网卡转发
[root@localhost ~]# route add -host 192.168.0.88 gw 192.168.0.1 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.88    192.168.0.1     255.255.255.255 UGH   0      0        0 eth0其中Flags为路由标志,标记当前网络节点的状态:
U Up表示此路由当前为启动状态
H Host,表示此网关为一主机
G Gateway,表示此网关为一路由器
R Reinstate Route,使用动态路由重新初始化的路由
D Dynamically,此路由是动态性地写入
M Modified,此路由是由路由守护程序或导向器动态修改
! 表示此路由当前为关闭状态


#指定去往192.168.0.0的目标网络网关为192.168.0.1,经由eth0网卡转发
[root@localhost ~]# route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1 dev eth0
[root@localhost ~]# route add -net 192.168.0.0/24 gw 192.168.0.1 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     192.168.0.1     255.255.255.0   UG    0      0        0 eth0  

#指定网关为192.168.0.1的默认路由,默认路由只需要添加一条,添加多条以第一条为准
[root@localhost ~]# route add -net 0.0.0.0/32 gw 192.168.0.1
[root@localhost ~]# route add default gw 192.168.0.1
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0  

  删除:route del
  route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
#删除主机路由
[root@localhost ~]# route del -host 192.168.0.88
#删除网络路由
[root@localhost ~]# route del -net 192.168.0.0 netmask 255.255.255.0
#删除默认路由
[root@localhost ~]# route del default  

  DNS服务器指定:
  编辑:/etc/resolv.conf
  nameserver DNS_SERVER_IP1(nameserver 8.8.8.8)
  nameserver DNS_SERVER_IP2
  nameserver DNS_SERVER_IP3
  FQDN:(Fully Qualified Domain Name)完全合格域名/全称域名,是指主机名加上全路径,全路径中列出了序列中所有域成员。全域名可以从逻辑上准确地表示出主机在什么地方,也可以说全域名是主机名的一种完全表示形式。从全域名中包含的信息可以看出主机在域名树中的位置。DNS解析流程:首先查找本机HOSTS表,有的直接使用表中定义,没有查找网络连接中设置的DNS 服务器由他来解析。
  正解:FQDN-->IP(www.google.com-->216.58.221.36)

  # dig -t A FQDN
[root@localhost ~]# dig -t A www.google.com
; <<>> DiG 9.3.4-P1 <<>> -t A www.google.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38677
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 4
;; QUESTION SECTION(这里是我们疑问的问题):
;www.google.com.INA
;; ANSWER SECTION(这里是我们回答我们的问题):
www.google.com.370INA216.58.221.36
;; AUTHORITY SECTION:
google.com.11188INNSns2.google.com.
google.com.11188INNSns1.google.com.
google.com.11188INNSns4.google.com.
google.com.11188INNSns3.google.com.
;; ADDITIONAL SECTION:
ns1.google.com.30626INA216.239.32.10
ns2.google.com.12121INA216.239.34.10
ns3.google.com.21582INA216.239.36.10
ns4.google.com.10738INA216.239.38.10
;; Query time: 12 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)(这里是我们指定为我们做解析的的DNS服务器)
;; WHEN: Thu Sep  3 17:27:02 2015
;; MSG SIZE  rcvd: 184  # host -t A FQDN
[root@localhost ~]# host -t A www.google.com
www.google.com has address 216.58.221.36  反解:IP-->FQDN(216.58.221.36-->www.google.com)
  # dig -x IP
[root@localhost ~]# dig -x 216.58.221.36
; <<>> DiG 9.3.4-P1 <<>> -x 216.58.221.36
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46451
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 4, ADDITIONAL: 4
;; QUESTION SECTION:
;36.221.58.216.in-addr.arpa.INPTR
;; ANSWER SECTION:
36.221.58.216.in-addr.arpa. 281INPTRhkg08s13-in-f4.1e100.net.
36.221.58.216.in-addr.arpa. 281INPTRhkg08s13-in-f36.1e100.net.
;; AUTHORITY SECTION:
221.58.216.in-addr.arpa. 38624INNSns1.google.com.
221.58.216.in-addr.arpa. 38624INNSns3.google.com.
221.58.216.in-addr.arpa. 38624INNSns2.google.com.
221.58.216.in-addr.arpa. 38624INNSns4.google.com.
;; ADDITIONAL SECTION:
ns1.google.com.30015INA216.239.32.10
ns2.google.com.11510INA216.239.34.10
ns3.google.com.20971INA216.239.36.10
ns4.google.com.10127INA216.239.38.10
;; Query time: 13 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Sep  3 17:37:14 2015
;; MSG SIZE  rcvd: 258  # host -t PTR IP
[root@localhost ~]# host -t PTR 216.58.221.36
36.221.58.216.in-addr.arpa domain name pointer hkg08s13-in-f36.1e100.net.
36.221.58.216.in-addr.arpa domain name pointer hkg08s13-in-f4.1e100.net  

  netstat命令:
  netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
  

  显示网络连接:
  netstat [--tcp|-t] [--udp|-u] [--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
#显示tcp协议相关的所有连接状态信息,以数字显示IP和端口
[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:833                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:6010                    :::*                        LISTEN      
tcp        0      0 ::ffff:192.168.0.114:22     ::ffff:192.168.0.107:62362  ESTABLISHED  

#显示tcp协议相关的所有连接状态信息的相关进程及PID,以数字显示IP和端口
[root@localhost ~]# netstat -tanp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:833                 0.0.0.0:*                   LISTEN      1923/rpc.statd      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1894/portmap        
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2182/cupsd         
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2206/sendmail: acce
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      2508/0              
tcp        0      0 :::22                       :::*                        LISTEN      2167/sshd           
tcp        0      0 ::1:6010                    :::*                        LISTEN      2508/0              
tcp        0      0 ::ffff:192.168.0.114:22     ::ffff:192.168.0.107:62362  ESTABLISHED 2508/0  

  显示路由表:

  netstat  {--route|-r} [--numeric|-n]
  -r: 显示内核路由表
  -n: 数字格式
[root@localhost ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.0.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.0.1     0.0.0.0         UG        0 0          0 eth0  

  显示接口统计数据:
  netstat  {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p] [--numeric|-n]
  -i:显示所有接口的统计数据

[root@localhost ~]# netstat -i
Kernel Interface table
Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500   0     8234      0      0      0     6545      0      0      0 BMRU
lo        16436   0       34      0      0      0       34      0      0      0 LRU  -I IFACE:显示IFACE接口的统计数据(-I接口,参数和接口之间不用空格,直接连着打)
[root@localhost ~]# netstat -Ieth0
Kernel Interface table
Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500   0     8265      0      0      0     6559      0      0      0 BMRU  

  ip命令:
  ip - show / manipulate routing, devices, policy routing and tunnels
  ip [ OPTIONS ] OBJECT { COMMAND | help }

  OBJECT := { link | addr | route }

  

  link OBJECT:
  ip link - network device configuration
  set dev IFACE up:禁用指定接口
  set dev IFACE down:禁用指定接口
[root@localhost ~]# ip link set dev eth0 down
[root@localhost ~]# ip link set dev eth0 up  

  show
[dev IFACE]:指定接口

[root@localhost ~]# ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 08:00:27:4d:f1:47 brd ff:ff:ff:ff:ff:ff  

  ip addr - protocol address management
  ip addr { add | del } IFADDR dev STRING

[label LABEL]:添加地址时指明网卡别名

[scope {global(全局可用)|link(仅链接可用)|host(本机可用)}]:指明作用域

      [broadcast ADDRESS]:指明广播地址
#add为添加多一个新IP地址
[root@localhost ~]# ip addr add 192.168.0.222/24 label eth0:0 scope global dev eth0
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 08:00:27:78:cb:fc brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.114/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.222/24 scope global secondary eth0:0
    inet6 fe80::a00:27ff:fe78:cbfc/64 scope link
       valid_lft forever preferred_lft forever  

  ip addr show - look at protocol addresses
[dev DEVICE]

[label PATTERN]

[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 08:00:27:78:cb:fc brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.114/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.222/24 scope global secondary eth0:0
    inet6 fe80::a00:27ff:fe78:cbfc/64 scope link
       valid_lft forever preferred_lft forever  

  ip addr flush - flush protocol addresses
[dev DEVICE]:清空指定设备的所有IP地址,慎用,一执行所有IP地址都清空

  

  ip route - routing table management
  添加路由:ip route add TARGET via GW dev IFACE src SOURCE_IP
  TARGET:主机路由直接输入IP。网络路由:NETWORK/MASK,地址加掩码
[root@localhost ~]# ip route add 192.168.0.88 via 192.168.0.1
[root@localhost ~]# ip route show
192.168.0.88 via 192.168.0.1 dev eth0  添加网关:ip route add defalt via GW dev IFACE
  

  ip route delete
  删除路由:ip route del TARGET
[root@localhost ~]# ip route del 192.168.0.99  ip route show

[root@localhost ~]# ip route show
192.168.0.88 via 192.168.0.1 dev eth0  ip route flush
  dev IFACE:清空指定设备的所有路由条目
  

  

  ss和netstat都是用来查看网络状态的。但是在连接数异常多的时候ss的性能会比netstat快几倍。所以在连接数很多的时候建议使用ss
  ss查看网络状态工具命令:
  格式:ss [OPTION]... [FILTER]
  选项:
  -t: tcp协议相关
  -u: udp协议相关
  -w: 裸套接字相关
  -x:unix sock相关
  -l: listen状态的连接
  -a: 所有
  -n: 数字格式
  -p: 相关的程序及PID
  -e: 扩展的信息
  -m:内存用量
  -o:计时器信息
  

  FILTER := [ state TCP-STATE ] [ EXPRESSION ]
  TCP的常见状态:

  tcp finite state machine:
  LISTEN: 监听
  ESTABLISHED:已建立的连接
  FIN_WAIT_1
  FIN_WAIT_2
  SYN_SENT
  SYN_RECV
  CLOSED
  

  EXPRESSION:
  dport =
  sport =
  示例:’( dport = :ssh or sport = :ssh )’
  

  常用组合:
  -tan, -tanl, -tanlp, -uan
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-453178-1-1.html 上篇帖子: centos创建ftp方法,个人经验 下篇帖子: 制作Redhat/CentOS/Fedora更新源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表