Linux系统基础(十)

来源:互联网 发布:树莓派python编程 编辑:程序博客网 时间:2024/05/29 16:54
###################
####11.管理网络####
###################

####1.ip基础知识####
1.ipv4
2进制32位-----10进制

172.25.0.10/255.255.255.0
172.25.0.10:ip地址
255.255.255.0:子网掩码
子网掩码255位对应的ip位为网络位
子网掩码0对应的ip位为主机位

####2.配置ip####
<<图形化>>
1.图形界面
nm-connection-editor
2.文本化图形
nmtui

<<命令>>
ifconfig 网卡 ip netmask    ##临时设定

nmcli connection add type ethernet con-name westos ifname eth0 autoconnect yes    #添加dhcp网络
nmcli connection add type ethernet con-name westos ifname eth0 ip4 ip/24    #添加静态网络
nmcli connection delete westos                            #删除westos链接
nmcli connection show                                 #显示所有网络链接
nmcli connection down westos                            #关闭指定链接

nmcli connection up westos                            #开启指定链接

nmcli connection modify "westos" ipv4.addresses newip/24            #改变wetos的ip
nmcli connection modify "westos" ipv4.method <auto|manual>            #改变westos的工作方式为动态或者静态
nmcli device connect eth0                            #开启设备
nmcli device disconnect eth0                            #关闭设备
nmcli device show                                 #显示设备信息
nmcli device status                                #显示设备状态


<<通过修改配置文件配置IP>>
dhcp        ##动态获取
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0        ##接口使用设备
BOOTPROTO=dhcp        ##boot protocol,启动的协议(动态获取)
ONBOOT=yes        ##网络服务开启时自动激活
NAME=eth0        ##网络接口名称
:wq
systemctl restart network



static|none    ##静态网络
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0             ##设备
BOOTPROTO=static|none         ##boot protocol,启动的协议(静态或无)
ONBOOT=yes             ##开启网络服务激活设备
NAME=eth0             ##网络接口名称
IPADDR=172.25.0.100          ##IP
NETMASK=255.255.255.0 | PREFIX=24 ##子网掩码
编写设置ip的命令:
    1. 编辑文件/bin/set-ip
    2. 文件内容:
    cd /etc/sysconfig/network-scripts
    rm -fr ifcfg-eth0
    cat > ifcfg-eth0 <<EOF
    DEVICE=eth0
    BOOTPROTO=none
    ONBOOT=yes
    NAME=eth0
    IPADDR=172.25.254.$1    # $1代表命令后面跟的第一个参数
    PREFIX=24
    EOF
    systemctl  restart network

    3. 文件其它操作:
        chmod +x /bin/set-ip            ##赋予文件执行权限
    4. 使用命令: set-ip 12             ##将IP改为172.25.254.12
    5. 理解:为什么脚本文件要放在/bin目录下?
                  因为只有存储在部分目录中的脚本可以直接输入名字调用,其他目录中的命令需要使用绝对路径调用。

可直接调用的目录:/usr/local/bin  :/usr/local/sbin  :/usr/bin:/usr/sbin  :/bin:/sbin  :/home/kiosk/.local/bin   :/home/kiosk/bin

# 网关


## 实验:
desktop:  172.25.0.10/24     GATEWAY=172.25.0.250

网关: 172.25.0.250/24   172.25.254.250/24

server:   172.25.254.100/24   GATEWAY=172.25.254.250



## 查看与设置

- 设置网关
/etc/sysconfig/network        (优先级低)
/etc/sysconfig/network-scripts/ifcfg-xxx  (优先级高)

编写内容:GATEWAY=xxx

- 查看网关: route -n


# DNS:domain name server

- 管理IP和域名关系的本地文件/etc/hosts
           ip                               域名
172.25.254.10        www.westos.org


- 指定DNS服务器的设定dns:
    /etc/sysconfig/network-scripts/ifcfg-xxx      
        # 永久修改,必须重启服务才生效
        DNS1=xxx        172.25.254.254
        DNS2=xxx
        DNS3=xxx
    /etc/resolv.conf    
        # 即可即生效,无需重启服务
        nameserver xxxx        
- 检测DNS是否设置成功
ping westos.example.com  172.25.254.1


- 设置本地解析和DNS服务器上解析优先级的文件/etc/nsswitch.conf

# 动态获取IP


server: 分配给别人IP
desktop: 动态获取IP


## server主机操作
1. server必须有一个IP: 172.25.x.11/24
2. 安装dhcp软件: yum install dhcp -y               ##注意:若想安装dhcp服务需先搭建服务
3. 查看dhcp软件的配置文件: rpm -qc dhcp
4. 配置dhcpd.conf文件:
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example  /etc/dhcp/dhcpd.conf
  - 删除36行以后的内容; dG
  - 删除27,28行;
 
```
  1 option domain-name "westos.org";
  2 option domain-name-servers 172.25.254.254;
  3 default-lease-time 600;
  4 max-lease-time 7200;
  5 log-facility local7;
  6 subnet 172.25.0.0 netmask 255.255.255.0 {
  7   range 172.25.0.100 172.25.0.120;
  8   option routers 172.25.0.250;
  9 }
```
5. 重启dhcp服务:
    systemctl  start dhcpd
    systemctl  status dhcpd
   
## desktop主机操作
设置ip获取方式为dhcp;






 

原创粉丝点击