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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云防火墙,500元 1000GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 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"}'

相关文章
|
11天前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
43 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
28天前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
104 16
|
20天前
|
Shell 开发工具 git
使用代理访问网络各项命令总结
工作中常需设置代理,容易导致环境混乱。本文总结了 SourceTree 无法拉取代码的问题,排查了环境变量、Git 全局配置及系统代理设置,最终通过清除 Git 代理配置解决。内容涵盖排查步骤、命令整理及脚本处理,帮助快速定位并解决代理相关网络问题。
69 1
|
20天前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
|
2月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
126 18
|
2月前
|
网络协议 关系型数据库 Linux
【App Service Linux】在Linux App Service中安装 tcpdump 并抓取网络包
在App Service for Linux环境中,无法像Windows一样直接使用网络排查工具抓包。本文介绍了如何通过TCPDUMP在Linux环境下抓取网络包,包括SSH进入容器、安装tcpdump、执行抓包命令及下载分析文件的完整操作步骤。
120 5
|
2月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
398 0
|
2月前
|
安全 Linux Shell
Linux系统中sudo命令的高效运用技巧。
用户可以通过sudo -l来列出自己目前可执行的命令列表,这有助于用户了解自己的权限范围。
123 0
|
10月前
|
SQL 安全 网络安全
网络安全与信息安全:知识分享####
【10月更文挑战第21天】 随着数字化时代的快速发展,网络安全和信息安全已成为个人和企业不可忽视的关键问题。本文将探讨网络安全漏洞、加密技术以及安全意识的重要性,并提供一些实用的建议,帮助读者提高自身的网络安全防护能力。 ####
226 17