LoginSignup
99
84

More than 5 years have passed since last update.

Linuxのディストリビューションを判別する

Last updated at Posted at 2014-06-14

最近、Linux の dotfile を Github で管理しているのですが、
ディストリビューションごとに設定を変えるのが面倒ですので、自動判定方法を調査しました。

dotfile や vim 用の設定方法は、また別の記事で書こうかと思います。

Linuxのディストリビューションを判定する

基本的には /etc/[ディストリビューション名]-release というファイルがそれぞれ存在しているはずなので、ファイルの存在チェックをすればよいみたいです

/etc/fedora-release
Fedora release 20 (Heisenbug)

Arch Linux以外はおそらく大丈夫かと思われます。
DebianとかUbuntuの区切りがアンダーバーだったりしますのでちょっと注意。

あと Ubuntu は /etc/lsb-releaseというファイルでさらに判別が必要

/etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04 LTS"

参考:

[Linux]インストールした Linux ディストリビューション名とバージョンを確認するには

OSのbitの判定

OSが32bitか64bitかはunameで取得できます

bitの判定
uname -m

結果は以下になります

実行結果
#32bit 
i686
#64bit
x86_64

参考:

Linuxが32ビット版か64ビット版かの確認Add Star

作成した shell script

一応汎用的に使えるように、scriptも作成しました
いっぱい列挙してありますが、fedoraとubuntuしか動作確認してません。

get_os_info.sh
#!/bin/bash

# Copyright (c) 2016 Kohei Arao
# https://github.com/koara-local/dotfiles/edit/master/bin/get_os_info
# Released under the Unlicense
# http://unlicense.org/

# Get OS bit
# 32bit : i686
# 64bit : x86_64
get_os_bit() {
    echo $(uname -m);
}

# Get Linux distribution name
get_os_distribution() {
    if   [ -e /etc/debian_version ] ||
         [ -e /etc/debian_release ]; then
        # Check Ubuntu or Debian
        if [ -e /etc/lsb-release ]; then
            # Ubuntu
            distri_name="ubuntu"
        else
            # Debian
            distri_name="debian"
        fi
    elif [ -e /etc/fedora-release ]; then
        # Fedra
        distri_name="fedora"
    elif [ -e /etc/redhat-release ]; then
        if [ -e /etc/oracle-release ]; then
            # Oracle Linux
            distri_name="oracle"
        else
            # Red Hat Enterprise Linux
            distri_name="redhat"
        fi
    elif [ -e /etc/arch-release ]; then
        # Arch Linux
        distri_name="arch"
    elif [ -e /etc/turbolinux-release ]; then
        # Turbolinux
        distri_name="turbol"
    elif [ -e /etc/SuSE-release ]; then
        # SuSE Linux
        distri_name="suse"
    elif [ -e /etc/mandriva-release ]; then
        # Mandriva Linux
        distri_name="mandriva"
    elif [ -e /etc/vine-release ]; then
        # Vine Linux
        distri_name="vine"
    elif [ -e /etc/gentoo-release ]; then
        # Gentoo Linux
        distri_name="gentoo"
    else
        # Other
        distri_name="unkown"
    fi

    echo ${distri_name}
}

# Get distribution and bit
get_os_info() {
   echo $(get_os_distribution) $(get_os_bit)
}

get_os_info
get_os_info使用例
#!/bin/bash -eu

declare -a info=($(./bin/get_os_info))

case ${info[0]} in
ubuntu)
    echo "ubuntu"

    if [[ ${info[1]} == "x86_64" ]]; then
        echo x86_64
    fi
    ;;
*)
    echo "unsupported"
    ;;
esac

参考:

Linuxのディストリビューションとバージョンの確認方法をまとめてみた
シェルスクリプト Tips
bash 配列まとめ - Qiita

99
84
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
99
84