linux基础之仓库、软件包管理、sed常见实战用法

1、自建yum仓库

1.1 网络源

#写个脚本配置centos6、7、8
[root@centos-8 ~]#cat yum.sh 
#!/bin/bash
#
#********************************************************************
#Author:                jiangshen
#QQ:                    1461918614
#Date:                  2021-07-27
#FileName:             yum.sh
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
version=`sed -nr ‘s/^.* ([0-9]+)\..*/\1/p‘ /etc/redhat-release`
        if [ $version -eq 7 -o $version -eq 6 ];then  
                rm -f /etc/yum.repos.d/* > /dev/null
cat << EOF > /etc/yum.repos.d/CentOS-net.repo
[base]
name=CentOS-\$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
enable=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/RPM-GPG-KEY-CentOS-\$releasever
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/x86_64/
enable=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever

EOF
        elif [ $version -eq 8 ];then
                rm -f /etc/yum.repos.d/* > /dev/null
cat << EOF > /etc/yum.repos.d/CentOS-net.repo
[base]
name=CentOS-\$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[App]
name=CentOS-\$releasever - App
baseurl=http://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
enable=1
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
enable=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8
EOF
        else
                echo 此脚本仅支持centos6,7,8三个版本
        fi
        yum repolist

[root@centos-8 ~]#bash yum.sh
repo id                                                        repo name
App                                                            CentOS-8 - App
base                                                           CentOS-8 - Base
epel                                                           epel

[root@centos-8 ~]#cd /etc/yum.repos.d/
[root@centos-8 yum.repos.d]#ls
CentOS-net.rep
[root@centos-8 yum.repos.d]#cat CentOS-net.repo 
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[App]
name=CentOS-$releasever - App
baseurl=http://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
enable=1
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
enable=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8

1.2 本地源

#做个centos7的光盘源
[root@centos-7 ~]#cat yum_iso.sh
#!/bin/bash
#
#********************************************************************
#Author:                jiangshen
#QQ:                    1461918614
#Date:                  2021-07-27
#FileName:             yum_iso.sh
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
                if `lsblk|grep ‘sr0‘ &> /dev/null`; then

         mkdir /jiangshen_iso &> /dev/null
         mount /dev/sr0 /jiangshen_iso &> /dev/null
         [ -f /etc/yum.repos.d/jiangshen.repo ] && rm -f /etc/yum.repos.d/* > /dev/null
                [ -f /etc/yum.repos.d/jiangshen_iso.repo ] || rm -f /etc/yum.repos.d/* > /dev/null
cat << EOF > /etc/yum.repos.d/jiangshen_iso.repo
[base_iso]
name=CentOS-iso
baseurl=file:///jiangshen_iso
enable=1
gpgcheck=0
EOF
        yum repolist
else
        echo "请连接光盘"
fi

[root@centos-7 ~]#bash yum_iso.sh 
repo id                                                           repo name
base_iso                                                          CentOS-iso

[root@centos-7 ~]#cd /etc/yum.repos.d/
[root@centos-7 yum.repos.d]#ls
jiangshen_iso.repo
[root@centos-7 yum.repos.d]#cat jiangshen_iso.repo 
[base_iso]
name=CentOS-iso
baseurl=file:///jiangshen_iso
enable=1
gpgcheck=0

2、编译安装http2.4

#安装前准备:关闭防火墙和SELinux
#1 安装相关包
[root@centos-8 ~]#dnf install gcc make autoconf apr-devel apr-util-devel pcredevel openssl-devel redhat-rpm-config -y

#2 下载并解压缩包
[root@centos-8 ~]#wget https://mirror.bit.edu.cn/apache//httpd/httpd2.4.46.tar.bz2
[root@centos-8 ~]#tar xvf httpd-2.4.46.tar.bz2 -C /usr/local/src

#3 配置
[root@centos-8 ~]#cd /usr/local/src/httpd-2.4.46/
[root@centos-8 httpd-2.4.46]#./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl

#报错,装apr,缺什么装什么
[root@centos-8 httpd-2.4.46]#./configure --prefix=/apps/httpd --sysconfdir=/etc/httpd --enable-ssl
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
configure: 
configure: Configuring Apache Portable Runtime library...
configure: 
checking for APR... no
configure: error: APR not found.  Please read the documentation.

#报错,下面报了好多个错,基本都是缺什么装什么,自己网上找
checking for APR-util... no
configure: error: APR-util not found.  Please read the documentation.

#报错
/usr/lib/rpm/redhat/redhat-hardened-cc1:No such file or directory
[root@centos-8 httpd-2.4.46]#dnf install redhat-rpm-config -y

#4 编译并安装
[root@centos-8 httpd-2.4.46]#make -j 2 && make install
#确认编译完成
[root@centos-8 httpd-2.4.46]#echo $?
0

#5 配置环境
[root@centos-8 ~]#echo ‘PATH=/apps/httpd/bin:$PATH‘ > /etc/profile.d/httpd.sh
[root@centos-8 ~]#. /etc/profile.d/httpd.sh

#6 运行
[root@centos-8 ~]#apachectl start

#7 指定用apache用户运行
[root@centos-8 ~]#useradd -r -s /sbin/nologin -d /var/www -c Apache -u 48 apache
User apache
Group apache
或者
[root@centos-8 ~]#sed -i.bak ‘s#^User.*#User apache#g‘ /etc/httpd/httpd.conf

#8 配置生效和验证
[root@centos-8 ~]#apachectl restart

#9 查看进程
[root@centos-8 ~]#ps aux|grep httpd

#10 用浏览器打开以下地址,可以看下面页面

linux基础之仓库、软件包管理、sed常见实战用法

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

#第一种方法
[root@centos-8 ~]#ifconfig eth0|sed -nr "2s/[^0-9]+([0-9.]+).*/\1/p"
10.0.0.150
#第二种方法
[root@centos-8 ~]#ifconfig eth0 |sed -En "2s/^[^0-9]+([0-9.]{7,15}).*/\1/p"
10.0.0.150
#第三种方法
[root@centos-8 ~]#ifconfig eth0 |sed -rn "2s/^[^0-9]+([0-9.]+) .*$/\1/p"
10.0.0.150
#第四种方法
[root@centos-8 ~]#ifconfig eth0 |sed -n "2s/^.*inet //p"|sed -n "s/netmask.*//p"
10.0.0.150
#第五种方法
[root@centos-8 ~]#ifconfig eth0 | sed -n ‘2s/^.*inet //; s/ netmask.*//p‘
10.0.0.150
#第六种方法
[root@centos-8 ~]#ifconfig eth0 | sed -rn ‘2s/(.*inet )([0-9].*)( netmask.*)/\2/p‘ 
10.0.0.150

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

[root@centos-8 ~]#sed -ri ‘s/^#\ ?(.*)/\1/‘ /etc/fstab |sed ‘/^$/d‘

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

#取出目录名
[root@centos-8 ~]#echo "/etc/fstab"|sed -rn ‘s#(.*/)[^/]+$#\1#p‘
/etc/
#取出基名
[root@centos-8 ~]#echo "/etc/fstab"|sed -rn ‘s#.*/([^/]+$)#\1#p‘
fstab

6、ubuntu软件管理工具apt的一些用法

6.1 dpkg

#安装包
dpkg -i package.deb 

#删除包,不建议,不自动卸载依赖于它的包
dpkg -r package

#删除包(包括配置文件)
dpkg -P package 

#列出当前已安装的包,类似rpm -qa
dpkg -l

#显示该包的简要说明
dpkg -l package

#列出该包的状态,包括详细信息,类似rpm –qi
dpkg -s package

#列出该包中所包含的文件,类似rpm –ql 
dpkg -L package

#搜索包含pattern的包,类似rpm –qf 
dpkg -S <pattern> 

#配置包,-a 使用,配置所有没有配置的软件包
dpkg --configure package 

#列出 deb 包的内容,类似rpm –qpl
dpkg -c package.deb

#解开 deb 包的内容
dpkg --unpack package.deb 

注意:一般建议不要使用dpkg卸载软件包。因为删除包时,其它依赖它的包不会卸载,可能导致无法正常运行

6.2 apt

#安装软件包
apt install

#移除软件包
apt remove

#移除软件包及配置文件
apt purge

#刷新存储库索引
apt update

#升级所有可升级的软件包
apt upgrade

#自动删除不需要的包
apt autoremove  

#在升级软件包时自动处理依赖关系
apt full-upgrade

#搜索应用程序
apt search

#显示安装细节
apt show

可以修改文件为国内的安装源,提高速度
范例: 修改阿里云的APT源为清华源

jiangmin@jiangmin:~$ sed -i ‘s/mirrors.aliyun.com/mirrors.tuna.tsinghua.edu.cn/‘ /etc/apt/sources.list

apt范例:

#安装包:
apt install tree zip
#安装图形桌面
apt install ubuntu-desktop

#删除包:
apt remove tree zip
#说明:apt remove中添加--purge选项会删除包配置文件,谨慎使用
#更新包索引,相当于yum clean all;yum makecache
apt update 
#升级包:要升级系统,请首先更新软件包索引,再升级
apt upgrade

#apt列出仓库软件包,等于yum list
apt list
#搜索安装包
apt search nginx
#查看某个安装包的详细信息
apt show apache2 

#在线安装软件包
apt install apache2

#卸载单个软件包但是保留配置?件
apt remove apache2 

#删除安装包并解决依赖关系
apt autoremove apache2 

#更新本地软件包列表索引,修改了apt仓库后必须执?
apt update

#卸载单个软件包删除配置?件
apt purge apache2 

#升级所有已安装且可升级到新版本的软件包
apt upgrade

#升级整个系统,必要时可以移除旧软件包。
apt full-upgrade

#编辑source源?件
apt edit-sources

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

#安装软件包的时候指定安装具体的版本
apt install nginx=1.14.0-0ubuntu1.6

#查看文件来自于哪个包,类似redhat中的yum provides <filename>
apt-file search ‘string‘ ?#默认是包含此字符串的文件
apt-file search -x ?‘正则表达式‘
apt-file search -F /path/file

ubuntu建议安装的包

apt purge ufw lxd lxd-client lxcfs liblxc-common
apt install iproute2 ntpdate tcpdump telnet traceroute nfskernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc openssh-server iotop unzip zip
上一篇:Linux LVM逻辑卷


下一篇:Windows XP with SP3大客户免激活日文版