- ssh: connect to host 127.0.0.1 port 2222: Connection refused
- 9 Answers 9
- Localhost Refused to Connect — How to Fix the Error
- What is the Localhost Refused to Connect Error?
- What Causes the Localhost Refused to Connect Error
- How to Solve the Localhost Refused to Connect Error?
- Temporary Disable the Firewall
- Disable Firewall on Linux
- Disable the Firewall on Mac OS
- Disable the Firewall on Windows
- Check Localhost IP Address
- Check Apache Server Status
- Flush the DNS
- Change Port Settings
- Change Port Number on XAMPP
- Change Port Number on WAMPP
- Check Browser Settings
- Change DNS Settings in Chrome
- Change DNS Settings in Firefox
- ssh connection refused
ssh: connect to host 127.0.0.1 port 2222: Connection refused
Did you install the ssh-server? Did you configure it to listen on port 2222? Normally it listens on port 22!
9 Answers 9
Note, that 127.0.0.1 aka localhost is your local machine. Usually at this point you use the IP address or hostname of the remote host.
- First install the ssh server and client on your target host and your local host
sudo nano /etc/ssh/sshd_config
sudo service ssh force-reload
Hi, When I execute command. $ sudo service ssh start It throws error «service: no such service ssh » When I try to install ssh using command $sudo apt-get install ssh i throws error «No provider of ‘ssh’ found. «
Try sudo apt-get update prior to the sudo apt-get install ssh and see if that helps, if not you have sources.list problem.
- By default Ubuntu has ssh client (which is for out-going connection from yours to somewhere else) but no ssh server (to allow in-coming connections from other computers to yours). That means, if you wanna ssh into our computer, you need the server, which you can get with sudo apt-get install openssh-server .
- ssh by default runs on port 22. If you try any other port, connection will be refused. So once you have ssh server, you can just ssh username@localhost , and that will direct you to port 22 by default. Now, if you want to enable ssh login on port 2222, you will need to enable port forwarding. Especially since you are using virtualbox.
+1 for outlining the default ubuntu ssh’s behaviour & reason why one has to install the openssh-server
It looks that you don’t have either:
For 1. you need to check whether sshd server is running on a vm by for example checking with VB console that service ssh status returns that is active and running. If not run sudo service ssh start .
For 2. you can check if VB is listening on that port. You can do that by checking all open ports in LISTENING mode. sudo netstat -anp | grep -w LISTEN should include port 2222 (assuming you’re using linux as a host OS).
You should have something like this in output:
tcp 0 0 0.0.0.0:**2222** 0.0.0.0:* LISTEN 1564/sshd
(in this case the sshd process is listening on port 2222)
Additionally you should tell us what is your network configuration inside VB. Without it it’s hard to tell whether command you’re using is right.
Localhost Refused to Connect — How to Fix the Error
Localhost refused to connect is a common error that may occur when working on a local machine. In this article, you will learn the most common causes of the localhost refused to connect error and how to resolve it.
What is the Localhost Refused to Connect Error?
Localhost is a loopback address used for testing and developing on a local machine. When a user pings localhost, the request remains on the local network and connects to the machine in use. If such a connection is not possible, the browser displays an error message:
localhost refused to connect.
The localhost refused to connect message displays one of two possible errors:
- ERR_CONNECTION_REFUSED (the server rejected the request for connection)
- ERR_CONNECTION_TIMEOUT (the server has not responded to the request and the client is still waiting for a response)
What Causes the Localhost Refused to Connect Error
Common causes that result in localhost refusing to connect:
- The server being blocked by the firewall.
- Localhost doesn’t resolve to 127.0.0.1.
- Apache failing to run properly.
- DNS failing to function.
- The server being accessed from the wrong port.
- The browser settings blocking the connection.
To solve the issue, identify the source of the error. The best way to do so it to test out each probable cause, starting from the most frequent ones.
How to Solve the Localhost Refused to Connect Error?
The sections below provide methods to resolve the localhost refused to connect error.
Temporary Disable the Firewall
Check whether the firewall settings are blocking the localhost connection by disabling the firewall temporarily. (For OS-specific instructions, see sections below.)
Once you disable the firewall, navigate to localhost in a browser to check whether it connects. If localhost still refuses to connect, move on to other potential causes. Also, make sure to turn on the firewall once you have finished working with localhost.
Disable Firewall on Linux
The firewall configuration tool on Linux systems varies depending on the distro. While Ubuntu and Debian distributions utilize UFW, CentOS and RHEL systems use firewalld by default.
Check the status by running:
sudo systemctl stop firewalld
Verify the status is inactive by running:
sudo systemctl status firewalld
Disable the Firewall on Mac OS
1. Click the Apple icon in the upper-left corner to open the menu and select System Preferences.
2. Navigate to Security and Privacy settings and move to the Firewall tab.
3. If the lock at the bottom left of the pane is locked, click the icon to unlock the preference pane. Type in your password to confirm.
4. Click Turnoff Firewall to complete the process.
Disable the Firewall on Windows
1. Open the Windows Control Panel and click the Windows Defender Firewall icon.
2. Select Turn Windows Defender Firewall on or off from the menu on the left side of the window.
3. Finally, check the Turn off Windows Defender Firewall checkbox and click OK.
Check Localhost IP Address
Localhost and the IP address 127.0.0.1 are not completely synonymous. In most cases, 127.0.0.1 is used for loopback, however not exclusively. For instance, IPv6 systems link the localhost to the address : :1.
Additionally, if the host file has been modified, the localhost could link to a different IP address.
Note: Learn more about the differences between localhost and 127.0.01 in our article Localhost vs. 127.0 0.1.
Try typing both localhost and 127.0.0.1 in the browser search bar, in case of a connection error.
Check Apache Server Status
Another important factor for accessing the localhost is that the Apache server is running properly. Therefore, if the system rejects the connection, it is necessary to check Apache’s status.
The command for verifying whether Apache is running on a Linux OS depends on the distribution in use. Follow the steps below and use the appropriate command.
1. Verify Apache status with the command:
Ubuntu/Debian:
sudo systemctl status apache2
sudo systemctl status httpd
The output should display that Apache is active (running), as in the image below. If that is the case, the Apache server is working correctly and you can move to the next probably cause for error.
2. If Apache is not running properly, the output displays that its status is inactive (dead).
3. To fix the issue, try restarting the Apache service using:
Ubuntu/Debian:
sudo systemctl restart apache2
sudo systemctl restart httpd
4. Check the Apache status again:
Ubuntu/Debian:
sudo systemctl status apache2
sudo systemctl status httpd
5. If the status remains inactive, move on to deleting and reinstalling Apache on the system:
Flush the DNS
DNS cache is stored to speed up loading of previously visited websites. If DNS collects too many records or some of the records get corrupt, it may fail to function. Such an issue can result in localhost refusing to connect.
The best way to ensure DNS is not preventing the localhost connection is to clear the DNS cache and delete all saved DNS lookup information.
The steps for flushing the DNS vary depending on the OS in use. For detailed instructions, refer to How to Flush DNS Cache in macOS, Windows, & Linux.
sudo /etc/init.d/nscd restart
sudo killall -HUP mDNSResponder
Change Port Settings
By default, localhost uses port number 80. If another application is using the same port, it can create a conflict resulting in localhost refusing to connect.
Change Port Number on XAMPP
If you are using the XAMPP stack, follow the steps below:
1. Open the XAMPP control panel and click on the Netstat button.
2. Netstat opens a list of all TCP listening sockets. If port 80 is already in use by another application, move on to choosing a free port for Apache.
3. Before proceeding to the next step, make sure to stop the Apache service.
4. Locate and open the httpd.conf file. The location of the file varies depending on the OS:
- Linux users will find it in bin/apache.
- Windows stores the file in C:\xampp\apache\conf.
- The path to the file on Mac OS is Applications/XAMPP/xamppfiles/etc/httpd.conf.
5. Find the following lines in the file:
Listen 80 ServerName localhost:80
6. Change the port number to any other free port. In this example, we change it to 8080.
Listen 8080 ServerName localhost:8080
7. Save and exit the file. Restart the Apache web server in the XAMPP control panel and try connecting to localhost again.
Change Port Number on WAMPP
If you are using the WAMP software stack and want to change the Apache port:
1. First, stop all instances running in WAMP with the End Task button.
2. Then, open the httpd.conf file located in C:\wamp\apache2\conf.
3. Locate the following lines in the file:
Listen 80 ServerName localhost:80
4. Change the port number to a free port (8080 for example).
Listen 8080 ServerName localhost:8080
5. Save the changes and exit the file.
Check Browser Settings
Make sure the browser is not responsible for causing the localhost error.
Start by navigating to localhost in a different browser. If the connection establishes, then the settings in the initial browser are the issue.
Often, browsers automatically redirect HTTP addresses to HTTPS causing the localhost refused to connect error.
Change DNS Settings in Chrome
1. To change the default setting on Chrome, paste the following URL in the address bar: chrome://net-internals/#hsts
2. Then, open the Domain Security Policy tab and scroll down to the Delete domain security policies section.
3. Add localhost as the domain name and click Delete.
4. Restart the browser to apply the changes.
Change DNS Settings in Firefox
1. Open the Application Menu in the top right corner of the browser window and select Settings.
2. Navigate to Privacy & Security and scroll down to the HTTPS-Only Mode section.
3. To apply the changes, reopen the browser.
After reading this article, you should know the steps to locate the cause and fix the localhost refused to connect error.
Another common issue that occurs when working locally on software stacks such as LAMP, WAMP, and others is the Access denied for user root@localhost error. Follow the link to troubleshoot Access denied for user root@localhost error in a few simple steps.
ssh connection refused
Hi I’m having a problem with my ssh, which is magically stopped working and I couldn’t figure out why. The message it give is:
ssh: connect to host port 22: Connection refused
Trying 127.0.0.1. telnet: Unable to connect to remote host: Connection refused
I tried the solutions that I saw in some similar cases [1] and [2] but it still didn’t solve my problem the entries and outputs are as follows:
>> sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT >> sudo iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:ssh Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
Note that this was already ACCEPT I didn’t had any problem even before entering the command sudo iptables -A INPUT -p tcp —dport ssh -j ACCEPT . I also tried to reboot ssh but it didn’t change anything
>>netstat -a | egrep 'Proto|LISTEN' Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:20128 *:* LISTEN tcp 0 0 localhost:17600 *:* LISTEN tcp 0 0 localhost:20129 *:* LISTEN tcp 0 0 localhost:17603 *:* LISTEN tcp 0 0 localhost:21128 *:* LISTEN tcp 0 0 deathstar:domain *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:db-lsp *:* LISTEN tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN tcp6 0 0 [::]:db-lsp [::]:* LISTEN
I don’t know what else to try so hope this is enough to solve the problem. Rest is added after @Ashu’s comment
>>netstat -l --numeric-ports | grep 22 unix 2 [ ACC ] STREAM LISTENING 1618183 @jack-com.canonical.Unity.Master.Scope.files.T1731348652205882 >>sudo iptables --list Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:ssh Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
>>ps aux | grep sshd jack 3711 0.0 0.0 15944 2220 pts/26 S+ 14:08 0:00 grep --color=auto sshd