SSH Tunnel: channel_setup_fwd_listener: cannot listen to port

I was attempting to use SSH to set up a tunnel to my Jenkins server from home, so I typed the following (Like I do in most days):

ssh -f -N -L 8080:remote_host:8080 -p 2222 user@host_address

…only to be greeted with the error:

bind: Address already in use
channel_setup_fwd_listener: cannot listen to port: 8080

Whats odd is that I was sure I didn't have anything else listening on that port, but I confirmed it with a quick netstat -anpt | grep '8080'. To my realization nothing was listening on the port.

More strangely, with the SSH session open, I can go to http://localhost:8080 and see Jenkins.

To my realization SSH itself had already opened the port(weird), and was complaining that it couldn’t do it a second time.

Resolution:

A simple pkill-9 ssh, saved the day

work around to avoid above scenario:

So after few Googling and reading up about ssh custom config, mostly from digitalocean

I’ve added the following in my ~/.ssh/config file:

Host Jenkins
ControlMaster auto
LocalForward 8080 host_ip:8080
Hostname _hostname
User _username
Port _custom port

Thereafter a simple,
ssh -f -N -l Jenkins, was all I needed.

Know more: man ssh