Network Configuration for CentOS7 using VirtualBox on MAC

本文介绍了一种使用CentOS7配置Wordpress服务器的方法,包括通过NAT模式及主机模式设置虚拟机网络连接,以及如何仅用一个NAT适配器并通过端口转发实现对外部的SSH和Web服务访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

18 Jan 2018

Just a workout from the Project “Build Wordpress Server with CentOS7“.

[An old way]

In the project of building Wordpress server, I config the network in VM with the following steps. There are two adapters for network config in total.

  • NAT mode:
    With this mode, the VM can access outbound websites via local PC.
    Virtual Machine → Host → Outbound (not serve as a server)
    这里写图片描述
    And we can check the IP address of NAT.
    这里写图片描述
    So far, there is no way for localhost PC to communicate the VM because NAT is not working in two ways. In this case, we need to config another mode “Host-only mode”.

  • Host-only mode:
    这里写图片描述
    This mode ensures localhost PC and VM can communicate with each other. Here we set the IP address as 192.168.56.*. Then check the ifconfig in VM again and get the IP in VM which is 192.168.56.101.

Finally, the configuration of two network adapters has finished. Then we can use ssh root@192.168.56.101 to talk to VM from local PC. And the VM can access to the websides outside as well.

[The new way]

Later, a better way has been found. In this new way, we just only use one NAT adapter to fulfill requirements above.

  1. NAT config: the same as above.
    这里写图片描述
  2. Add “Port Forwarding” rules in NAT setting.
    这里写图片描述
    • The first rule is for SSH and the second one is for web access. The IP address 10.0.2.15 is IP for VM under NAT mode.
      In this case, local PC can SSH to VM using the command ssh -p 2222 root@10.2.209.56. Here, the IP address 10.2.209.56 is the IP address of local PC.(For example, the IP in Internal network of an office).
      Therefore, every requests for accessing port 2222 in 10.2.209.56 will be forwarded to port 22 in 10.0.2.15(the VM itself).
    • As for the second rule, it’s for web service. As I have mentioned, the VM will serve as a web server of Wordpress, so we need a port-forward for port 80 in VM. In this case, the requests from outside can access to the VM port 80.
      We randomly choose a port 8888 for local PC. Therefore, every requests for port 8888 in 10.2.209.56 will be forwarded to port 80 in 10.0.2.15 so that VM can provide the web service. (just try http://10.2.209.56:8888 in browser)
      Actually the ideal case is that we can open port 80 in 10.2.209.56 so that we don’t need to type the port number anymore(just typing http://10.2.209.56) because port 80 is the default port for web service.
      However, ports lower than 1024 are the privilege ports, which need root to open. In this case, we need to open VirtualBox as root. Some other methods are listed in this blog as well.

Ref:
- Config port forwarding to access to websites inside VM
- Install a CentOS 7 Minimal Virtual Machine with VirtualBox

Notes:
In CentOS7, maybe SELinux somehow will affect port forwarding. So I disable it here. Use getenforce to check the status of SELinux.

[root@localhost ~]# getenforce
Enforcing

To disable SELinux, just edit config, then restart VM:

/etc/selinux/config
#change SELINUX=disabled

Another factor will be the Firewall. So if it doesn’t work as intended, just check the Firewall as well.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值