There are two commands which are popular to get the result: netstat and lsof. We probably already have been familiar with
to list all the listening ports. We actually could do
lsof -Pnl +M -i4
to list all IPv4 ports or
lsof -Pnl +M -i6
to list all IPv6 ports as well.