linux中网络的配置

1.什么是IP  ADDRESS
internet  protocol    ADDRESS   ##网络进程地址
ipv4     internet  protocol   version  4

由32个二进制代码组成
ip是由32个01组成
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    ##检测网络是否通畅
ifconfig  #查看或设定网络接口
ifconfig   device  ip/24   ##设定ip
ifconfig    device   down    ##关闭
ifcongig    device   up      ##打开


ip  addr  ##检测和设定网络接口
ip  addr  show  ##检测


ip addr     add  ip/24    dev  device  #设定


注意:“device的名字是个物理事实,看到什么名字只能用什么名字


5.图形的设定方式
nm-connection-editor
systemctl    stop  NetworkManager(网络记录服务)
systemctl  restart   network  (重启网络)
systemctl  start   NetworkManager


2.nmtui  (使用tab键进行选定)


6.命令方式设定网络
nmcli  ##NetworManager必须开启
nmcli   device  connect  etho   ##启用eth0网卡


nmcli       device  disconnect  eth0##关闭eth0网卡
nmcli    device  show   eth0   #查看网卡信息


nmcli  device  status   eth0   ##查看网卡服务接口信息
nmcli  connection  show    ##显示接口名称


nmcli  connection  down  westos    ##关闭名叫westos的网卡


nmcli  connection   up  westos   ##开启名叫westos的网卡


nmcli   connection   delete   westos##删除名叫westos的网卡
nmcli  connection  add  type  ethernet  con-name  westos  ifname  eth0  ip4  172.25.254.249/24      ##设置ip


nmcli  connection  modify  westos  ipv4.method  auto  ##修改网络为动态ip
nmcli  connection  modify  westos  ipv4.method  manual##修改网络为静态ip
nmcli  connection  modify  westos  ipv4.address  172.25.254.249/24###修改ip地址
7.管理网络配置文件
/etc/sysconfig/network-scripts/
网络配置文件的命名规则
ifcfg-xxxx
DEVICE=XXX    ##设备名称
BOOTPROTO=dhcp|static|none    ##设备工作方式
ONBOOT=yes   ##网络服务开启时自动激活网卡
IPADDR=  ##ip地址
PREEIX=24##子网掩码
NETMASK=255.255.255.0##子网掩码
NAME=    ##接口名称
示例:
静态网络设定文件
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
 
 文件设定完成之后一定要重启网络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地址时使用       ip  addr  show  eth0     这个命令


8.lo回环接口(主机内部进行交流,不与外界交流)
回环接口-----人的神经------127.0.0.1(只能本机人操作)---localhost
9.网关
1.把真实主机变成路由器
systemctl   stop   libvirtd
systemctl   restart   firewalld
systemctl  start   libvirtd
firewall-cmd  --list
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.设定虚拟机网关
vim   /etc /sysconfig/network  #全局网关,针对所有没有设定网管的网卡生效
GATEWAY=172.25.254.250


 vim  /etc/syscongig/network-scripts/ifcfg-eth0
GATEWAY0=172.25.254.250  #当网卡中设定的ip有多个时,指定对于那个ip生效
GATEWAY=172.25.254.250  #当网卡设定的ip只有一个时
systemctl  restart   network
route  -n   ##查网关

10.dns 
domain  name  server  ==域名解析服务##解析就是把域名变成ip
vim  /etc/hosts      #本地解析文件(如果里面没有解析,则ping不到)


ip  域名
220.181.111.188     www.baidu.com


vim  /etc/resolv.conf     #dns的指向文件
nameserver   114.114.114.114##当需要某个域名的ip地址时去问114.114.114.114


vim  /etc/sysconfig/network-scripts/ifcfg-xxxx
DNS1=114.114.114.114
注意:
当网络工作模式为dhcp时,系统会自动获得ip网关dns
那么/etc/resolv.conf会被获得到的信息修改
如果不需要获得dns信息
在网卡配置文件加入
PEERDNS=no
11.设定解析的优先级
系统默认:/etc/hosts   >  /etc/resolve.conf
vim  /etc/nsswitch.conf
39  hosts:   files  dns    ##/etc/hosts优先


vim  /etc/nsswitch.conf
39  hosts:  dns files     ##/etc/resolv.conf    dns指向优先

12.配置服务器环境
vim   /etc/sysconfig/network-scripts/ifcfg-eth0
添加id号;
IPADDR=172.25.254.249
NETMASK=255.255.255.0

BOOTPROTO=none
systemctl   restart   network
vim  /etc/yum.repos.d/rhel_dvd.repo
修改baseurl=http://172.25.254.250/rhel7
yum  clean  all
yum  repolist
yum     install   dhcp  -y


cd  /etc/dhcp/
ls
vim   dhcpd.conf
cp  /usr/share/doc/dhcp*/dhcpd.conf.example  /etc/dhcp/dhcpd.conf
vim  dhcpd.conf
进入文件之后35行 以后删除  27.28行删除,改域名,改指向
最后几行改为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(设置网关)


systemctl  restart   dhcpd
systemctl  stop   firewalld


less  /var/lib/dhcpd/dhcpd.leases
在另一个虚拟机做下列操作测试:
在网络工作模式是dhcpd的主机中重启网络可以看到ip  gw  dns全部获取成功
systemctl    restart    network

设定该虚拟机的网卡,将原有的ip地址删了,才能看见结果,其ip地址的获得是从ip地址池中所获取的
 

猜你喜欢

转载自blog.csdn.net/weixin_42709236/article/details/81265403