巧用防火墙让虚拟机上网

本文介绍了两种方法让虚拟机通过主机防火墙上网。一种是使用iptables的masquerade模式,另一种是通过编辑iptables策略。在真机上,需要安装并启动iptables服务,关闭防火墙,清空策略并添加新策略。同时,需要开启路由转发。在虚拟机上,需设置网关、DNS,并测试外网访问。对于IP变化的情况,建议使用iptables升级版,即创建脚本以适应IP变动。
摘要由CSDN通过智能技术生成

1、通过firewall-cmd中的masquerade进行伪装

1)前提:真机的防火墙要开启
[root@foundation8 kiosk]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-01-27 15:48:51 CST; 20s ago
     Docs: man:firewalld(1)
 Main PID: 4677 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─4677 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Jan 27 15:48:50 foundation8.ilt.example.com systemd[1]: Starting firewalld - ...
Jan 27 15:48:51 foundation8.ilt.example.com systemd[1]: Started firewalld - d...
Hint: Some lines were ellipsized, use -l to show in full.
2)用firewall-cmd --list-all查看火墙服务状态
[root@foundation8 kiosk]# firewall-cmd --list-all
trusted (active)
  target: ACCEPT
  icmp-block-inversion: no
  interfaces: br0 enp1s0 wlp2s0
  sources: 
  services: ftp
  ports: 
  protocols: 
  masquerade: yes             //此处为yes才okay!!!
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
    rule family="ipv4" source address="192.168.1.100" masquerade

如果显示的是masquerade: no,那么需要执行下面的命令,显示success则表示添加成功~

firewall-cmd --permanent --add-masquerade

目前只是开启了masquerade模式,需要更加详细的指定其工作方式:

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=x.x.x.x masquerade'
//此处添加的IP(x.x.x.x)是可上网的IP,比如某个热点

重新启动火墙,便会出现最上面,我们看到的yes状态

3)配置需要上网的虚拟机
虚拟机只需要注意三个地方:
- 与真机在同一网段
-
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值