Linux:端口:修订间差异
来自康健生活
无编辑摘要 |
|||
| 第2行: | 第2行: | ||
查看到进程占用的端口号 | 查看到进程占用的端口号 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
netstat -anp | grep pid</syntaxhighlight>}} | netstat -anp | grep pid</syntaxhighlight>|#}} | ||
netstat命令各个参数说明如下: | netstat命令各个参数说明如下: | ||
* -t : 指明显示TCP端口 | * -t : 指明显示TCP端口 | ||
| 第13行: | 第13行: | ||
|- | |- | ||
| 显示系统端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | | 显示系统端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -anp</syntaxhighlight>}} | netstat -anp</syntaxhighlight>|#}} | ||
|- | |- | ||
| TCP类型的端口 || {{shc|<syntaxhighlight lang="bash"> | | TCP类型的端口 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -ntpl</syntaxhighlight>}} | netstat -ntpl</syntaxhighlight>|#}} | ||
|- | |- | ||
| UDP类型的端口 || {{shc|<syntaxhighlight lang="bash"> | | UDP类型的端口 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -nupl</syntaxhighlight>}} | netstat -nupl</syntaxhighlight>|#}} | ||
|- | |- | ||
| 端口扫描 || {{shc|<syntaxhighlight lang="bash"> | | 端口扫描 || {{shc|<syntaxhighlight lang="bash"> | ||
nmap</syntaxhighlight>}} | nmap</syntaxhighlight>|#}} | ||
|- | |- | ||
| 查看网络端口 || {{shc|<syntaxhighlight lang="bash"> | | 查看网络端口 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -an</syntaxhighlight>}} | netstat -an</syntaxhighlight>|#}} | ||
|- | |- | ||
| 查看某一端口的连接客户端IP 比如3306端口 || {{shc|<syntaxhighlight lang="bash"> | | 查看某一端口的连接客户端IP 比如3306端口 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -anp |grep 3306</syntaxhighlight>}} | netstat -anp |grep 3306</syntaxhighlight>|#}} | ||
|- | |- | ||
| 查看某一端口的连接数量,比如3306端口 || {{shc|<syntaxhighlight lang="bash"> | | 查看某一端口的连接数量,比如3306端口 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -pnt |grep :3306 |wc</syntaxhighlight>}} | netstat -pnt |grep :3306 |wc</syntaxhighlight>|#}} | ||
|- | |- | ||
| 查看一个服务有几个端口。比如要查看mysqld || {{shc|<syntaxhighlight lang="bash"> | | 查看一个服务有几个端口。比如要查看mysqld || {{shc|<syntaxhighlight lang="bash"> | ||
ps -ef |grep mysqld</syntaxhighlight>}} | ps -ef |grep mysqld</syntaxhighlight>|#}} | ||
|- | |- | ||
| 查看一台服务器上面哪些服务及端口 || {{shc|<syntaxhighlight lang="bash"> | | 查看一台服务器上面哪些服务及端口 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -lanp</syntaxhighlight>}} | netstat -lanp</syntaxhighlight>|#}} | ||
|- | |- | ||
| 查看所有3306端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | | 查看所有3306端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -an | grep 3306</syntaxhighlight>}} | netstat -an | grep 3306</syntaxhighlight>|#}} | ||
|- | |- | ||
| 查看所有80端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | | 查看所有80端口使用情况 || {{shc|<syntaxhighlight lang="bash"> | ||
netstat -ntulp |grep 80</syntaxhighlight>}} | netstat -ntulp |grep 80</syntaxhighlight>|#}} | ||
|} | |} | ||
查看8000端口的使用情况 | 查看8000端口的使用情况 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
lsof -i:8000</syntaxhighlight>}} | lsof -i:8000</syntaxhighlight>|#}} | ||
根据线程查出具体的应用 | 根据线程查出具体的应用 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
ps axu |grep 20329</syntaxhighlight>}} | ps axu |grep 20329</syntaxhighlight>|#}} | ||
{| class="wikitable" | {| class="wikitable" | ||
| 第81行: | 第81行: | ||
firewall-cmd --zone=public --add-port=5672/tcp --permanent # 开放5672端口 | firewall-cmd --zone=public --add-port=5672/tcp --permanent # 开放5672端口 | ||
firewall-cmd --zone=public --remove-port=5672/tcp --permanent #关闭5672端口 | firewall-cmd --zone=public --remove-port=5672/tcp --permanent #关闭5672端口 | ||
firewall-cmd --reload # 配置立即生效</syntaxhighlight>}} | firewall-cmd --reload # 配置立即生效</syntaxhighlight>|#}} | ||
查看防火墙所有开放的端口 | 查看防火墙所有开放的端口 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
firewall-cmd --zone=public --list-ports</syntaxhighlight>}} | firewall-cmd --zone=public --list-ports</syntaxhighlight>|#}} | ||
关闭防火墙,如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估 | 关闭防火墙,如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
systemctl stop firewalld.service</syntaxhighlight>}} | systemctl stop firewalld.service</syntaxhighlight>|#}} | ||
查看防火墙状态 | 查看防火墙状态 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
firewall-cmd --state</syntaxhighlight>}} | firewall-cmd --state</syntaxhighlight>|#}} | ||
查看监听的端口 | 查看监听的端口 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
netstat -lnpt</syntaxhighlight>}} | netstat -lnpt</syntaxhighlight>|#}} | ||
检查端口被哪个进程占用 | 检查端口被哪个进程占用 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
netstat -lnpt |grep 5672</syntaxhighlight>}} | netstat -lnpt |grep 5672</syntaxhighlight>|#}} | ||
查看进程的详细信息 | 查看进程的详细信息 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
ps 6832</syntaxhighlight>}} | ps 6832</syntaxhighlight>|#}} | ||
中止进程 | 中止进程 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
kill -9 6832</syntaxhighlight>}} | kill -9 6832</syntaxhighlight>|#}} | ||
== 端口(ubuntu) == | == 端口(ubuntu) == | ||
查看端口开启状态 | 查看端口开启状态 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo ufw status</syntaxhighlight>}} | sudo ufw status</syntaxhighlight>|#}} | ||
开启某个端口,比如我开启的是8381 | 开启某个端口,比如我开启的是8381 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo ufw allow 8381</syntaxhighlight>}} | sudo ufw allow 8381</syntaxhighlight>|#}} | ||
开启防火墙 | 开启防火墙 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo ufw enable</syntaxhighlight>}} | sudo ufw enable</syntaxhighlight>|#}} | ||
关闭防火墙 | 关闭防火墙 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo ufw disable</syntaxhighlight>}} | sudo ufw disable</syntaxhighlight>|#}} | ||
重启防火墙 | 重启防火墙 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo ufw reload</syntaxhighlight>}} | sudo ufw reload</syntaxhighlight>|#}} | ||
禁止外部某个端口比如80 | 禁止外部某个端口比如80 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo ufw delete allow 80</syntaxhighlight>}} | sudo ufw delete allow 80</syntaxhighlight>|#}} | ||
查看端口ip | 查看端口ip | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
netstat -ltn</syntaxhighlight>}} | netstat -ltn</syntaxhighlight>|#}} | ||