第四周


1.自建yum仓库,分别为网络源和本地源

        centos7安装光盘做本地yum仓库

        1.挂载centos7安装光盘
        [root@centos7903 ~]# mount /dev/cdrom /mnt/
        mount: /dev/sr0 is write-protected, mounting read-only

        [root@centos7903 ~]# ls -1  /mnt/
        CentOS_BuildTag
        EFI
        EULA
        GPL
        images
        isolinux
        LiveOS
        Packages
        repodata
        RPM-GPG-KEY-CentOS-7
        RPM-GPG-KEY-CentOS-Testing-7
        TRANS.TBL

        2.repo仓库配置文件指向本地yum仓库

        [root@centos7903 ~]# vim /etc/yum.repos.d/local.repo
        [base]
        name=centos dvd local
        baseurl=file:///mnt/
        enabled=1
        gpgcheck=1
        gpgkey=file:///mnt/rpm-gpg/RPM-GPG-KEY-CentOS-7

        说明:yum配置仓库服务器 baseurl 的形式支持如下的协议

        baseurl=file://       ##本地路径,即光盘文件直接挂在到本地或者拷贝光盘到本地
                http://       ##web服务
                https://
                ftp://

        注意:yum仓库指向的路径一定必须是repodata目录所在目录

              centos7安装光盘就一个仓库base配置一个即可

        ##查看配置的yum 源
        [root@centos7903 ~]# yum repolist  --disablerepo=*  --enablerepo=local
        Loaded plugins: fastestmirror
        Loading mirror speeds from cached hostfile
        repo id                                                             repo name                                                              status
        local                                                               centos dvd                                                             4,070
        repolist: 4,070

        centos8安装光盘做网络yum仓库

        配置网络yum仓库,需要配置yum仓库服务器,常用的yum仓库服务器可以是 http  https ftp 等服务器


        1.配置http服务器

        [root@centos808 ~]# rpm -q httpd|yum install -y httpd

        [root@centos808 ~]# systemctl status httpd

        配置测试页面
        [root@centos808 html]# cd /var/www/html/
        [root@centos808 html]# echo kuo shao gan >>index.html

        [root@centos818 ~]# curl -I http://192.168.80.8 &>/dev/null && echo "http it is ok"
        http it is ok

        2.挂载安装光盘至/var/www.html/

        [root@centos808 ~]# mount /dev/cdrom   /var/www/html/

        [root@centos808 html]# ls -1
        AppStream
        BaseOS
        EFI
        images
        isolinux
        media.repo
        TRANS.TBL


        注意:yum仓库指向的路径一定必须是repodata目录所在目录

             centos8 安装管盘中有两个软件仓库 BaseOS和AppStream都需要配置

        [root@centos818 ~]# vim /etc/yum.repos.d/local.repo

        [BaseOS]
        name=BaseOS centos8
        baseurl=http://192.168.80.8/BaseOS
        gpgcheck=1
        enabled=1
        gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

        [AppStream]
        name=AppStream centos8
        baseurl=http://192.168.80.8/AppStream
        gpgcheck=1
        enabled=1
        gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial


        #查看配置的 yum 源
        [root@centos818 yum.repos.d]# yum repolist --disablerepo=* --enablerepo=BaseOS 
        repo id                                                                   repo name
        BaseOS                                                                    BaseOS centos8
        [root@centos818 yum.repos.d]# yum repolist --disablerepo=* --enablerepo=AppStream 
        repo id                                                                  repo name
        AppStream                                                                AppStream centos8

2.编译安装http2.4,实现可以正常访问,并将编译步骤和结果提交

        #卸载已经安装的httpd rpm包
        [root@centos7903 ~]# rpm -q httpd &>/dev/null  && yum  -y remove httpd &>/dev/null 
        #安装相关依赖包
        [root@centos7903 ~]#yum install -y  gcc gcc-c++ glibc glibc-devel make cmake  autoconf apr-devel apr-util-devel pcre pcre-devel openssl  openssl-devel systemd-devel zlib-devel

        #下载并解压源码文件
        [root@centos7903 ~]# wget https://dlcdn.apache.org//httpd/httpd-2.4.48.tar.bz2
        [root@centos7903 ~]# tar xvf httpd-2.4.48.tar.bz2 &>/dev/null
        [root@centos7903 ~]# cd httpd-2.4.48/

        #预编译
        [root@centos7903 httpd-2.4.48]# ./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd --enable-ssl
        #编译
        [root@centos7903 httpd-2.4.48]# make -j 2 
        #安装
        [root@centos7903 httpd-2.4.48]# make install

        #配置环境变量  
        [root@centos7903 ~]# echo 'PATH=/app/httpd/bin:$PATH' /etc/profile.d/httpd.sh 
        PATH=/app/httpd/bin:$PATH /etc/profile.d/httpd.sh
        [root@centos7903 ~]# chmod u+x /etc/profile.d/httpd.sh
        [root@centos7903 ~]# . /etc/profile.d/httpd.sh 

        注意:编译安装 --prefix=/usr/local/httpd  会自动加入 $PATH  环境变量 

                     --prefix=/app/httpd       指定了编译安装在/app/httpd 目录,则需要单独配置环境变量     

        #创建apache用户
        [root@centos7903 ~]# id apache &>/dev/null || useradd  -r  -s /sbin/nologin  apache

        ##修改配置文件以apache 用户身份运行
        
        [root@centos7903 httpd-2.4.48]# sed -ri.org -e  's/User daemon/User apache/g' -e 's/Group daemon/Group apache/g' /etc/httpd/httpd.conf
         
        # 修改默认的监听端口 和域名 
        [root@centos7903 httpd-2.4.48]#sed -ri.org  's/^(listen).*/\1 8080/gI'  /etc/httpd/httpd.conf
        [root@centos7903 httpd-2.4.48]#sed -ri.org  's/^#(servername).*/\1 www.magedu.com:8080/gI' /etc/httpd/httpd.conf

        #检查修改的结果
        [root@centos7903 htdocs]# grep -iE "^listen.*|^user.*|^group.*|^servername.*" /etc/httpd/httpd.conf
        Listen 8080
        User apache
        Group apache
        ServerName www.magedu.com:8080

        #启动服务
        [root@centos7903 httpd-2.4.48]# apachectl start   ##  apachectl stop 停止服务  restart  重启服务
        [root@centos7903 httpd-2.4.48]# ps -ef | grep httpd 
        root      22754      1  0 18:05 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
        apache    22755  22754  0 18:05 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
        apache    22756  22754  0 18:05 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
        apache    22757  22754  0 18:05 ?        00:00:00 /usr/local/httpd/bin/httpd -k start

        #创建一个测试页面
        [root@centos7903 htdocs]# echo  '<html><body><h1>It httpd  works!</h1></body></html>' >/usr/local/httpd/htdocs/index.html

        [root@centos7903 ~]# curl -I http://192.168.80.3:8080 &>/dev/null  && echo "httpd is ok "
        httpd is ok 

3.利用sed 取出ifconfig命令中本机的IPv4地址

        [root@centos7903 httpd]# ifconfig eth0|sed -rn '2s/.*inet ([0-9.]+) .*/\1/p'
        192.168.80.3

        root@ubuntu18045:~# ifconfig ens32 |awk  '/.*inet [0-9.]+ .*/  {print $2}' 
        192.168.80.10

4.删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行 的行首的 #和空白字符

         1.构造fstab 文件
        [root@centos7903 data00]# cat -A fstab
        $
        $
        #add  # space $
        $
        # $
        #  $
        #   $
        $
        #$
        # /etc/fstab$
        # Created by anaconda on Mon Aug 23 09:11:02 2021$
        #$
        # Accessible filesystems, by reference, are maintained under '/dev/disk'$
        # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info$
        #$
        UUID=2d44b7c9-dfd8-4b23-a4fe-e1e27c181abe /                       xfs     defaults        0 0$
        UUID=885aad5f-bad0-40d5-a459-defc44fcdd75 /boot                   ext4    defaults        1 2$
        #UUID=3f3b85df-c727-4f19-85b0-e26703d8bf06 /data                   xfs     defaults        0 0$
        UUID=aebaa5d3-c0ed-43a4-93ab-d4d57be62737 /data2                 xfs     defaults        0 0 $
        $
        $
        $
        $
        /dev/mapper/vg0-data              /data00                 ext4      defaults       0  0$
        $
        $

2.删除
        [root@centos7903 data00]# sed -ri.org    's/^(#[[:space:]]+)(.*)/\2/g' fstab

3.查看效果
        [root@centos7903 data00]# cat -A fstab
        $
        $
        #add  # space $
        $
        $
        $
        $
        $
        #$
        /etc/fstab$
        Created by anaconda on Mon Aug 23 09:11:02 2021$
        #$
        Accessible filesystems, by reference, are maintained under '/dev/disk'$
        See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info$
        #$
        UUID=2d44b7c9-dfd8-4b23-a4fe-e1e27c181abe /                       xfs     defaults        0 0$
        UUID=885aad5f-bad0-40d5-a459-defc44fcdd75 /boot                   ext4    defaults        1 2$
        #UUID=3f3b85df-c727-4f19-85b0-e26703d8bf06 /data                   xfs     defaults        0 0$
        UUID=aebaa5d3-c0ed-43a4-93ab-d4d57be62737 /data2                 xfs     defaults        0 0 $
        $
        $
        $
        $
        /dev/mapper/vg0-data              /data00                 ext4      defaults       0  0$
        $
        $

        5.处理/etc/fstab路径,使用sed命令取出其目录名和基名

        [root@centos7903 ~]# echo /etc/fstab| sed -nr 's#^(.*)/([^/]+)#\1#p'
        /etc
        [root@centos7903 ~]# echo /etc/fstab| sed -nr 's#^(.*)/([^/]+)#\2#p'
        fstab

6.列出ubuntu软件管理工具apt的一些用法(自由总结)

 Debian 使用apt 工具集来管理包系统

apt 常用子命令

    apt install  安装软件包
    apt remove   移除软件包
    apt purge    移除软件包及配置文件
    apt update   刷新存储库索引
    apt upgrade  升级所有可升级的软件包
    apt autoremove 自动删除不需要的包
    apt full-upgrade 在升级软件包时自动处理依赖关系
    apt search 搜索引用程序
    apt show 显示安装细节

    apt update  更新包的索引,相当于 yum clean all  yum makecache dnf

    注意: 每次执行软件的安装 卸载 之前都需要先执行 apt update  更新包的索引


使用示例

root@ubuntu1804:~# apt install tree zip -y  安装包

root@ubuntu1804:~# apt install ubuntu-desktop 安装图形桌面

root@ubuntu1804:~# apt remove tree zip  -y

说明: apt remove 中添加 --purge 选项会删除软件包配置文件,谨慎使用

root@ubuntu1804:~# apt update  更新包的索引,相当于 yum clean all  yum makecache dnf

root@ubuntu1804:~# apt upgrade -y  升级包 要升级系统 请首先更新软件包 索引 再升级

root@ubuntu1804:~# apt list 列出仓库软件包 等于  yum  list 

root@ubuntu1804:~# apt search nginx  搜索软件包

root@ubuntu1804:~# apt show vsftpd  查看某个软件包的详细信息

root@ubuntu1804:~# apt remove apache2  卸载软件包但是保留配置文件

root@ubuntu1804:~# apt autoremove apache2 删除安装包并解决依赖关系

root@ubuntu1804:~# apt update  更新本地软件包列表索引 修改了 apt  仓库后必须执行

root@ubuntu1804:~# apt purge apache2 -y 卸载单个软件包删除配置文件

root@ubuntu1804:~# apt upgrade 升级所有一安装且可以升级到新版本的软件包

root@ubuntu1804:~# apt full-upgrade -y  升级整个系统 必要时可以移除旧软件包

root@ubuntu1804:~# apt edit-sources  编辑 source 源文件

root@ubuntu1804:~# apt-cache madison nginx 查看仓库中软件包有哪些版本可以安装

root@ubuntu1804:~# apt install nginx=1.14.0-0ubuntu1.9 安装软件包的时候指定要安装的具体版本


#查看文件来自于哪个包,类似redhat中的yum provides <filename>
root@ubuntu1804:~# apt install apt-file

root@ubuntu1804:~# apt update

root@ubuntu18045:~# apt-file search -F /usr/sbin/nginx
nginx-core: /usr/sbin/nginx
nginx-extras: /usr/sbin/nginx
nginx-full: /usr/sbin/nginx
nginx-light: /usr/sbin/nginx


查看包相关信息

显示系统安装包的统计信息 可以统计已经安装包的数量 大小 占用空间等

root@ubuntu1804:~# apt-cache stats

显示xxx 包的信息 可以看到某个包的源 版本等信息

root@ubuntu18045:~# apt-cache show nginx


查看仓库中的指定软件的所有版本

root@ubuntu1804:~# apt-cache madison docker-ce

root@ubuntu1804:~# apt -y install  docker-ce=18.06.3~ce~3-0~ubuntu 安装指定版本的 docker-ce


ubuntu 建议安装的常用的包

root@ubuntu1804:~# apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop unzip zip