linux下的网络部署

mac2024-05-24  54

#######linux下的网络配置############ ###1.什么是IP ADDRESS internet protocol ADDRESS ##网络进程地址

ipv4 internet protocol version 4 1.2x32 ##二进制,32位 ip是由32个0或1组成 11111110.11111110.11111110.11111110 = 254.254.254.254

###2.子网掩码 用来划分网络区域 子网掩码非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/16

####4.网络设定工具

ping ##检测网络和目的地是否通畅 ping -c 1 ##ping 1次 ping -w 1 ##等待1秒 ping -c1 -w1 ip ##ping一次等待一秒 ![在这里插入图片描述](https://img-blog.csdnimg.cn/2019103117033854.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NfdGVhY2hlcg==,size_16,color_FFFFFF,t_70) ifconfig ##查看或设定网络接口 ifconfig ##查看 ifconfig device ip/24 ##设定 ifconfig device down ##关闭 ifconfig device up ##开启

ip addr ##检测或设定网络接 ip addr show ##检测 ip addr add ip/24 dev device ##设定 ip addr del dev eth0 ip/24 ##删除 ip addr flush eth0 ##刷新网卡信息

“注意: device的名字一个物理事实,看到什么命字只能用什么命字” ip addr和ifconfig都是临时设定的方法,好处是即改即生效,速度快,但是不能永久保存,网络重启之后,ip就会恢复为之前的设定

####5.图形方式设定ip 1.nm-connection-editor ##nm = NetworkManager,图形方式改了ip重启网络(有可能)不生效 systemctl stop NetworkManager ##记录网络服务状态 systemctl restart network systemctl start NetworkManager

2.nmtui 在shell中执行nmtui命令,可以进入到另一种设定ip的图形界面。 在此图形界面中,可以通过上下左右键进行操作,enter键执行。

命令方式设定网络 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.100/24 ##对应nm-connection-editor来写 nmcli connection modify westos ipv4.addresses 172.25.254.200/24 ####用nmcli将静态网络改成动态网必须先设定ip

####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 DEVICE=eth0 ONBOOT=yes IPADDR=172.25.254.100 NETMASK=255.255.255.0 BOOTPROTO=none NAME=westos ##加与不加在图形化看区别 ##不要乱动物理机上br0

systemctl restart network

一块网卡上配置多个IP vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBOOT=yes IPADDR0=172.25.254.100 NETMASK0=255.255.255.0 BOOTPROTO=none NAME=westos IPADDR1=172.25.0.100 PREFIX1=24

systemctl restart network

ip addr show eth0

####8.lo回环接口 回环接口------人的神经----127.0.0.1-----localhost ping localhost

假如包是由一个本地进程为另一个本地进程产生的, 它们将通过外出链的’lo’接口,然后返回进入链的’lo’接口.相当于系统对内进程的接口

####9.网关##### 网关是网络的进口和出口(网关还有其他功能,如协议翻译……),网关定义网络的边界。 比如有网络A和网络B,网络A的IP地址范围为“192.168.1.1~192. 168.1.254”,子网掩码为255.255.255.0;网络B的IP地址范围为“192.168.2.1~192.168.2.254”,子网掩码为255.255.255.0。在没有路由器的情况下,两个网络之间是不能进行TCP/IP通信的,即使是两个网络连接在同一台交换机(或集线器)上,TCP/IP协议也会根据子网掩码(255.255.255.0)判定两个网络中的主机处在不同的网络里。而要实现这两个网络之间的通信,则必须通过网关。 路由器: 路由器使用一系列算法决定网络间的最短路径。 路由器使用静态路由或动态路由来决定网络间的最短路径。静态路由需要管理员手动设置,而动态路由使用一些协议来动态发现网络间的路径并判断最短路径。通常,对于小型网络使用静态路由,大型复杂网络使用动态路由。 一、假设需要ip为1.1.1.115的主机与ip为172.25.254.215的主机进行通信 实验环境设定:将虚拟机desktop设定为双网卡,一块网卡eth1的ip为172.25.254.215,另一块网卡eth0的ip为1.1.1.215,将另一台虚拟机server的ip设为1.1.1.115 1.把真实主机变成路由器 (1)开启系统中火墙的地址伪装功能 firewall-cmd --list -all firewall-cmd --permanent --add-masquerade firewall-cmd --reload firewall-cmd --list-all

public (default, active) interfaces: br0 enp0s25 wlp3s0 sources: services: dhcpv6-client ssh ports: masquerade: yes <<<地址伪装功能开启,真实主机变成路由器 forward-ports: icmp-blocks: rich rules: (2)打开内核路由功能 sysctl -a | grep ip_forward net.ipv4.ip_forward=0 <<内核路由功能处于关闭状态

vim /etc/sysctl.conf net.ipv4.ip_forward=1 <<配置内核路由功能开启

sysctl -p #使/etc/sysctl.conf文件中的设定生效 2.设定虚拟机网关

vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效 GATEWAY=1.1.1.115

vim /etc/sysconfig/network-scripts/ifcfg-eth0 GATEWAY0=172.25.254.250 ##当网卡中设定的IP有多个时,指定对于那个IP生效 GATEWAY=172.25.254.250 ##当网卡中设定的IP只有一个时

route -n ##查看网关 Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 “172.25.254.250” 0.0.0.0 UG 1024 0 0 eth0 172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 172.25.254.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

##虚拟机网卡有问题就删了重新加

##10.设定dns domain name server == 域名解析服务 ##解析就是把域名变成IP

vim /etc/hosts ##本地解析文件 ip 域名 220.181.111.188 www.baidu.com

vim /etc/resolv.conf ##dns的指向文件 nameserver 114.114.114.114 ##当需要某个域名的IP地址时去问114.114.114.114 ##陕西省dns 218.30.19.50 ##谷歌dns 8.8.8.8 vim /etc/sysconfig/network-scripts/ifcfg-xxxx DNS1=114.114.114.114 注意: 档网络工作模式为dhcp时 系统会自动获得ip 网关 dns 那么/etc/resolv.conf会被获得到的信息修改 如果不需要获得dns信息 在网卡配置文件中加入 PEERDNS=no

##网卡参数怎么写:/usr/share/doc/initscripts-9.49.37/sysconfig.txt ##11.设定解析的优先级 系统默认: /etc/hosts > /etc/resolv.conf

vim /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 cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf vim /etc/dhcp/dhcpd.conf

#dhcpd.conf

#Sample configuration file for ISC dhcpd

#option definitions common to all supported networks… option domain-name “westos.com”; ##域名 option domain-name-servers 114.114.114.114; ##dns

default-lease-time 600; ##默认租约 max-lease-time 7200; ##最长租约

#Use this to enble / disable dynamic dns updates globally. #ddns-update-style none;

#If this DHCP server is the official DHCP server for the local network, the authoritative directive should be uncommented. #authoritative;

#Use this to send dhcp log messages to a different log file (you also #have to hack syslog.conf to complete the redirection). log-facility local7;

#No service will be given on this subnet, but declaring it helps the #DHCP server to understand the network topology. 27 删除 28 删除

#This is a very basic subnet declaration.

subnet 172.25.254.0 netmask 255.255.255.0 { ##子网设定 range 172.25.254.160 172.25.254.200; ##IP地址池 option routers 172.25.254.250; ##网关 } 35行有面全删除 ##学生做实验时拔掉网线

systemctl restart dhcpd systemctl stop firewalld 可以在服务端和客户端看到ip分配的记录 服务端:/var/lib/dhcpd/dhcpd.leases 客户端:/var/log/messages

测试: 在网络工作模式是dhcp的主机中重启网络 可以看到 ip GW dns 全部获取成功

最新回复(0)