本文共 3541 字,大约阅读时间需要 11 分钟。
10.linux下的网络配置
1.什么是IP ADDRESS
internet protocol ADDRESS ##网络进程地址 ipv4 internet protocol version 4 ##二进制,32位 11111110.11111110.11111110.11111110 = 254.254.254.2542.子网掩码
用来划分网络区域 子网掩码非0的位对应的ip上的数字表示这个ip的网络位 子网掩码0位对应的数字是ip的主机位 网络位表示网络区域 主机位表示网络区域里某台主机3.ip通信判定
网络位一致,主机位不一致的2个IP可以直接通讯 172.25.254.1/24 24=255.255.255.0 172.25.254.2/24 172.25.0.1/16List item
4.网络设定工具
ping ##检测网络是否通畅ifconfig ##查看网络信息ifconfig device ip/24 ##设定网络ip ifconfig device down ##关闭 ifconfig device up ##开启
ip addr ##检测或设定网络接口ip addr show ##查看网络信息ip addr add ip/24 dev device ##添加网络ip
“注意:device只是一个名字(如图中的eth0)”
“注意:device只是一个名字(也就是图中的eth0)”5.图形方式设定ip
1.nm-connection-editor ##nm = NetworkManager,图形方式改了ip重启网络(有可能)不生效 systemctl stop NetworkManager ##禁用网络管理 systemctl start NetworkManager ##打开网络管理 systemctl restart network ##重启网络 2.nmtui####6.命令方式设定网络
nmcli ##NetworkManager必须开启nmcli device connect eth0 ##启用eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡 nmcli device show eth0 ##查看网卡信息 nmcli device status eth0 ##查看网卡服务接口信息##用nm-connection-editor改名称为westos
nmcli connection show nmcli connection down westos nmcli connection up westos nmcli connection delete westos nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.239/24 ##对应nm-connection-editor来写 nmcli connection modify westos ipv4.method auto nmcli connection modify westos ipv4.method manual nmcli connection modify westos ipv4.addresses 172.25.254.239/24####7.管理网络配置文件
网络配置目录 /etc/sysconfig/network-scripts/ 网络配置文件的名命规则 ifcfg-xxxx DEVICE=xxx ##设备名称 BOOTPROTO=dhcp|static|none ##设备工作方式 ONBOOT=yes ##网络服务开启时自动激活网卡 IPADDR= ##IP地址 PREFIX=24 ##子网掩码 NETMASK=255.255.255.0 ##子网掩码 NAME= ##接口名称(可有可无) #nmcli connection delete westos 先删除,再添加(删除的是配置文件)示例:
静态网络设定文件 vim /etc/sysconfig/network-scripts/ifcfg-eth0 systemctl restart network 配置完成后重启网络 ip addr show eth0 查看eth0的网络ip信息一块网卡上配置多个IP
vim /etc/sysconfig/network-scripts/ifcfg-eth0systemctl restart network ##重启网络
ip addr show eth0 ##查询 eth0 的IP 信息####8.lo回环接口
回环接口------人的神经----127.0.0.1-----localhost ping localhost####9.网关#####
1.把真实主机变成路由器
systemctl restart firewalld ##打开防火墙 systemctl start libvirtdfirewall-cmd --list
firewall-cmd --permanent --add-masquerade firewall-cmd --reload firewall-cmd --list-all <<查询防火墙状态(yes表示开启) <<路由转换开启 这时真实主机就能当做路由器使用2.设定虚拟机网关
vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.39.250vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY0=172.25.39.250 ##档网卡中设定的IP有多个时,指定对于那个IP生效 GATEWAY=172.25.39.250 ##档网卡中设定的IP只有一个时route -n ##查看网关
##虚拟机网卡有问题就删了重新加 一般为虚拟机连接不到主机
##10.设定dns (设定DNS后当访问某个域名时会自动进入DNS设定的ip地址查询该域名的ip)
domain name server == 域名解析服务 ##解析就是把域名变成IPvim /etc/hosts ##本地解析文件
ip 220.181.111.188 域名vim /etc/resolv.conf ##dns的指向文件
nameserver 114.114.114.114 ##当需要某个域名的IP地址时去问114.114.114.114 ##陕西省dns 218.30.19.50 vim /etc/sysconfig/network-scripts/ifcfg-xxxx DNS1=114.114.114.114注意:
档网络工作模式为dhcp时 系统会自动获得ip 网关 dns 那么/etc/resolv.conf会被获得到的信息修改 如果不需要获得dns信息 在网卡配置文件中加入 PEERDNS=no##11.设定解析的优先级(DNS查询的优先级)
系统默认: /etc/hosts > /etc/resolv.confvim /etc/nsswitch.conf
39 hosts: files dns ##/etc/hosts优先vim /etc/nsswitch.conf
39 hosts: dns files ##/etc/resolv.conf dns指向优先####12.dhcp服务配置
先reset两台虚拟机 yum install dhcp -y 安装dhcp服务 cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf vim /etc/dhcp/dhcpd.conf ##做实验时拔掉网线systemctl restart dhcpd
systemctl stop firewalld 可以在服务端和客户端看到ip分配的记录 服务端:/var/lib/dhcpd/dhcpd.leases 客户端:/var/log/messages写自定义目录标题)转载地址:https://blog.csdn.net/weixin_45426401/article/details/96772360 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!