背景
平时开发或线上运维少不了跟端口打交道,今天我们简单聊聊平时跟端口相关的几个场景
首先查看某台主机某个端口服务是否正常
比如查看本机 redis服务是否正常
telnet 127.0.0.1 6379
如果提示如下:
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
则说明服务没有正常监听端口或者端口没有开放,可以用下面2中方法一一排除
查看端口是否正常监听或被谁占用(lsof netstat ss三种方法)
1. lsof指 列出打开文件(lists openfiles)。而有一点要切记,在Unix中一切(包括网络套接口)都是文件。
-i 列出所有的网络连接
lsof -i :6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 2542 redis 6u IPv4 19003 0t0 TCP VM_0_15_centos:6379 (LISTEN)
列出所有tcp 网络连接信息
lsof -i tcp
更详细的用法:https://www.phpmianshi.com/?id=103
2. netstat命令
netstat -tunplp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 2542/redis-server 1
更详细的用法:https://www.phpmianshi.com/?id=105
3. ss命令
ss -a |grep 6379
tcp LISTEN 0 511 127.0.0.1:6379 *:*
更详细的用法:https://www.phpmianshi.com/?id=105
1. 直接命令行操作
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
service iptables save
service iptables restart
/sbin/iptables -L -n
2. 编辑/etc/sysconfig/iptables
vi /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22022 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2100:2300 -j ACCEPT
service iptables restart
/sbin/iptables -L -n