The lsof (list open files) command will list open files. The -i option limits it to open network connections:
$ lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE
NAME
firefox-b 2261 slynux 78u IPv4 63729 0t0 TCP
localhost:47797->localhost:42486 (ESTABLISHED)
firefox-b 2261 slynux 80u IPv4 68270 0t0 TCP
slynux-laptop.local:41204->192.168.0.2:3128 (CLOSE_WAIT)
firefox-b 2261 slynux 82u IPv4 68195 0t0 TCP
slynux-laptop.local:41197->192.168.0.2:3128 (ESTABLISHED)
ssh 3570 slynux 3u IPv6 30025 0t0 TCP
localhost:39263->localhost:ssh (ESTABLISHED)
ssh 3836 slynux 3u IPv4 43431 0t0 TCP
slynux-laptop.local:40414->boney.mt.org:422 (ESTABLISHED)
GoogleTal 4022 slynux 12u IPv4 55370 0t0 TCP
localhost:42486 (LISTEN)
GoogleTal 4022 slynux 13u IPv4 55379 0t0 TCP
localhost:42486->localhost:32955 (ESTABLISHED)
Each entry in the output of lsof corresponds to a service with an active network port. The last column of output consists of lines similar to this:
laptop.local:41197->192.168.0.2:3128
In this output, laptop.local:41197 corresponds to the localhost and 192.168.0.2:3128 corresponds to the remote host. 41197 is the port used on the current machine, and 3128 is the port to which the service connects at the remote host.
To list the opened ports from the current machine, use the following command:
$ lsof -i | grep ":[0-9a-z]+->" -o | grep "[0-9a-z]+" -o | sort | uniq