How to do port forwarding properly with VirtualBox? - linux

I have a trouble connecting to a guest OS in VirtualBox.
My environtment is as follows:
Host: MacOSX (Lion)
Guest: CentOS (5.7, 2.6.18-274.3.1.el5)
VirtualBox: 4.1.4 r74291
-- Network Adapter Type: NAT
In the guest OS, I can see that the httpd is running properly like:
$ ps aux | grep httpd
root 2571 0.0 3.5 258440 8824 ? Ss 08:20 0:00 /usr/sbin/httpd
apache 2573 0.0 2.2 258440 5780 ? S 08:20 0:00 /usr/sbin/httpd
apache 2574 0.0 2.0 258440 5184 ? S 08:20 0:00 /usr/sbin/httpd
apache 2575 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2576 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2577 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2578 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2579 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
apache 2580 0.0 2.0 258440 5180 ? S 08:20 0:00 /usr/sbin/httpd
$ curl localhost
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
...
In the host OS, I did a configuration to do port forwarding as follows:
$ VBoxManage showvminfo CentOS_3 | grep "NIC 1"
NIC 1: MAC: 080027B1FA87, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny
NIC 1 Settings: MTU: 0, Socket (send: 64, receive: 64), TCP Window (send:64, receive: 64)
NIC 1 Rule(0): name = http, protocol = tcp, host ip = , host port = 8003, guest ip = , guest port = 80
NIC 1 Rule(1): name = ssh, protocol = tcp, host ip = , host port = 2203, guest ip = , guest port = 22
As this information go, I'm trying to do port forwarding from 8003 to 80 and from 2203 to 22.
In fact, I can make the ssh connection to the guest OS with "ssh -p 2203 localhost".
However, when I try to see http://localhost:8003, the browser says
"Unable to load the webpage because the server sent no data."
When I try "telnet localhost 8003" in the host OS
it seems the port is listening, but there
appears no log in the access log of the guest OS.
What should I do to solve this problem? Thanks in advance!
(addition: Oct. 14 15:55)
$ netstat -nl | grep tcp | grep 127.0.0.1 # Host OS
tcp4 0 0 127.0.0.1.8003 127.0.0.1.64698 ESTABLISHED
tcp4 0 0 127.0.0.1.64698 127.0.0.1.8003 ESTABLISHED
tcp4 0 0 127.0.0.1.26164 127.0.0.1.53917 ESTABLISHED
tcp4 0 0 127.0.0.1.53917 127.0.0.1.26164 ESTABLISHED
tcp4 0 0 127.0.0.1.2203 127.0.0.1.53183 ESTABLISHED
tcp4 0 0 127.0.0.1.53183 127.0.0.1.2203 ESTABLISHED
$ netstat -nl | grep tcp # Guest OS
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:952 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::443 :::* LISTEN

It seems that like mkj wrote, you have a firewall installed.
try running (as root):
yum install system-config-firewall-tui
run system-config-firewall-tui, select customize and make sure that "WWW (HTTP)" is checked.

Although old question, but this may be helpful to someone, as I faced this problem today.
As mentioned by mkj, the issue is with Guest OS ( Centos 6 in my case) firewall. It allows ssh (port 22) connection, but blocks others. I had issue accessing postgresql server running in Guest OS (at port 5432) from Host. So executing following iptables command on Guest OS worked:
$ sudo iptables -I INPUT -p tcp -m tcp --dport 5432 -j ACCEPT
In your case, for httpd server running at port 80, you may have to do below:
$ sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
I never faced this problem when running ubuntu/debian as Guest OS, so probably firewall is not configured in them.

Related

Cannot find process which is using port:8080?

First, I created a docker container by a docker-compose file:
version: '3'
services:
nginx:
container_name: some-name
image: some-name
restart: always
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:80
environment:
- NGINX_HOST=localhost
- NGINX_PORT=80
this Nginx container is just a static web-server, it contains default Nginx page /index.html.
Then I stopped this container, and use curl command for testing:
op#dev-test:~/dev$ curl 127.0.0.1:8080
curl: (7) Failed to connect to 127.0.0.1 port 8080 after 0 ms: Connection refused
op#dev-test:~/dev$ curl 192.168.0.30:8080
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, user-scalable=yes"
/>
<title>Hello World!</title>
</head>
...
Both command "ps" and "netstat" shows no process is using port 8080:
root#dev-test:/home/op# ps aux | egrep "nginx|docker|PID"
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1803 0.0 0.2 10640 5792 ? Ss 01:21 0:00 nginx: master process nginx -g daemon off;
systemd+ 1884 0.0 0.1 11176 2688 ? S 01:21 0:00 nginx: worker process
root 7759 0.1 4.2 1528704 83708 ? Ssl 02:57 0:26 /usr/bin/dockerd -H fd:// -H unix:///var/run/docker.sock --containerd=/run/containerd/containerd.sock
root 21866 0.0 0.1 6956 2384 pts/0 S+ 06:41 0:00 grep -E --color=auto nginx|docker|PID
root#dev-test:/home/op# netstat -tlpnao
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name Timer
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 972/sshd: /usr/sbin off (0.00/0/0)
tcp 0 0 127.0.0.1:45265 0.0.0.0:* LISTEN 926/containerd off (0.00/0/0)
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 854/systemd-resolve off (0.00/0/0)
tcp 0 256 192.168.0.30:22 192.168.0.5:54252 ESTABLISHED 2166/sshd: op [priv on (0.20/0/0)
tcp 0 0 192.168.0.30:22 192.168.0.15:10754 ESTABLISHED 21313/sshd: op [pri keepalive (5271.90/0/0)
tcp 0 0 192.168.0.30:22 192.168.0.5:61671 ESTABLISHED 3737/sshd: op [priv keepalive (1560.31/0/0)
tcp6 0 0 :::22 :::* LISTEN 972/sshd: /usr/sbin off (0.00/0/0)
root#dev-test:/home/op# iptables -L DOCKER
# Warning: iptables-legacy tables present, use iptables-legacy to see them
Chain DOCKER (2 references)
target prot opt source destination
ACCEPT tcp -- anywhere 172.18.0.2 tcp dpt:http
I want know why the command "curl 192.168.0.30:8080" can responses the content of /index.html when the container was stopped. But it's works fine for other container(running/stop) which specified port-binding to 8090?

how to change default start-up folder jupyter hub on Azure DSVM virtual machine

Azure DSVM has jupyterlab enabled on port 8000.
However, the startup folder in jupyterlab always starts at /home/*user-name/notbook.
I want to change the jupyterlab start folder, but I don't know what to do.
tried
1.
I created a jupyterlab configuration by referring to stackoverflow to change c.NotebookApp.notbook_dir, but rebooted and checked again for server operation. I've confirmed it's not working.
2.
I also tried to create a jupyterhub configuration, but it came out that there was no command called jupyterhub.
I thought it was working in docker, but there was no process for jupyterhub in docker.
3.
netstat -tnlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:8081 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:8001 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:44675 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:46277 0.0.0.0:* LISTEN 2048/python
tcp6 0 0 :::111 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 :::8000 :::* LISTEN -
ps aux | grep jupyter
root 1345 0.0 0.0 13316 3304 ? Ss 00:06 0:00 /bin/bash /etc/jupyterhub/start_jupyterhub.sh
root 1624 0.0 0.0 246340 64268 ? Sl 00:06 0:01 /anaconda/bin/python /anaconda/bin/jupyterhub --log-file=/var/log/jupyterhub.log
root 1949 0.0 0.0 606436 51356 ? Ssl 00:06 0:01 node /usr/local/bin/configurable-http-proxy --ip * --port 8000 --api-ip 127.0.0.1 --api-port 8001 --error-target http://127.0.0.1:8081/hub/error --ssl-key /etc/jupyterhub/srv/server.key --ssl-cert /etc/jupyterhub/srv/server.crt
rootadm+ 2048 0.2 0.0 343804 101160 ? Ssl 00:07 0:03 /anaconda/bin/python /anaconda/bin/jupyterhub-singleuser --port=46277 --notebook-dir=~/notebooks --SingleUserNotebookApp.default_url=/lab --config=/etc/jupyterhub/default_jupyter_config.py
rootadm+ 3841 0.0 0.0 14864 1040 pts/0 S+ 00:27 0:00 grep --color=auto jupyter

Apache2: "Address already in use" when trying to start it ('httpd.pid' issue?)

Using Apache2 on Linux, I get this error message when trying to start it.
$ sudo /usr/local/apache2/bin/apachectl start
httpd not running, trying to start
(98)Address already in use: make_sock: unable to listen for connections on address 127.0.0.1:80
no listening sockets available, shutting down
Unable to open logs
$ sudo /usr/local/apache2/bin/apachectl stop
httpd (no pid file) not running
Some facts:
This is one of the last lines in my Apache logs:
[Mon Jun 19 18:29:01 2017] [warn] pid file /usr/local/apache2/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
My '/usr/local/apache2/conf/httpd.conf' contains
Listen 127.0.0.1:80
I have "Listen 80" configured at '/etc/apache2/ports.conf'
Disk is not full
I've checked that I do not have two or more "Listen" at '/usr/local/apache2/conf/httpd.conf'
Some outputs:
$ sudo ps -ef | grep apache
root 1432 1 0 17:35 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 1435 1432 0 17:35 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 1436 1432 0 17:35 ? 00:00:00 /usr/sbin/apache2 -k start
myuserr 1775 1685 0 17:37 pts/1 00:00:00 grep --color=auto apache
$ sudo grep -ri listen /etc/apache2
/etc/apache2/apache2.conf:# supposed to determine listening ports for incoming connections which can be
/etc/apache2/apache2.conf:# Include list of ports to listen on
/etc/apache2/ports.conf:Listen 80
/etc/apache2/ports.conf: Listen 443
/etc/apache2/ports.conf: Listen 443
What can I do to restart Apache? Should I repair 'httpd.pid'?
This error means that something already uses 80 port.
If you really don't have 2 line of Listen 80 in apache configurations then execute this command to see what uses 80 port: netstat -antp | grep 80.
I fixed it by killing the three processes
root 1621 1 0 18:46 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 1624 1621 0 18:46 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 1625 1621 0 18:46 ? 00:00:00 /usr/sbin/apache2 -k start
However, each time I want to reboot my server, I must kill thee processes. What is starting them?

nginx is installed but site does not load

I installed LEMP with this tutorial I have done all it says but when I enter my server ip address in my browser it takes some time to load the page and it seems like my browser is doing some processes to load the page but at last the page is blank.
I'v checked all the installation again and they were all installed.
I'v installed apache at the past and I know for apache server if you enter server ip address it shows " apache is functioning " but It's my first time installing nginx ; I want to install DirectAdmin and I don't know nginx is installed or not.
If anyone is familiar with nginx and can help me, I'll be apreciate that.
Update: When I use service nginx status it shows nginx is running.
Update: Flowing are the results of my checkings:
Results of netstat -tulpn | grep :80 :
tcp 0 0 0.0.0.0:80 0.0.0.0:* LIST EN 10302/nginx
Results of ps aux | grep nginx :
nginx 8556 0.0 0.0 222416 3552 ? S 09:04 0:00 php-fpm: pool w ww
nginx 8557 0.0 0.0 222416 3552 ? S 09:04 0:00 php-fpm: pool w ww
nginx 8558 0.0 0.0 222416 3552 ? S 09:04 0:00 php-fpm: pool w ww
nginx 8559 0.0 0.0 222416 3552 ? S 09:04 0:00 php-fpm: pool w ww
nginx 8560 0.0 0.0 222416 3552 ? S 09:04 0:00 php-fpm: pool w ww
root 10302 0.0 0.0 96032 1980 ? Ss 09:11 0:00 nginx: master p rocess /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 10304 0.0 0.0 96436 2692 ? S 09:11 0:00 nginx: worker p rocess
nginx 10305 0.0 0.0 96436 2692 ? S 09:11 0:00 nginx: worker p rocess
nginx 10306 0.0 0.0 96436 2660 ? S 09:11 0:00 nginx: worker p rocess
nginx 10307 0.0 0.0 96436 2672 ? S 09:11 0:00 nginx: worker p rocess
root 12564 0.0 0.0 103312 856 pts/0 S+ 09:59 0:00 grep nginx
FIIIIINNNNNNAAAAAALLLLLLLY I Found the problem!
The problem was with my firewall.
If anyone else has the same problem it may be the firewall settings problem like mine So Do the flowing to fix this:
vim /etc/sysconfig/iptables
in the opened file find this line :
-A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
Check that --dport 80 is set to the port that you entered in /etc/nginx/conf.d/default.conf if its something else like --dport 22 change it to your default port.
Update:
After you changed the port you should run this code:
service iptables restart
And this is the page shows nginx is running :

can not ftp localhost but can ftp from remote server

I have installed vsftp to the server , the status is running now , but when use the command ftp localhost , it pops the 421 Service not available. , would advise what is wrong ? thanks
bash-3.2# /etc/rc.d/init.d/vsftpd status
vsftpd (pid 580) is running...
bash-3.2# ps -ef |grep vsftpd
root 580 1 0 15:44 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root 607 467 0 15:45 pts/0 00:00:00 grep vsftpd
bash-3.2# ftp localhost
Connected to localhost (127.0.0.1).
421 Service not available.
the below is the output , would advise what can I do ? thanks
bash-3.2# netstat -tanp |grep ftp
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 3205/vsftpd
bash-3.2# ps -ef |grep ftp
root 3205 1 0 Feb02 ? 00:00:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root 6075 5626 0 21:35 pts/0 00:00:00 grep ftp
bash-3.2# /sbin/iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
bash-3.2# /sbin/chkconfig --list |grep ip
all iptables are off
There is a parameter called tcp_wrappers=YES in vsftpd.conf. And it checks your hosts.deny file under /etc. You can change tcp_wrappers=NO or you can check your hosts.deny file under /etc
bash-3.2# ftp localhost
Connected to localhost (127.0.0.1).
421 Service not available.
It looks like you can connect, that is the server listens to this address (like you see with netstat too) and there are no iptables rules which block the connection.
Instead the server itself refuses to serve you. Therefore you need to look at the server logs and server configuration for help. Maybe you should look at the tcp_wrappers option and the related configuration in /etc/hosts.allow etc.

Resources