Linux基本指令之网络通信命令

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云防火墙,500元 1000GB
简介: Linux基本指令之网络通信命令

1、netstat监控命令

netstat命令是用于监控进出网络的包和网络接口统计的命令行工具,查看本机开放了哪些端口;本机开放的所有的端口。

需要先安装:yum install net-tools -y

语法结构:netstat [param]

param参数可以为:

-h 查看帮助
-r  查看路由表
-i 查看网络接口

2、lsof:查看端口被占用情况

需要先安装:

yum install lsof -y

① 查看哪个端口被哪个进程占用了。

lsof -i:22 查看22端口被哪个进程占用了

② 查看某个进程打开了哪些文件、加载库,依赖关系

lsof -p 64138

③  或者是哪个文件夹被哪个进程打开

lsof  /root/

3、ss:显示更多更详细的有关 TCP 和连接状态的信息

ss 是 Socket Statistics 的缩写。ss 命令可以用来获取 socket 统计信息,它显示的内容和 netstat 类似。但 ss 的优势在于它能够显示更多更详细的有关 TCP 和连接状态的信息,而且比 netstat 更快。

常用选项:

ss 输出所有建立的连接(不包含监听的端口),包括 tcp, udp, and unix
ss -tnl 查看主机监听的tcp端口信息
ss -tna 查看监听的tcp连接

4、nc和nmap:扫描别人机器上开放了哪些端口。

nc:扫描别人机器上开放了哪些端口。

需要先安装:yum  install  nc -y

nc:ncat - Concatenate and redirect sockets   ----cat: Concatenate 连接递归到socket。

可接选项:

-z     Only scan for listening daemons, without sending any data to them. 查看端口号是否开发
[-w timeout]  超时,等待时间

例如:-z :查看192.168.0.1的80端口是否开放。

1. [root@slave-mysql ~]# nc -z 192.168.0.1 80
2. Connection to 192.168.0.1 80 port [tcp/http] succeeded!
3. [root@slave-mysql ~]# echo $?
4. 0   为0表示上层命令执行成功
5. [root@slave-mysql ~]# nc -z 192.168.0.1 8080
6. ^C
7. [root@slave-mysql ~]# man nc
8. [root@slave-mysql ~]# nc -w 1 -z 192.168.0.1 8080
9. [root@slave-mysql ~]# echo $?
10. 1

nmap:探测一个机器或者整个局域网里机器开放了哪些端口。网络探测工具和安全/端口扫描器。速度比较慢。

例如:扫描本台机器上常见的端口号:nmap 192.168.2.137

1. [root@nginx-kafka01 ~]# nmap 192.168.2.137
2. Starting Nmap 6.40 ( http://nmap.org ) at 2022-08-04 10:31 CST
3. Nmap scan report for nginx-kafka03 (192.168.2.137)
4. Host is up (0.00014s latency).
5. Not shown: 997 closed ports
6. PORT     STATE SERVICE
7. 22/tcp   open  ssh
8. 111/tcp  open  rpcbind
9. 3306/tcp open  mysql
10. MAC Address: 00:0C:29:3F:07:8B (VMware)
11. 
12. Nmap done: 1 IP address (1 host up) scanned in 1.85 seconds

5、查看系统资源使用情况

1、glances:监听系统CPU、内存、磁盘I/O等使用情况

基于Python开发,使用psutil库来从系统抓取信息的基于curses开发的跨平台的命令行系统监视工具。使用glances,我们可以监视CPU、平均负载、内存、网络流量,磁盘I/O,其他处理器和文件、系统的利用情况。

安装:

yum install epel-release -y

yum install glances -y

按q退出

2、nethogs:查看某个进程消耗了多少流量(动态显示)

查看某个进程消耗了多少流量,知道哪些进程和外面进行通信。

yum install epel-release -y

yum install nethogs -y

按q退出

1. [root@nginx-kafka01 shell]# nethogs
2. Ethernet link detected
3.                       Ethernet link detected
4.                                             Waiting for first packet to arrive (see sourceforge.net bug 1019381)

6、查看网络流量

1、tcpdump:抓包查看网络流量

     ifconfig :字符界面的抓包工具---Linux中

需要安装:

yum install tcpdump -y

详解:计算机网络抓包工具——tcpdump详解

2、wireshark:抓包查看网络流量

Linux和windows里的图形化的抓包工具。需要先安装:

yum install tcpdump -y

3、iftop:查看网络上的流量情况

用于查看网络上的流量情况(查看你的机器和别人机器之间的流量。),包括实时速率、总流量、平均流量等,是一款实时流量监控工具。需要先安装。

iftop命令不记录历史数据,无报表,且只能显示从程序启动到现在的总流量。它的运行需要root权限。

yum install iftop -y

7、Linux重启网络

设置了Linux网络,需要重启网络,可以使用命令:

service network restart

systemctl network restart

8、SELinux简介

selinux是Linux操作系统里面一个安全相关的系统,它里面定义了很多跟安全相关的规则限制。selinux非常安全,但是规则非常繁琐,所以日常运维里,一般都会将它这个服务关闭。

其实SELinux的功能也就是类似于防火墙的功能,不过安全相对比较好,ok,所以本博客介绍一下SELinux的使用命令。

1、查看selinux的状态

getenforce

临时放宽selinux的规则:1为强制模式,0为宽容模式。

1. [root@localhost /]# getenforce   # 获取当前selinux的策略
2. Disabled
3. [root@localhost /]# setenforce 1 设置它的策略,为强制模式--强制执行selinux的规则
4. setenforce: SELinux is disabled
5. [root@localhost /]# setenforce 0  #设置它的策略为 宽容模式--对对大多数情况都不限制
6. setenforce: SELinux is disabled

2、临时禁用SELINUX(学习用的linux才可以禁用,生产的不建议禁用SELINUX)

setenforce

3、永久修改 ----

修改配置文件 /etc/selinux/config里的 :SELINUX=disabled

# 将SELINUX=enforce改成SELINUX=disabled

vi /etc/selinux/config

然后重启服务器生效。

9、防火墙设置

查询Linux系统防火墙开启状态

firewall-cmd  --state

1. [root@localhost ~]# firewall-cmd --state
2. not running

临时关闭防火墙(学习用的Linux才可以禁用,生产的不建议禁用selinux)

systemctl stop firewalld.service

永久关闭防火墙(学习用的Linux才可以禁用,生产的不建议禁用selinux)

systemctl disabled  firewalld.service

10、curl命名使用

curl命令是一个利用url规则在shell终端命令行下工作的文件传输工具;curl命令作为一款强力工具,curl支持包括HTTP、HTTPS、FTP等众多协议,还支持POST、cookies、认证、从指定偏移出下载部分文件、用户代理字符串、限速、文件大小、进度条等特征。

语法格式为:curl 【参数】【网址】

1、查看命令参数

使用curl --help可以查看;

2、查看curl手册

查看详细使用:curl --manual查看基本用法例子;

3、提取指定网页

① 查看网页源码:curl url(url为网站域名),如:

1. [root@localhost ~]# curl www.baidu.com
2. <!DOCTYPE html>
3. <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

② 显示头信息:curl -i 网址

如:curl -i https://www.baidu.com

③ 显示通信过程

curl -v www.baidu.com

1. [root@localhost ~]# curl -v www.baidu.com
2. * Rebuilt URL to: www.baidu.com/
3. *   Trying 180.101.49.12...
4. * Connected to www.baidu.com (180.101.49.12) port 80 (#0)
5. > GET / HTTP/1.1
6. > Host: www.baidu.com
7. > User-Agent: curl/7.49.0
8. > Accept: */*
9. >
10. < HTTP/1.1 200 OK
11. < Accept-Ranges: bytes
12. < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
13. < Connection: keep-alive
14. < Content-Length: 2381
15. < Content-Type: text/html
16. < Date: Wed, 28 Sep 2022 01:31:49 GMT
17. < Etag: "588604c8-94d"
18. < Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
19. < Pragma: no-cache
20. < Server: bfe/1.0.8.18
21. < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/

④ 查看协议书:curl --trace-ascii output.txt https://www.baidu.com

1. [root@localhost ~]# curl --trace-ascii output.txt https://www.baidu.com
2. curl: (60) SSL certificate problem: certificate is not yet valid
3. More details here: https://curl.haxx.se/docs/sslcerts.html
4. 
5. curl performs SSL certificate verification by default, using a "bundle"
6.  of Certificate Authority (CA) public keys (CA certs). If the default
7.  bundle file isn't adequate, you can specify an alternate file
8.  using the --cacert option.
9. If this HTTPS server uses a certificate signed by a CA represented in
10.  the bundle, the certificate verification probably failed due to a
11.  problem with the certificate (it might be expired, or the name might
12.  not match the domain name in the URL).
13. If you'd like to turn off curl's verification of the certificate, use
14.  the -k (or --insecure) option.
15.

⑤ 查看时间:

curl --trace-ascii output.txt --trace-time https://www.baidu.com

4、使用curl进行认证

referer设置

referer参数描述了一个网页的来源,可以按F12调试,查看

访问百度时候,加上referer为csdn:

curl --referer https://blog.csdn.net https://www.baidu.com

① 用户代理设置

用户代理常被用于兼容ie,有时也会看到获取带有curl的页面不会返回与使用浏览器获取页面时看到的页面相同的页面,这时候可以设置用户代理:

curl www.baidu.com --user-agent "Mozilla/5.0"
curl www.baidu.com -A "Mozilla/5.0"

5、curl文件操作

① 保存文件

保存文件语法格式:curl -o filename uri

1. 
2. [root@localhost ~]# curl -o baidu.html www.baidu.com
3.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
4.                                  Dload  Upload   Total   Spent    Left  Speed
5. 100  2381  100  2381    0     0  25869      0 --:--:-- --:--:-- --:--:-- 34507

② 文件下载

语法结构:curl uri filename --silent|progress

  • –silent:不显示进度条
  • –progress:显示进度条

curl https://avatar.csdnimg.cn/3/5/E/1_meituantech_1564971608.jpg -omeituan.jpg --progress

③ 上传文件

语法结构: curl --form upload=@localfilename --form press=OK [URL]

例如:curl --form upload=@baidu.html --form press=OK url

6、设置cookie

设置cookie,可以用命令   --cookie

-c cookie-file可以保存服务器返回的cookie到文件,-b cookie-file可以使用这个文件作为cookie信息,进行后续的请求。

如:curl -c cookies http://example.com

curl -b cookies http://example.com

curl -c "name=cookiename" www.baidu.com

也可以用–cookie-jar指定cookie文件:curl www.baidu.com --cookie-jar cookie_file

指定可下载的最大文件大小,可以用命令–limit-rate:curl www.baidu.com --limit-rate 50k

7、restful api

curl默认是GET方式请求url的,要调POST等接口,必须加上-X参数指定.

-H指定请求header,-d指定参数。

curl post:

curl -H "Content-Type:application/json" -X POST localhost:9200/company/employees/1 -d '{"id" :1,"name":"admin","password":"xxx"}'

curl get:

curl -X GET localhost:9200/company/employees/1?pretty=true

curl delete:

curl -X DELETE localhost:9200/company/employees/1

curl put:

curl -H "Content-Type:application/json" -X PUT localhost:9200/company/employees/1 -d '{"id" :1,"name":"admin","password":"xxx"}'

相关文章
|
3天前
|
Linux Python Perl
Linux命令删除文件里的字符串
Linux命令删除文件里的字符串
15 7
|
4天前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
6天前
|
Linux Perl
Linux之sed命令
Linux之sed命令
|
5天前
|
Linux
深入理解Linux中的cp命令:文件与目录的复制利器
深入理解Linux中的cp命令:文件与目录的复制利器
|
6天前
|
Linux Docker 容器
9. 同步执行Linux多条命令
9. 同步执行Linux多条命令
|
6天前
|
Linux Shell
10-10|linux命令查询 关键字在文本中出现的行数
10-10|linux命令查询 关键字在文本中出现的行数
|
6天前
|
Ubuntu Linux Shell
10-21|在linux我如果修改了时间,什么命令可以恢复正确啊
10-21|在linux我如果修改了时间,什么命令可以恢复正确啊
|
3天前
|
存储 安全 网络安全
云计算与网络安全:技术融合下的信息安全新挑战
【9月更文挑战第29天】在数字化浪潮的推动下,云计算服务如雨后春笋般涌现,为各行各业提供了前所未有的便利和效率。然而,随着数据和服务的云端化,网络安全问题也日益凸显,成为制约云计算发展的关键因素之一。本文将从技术角度出发,探讨云计算环境下网络安全的重要性,分析云服务中存在的安全风险,并提出相应的防护措施。我们将通过实际案例,揭示如何在享受云计算带来的便捷的同时,确保数据的安全性和完整性。
|
3天前
|
SQL 安全 算法
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
【9月更文挑战第29天】随着互联网的普及,网络安全问题日益严重。本文将介绍网络安全漏洞、加密技术以及安全意识等方面的内容,帮助读者了解网络安全的重要性,提高自身的网络安全意识。
|
4天前
|
存储 SQL 安全
网络安全与信息安全:构建安全防线的关键策略
本文深入探讨了网络安全与信息安全领域的核心要素,包括网络安全漏洞、加密技术以及安全意识的重要性。通过对这些关键领域的分析,旨在为读者提供一套综合性的防护策略,帮助企业和个人在日益复杂的网络环境中保障数据安全。
14 4
下一篇
无影云桌面