Linux:端口:修订间差异
来自康健生活
创建页面,内容为“== 端口(Centos7) == 开放端口 <syntaxhighlight lang="bash"> firewall-cmd --zone=public --add-port=5672/tcp --permanent # 开放5672端口 firewall-cmd --…” |
无编辑摘要 |
||
| (未显示同一用户的6个中间版本) | |||
| 第1行: | 第1行: | ||
== | == 端口使用情况 == | ||
查看到进程占用的端口号 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
netstat -anp | grep pid</syntaxhighlight>|#}} | |||
netstat命令各个参数说明如下: | |||
* -t : 指明显示TCP端口 | |||
* -u : 指明显示UDP端口 | |||
* -l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protocol)与资料的程序) | |||
* -p : 显示进程标识符和程序名称,每一个套接字/端口都属于一个程序。 | |||
* -n : 不进行DNS轮询,显示IP(可以加速操作) | |||
<syntaxhighlight lang="bash"> | {| class="wikitable" | ||
|- | |||
| 显示系统端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -anp</syntaxhighlight>|#}} | |||
|- | |||
| TCP类型的端口 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -ntpl</syntaxhighlight>|#}} | |||
|- | |||
| UDP类型的端口 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -nupl</syntaxhighlight>|#}} | |||
|- | |||
| 端口扫描 || {{shc|<syntaxhighlight lang="bash"> | |||
nmap</syntaxhighlight>|#}} | |||
|- | |||
| 查看网络端口 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -an</syntaxhighlight>|#}} | |||
|- | |||
| 查看某一端口的连接客户端IP 比如3306端口 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -anp |grep 3306</syntaxhighlight>|#}} | |||
|- | |||
| 查看某一端口的连接数量,比如3306端口 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -pnt |grep :3306 |wc</syntaxhighlight>|#}} | |||
|- | |||
| 查看一个服务有几个端口。比如要查看mysqld || {{shc|<syntaxhighlight lang="bash"> | |||
ps -ef |grep mysqld</syntaxhighlight>|#}} | |||
|- | |||
| 查看一台服务器上面哪些服务及端口 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -lanp</syntaxhighlight>|#}} | |||
|- | |||
| 查看所有3306端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -an | grep 3306</syntaxhighlight>|#}} | |||
|- | |||
| 查看所有80端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | |||
netstat -ntulp |grep 80</syntaxhighlight>|#}} | |||
|} | |||
查看8000端口的使用情况 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
lsof -i:8000</syntaxhighlight>|#}} | |||
根据线程查出具体的应用 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
ps axu |grep 20329</syntaxhighlight>|#}} | |||
{| class="wikitable" | |||
|+ PS 参数 | |||
|- | |||
| -A || 所有的进程均显示出来,与 -e 具有同样的效用; | |||
|- | |||
| -a || 显示现行终端机下的所有进程,包括其他用户的进程; | |||
|- | |||
| -u || 以用户为主的进程状态 ; | |||
|- | |||
| -x || 通常与 a 这个参数一起使用,可列出较完整信息 | |||
|} | |||
{| class="wikitable" | |||
|+ PS 输出 | |||
|- | |||
| -l || 较长、较详细的将该PID 的的信息列出; | |||
|- | |||
| -j || 工作的格式 (jobs format) | |||
|- | |||
| -f || 做一个更为完整的输出。 | |||
|} | |||
== 端口(Centos7) == | |||
开放端口 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
firewall-cmd --zone=public --add-port=5672/tcp --permanent # 开放5672端口 | |||
firewall-cmd --zone=public --remove-port=5672/tcp --permanent #关闭5672端口 | |||
firewall-cmd --reload # 配置立即生效</syntaxhighlight>|#}} | |||
查看防火墙所有开放的端口 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
firewall-cmd --zone=public --list-ports</syntaxhighlight>|#}} | |||
关闭防火墙,如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
systemctl stop firewalld.service</syntaxhighlight>|#}} | |||
查看防火墙状态 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
firewall-cmd --state</syntaxhighlight>|#}} | |||
查看监听的端口 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
netstat -lnpt</syntaxhighlight>|#}} | |||
检查端口被哪个进程占用 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
netstat -lnpt |grep 5672</syntaxhighlight>|#}} | |||
查看进程的详细信息 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
ps 6832</syntaxhighlight>|#}} | |||
中止进程 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
kill -9 6832</syntaxhighlight>|#}} | |||
== 端口(ubuntu) == | |||
查看端口开启状态 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo ufw status</syntaxhighlight>|#}} | |||
开启某个端口,比如我开启的是8381 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo ufw allow 8381</syntaxhighlight>|#}} | |||
开启防火墙 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo ufw enable</syntaxhighlight>|#}} | |||
关闭防火墙 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo ufw disable</syntaxhighlight>|#}} | |||
重启防火墙 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo ufw reload</syntaxhighlight>|#}} | |||
禁止外部某个端口比如80 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo ufw delete allow 80</syntaxhighlight>|#}} | |||
查看端口ip | |||
{{shc|<syntaxhighlight lang="bash"> | |||
netstat -ltn</syntaxhighlight>|#}} | |||