一、ip基础知识


1.常用网络协议ipv4

 

2进制 32位 -----  10进制 


#比如 11111110.11111110.11111110.11111110=254.254.254.254

172.25.0.10/255.255.255.0

172.25.0.10:          #ip地址

255.255.255.0:        #子网掩码


#子网掩码255位对应的ip位为网络位

#子网掩码0对应的ip位为主机位



二、主机联网


主机有两块网卡:有线和无线;(有些没有无线可以买一个外置驱动)


注:如下操作都是root用户下的

route -n       查询网关

Kernel IP routing table

Destination      Gateway            Genmask         Flags Metric Ref   

0.0.0.0   (网关)  0.0.0.0         255.255.255.0          U

wKiom1nnMsTw_yYhAABHX1G1oDE779.png-wh_50

wKiom1nnMsSBawmDAACeXYsF_ok798.png-wh_50

右上角会出现无线标识

       wKioL1nnMA6CkFBzAABCOc2Onvc466.png-wh_50

       选择网络:点击wi-fi按钮,turn on

        wKiom1nnMsWD9GxZAAFWRLk-MT8801.png-wh_50

    确认连上无线网,ping IP,如下连上网

   wKioL1nnMA-wDRDLAACqZKAVcY4816.png-wh_50 


三、配置ip

<<图形化>>


1.图形界面

nm-connection-editor


2.文本化图形

nmtui 

<<命令>>

ifconfig 网卡 ip netmask 临时设定

<<文件>> 

static|none##静态网络

vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0 ##设备

BOOTPROTO=static|none ##设备工作方式

ONBOOT=yes ##开启网络服务激活设备

NAME=eth0 ##网络接口名称

IPADDR=172.25.0.100  ##IP

NETMASK=255.255.255.0 | PREFIX=24 ##子网掩码

wKiom1nnQdLxFMZ2AAA5zNfBBwA821.png-wh_50

wKioL1nnPx3woiUmAADQbV2d6r4208.png-wh_50

wKiom1nnQdOzPiidAAA9x-7Y7Mo227.png-wh_50

wKioL1nnPx2R7sLhAAAyxHd_4RU639.png-wh_50

 dhcp   ##动态获取


vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0               ##接口使用设备

BOOTPROTO=dhcp            ##网卡工作模式

ONBOOT=yes                ##网络服务开启时自动激活

NAME=eth0                 ##网络接口名称

:wq

systemctl restart networ

wKioL1nnPx3hE03_AAA6Rjqg4AA406.png-wh_50

wKiom1nnRjOxRCpfAACGalLQoL8215.png-wh_50

wKiom1nnRjOgKrqIAAAnT_fjFpM539.png-wh_50

wKioL1nnQ36TC2aHAAA28LNdm2g783.png-wh_50

nmcli connection add type ethernet con-name nameifname eth0 autoconnect yes 

                                                              -----------> 添加dhcp网络

nmcli connection add type ethernet con-name name ifname eth0 ip4 ip/24 

                                                              ----------->添加静态网络

nmcli connection delete name                                  ----------->删除name链接

nmcli connection show                                         ----------->显示所有网络链接

nmcli connection down name                                    ----------->关闭指定链接

nmcli connection up name                                      ----------->开启指定链接

nmcli connection modify "name" ipv4.addresses newip/24        -----------> 改变name的ip

nmcli connection modify "name" ipv4.method <auto|manual> 

                                                   ----------->改变name的工作方式为动态或者静态

nmcli device connect eth0                                     ----------->开启设备

nmcli device disconnect eth0                                  ----------->关闭设备

nmcli device show                                             ----------->显示设备信息

nmcli device status                                           ----------->显示设备状态

nmcli connection add type ethernet con-name nameifname eth0 autoconnect yes 

                                                              ----------->添加dhcp网络

wKioL1nnQ37CjhhxAACKJbM-RoU278.png-wh_50

wKiom1nnRjTx3O39AAAubT13zSg163.png-wh_50

nmcli connection delete didi 删除didi

wKiom1nnRjSQKehBAAAmwrEMkcI530.png-wh_50

wKioL1nnQ37QV7DXAAAxXbFREVE856.png-wh_50

nmcli connection show 显示所有网络

wKioL1nnQ3_xzUKbAAB7gqkKZR4932.png-wh_50

nmcli connection add type ethernet con-name name ifname eth0 ip4 ip/24 添加静态网络

wKiom1nnRjbg1NxzAACuknlD4Wk237.png-wh_50

wKiom1nnRjaSbypZAACc0bmMVfs808.png-wh_50

wKioL1nnQ4HQYr79AACaEwwYVo0366.png-wh_50

wKiom1nnRjeRypekAAC-0lOqIBQ894.png-wh_50

wKioL1nnQ4LDLwjBAACRDv33MWY964.png-wh_50

wKiom1nnRjjj4ZXsAAC4xhCy69w008.png-wh_50

nmcli connection modify "didi" ipv4.addresses newip/24 改变didi的ip

wKiom1nnRjmAXppbAABCkAbn9dA421.png-wh_50

wKioL1nnQ4OiGLk_AAAilS7-vd4401.png-wh_50

nmcli connnection modiy "didi" ipv4.method auto 改变didi的工作状态为动态

wKioL1nnQ4TAmQ66AAELOvIg6vw796.png-wh_50

nmcli connnection modiy "didi" ipv4.method manual 改变didi的工作状态为静态

wKiom1nnRjqTVmYWAADpTiUaD18684.png-wh_50

nmcli device connect eth0 开启设备

wKiom1nnRjvjTByAAAB_ULXgop0122.png-wh_50

nmcli device disconnect eth0 关闭设备

wKioL1nnQ4aThfwSAADU6L0gDOU992.png-wh_50

nmcli device show 显示设备信息

wKiom1nnRjzTAZXzAAGJKGJWh9M257.png-wh_50

nmcli device status 显示设备状态

wKioL1nnQ4eTiGNJAAB0yRteBi4468.png-wh_50


四、gateway 网关


1.路由器


主要功能是用来作nat的

dnat目的地地址转换

snat源地址转换 


2.网关


路由器上和自己处在同一个网段的那个ip 


3.设定网关


systemctl stop NetwrokManager

vim /etc/sysconfig/network  全局网关

GATEWAY=网关ip 

vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件  网卡接口网关

GATEWAY=网关ip

systemctl restart netwrok


4.将真机变成路由器


(1)开启火墙

wKiom1nnMsWyyZjlAAASAHDfzdw137.png-wh_50

(2)设置火墙在开机时启动

wKiom1nnNh3S7Z27AAASF4Nvvyw890.png-wh_50

(3)ifconfig网卡

wKioL1nnM2fB2oABAAFR-XpRFqY067.png-wh_50

wKioL1nnM2fQz3awAAFMj7F9XLk899.png-wh_50

可知网卡接口ip----br0:172.25.254.63;  wlp3so:192.168.43.253


5.将真机伪装成路由器


wKiom1nnNh3iPZomAAAWwpLvK0Y732.png-wh_50

wKioL1nnM2iDNrwhAAAomkxRL-E045.png-wh_50

伪装完成后,一定要reload

wKiom1nnNh7xwgUpAAAT-WTF3EE655.png-wh_50


6.虚拟机通过路由成功上网


(1)添加网络

wKiom1nnNh7yglgFAAAXC63153k333.png-wh_50

wKioL1nnM2jTJ-bHAAAlVRfc1CA044.png-wh_50

wKiom1nnNh7wVkBMAABRvolBamQ678.png-wh_50

添加的网络,网关与真机ip一致

wKioL1nnPxSgXVloAACVr_RqFXI355.png-wh_50

关闭全局网络

wKiom1nnQcqC2gYQAAASkYlq89k373.png-wh_50

wKioL1nnPxSwTJiKAAAuGMHzNhc511.png-wh_50

wKioL1nnPxXCpwByAAAP0R3x1ks114.png-wh_50

wKiom1nnQcvjGZEHAACHTQ1ZvTk150.png-wh_50

wKiom1nnQcvgDZ63AAANqObFABI095.png-wh_50

测试网络是连通的

wKioL1nnPxXRIjmwAABvsVl5SdY975.png-wh_50

以上操作只能用地址访问

设定dns

vim /etc/resolv.conf

nameserver dns服务器ip

要想通过网络名称访问服务器,需真机和虚拟机同时打开 vim /etc/resolv.conf

wKiom1nnQtLz9b0aAAAMn3_7a7c583.png-wh_50

wKioL1nnPxbQbNA3AAAQslQ1rgA148.png-wh_50


wKioL1nnPxaikpWnAAEMBWA6tJY971.png-wh_50

wKiom1nnQcyy-pqIAABiAEiThoA640.png-wh_50

wKioL1nnPxfz2iLxAACf7JLVWDs895.png-wh_50

此时能就能访问服务器了

wKiom1nnQc6zEVEgAALP_VIQZEM000.png-wh_50

wKioL1nnPxii2cSMAADXcsrMQe8461.png-wh_50






Logo

更多推荐