ipconfig.co.uk

Firewall commands: is the port open, and how do I open it?

When the address is right, the route is right and DNS works, the last place a connection dies is a firewall: on your machine, on the far machine, or in between. Here is how to look at each, and the one test that tells you which.

First, find out where it is blocked

  1. Is anything listening? On the server: ss -tlnp | grep :8080, netstat -ano | findstr :8080, lsof -iTCP:8080 -sTCP:LISTEN. If nothing is, no firewall rule will help. Check it listens on 0.0.0.0 or *, not only 127.0.0.1. Details →
  2. Does it work from the server itself? curl -v http://127.0.0.1:8080/ or nc -zv 127.0.0.1 8080. If not, the application is the problem.
  3. Does it work from the same LAN? Test-NetConnection 192.168.1.10 -Port 8080 or nc -zv 192.168.1.10 8080 from another machine. If not, it is the host firewall on the server (this page).
  4. Does it work from the internet? If LAN works and internet does not, it is the router's NAT/port forwarding, a CGNAT ISP, or a cloud security group, not the host.
  5. Watch it arrive. tcpdump -i eth0 -n 'tcp port 8080' on the server while connecting: SYN arrives but no reply means the host firewall dropped it; nothing arrives means it never got there. Capture guide →

Windows Defender Firewall

Windows · Command Prompt
netsh advfirewall show allprofiles                            :: state of Domain, Private, Public profiles
netsh advfirewall show currentprofile
netsh advfirewall firewall show rule name=all | findstr /i "Rule Name Enabled Direction LocalPort" 
netsh advfirewall firewall show rule name="Allow 8080"

:: allow inbound TCP 8080 (admin)
netsh advfirewall firewall add rule name="Allow 8080" dir=in action=allow protocol=TCP localport=8080
:: only from the LAN
netsh advfirewall firewall add rule name="Allow 8080 LAN" dir=in action=allow protocol=TCP localport=8080 remoteip=192.168.1.0/24 profile=private
:: allow a program instead of a port
netsh advfirewall firewall add rule name="MyApp" dir=in action=allow program="C:\Apps\myapp.exe" enable=yes
:: allow ping (ICMP echo)
netsh advfirewall firewall add rule name="Allow ICMPv4 echo" protocol=icmpv4:8,any dir=in action=allow
:: block an outbound destination
netsh advfirewall firewall add rule name="Block host" dir=out action=block remoteip=203.0.113.5
:: delete
netsh advfirewall firewall delete rule name="Allow 8080"
:: turn off/on (testing only)
netsh advfirewall set allprofiles state off
netsh advfirewall set allprofiles state on
:: reset to defaults
netsh advfirewall reset
:: log dropped packets to %systemroot%\system32\LogFiles\Firewall\pfirewall.log
netsh advfirewall set allprofiles logging droppedconnections enable
Windows · PowerShell
Get-NetFirewallProfile | Format-Table Name, Enabled, DefaultInboundAction, DefaultOutboundAction
Get-NetFirewallRule -Enabled True -Direction Inbound -Action Allow | Format-Table DisplayName, Profile
Get-NetFirewallRule -DisplayName "*8080*" | Get-NetFirewallPortFilter
# which rules apply to a port
Get-NetFirewallPortFilter | Where-Object LocalPort -eq 8080 | Get-NetFirewallRule | Format-Table DisplayName, Enabled, Direction, Action

New-NetFirewallRule -DisplayName "Allow 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow -Profile Private,Domain
New-NetFirewallRule -DisplayName "Allow ICMPv4" -Protocol ICMPv4 -IcmpType 8 -Direction Inbound -Action Allow
Set-NetFirewallRule -DisplayName "Allow 8080" -Enabled False
Remove-NetFirewallRule -DisplayName "Allow 8080"
Set-NetFirewallProfile -Profile Public -Enabled False        # testing only
Get-NetConnectionProfile                                     # is this network Public or Private? Public blocks almost everything inbound
Set-NetConnectionProfile -InterfaceAlias Ethernet -NetworkCategory Private

The most common Windows surprise is a network classed as Public: file sharing, ping and most inbound rules are then off regardless of what you add. The second is a third-party security suite replacing Defender Firewall; its rules live in its own console. wf.msc opens the graphical console.

Linux

Which one you have: ufw on Ubuntu and Debian derivatives, firewalld on Fedora, RHEL, Rocky, Alma and openSUSE, raw nftables or iptables on Arch, Alpine, containers and older systems. All of them end up as nftables (or iptables) rules in the kernel, so nft list ruleset shows the truth whichever front end you use.

Linux · Terminal
sudo ufw status verbose
sudo ufw status numbered
sudo ufw allow 8080/tcp
sudo ufw allow from 192.168.1.0/24 to any port 8080 proto tcp
sudo ufw allow ssh                    # by service name from /etc/services
sudo ufw allow 60000:61000/udp        # a range (mosh)
sudo ufw deny 23
sudo ufw delete allow 8080/tcp
sudo ufw delete 3                     # by number from 'status numbered'
sudo ufw limit ssh                    # rate-limit connection attempts
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw disable                      # testing only
sudo ufw logging on; sudo tail -f /var/log/ufw.log
sudo ufw reset

Docker adds its own chains (DOCKER, DOCKER-USER) that bypass ufw for published ports; put restrictions in the DOCKER-USER chain. Cloud VMs additionally have a provider firewall (AWS security groups, Azure NSGs, GCP firewall rules, Hetzner/DigitalOcean cloud firewalls) that the guest cannot see; check the console when a port stays closed despite correct guest rules.

macOS

Two layers: the application firewall (System Settings › Network › Firewall) which allows or blocks per app, and pf, the BSD packet filter, off by default and used by some VPN and security products.

macOS · Terminal
# application firewall
/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on
/usr/libexec/ApplicationFirewall/socketfilterfw --listapps
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /Applications/MyApp.app
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblockapp /Applications/MyApp.app
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setstealthmode on      # ignore ping
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setblockall off

# pf
sudo pfctl -s info                     # enabled?
sudo pfctl -s rules
sudo pfctl -s nat
sudo pfctl -s states
sudo pfctl -e                          # enable
sudo pfctl -d                          # disable
sudo pfctl -f /etc/pf.conf             # load rules
sudo pfctl -nf /etc/pf.conf            # syntax check only
# a custom anchor with a rule, without editing the system pf.conf:
echo 'pass in proto tcp from 192.168.1.0/24 to any port 8080' | sudo pfctl -a com.local.custom -f -
sudo pfctl -a com.local.custom -s rules

Signed apps from the App Store and Apple are allowed inbound automatically; a self-built server binary is what usually triggers the "allow incoming connections?" dialog, and blocking it there is the usual reason a Mac cannot be reached on a port that is definitely listening.

Test a port from the other side

Any shell
Test-NetConnection 192.168.1.10 -Port 8080         # PowerShell: TcpTestSucceeded True/False
nc -zv 192.168.1.10 8080                           # macOS/Linux
nc -zvu 192.168.1.10 53                            # UDP (a missing reply proves nothing)
curl -v telnet://192.168.1.10:8080
nmap -p 8080 192.168.1.10                          # open / closed / filtered
nmap -sU -p 53 192.168.1.10

nmap's three answers are the useful part: open (a service answered), closed (the host replied with a reset: reachable, nothing listening, no firewall in the way), filtered (no reply at all: a firewall dropped it somewhere). Windows hosts with the firewall on show filtered for everything they do not allow.

Router port forwarding

For a service reachable from the internet you also need the router to forward the port to the host's LAN address (Port Forwarding, Virtual Server, NAT rules, Games & Applications). Give the host a reserved address first, forward TCP and/or UDP as needed, then test from outside your network (a phone on mobile data). If your router's WAN address is in 100.64.0.0/10 or a private range, you are behind carrier-grade NAT and forwarding cannot work; see CGNAT. UPnP lets applications open ports automatically; many people prefer to disable it and forward by hand.

Related pages

Last reviewed . Command syntax verified against Windows 11, Ubuntu 24.04, macOS 15 and FreeBSD 14 unless noted otherwise.

Spotted a mistake or a switch we have missed? Every page on this site is written to be checked against real output, so please test on your own machine and compare.