一、ip基础知识
1.ipv4
2进制32位-----10进制
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
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112047137.jpg" title="200.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112047233.jpg" title="201.png" />
右上角会出现无线表示
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112047622.jpg" title="202.png" />
选择网络:点击wi-fi按钮,turn o
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112047717.jpg" title="203.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112047903.jpg" title="204.png" />
确认连上无线网,ping IP,如下连上网
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112048020.jpg" title="205.png" />
二、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)开启火墙
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112048329.jpg" title="206.png" />
(2)设置火墙在开机时启动
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112048593.jpg" title="207.png" />
(3)ifconfig网卡
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112048793.jpg" title="208.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112049027.jpg" title="209.png" />
可知网卡接口ip----br0:172.25.254.63; wlp3so:192.168.43.253
5.将真机伪装成路由器
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112049477.jpg" title="210.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112049718.jpg" title="211.png" />
伪装完成后,一定要reload
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112049920.jpg" title="212.png" />
6.虚拟机通过路由成功上网
(1)添加网络
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112050135.jpg" title="213.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112050330.jpg" title="214.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112050517.jpg" title="215.png" />
添加的网络,网关与真机ip一致
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112050615.jpg" title="217.png" />
关闭全局网络
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112050724.jpg" title="218.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112051092.jpg" title="219.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112051311.jpg" title="220.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112051547.jpg" title="221.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112051649.jpg" title="222.png" />
测试网络是连通的
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112051826.jpg" title="223.png" />
以上操作只能用地址访问
设定dns
vim /etc/resolv.conf
nameserver dns服务器ip
要想通过网络名称访问服务器,需
真机和虚拟机同时打开 vim /etc/resolv.conf
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112051928.jpg" title="226.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112052123.jpg" title="225.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112052347.jpg" title="227.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112052633.jpg" title="228.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112052805.jpg" title="229.png" />
此时能就能访问服务器了
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112052976.jpg" title="230.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112053396.jpg" title="231.png" />
三、dsn
1.dns
dns是一台服务器
这太服务器提供了回答客户主机名和ip对应关系的功能
2.设定dns
vim /etc/resolv.conf
nameserver dns服务器ip
vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件
DNS1=dns服务器ip
3.本地解析文件
vim /etc/hosts
ip
主机名称
4.本地解析文件和dns读取的优先级调整
/etc/nsswitch.conf
38 hosts: db files nisplus nis dns
39 hosts: files dns
files代表本地解析文件,dns代表dns服务器,那个在前面那个优先
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112053663.jpg" title="248.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112053861.jpg" title="249.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112054278.jpg" title="250.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112054669.jpg" title="251.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112054876.jpg" title="252.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112055025.jpg" title="253.png" />
以上优先级不同,访问服务器速度不同
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112055160.jpg" title="254.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112055485.jpg" title="255.png" />
四、配置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 ##子网掩码
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112055577.jpg" title="256.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112055755.jpg" title="257.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112056097.jpg" title="258.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112056252.jpg" title="259.png" />
dhcp
##动态获取
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
##接口使用设备
BOOTPROTO=dhcp
##网卡工作模式
ONBOOT=yes
##网络服务开启时自动激活
NAME=eth0
##网络接口名称
:wq
systemctl restart networ
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112056415.jpg" title="260.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112056611.jpg" title="261.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112056955.jpg" title="262.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112057151.jpg" title="263.png" />
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网络
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112057367.jpg" title="264.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112057614.jpg" title="265.png" />
nmcli connection delete didi 删除didi
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112057785.jpg" title="266.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112100647.jpg" title="267.png" />
nmcli connection show 显示所有网络
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112100814.jpg" title="268.png" />
nmcli connection add type ethernet con-name name ifname eth0 ip4 ip/24
添加静态网络
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112100914.jpg" title="269.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112101116.jpg" title="270.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112101401.jpg" title="271.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112101690.jpg" title="272.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112101919.jpg" title="273.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112102170.jpg" title="274.png" />
nmcli connection modify "didi" ipv4.addresses newip/24
改变didi的ip
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112102531.jpg" title="275.png" />
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112102715.jpg" title="276.png" />
nmcli connnection modiy "didi" ipv4.method auto 改变didi的工作状态为动态
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112102958.jpg" title="277.png" />
nmcli connnection modiy "didi" ipv4.method manual 改变didi的工作状态为静态
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112103081.jpg" title="278.png" />
nmcli device connect eth0 开启设备
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112103198.jpg" title="279.png" />
nmcli device disconnect eth0 关闭设备
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112103387.jpg" title="280.png" />
nmcli device show 显示设备信息
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112103689.jpg" title="281.png" />
nmcli device status 显示设备状态
650) this.width=650;" src="/upload/getfiles/default/2022/11/4/20221104112103786.jpg" title="282.png" />
原文:http://13363488.blog.51cto.com/13353488/1974013