SSH "Address already in use" on a local forward
Address already in use means ssh tried to listen on a port on
your own machine for a forward, and something was already listening there. The
forward did not start. The rest of the session may still open. The message
comes from the machine you typed the command on, not from the server.
This page describes standard OpenSSH behaviour. The commands are the usual ones, and wording and output differ between versions, distributions and platforms. We did not run them on your system for this page.
If the line mentions a ControlSocket or a
unix_listener instead of a TCP address and port, that is a
different problem: see
ControlSocket already exists.
What the message means
bind [127.0.0.1]:8080: Address already in use
A local forward, set with -L or a LocalForward line,
makes ssh open a listening port on your machine. A dynamic forward, set with
-D or a DynamicForward line, does the same for a
proxy port. Only one program can listen on a given address and port at a time.
If another one already does, the operating system refuses ssh, and ssh prints
a line like the one above. The address in brackets and the port are the ones
it tried, and the exact wording and prefix differ between versions.
ssh may follow it with a second line about the listener, and a line that says
it could not request local forwarding. By default it then carries on and opens
the session without that forward. If you set
ExitOnForwardFailure to yes, ssh ends the connection
instead, which is what you want in a script that depends on the tunnel. It may
also print more than one bind line when it tries more than one
address.
This is a different message from the ones that sound alike, so confirm that first:
- Connection refused or timed out is about reaching a remote machine. Here the failure is on your side, before any traffic goes through the tunnel. A forward can also start fine and still give a refused connection later, when you use it and the remote target is not listening. That is a different problem, covered in channel open failed: connect failed.
- remote port forwarding failed for listen port is the remote counterpart: the far side, not your machine, did not start listening.
-
A line that says a channel was
administratively prohibitedis a later message. It means the server refused to open a forward after the tunnel was up. It does not appear as a bind failure, and the fix is usually on the server's side. - Bad configuration option means ssh did not accept a keyword in the config at all. Here ssh accepted the forward and could not open the port.
- SSH behind a firewall without port forwarding is about reaching a device without opening a port on a router. This page is about a port on your own computer.
1. Read the address and the port
Note the port in the message. Then find where it came from. Look at the
command you ran for -L and -D, and at the config for
the host, for LocalForward and DynamicForward
lines. A forward written in the config and the same one typed with
-L are both applied, so the same port can be requested twice by
one command.
The ssh -v output reader can check a pasted log for this line. It runs in your browser and sends nothing.
Once the client config is accepted, ssh -G prints the values ssh
would use for a host without connecting. Look for lines that start with
localforward or dynamicforward:
ssh -G myserver
The ssh -G output reader covers the main settings, and it does not single these lines out, so read them in the raw output.
2. See what already holds the port
On Linux, ss lists listening TCP ports and, with enough rights,
the program that owns each one. Put your own port in place of 8080:
ss -ltnp | grep ':8080'
On macOS and many other systems, lsof does the same job, if it is
installed:
lsof -nP -iTCP:8080 -sTCP:LISTEN
Other systems have their own tools, and a program run by another user may be
hidden unless you use sudo. The usual holders are:
-
An earlier ssh. A tunnel started in a background terminal,
with
-f, or from a script or service, may still be running. It is easy to forget after a reconnect. - Another program. A development server, a database, a proxy or a desktop tool can be on the same port. Ports such as 8080 and 5432 are popular and often taken.
- The same forward twice. See the config and command line check above.
3. Pick another port, or free this one
The port on your side is yours to choose. It does not have to match the port on the remote side. If you need the remote service to appear on a different local port, change only the first number:
ssh -N -L 18080:localhost:80 user@server.example.com
Here ssh listens on 18080 on your machine and forwards to port 80 as seen from
the server. Then point your browser or client at 18080. Do the same in the
config by changing the number in the LocalForward line.
If the port has to stay as it is, stop the program that holds it. If it is an old ssh you started, end that process, or close the terminal it runs in. Check what it is before you stop it: a process you do not recognise may belong to something else. Then run the same command again.
If ssh now starts and the forward is up, a request to the new local port should reach the target. If it does not, the problem has moved to the remote side, and the checks for a refused or prohibited connection apply.
If the machine is a RescueShell device
A path that starts from the device can avoid a public SSH port. The agent on the device keeps an outbound HTTPS connection to our relay, so reaching a shell does not depend on an inbound port on the device. See SSH behind a firewall without port forwarding.
RescueShell gives a shell on the device. It is not a forwarded port, so it does not replace a tunnel you need to a web page or a database, and it does not free a port on your own computer. If a shell does not open once the agent is installed, see the shell does not open.
Try it on one device
The first device is free to try with no card, for 30 days from when it connects, not from when you sign up. Start a trial.