如何判断Linux系统的具体发行版本信息


=Start=

缘由:

不同的Linux发行版有不同的「包管理工具」,所以有时同一个软件对应的安装包名称和安装命令也都不一样(甚至同一发行版的不同版本之间也会有所不同),如果需要写类似于 LNMP 等兼容各Linux系统的一键安装工具时,是需要提前判断Linux系统的具体发行版信息的,然后根据对应的版本信息进行操作。这里我根据以往的一些经验记录一下常见Linux系统发行版的判断方法,作为备忘和参考。

正文:

准确判断Linux系统的发行版需要对LFS和各Linux发行版足够熟悉才行,下面是我碰到的一些常用的Linux发行版及对应包管理工具:

  • Fedora / RedHat / CentOS
    $ yum/rpm
  • Gentoo
    $ emerge
  • Debian / Ubuntu
    $ apt-get/dpkg
  • Arch
    $ pacman

根据自己之前的一些经验,一般情况下可以根据如下3个文件来判断Linux系统的具体发行版本信息

  • /etc/*release
  • /etc/*version
  • /etc/*issue*
一个简单的Linux系统发行版本检测脚本:
function check_os_type()
{
    local line

    line=`head -n 1 /etc/issue`
    if echo $line|grep "[Cc]ent[Oo][Ss]" >/dev/null; then
        br_os_type=1
    elif echo $line|grep "[Rr]ed.Hat.Enterprise" >/dev/null; then
        br_os_type=2
    elif echo $line|grep "[Uu]buntu" >/dev/null; then
        br_os_type=3
    elif echo $line|grep "[Dd]ebian" >/dev/null; then
        br_os_type=4
    elif echo $line|grep "[Ff]edora" >/dev/null; then
        br_os_type=5
    else
        echo -e "unknown os type - $line is not supported."
        exit 0
    fi

    echo -e "target os type - $line"
    echo $br_os_type
}
一个较为通用的Linux系统版本信息检测脚本:
#!/bin/bash
# Detects which OS and if it is Linux then it will detect which Linux Distribution.
# https://www.novell.com/coolsolutions/feature/11251.html

OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`

if [ "${OS}" = "SunOS" ] ; then
    OS=Solaris
    ARCH=`uname -p`
    OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" = "AIX" ] ; then
    OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" = "Linux" ] ; then
    KERNEL=`uname -r`
    if [ -f /etc/redhat-release ] ; then
        DIST='RedHat'
        PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
        REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
    elif [ -f /etc/SUSE-release ] ; then
        DIST=`cat /etc/SUSE-release | tr "\n" ' '| sed s/VERSION.*//`
        REV=`cat /etc/SUSE-release | tr "\n" ' ' | sed s/.*=\ //`
    elif [ -f /etc/mandrake-release ] ; then
        DIST='Mandrake'
        PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
        REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
    elif [ -f /etc/debian_version ] ; then
        DIST="Debian `cat /etc/debian_version`"
        REV=""
    fi

    if [ -f /etc/UnitedLinux-release ] ; then
        DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
    fi

    OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
fi

echo ${OSSTR}
参考链接:

=END=


《 “如何判断Linux系统的具体发行版本信息” 》 有 5 条评论

  1. Rocky Linux简单操作
    https://mp.weixin.qq.com/s/jsJnle4TgD0CJOVE9KIkbA
    `
    Rocky Linux 其实就是centos8的更稳定版本:centos8 测试版 –> rhel8 –> Rocky8

    CentOS 8 新特性
    * DNF 成为了默认的软件包管理器,同时 yum 仍然是可用的(yum 是 dnf 的链接)
    * 使用网络管理器(nmcli 和 nmtui)进行网络配置,移除了网络脚本
    * 使用 Podman 进行容器管理
    * 引入了两个新的包仓库:BaseOS 和 AppStream
    * 使用 Cockpit 作为默认的系统管理工具
    * 默认使用 Wayland 作为显示服务器
    * iptables 将被 nftables 取代
    * 使用 Linux 内核 4.18
    * 动态编程语言、Web 和数据库服务器
    * Python 3.6 是默认的 Python 环境,支持 Python 2.7
    * Node.js 是在 RHEL 最新包含的,其他动态语言更新包括: PHP 7.2 , Ruby 2.5 , Perl 5.26 , SWIG 3.0
    * RHEL 8 提供的数据库服务包括:MariaDB 10.3 , MySQL 8.0 , PostgreSQL 10 , PostgreSQL 9.6 , 和 Redis 5
    * RHEL 8 提供 Apache HTTP Server 2.4 以及首次引入的, nginx 1.14
    * Squid 版本升级到 4.4 ,同时也首次提供 Varnish Cache 6.0

    ==

    nmcli c reload #重新加载网络配置
    ping http://www.baidu.com #测试网络是否正常
    ip addr #查看 IP 地址

    # 重启网络
    systemctl restart NetworkManager.service
    nmcli networking off && nmcli networking on

    nmcli n #查看 nmcli 状态
    nmcli n on #启动 nmcli
    nmcli c up eth0 #启动网卡 eth0
    nmcli c down eth0 #关闭网卡 eth0
    nmcli d c eth0 #激活网卡
    nmcli d show eth0 #查看网卡 eth0 信息
    nmcli r all off #关闭无线

    # 安装一些必备工具

    # 一些基本工具最小化安装可能没有 (经测 vim 和 wget 等没有安装)
    yum -y install vim wget zip unzip

    # gcc 等选择开发工具(上述安装过程建议勾选了”Development Tools”)
    dnf groupinstall “Development Tools”

    # Rocky Linux 默认没有 tar 命令,AlmaLinux 也没有,但是 CentOS 默认有
    yum -y install tar

    # 以下网络工具已经不存在
    # yum -y install setuptool system-config-network-tui system-config-firewall-tui

    # 默认安装没有 ifconfig 命令,安装 net-tools
    # ifconfig、netstat、route 等命令集
    yum -y install net-tools

    # host、dig 和 nslookup
    yum -y install bind-utils

    # 更好的 top 工具
    yum -y install htop
    #Anolis OS 中没有
    #https://github.com/hishamhm/htop

    # 文件传输:sz 和 rz
    yum -y install lrzsz

    # 查看日志神器 Log file Navigator
    yum -y install lnav
    # 该软件现在已经包含在 EPEL 中
    # 初期 EPEL 中没有,直接在线安装
    #rpm -ivh https://github.com/tstack/lnav/releases/download/v0.9.0/lnav-0.9.0-1.x86_64.rpm
    #Anolis OS 中没有

    # NTP 已经废弃,改用 chrony,默认自带
    # yum -y install ntp

    # nc:
    yum -y install nc

    # lsof:
    yum -y install lsof

    # tree:
    yum -y install tree

    # pstree:
    yum -y install psmisc

    # ncdu:NCurses Disk Usage
    yum -y install ncdu #新增工具,模板尚未加入,下个版本更新

    # dstat 监控 CPU、磁盘和网络使用率,下个版本加入
    yum -y install dstat

    # 更新软件包
    dnf clean all
    dnf mackecache #缓存软件包信息
    dnf upgrade #更新软件包
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注