分享

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

 迎风初开 2014-01-22

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

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?

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

share|improve this question

add comment

11 Answers

up vote 277 down vote accepted

Try uname -m. It seems like the uname -m actually gives

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

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

cat /proc/cpuinfo

or:

grep flags /proc/cpuinfo

Under "flags" parameter, you will see various values.
Among them, one is named tm(transparent mode) or rm(real mode) or lm(long mode)

rm ==> 16-bit processor
tm ==> 32-bit processor
lm ==> 64-bit processor

Note: you can have a 64-bit CPU with a 32-bit kernel installed

share|improve this answer

1  
grep flags /proc/cpuinfo only tells you wether the CPU is 64bit. As I understand the question it was about the OS. uname -m only tells me "i686". –  Kim Stebel Aug 23 '09 at 16:40

 
I run a 32bit kernel. –  Kim Stebel Aug 23 '09 at 16:41

 
@Kim: true. I have update my answer to adequately reflect the difference between the kernel and the CPU –  VonC Aug 25 '09 at 14:12
2  
I have a 32 bit kernel on 64 bit hardware and get "x86_64" from 'uname -m' (on Debian). The man page for uname says that -m shows the machine hardware name, so that seems correct. –  Tony Meyer Sep 4 '09 at 20:33
1  
what if tm and lm are both present? –  Javier Novoa C. Oct 27 '13 at 16:55
show 3 more comments

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

To get your specific machine hardware name run

uname -m

You can also call

getconf LONG_BIT

which returns either 32 or 64

share|improve this answer

11  
That getconf LONG_BIT is especially convenient, thanks! –  chronos Mar 26 '10 at 22:59
2  
uname -m outputs x86_64 getconf LONG_BIT outputs 32 Which one is correct ?? :\ –  Alex Nov 19 '11 at 21:13
5  
That means the CPU is 64-bit, but you've only installed a 32-bit operating system upon it, even though you could have used a 64-bit one. –  Steve Kemp Nov 28 '11 at 0:17
1  
Steve Kemp is right, so be careful (Mac OS X 10.5 on 2009 MacBooks comes to mind, where the OS is 32-bit but its capable of running 64-bit apps) –  noloader Jun 17 '13 at 22:00
add comment

lscpu will list out these among other information regarding your CPU:

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
...
share|improve this answer

add comment

If you have a 64-bit OS, instead of i686, you have x86_64 or ia64 in 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).

share|improve this answer

add comment

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

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
share|improve this answer

add comment
#include <stdio.h>

int main(void)
{
    printf("%d\n", __WORDSIZE);
    return 0;
}
share|improve this answer


 
Why -1? It works on Linux. Made it 0. –  kaiwan Oct 16 '12 at 10:35
add comment

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 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:

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.

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).

share|improve this answer


 
You can just use: dpkg --architecture to get the host system architecture, which doesn't require the dpkg-dev package to be installed. –  Mark Longair Feb 16 '12 at 11:36
add comment

With respect to the answer "getconf LONG_BIT".

I wrote a simple function to do it in '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'!

share|improve this answer

4  
Silly idea! Use CHAR_BIT*sizeof(void*) or __WORDSIZE in C. –  ceving Jul 11 '12 at 10:46
1  
No it is not silly. You may have a 32-bit executable and you want to figure out if the system would support a 64-bit one, for example. –  Ludvig A Norin Nov 17 '13 at 8:33
add comment

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

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

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

share|improve this answer

add comment
$ grep "CONFIG_64" /lib/modules/*/build/.config
# CONFIG_64BIT is not set
share|improve this answer

add comment

Another Useful command for easy determination is as below:

Command: getconf LONG_BIT

Answer: - 32, if OS is 32 bit - 64, if OS is 64 bit

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多