一、设定ip(nmcli 命令)
-
首先执行systemctl status NetworkManager命令查看服务是否开启,该命令需要在服务开启才可以执行。
-
nmcli device命令:
nmcli device show 查看详细信息
nmcli device status 查看设备状态
nmcli device disconnect eth0 关闭eth0设备
nmcli device connect eth0 开启eth0设备
nmcli device show eth0 显示eth0详细信息
nmcli device status eth0 显示eth0的设备状态
-
nmcli connection 命令
nmcli connection show 显示所有网络连接
nmcli connection delete eth0 删除eth0 的网络连接
nmcli connection show
nmcli connection add type ethernet con-name eth0 ifname eth0 新建eth0网络连接
nmcli connection show
nmcli connection delete eth0
nmcli connection add type ethernet con-name eth0 ifname eth0 autoconnect yes 新建动态连接
nmcli connection delete eth0
nmcli connection add con-name eth0 ifname eth0 type ethernet ip4 172.25.254.210/24 新建静态连接
ifconfig eth0
将静态连接方式修改成动态连接
nmcli connection modify eth0 ipv4.method auto
nmcli connection reload
nmcli connection down eth0 关闭
nmcli connection up eth0 开启
将动态连接方式修改为静态连接方式
nmcli connection delete eth0
nmcli connection add type ethernet con-name eth0 ifname eth0 autoconnect yes 建立动态ip
nmcli connection modify eth0 ipv4.addresses 172.25.254.210/24修改为指定静态ip
nmcli connection modify eth0 ipv4.method manual
nmcli connection down eth0
nmcli connection up eth0
ifconfig eth0
二、虚拟机实现wifi上网功能:(ping www.baidu.com)
- 首先真机连网ping www.baidu.com 成功
- 搭建环境及步骤如下:
用虚拟机带有双网关的主机server来ping百度
首先在双网关主机的与真机ip区域一致的eth0中添加网关
网关为真机主机的ip例如172.25.254.50
编辑vim /etc/sysconfig/network-scripts/ifcfg-eth0
在里面添加
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
IPADDR=172.25.254.110
NETMASK=255.255.255.0
GATEWAY=172.25.254.50
编辑systemctl restart network重新启动网络服务
编辑route -n查看网关
在真机中首先ping www.baidu.com
systemctl stop libvirtd 关掉虚拟机服务
systemctl restart firewalld 重新启动火墙
firewall-cmd --add-masquerade开启伪装
firewall-cmd --list-all查看
systemctl start libvirtd开启虚拟机
结束后用有双网卡的虚拟机ping 百度ip( )
如果要让desktop主机ping 百度成功
在desktop中编辑vim /etc/sysconfig/network-scripts/ifcfg-eth0
编辑systemctl restart network重新启动网络服务
查看网关
在路由主机 sysctl -p 查看
sysctl -a | grep ip_forward
net.ipv4.ip_forward=0
在桌面编辑vim /etc/sysctl.conf
添加net.ipv4.ip_forward=1
退出执行:sysctl -p
net.ipv4.ip_forward=1
成功后在desktop主机ping 百度(ip)成功
三、DNS 地址解析
实验:
在server中ping www.baidu.com ping 不通
-
编辑vim /etc/hosts(文本编辑,每次都需要在里面添加)
格式: ip 名称
183.232.231.173 www.baidu.com添加
ping www.baidu.com成功
文件中没有再次ping www.taobao.com不会成功 -
编辑vim /etc/resolv.conf网络模式设定dns
nameserver 114.114.114.114添加
ping taobao.com成功
-
编辑vim /etc/nsswitch.conf转换优先级
hosts: files dns hosts 优先级高
hosts: dns files dns优先级高
-
静态dns设置
编辑cd /etc/sysconfig/network-scripts/
vim ifcfg-eth0
cat ifcfg-eth0
cat /etc/resolv.conf
systemctl restart network重新启动网络服务
cat /etc/resolv.conf 查看
-
动态dns设置
编辑vim ifcfg-eth0
systemctl restart network重新启动服务
vim /etc/resolv.conf 编辑文件
systemctl restart network再次重新启动服务
cat /etc/resolv.conf