redhat中虚拟机如何上网?(方法一) | iptables 配置DNS服务器

###前提条件,主机与虚拟机网络连通#####

一,配置真机

1,真机的防火墙关闭

[root@foundation156 ~]# systemctl stop firewall
Failed to issue method call: Unit firewall.service not loaded.
[root@foundation156 ~]# systemctl stop firewalld
[root@foundation156 ~]# systemctl status firewalld.service 
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: inactive (dead) since Fri 2018-04-20 18:35:00 CST; 1h 51min ago
  Process: 12431 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 12431 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/firewalld.service

2,查看无线网卡的名称 (下面绿色为无线网卡名字,红色为其相应ip)

[root@foundation156 ~]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.156  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::1adb:f2ff:fe2b:8e94  prefixlen 64  scopeid 0x20<link>
        ether 18:db:f2:2b:8e:94  txqueuelen 0  (Ethernet)
        RX packets 6220  bytes 2617047 (2.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8481  bytes 935692 (913.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 111139  bytes 30726312 (29.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 111139  bytes 30726312 (29.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

p9p1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 18:db:f2:2b:8e:94  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 36:b1:16:f4:01:25  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1  bytes 90 (90.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether c6:8c:db:7e:60:a9  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1  bytes 90 (90.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe00:9c0a  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:00:9c:0a  txqueuelen 500  (Ethernet)
        RX packets 3327  bytes 2333418 (2.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4642  bytes 607061 (592.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe00:9c0b  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:00:9c:0b  txqueuelen 500  (Ethernet)
        RX packets 3717  bytes 457467 (446.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5264  bytes 476664 (465.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp0s20f0u3u3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.31.78  netmask 255.255.255.0  broadcast 192.168.31.255
        inet6 fe80::72f1:1cff:fe0c:2d09  prefixlen 64  scopeid 0x20<link>
        ether 70:f1:1c:0c:2d:09  txqueuelen 1000  (Ethernet)
        RX packets 40514  bytes 14026263 (13.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19656  bytes 3885737 (3.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2,查看

[root@foundation156 ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

3,执行以下命令

[root@foundation156 ~]# iptables -t nat -A POSTROUTING -o wlp0s20f0u3u3 -j SNAT  --to-source 192.168.31.78

4,确定真机网络连通

[root@foundation156 ~]# ping www.baidu.com
PING www.a.shifen.com (180.149.132.151) 56(84) bytes of data.
64 bytes from 180.149.132.151: icmp_seq=1 ttl=52 time=37.2 ms
64 bytes from 180.149.132.151: icmp_seq=3 ttl=52 time=24.5 ms
64 bytes from 180.149.132.151: icmp_seq=4 ttl=52 time=34.3 ms
64 bytes from 180.149.132.151: icmp_seq=5 ttl=52 time=24.4 ms
^C
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 4 received, 20% packet loss, time 4004ms
rtt min/avg/max/mdev = 24.447/30.152/37.245/5.753 ms

二,配置虚拟机

1,此时用百度的ip地址(上方黄色)来测试是否网通(由于还未设置DNS服务器,所以不能用网址)

[root@node1 ~]# ping 180.149.132.151
connect: Network is unreachable

    由于还未对虚拟机进行配置,显然网络不通

2,改配置文件,加入网关

[root@node1 ~]# vim /etc/sysconfig/network
NETWORKING=yes
NOZEROCONF=yes
GATEWAY=172.25.254.156       ##添加此行,其中ip为真机ip

3,重启网络服务

[root@node1 ~]# systemctl restart network

        此时可用route -n查看网关出现

[root@node1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.254.156  0.0.0.0         UG    1024   0        0 eth0
172.25.254.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

4,虚拟机网络连通

[root@node1 ~]# ping 180.149.132.151
PING 180.149.132.151 (180.149.132.151) 56(84) bytes of data.
64 bytes from 180.149.132.151: icmp_seq=1 ttl=51 time=39.6 ms
64 bytes from 180.149.132.151: icmp_seq=2 ttl=51 time=24.5 ms
64 bytes from 180.149.132.151: icmp_seq=3 ttl=51 time=25.1 ms
64 bytes from 180.149.132.151: icmp_seq=4 ttl=51 time=34.9 ms
^C
--- 180.149.132.151 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 24.556/31.081/39.675/6.460 ms

   用traceroute也 ip 也可以查看

[root@node1 ~]# traceroute 180.149.132.151
traceroute to 180.149.132.151 (180.149.132.151), 30 hops max, 60 byte packets
 1  172.25.254.156 (172.25.254.156)  4.889 ms  4.826 ms  4.778 ms
 2  192.168.31.1 (192.168.31.1)  28.126 ms  28.193 ms  28.248 ms
 3  10.173.255.254 (10.173.255.254)  29.423 ms  29.463 ms  29.547 ms
 4  * * *
 5  172.16.255.254 (172.16.255.254)  31.440 ms  31.427 ms  31.413 ms

5,此时用地址还是无法访问,必须要修改DNS服务器配置文件

[root@node1 ~]# vim /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 114.114.114.114 ###添加此行


# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com

        重启网络服务

[root@node1 ~]# systemctl restart network
6,此时直接ping百度等地址
[root@node1 ~]# ping www.baidu.com
PING www.a.shifen.com (180.149.131.98) 56(84) bytes of data.
64 bytes from 180.149.131.98: icmp_seq=1 ttl=51 time=30.1 ms
64 bytes from 180.149.131.98: icmp_seq=2 ttl=51 time=23.8 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 23.890/27.040/30.191/3.154 ms
[root@node1 ~]# ping -c 1 www.taobao.com
PING www.taobao.com.danuoyi.tbcache.com (124.238.232.220) 56(84) bytes of data.
64 bytes from 124.238.232.220: icmp_seq=1 ttl=38 time=31.7 ms

--- www.taobao.com.danuoyi.tbcache.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 31.749/31.749/31.749/0.000 ms

 #######################################################################################

如果不修改DNS服务器,手动添加也是可以的,只是比较麻烦,而DNS服务器可以自动解析

[root@node1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.254.254 classroom.example.com
172.25.254.254 content.example.com
180.149.131.98 www.baidu.com  ###添加这一行,注意,ip和地址必须一一对应,否则在浏览器中打开会出错
##########本地解析的优先级高于DNS解析####################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值