如何确定给定的 Linux 是 32 位还是 64 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:40:27  来源:igfitidea点击:

How to determine whether a given Linux is 32 bit or 64 bit?

linuxshell32bit-64bitprocessor

提问by Swapnonil Mukherjee

When I type uname -a, it gives the following output.

当我输入时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

How can I know from this that the given OS is 32 or 64 bit?

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

This is useful when writing configurescripts, for example: what architecture am I building for?

这在编写configure脚本时很有用,例如:我要为什么架构构建?

采纳答案by VonC

Try uname -m. Which is short of uname --machineand it outputs:

试试uname -m。这是短的uname --machine,它输出:

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


Otherwise, not for the Linux kernel, but for the CPU, you type:

否则,不是针对 Linux 内核,而是针对 CPU键入:

cat /proc/cpuinfo

or:

或者:

grep flags /proc/cpuinfo

Under "flags" parameter, you will see various values: see "What do the flags in /proc/cpuinfo mean?" Among them, one is named lm: Long Mode(x86-64: amd64, also known as Intel 64, i.e. 64-bit capable)

在“标志”参数,你会看到不同的值:见“什么做标志在/ proc / cpuinfo中的意思是?”其中一个被命名为lmLong ModeX86-64:AMD64,又称英特尔64位,即64位有能力的)

lm ==> 64-bit processor

Or using lshw(as mentioned belowby Rolf of Saxony), without sudo(just for grepping the cpu width):

或者使用lshw(如所提到的下面通过萨克森罗尔夫),无sudo(只是为了grepping cpu的宽度):

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

Note: you can have a 64-bit CPU with a 32-bit kernel installed.
(as ysdxmentions in his/her own answer, "Nowadays, a system can be multiarchso it does not make sense anyway. You might want to find the default target of the compiler")

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

回答by Thomas Watnedal

If you were running a 64 bit platform you would see x86_64 or something very similar in the output from uname-a

如果您运行的是 64 位平台,您会在uname-a的输出中看到 x86_64 或非常相似的内容

To get your specific machine hardware name run

运行您的特定机器硬件名称

uname -m

You can also call

你也可以打电话

getconf LONG_BIT

which returns either 32 or 64

返回 32 或 64

回答by Louis Gerbarg

That system is 32bit. iX86 in unamemeans it is a 32-bit architecture. If it was 64 bit, it would return

那个系统是32位的。iX86 inuname意味着它是一个 32 位架构。如果是 64 位,它将返回

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

回答by Denis R.

If you have a 64-bit OS, instead of i686, you have x86_64or ia64in the output of uname -a. In that you do not have any of these two strings; you have a 32-bit OS (note that this does not mean that your CPU is not 64-bit).

如果你有一个64位操作系统,而不是i686的,你x86_64还是ia64在的输出uname -a。因为您没有这两个字符串中的任何一个;您有 32 位操作系统(请注意,这并不意味着您的 CPU 不是 64 位)。

回答by kaiwan

With respect to the answer "getconf LONG_BIT".

关于答案 "getconf LONG_BIT"

I wrote a simple function to do it in 'C':

我写了一个简单的函数来用'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;
    }
}

Good idea, the 'getconf'!

好主意,'getconf'!

回答by Reed Hedges

I was wondering about this specifically for building software in Debian(the installed Debian system can be a 32-bit version with a 32 bit kernel, libraries, etc., or it can be a 64-bit version with stuff compiled for the 64-bit rather than 32-bit compatibility mode).

我想知道这个专门用于在Debian 中构建软件(安装的 Debian 系统可以是带有 32 位内核、库等的 32 位版本,也可以是带有为 64 位编译的东西的 64 位版本)位而不是 32 位兼容模式)。

Debian packages themselves need to know what architecture they are for (of course) when they actually create the package with all of its metadata, including platform architecture, so there is a packaging tool that outputs it for other packaging tools and scripts to use, called dpkg-architecture. It includes both what it's configured to build for, as well as the current host. (Normally these are the same though.) Example output on a 64-bit machine:

Debian 软件包本身需要知道它们是用于什么架构的(当然),当他们实际创建带有所有元数据(包括平台架构)的包时,因此有一个打包工具可以输出它以供其他打包工具和脚本使用,称为dpkg 架构。它包括它配置为构建的内容以及当前主机。(通常这些是相同的。) 64 位机器上的示例输出:

DEB_BUILD_ARCH=amd64
DEB_BUILD_ARCH_OS=linux
DEB_BUILD_ARCH_CPU=amd64
DEB_BUILD_GNU_CPU=x86_64
DEB_BUILD_GNU_SYSTEM=linux-gnu
DEB_BUILD_GNU_TYPE=x86_64-linux-gnu
DEB_HOST_ARCH=amd64
DEB_HOST_ARCH_OS=linux
DEB_HOST_ARCH_CPU=amd64
DEB_HOST_GNU_CPU=x86_64
DEB_HOST_GNU_SYSTEM=linux-gnu
DEB_HOST_GNU_TYPE=x86_64-linux-gnu

You can print just one of those variables or do a test against their values with command line options to dpkg-architecture.

您可以仅打印这些变量之一,或者使用dpkg-architecture 的命令行选项对它们的值进行测试。

I have no idea how dpkg-architecture deduces the architecture, but you could look at its documentation or source code (dpkg-architecture and much of the dpkg system in general are Perl).

我不知道 dpkg-architecture 如何推断架构,但您可以查看其文档或源代码(dpkg-architecture 和大部分 dpkg 系统一般都是 Perl)。

回答by asharma

lscpuwill list out these among other information regarding your CPU:

lscpu将列出这些以及有关您的 CPU 的其他信息:

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
...

回答by scotty

#include <stdio.h>

int main(void)
{
    printf("%d\n", __WORDSIZE);
    return 0;
}

回答by Michael Shigorin

If one is severely limited in available binaries (e.g. in initramfs), my colleagues suggested:

如果一个在可用二进制文件中受到严重限制(例如在 initramfs 中),我的同事建议:

$ ls -l /lib*/ld-linux*.so.2

On my ALT Linux systems, i586 has /lib/ld-linux.so.2and x86_64 has /lib64/ld-linux-x86-64.so.2.

在我的 ALT Linux 系统上,i586/lib/ld-linux.so.2和 x86_64 有/lib64/ld-linux-x86-64.so.2.

回答by alex

$ grep "CONFIG_64" /lib/modules/*/build/.config
# CONFIG_64BIT is not set