Linux系统下的网络配置

 

网络设定

1、什么是IP ADDRESS    
    internet protocol ADDRESS    网络进程地址

    ipv4  interent protocol  version 4

    1.2x32
    ip是由32个01组成
    11111110.11111110.11111110.11111110=254.254.254.254
    


2、子网掩码
    用来划分网络区域
    子网掩码非零位对应的ip上的数字表示这个ip的网络位
    子网掩码零位对应的数字是ip的主机位
    网络位表示网络区域
    主机位表示网络区域里的某台主机
    网络位一致主机位不一致的两个IP是可以直接通信的,这样两台主机叫直连网络


3、ip通信判定
    网络位一致,主机位不一致的两个IP可以直接通讯
    172.25.254.1/24        24=255.255.255.0

netmask:用来标识IP的网络位和主机位

 


4、网络设定工具    
    ping            检验网络是否通畅    
    ifconfig        查看或设定网络接口
    ifconfig         查看
    ifconfig device ip/24    设定
    ifconfig device down    关闭 

    ifconfig device up          开机

   

                                            ##此处Ping通了172.25.254.138的IP##

 

    

                            ####此处用ifconfig查看网络接口#####

   

    ip addr            检测或置顶网络接口
    ip addr show        检测
    ip addr add ip/24 dev device    设定

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


5、设定IP

方法一

ifconfig device ip netmask 255.255.255.0

ifconfig etho 172.25.254.xxx netmask 255.255.255.0

                        重新设置IP                         子网掩码

##此设定方式为临时的,网络服务重启后失效##

####先查看IP####

 

####临时设定后IP为172.25.254.238#####

 

#####重启服务后IP更改######

 

 

方法二

##圆形设定IP的方式##

nm-connection-eitor   

网络设备设定IP的方式有两种

dhcp  动态获取

static   景泰IP为执行者自行设定,ip固定

nmtlli 命令TAB建选择——当系统没有图形界面可以用此命令开启网络设定的ui界面

第一步:选中eth0 然后Delete删除

 

第二步:选Add添加

 

第三步:添加名称eth0,并且在Device MAC adress 选中含eth0的选项

 

第四步:在IPv4 Settings选项下的Method选项卡中

                选择Manual ,并且添加Adresses,Save

                保存即可完成。


第五步:重置网络     
                systemctl stop NetworkManager
                systemctl restart network
                nmtui
    
 

 

方法三:文本方式设定

cd /etc/sysconfig/network-scripts

vim  ifcfg-名称

DEVICE=网卡

ONBOOT=yes

BOOTPROTO=dhcp或none或static   后两个都是静态网络,两个选一个就可以

IPADDR=IP地址

NETMASK=子网掩码 或PREFIX=子网掩码的缩写

NAME=连接名称 可写可不写

 

###网卡名称:eth0;静态网络;IP为172.25.254.238###

 

一个静态网卡设置多个IP

cd /etc/sysconfig/network-scripts

vim ifcfg-westos

DEVICE=eth0

ONBOOT=yes

BOOTPROTO=none

IPADDRP0=172.25.254.100

PREFIX0=24

IPADDR1=172.25.254.100

PREFIX1=24

systemctl restart network

ip addr show eth0


6、命令方式设定网络
nmcli                                                 ###NetworkManager必须开启###

nmcli device connect eth0   启用eth0网卡
nmcli device disconnect eth0    关闭eth0网卡
nmcli device show eth0        查看网卡信息
nmcli device status eth0    查看网卡服务


7、lo回环接口
回环接口相当于人的神经


8、网关

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.真机ID

vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY0=172.25.254.真机ID    当网卡中设定的IP有多个时,指定对那个IP生效
GETEWAY=172.25.254.真机ID    当网卡中设定的IP只有一个时

route -n               查看网关

################拔掉网线,在链接wifi的情况下用真机ping百度#########

##############在虚拟机上ping 在真机上得到的地址###############

 

 

##可能出现的问题:

 

(1)网卡内核没有开启:
    sysctl -a | grep ip_forward
    net.ipv4.ip_forward = 0        ##证明没有开启##
    vim /etc/sysctl.conf
    net.ipv4.ip_forward=1        ##在最后一行添加##
    sysctl -p            ##开启配置##    
    sysctl -a | grep ip_forward
    net.ipv4.ip_forward = 1        ##即为开启##

                  #########此时网卡内核没有开启############

                 ############此时网卡内核开启################

 

(2)网卡出现问题
    virt-manager            ##在真机上输入##
    

双击出问题的虚拟机


    ifconfig eth0 up        
    点小灯泡后,右键删除NIC :00:00:0a    
  

 

 在Network下新建一个网卡,选中virtio        
    finish完成
    
               



9、设定dns
domain name server == 域名解析服务    解析就是把域名变成IP

vim /etc/hosts                 本地解析文件
ip    域名
220.181.111.188  www.baidu.com              (此处不固定,需用真机ping网址来得到域名)

 

 

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

将原有第二行去掉,改为nameservver 114.114.114.114

添加DNS1=114.114.114.114

                         ##################ping通#####################

注意:
当网络工作模式为dhcp时
系统会自动获得ip 网关 dns
那么 /etc/resolv.conf会被获得到的信息修改
如果不需要获得dns信息
在网卡配置文件中加入 PREEDNS=no

 



10、设定解析的优先级
系统默认:
/etc/hosts    >        /etc/resolv.conf
vim /etc/nsswitch.conf
39 hosts:   file dns        ##/etc/hosts/优先

vim /etc/nsswitch.conf        
39 hosts:   dns  file        ##/etc/resolv.conf 优先

正常情况下,www.taobao.com的为120.192.89.188

我们在hosts文件内将www.baidu.com的域名保留,将名字改为www.taobao.com

再次ping www.taobao.com便成了刚刚更改的错误域名,因为hosts的优先级高

##########原本优先级    file>dns#############

 

###########更改优先级后###########

 

再次ping www.taobao.com这个网址的域名便又变成正确的域名



11、dhcp服务配置

在重置虚拟机后,编辑配置文件

vim   /etc/sysconfig/network-scripts/ifcfg-eth0

重置虚拟机后,编辑文本,在172.25.254.250上下载配置文件,保存后按照图片中的步骤进行操作

下载完配置文件,再进行安装

#############安装完成!!!############

 

############查看文件已经存在###########

 

################复制文件##############

 

 

 

vim  /etc/dhcpd/dhcpd.conf
  1 # dhcpd.conf
  2 #
  3 # Sample configuration file for ISC dhcpd
  4 #
  5
  6 # option definitions common to all supported networks...
  7 option domain-name "westos.com";                                                           ####名称
  8 option domain-name-servers 114.114.11.114 ;                                         ####dns
  9
 10 default-lease-time 600;                                                                                 ####默认租约
 11 max-lease-time 7200;                                                                                   ####最长租约
 12
 13 # Use this to enble / disable dynamic dns updates globally.
 14 #ddns-update-style none;
 15
 16 # If this DHCP server is the official DHCP server for the local
 17 # network, the authoritative directive should be uncommented.
 18 #authoritative;
 19
 20 # Use this to send dhcp log messages to a different log file (you also
 21 # have to hack syslog.conf to complete the redirection).
 22 log-facility local7;
 23
 24 # No service will be given on this subnet, but declaring it helps the
 25 # DHCP server to understand the network topology.
 26
 27
 28 # This is a very basic subnet declaration.
 29
 30 subnet 172.25.254.0 netmask 255.255.255.0 {        子网设定
 31   range 10.254.239.160 10.254.239.200;        IP地址池
 32   option routers 172.25.254.38;            网关
 33   }


删除35行以后
删除27、28行

 

 

验证:

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

######网络工作模式为DHCP#########

 

##############IP地址为IP地址池范围内的IP#########

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值