使用SS命令检查打开的端口

SS命令可用于显示哪些端口正在侦听连接。

建议使用-ltn选项来看看简洁和相关的输出。

$ sudo ss -ltn
State      Recv-Q     Send-Q         Local Address:Port            Peer Address:Port     Process     
LISTEN     0          4096           127.0.0.53%lo:53                   0.0.0.0:*                    
LISTEN     0          5                  127.0.0.1:631                  0.0.0.0:*                    
LISTEN     0          70                 127.0.0.1:33060                0.0.0.0:*                    
LISTEN     0          151                127.0.0.1:3306                 0.0.0.0:*                    
LISTEN     0          5                      [::1]:631                     [::]:*                    
LISTEN     0          511                        *:80                         *:*

我们可以看到我们的服务器正在侦听端口80,3306和33060上的连接。
这些是与HTTP和MySQL相关联的众所周知的端口。

我们还可以看到“SS”输出显示端口53和631处于聆听状态。
这些都分别用于DNS和Internet打印协议。

要查看侦听端口对应的进程,使用“-P”选项。

$ sudo ss -ltnp
State    Recv-Q   Send-Q      Local Address:Port        Peer Address:Port   Process                                      
LISTEN   0        4096        127.0.0.53%lo:53               0.0.0.0:*       users:(("systemd-resolve",pid=530,fd=13))   
LISTEN   0        5               127.0.0.1:631              0.0.0.0:*       users:(("cupsd",pid=572,fd=7))              
LISTEN   0        70              127.0.0.1:33060            0.0.0.0:*       users:(("mysqld",pid=2320,fd=32))           
LISTEN   0        151             127.0.0.1:3306             0.0.0.0:*       users:(("mysqld",pid=2320,fd=34))           
LISTEN   0        5                   [::1]:631                 [::]:*       users:(("cupsd",pid=572,fd=6))              
LISTEN   0        511                     *:80                     *:*       users:(("apache2",pid=2728,fd=4),("apache2",pid=2727,fd=4),("apache2",pid=2725,fd=4))
如何查看Ubuntu Linux打开了哪些端口

查看UFW防火墙打开的端口

使用以下命令检查UFW防火墙的状态。

$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

开放某个端口:

$ sudo ufw allow 80/tcp
Rule added
Rule added (v6)
$ sudo ufw allow 3306/tcp
Rule added
Rule added (v6)

我们可以再次检查UFW的状态,以查看端口现在已打开。

$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To                         Action      From
--                         ------      ---
80/tcp                     ALLOW IN    Anywhere                  
3306/tcp                   ALLOW IN    Anywhere                  
80/tcp (v6)                ALLOW IN    Anywhere (v6)             
3306/tcp (v6)              ALLOW IN    Anywhere (v6)

使用NMAP检查打开端口

NMAP是一种网络侦探工具,可用于检查远程主机上的打开端口。
但是,我们也可以使用它来检查自己的系统,以便快速列出已被打开的端口。

通常,我们将指定要扫描NMAP的远程IP地址。
这里,我们可以通过在命令中指定localhost来扫描我们自己的系统。

$ sudo nmap localhost
Starting Nmap 7.80 ( https://nmap.org ) at 2021-03-12 20:43 EST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000012s latency).
Not shown: 997 closed ports
PORT     STATE SERVICE
80/tcp   open  http
631/tcp  open  ipp
3306/tcp open  mysql
Nmap done: 1 IP address (1 host up) scanned in 0.18 seconds
日期:2020-07-07 20:55:14 来源:oir作者:oir