1、使用lsof 命令来查看端口是否开放
- lsof -i:6379 //如果有显示说明已经开放了,如果没有显示说明没有开放
[root@localhost ~]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 431717 root 4u IPv4 133236252 0t0 TCP *:6379 (LISTEN)
2、使用netstat 命令来查看端口是否开放
- netstat -aptn |grep -i 6379 //查看是否监听在0.0.0.0:6379
[root@localhost ~]# netstat -aptn |grep -i 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 431717/./bin/redis-
- netstat -lptn |grep -i 6379 //查看TCP类型的端口
[root@localhost ~]# netstat -lptn |grep -i 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 431717/./bin/redis-
- netstat -lpun |grep -i 6379 //查看UDP类型的端口
[root@localhost ~]# netstat -lpun |grep -i 6379
[root@localhost ~]#
3、使用telnet方式测试远程主机端口是否打开
- telnet 127.0.0.1 6379 //telnet IP 端口号
telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
4、netstat -tunlp 用于显示 tcp,udp 的端口和进程等相关情况。
netstat 查看端口占用语法格式:
netstat -tunlp | grep 端口号
- -t (tcp) 仅显示tcp相关选项
- -u (udp)仅显示udp相关选项
- -n 拒绝显示别名,能显示数字的全部转化为数字
- -l 仅列出在Listen(监听)的服务状态
- -p 显示建立相关链接的程序名
例如查看 8000 端口的情况,使用以下命令:
netstat -tunlp | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 26993/nodejs
更多命令:
netstat -ntlp //查看当前所有tcp端口
netstat -ntulp | grep 80 //查看所有80端口使用情况
netstat -ntulp | grep 3306 //查看所有3306端口使用情况