当我输入uname -a时,它会给出以下输出。

Linux mars 2.6.9-67.0.15.ELsmp #1 SMP Tue Apr 22 13:50:33 EDT 2008 i686 i686 i386 GNU/Linux

我怎么知道给定的操作系统是32位还是64位?

这在编写配置脚本时很有用,例如:我为什么体系结构构建?


当前回答

如今,一个系统可以是多拱的,所以无论如何它都没有意义。你可能想要找到编译器的默认目标:

$ cc -v 2>&1 | grep ^Target
Target: x86_64-pc-linux-gn

你可以尝试编译一个hello world:

$ echo 'int main() { return 0; }' | cc -x c - -o foo
$ file foo
foo: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=b114e029a08abfb3c98db93d3dcdb7435b5bba0c, not stripped

其他回答

另一个容易确定的有用命令如下:

命令:

getconf LONG_BIT

答:

32,如果OS为32位 如果OS为64位,则为64位

试试uname -m。它缺少uname——machine,输出:

x86_64 ==> 64-bit kernel
i686   ==> 32-bit kernel

否则,对于Linux内核,而是CPU,输入:

cat /proc/cpuinfo

or:

grep flags /proc/cpuinfo

在“flags”参数下,您将看到各种值:参见“/proc/cpuinfo中的flags是什么意思?” 其中一种是lm: Long Mode (x86-64: amd64,又称Intel 64,即64位)

lm ==> 64-bit processor

或者使用lshw(如下文由Rolf of Saxony所述),而不使用sudo(只是为了控制cpu宽度):

lshw -class cpu|grep "^       width"|uniq|awk '{print $2}'

注意:你可以安装一个64位CPU和一个32位内核。 (正如ysdx在他/她自己的回答中提到的,“现在,一个系统可以是多拱的,所以它无论如何都没有意义。你可能想要找到编译器的默认目标”)

[-z ' uname -m | grep 64 '] && echo "32-bit" || echo "64-bit"

基于64位通常是x86_64, 32位是i686等等的事实。

你也可以使用环境变量进行检查:

echo $HOSTTYPE

结果:

I386 -> 32位

X86_64 -> 64位

摘自:http://www.sysadmit.com/2016/02/linux-como-saber-si-es-32-o-64-bits.html

关于答案“getconf LONG_BIT”。

我用C语言写了一个简单的函数:

/*
 * check_os_64bit
 *
 * Returns integer:
 *   1 = it is a 64-bit OS
 *   0 = it is NOT a 64-bit OS (probably 32-bit)
 *   < 0 = failure
 *     -1 = popen failed
 *     -2 = fgets failed
 *
 * **WARNING**
 * Be CAREFUL! Just testing for a boolean return may not cut it
 * with this (trivial) implementation! (Think of when it fails,
 * returning -ve; this could be seen as non-zero & therefore true!)
 * Suggestions?
 */
static int check_os_64bit(void)
{
    FILE *fp=NULL;
    char cb64[3];

    fp = popen ("getconf LONG_BIT", "r");
    if (!fp)
       return -1;

    if (!fgets(cb64, 3, fp))
        return -2;

    if (!strncmp (cb64, "64", 3)) {
        return 1;
    }
    else {
        return 0;
    }
}

“getconf”是个好主意!