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

LinuxShell32bit 64bitProcessor

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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

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

lm ==> 64-bit processor

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

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

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

Solution 2 - Linux

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

To get your specific machine hardware name run

uname -m

You can also call

getconf LONG_BIT

which returns either 32 or 64

[1]: http://manpages.ubuntu.com/manpages/intrepid/man1/uname.html "uname"

Solution 3 - Linux

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

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

Solution 4 - Linux

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

Solution 5 - Linux

The command

$ arch    

is equivalent to

$ uname -m

but is twice as fast to type

Solution 6 - Linux

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

Solution 7 - Linux

#include <stdio.h>

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

Solution 8 - Linux

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

Solution 9 - Linux

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

Solution 10 - Linux

Nowadays, a system can be multiarch so it does not make sense anyway. You might want to find the default target of the compiler:

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

You can try to compile a 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

Solution 11 - Linux

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'!

Solution 12 - Linux

In Bash, using integer overflow:

if ((1 == 1<<32)); then
  echo 32bits
else
  echo 64bits
fi

It's much more efficient than invoking another process or opening files.

Solution 13 - Linux

You can also check using a environment variable:

echo $HOSTTYPE

Result:

i386 -> 32 bits

x86_64 -> 64 bits

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

Solution 14 - Linux

getconf uses the fewest system calls:

$ strace getconf LONG_BIT | wc -l
253

$ strace arch | wc -l
280

$ strace uname -m | wc -l
281

$ strace grep -q lm /proc/cpuinfo | wc -l
301

Solution 15 - Linux

If you shift 1 left by 32 and you get 1, your system is 32 bit. If you shift 1 left by 64 and you get 1, your system is 64 bit.

In other words,

if echo $((1<<32)) gives 1 then your system is 32 bit.

if echo $((1<<64)) gives 1 then your system is 64 bit.

Solution 16 - Linux

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.

Solution 17 - Linux

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

Solution 18 - Linux

Simple script to get 64 bit or 32 bit

        if $(getconf LONG_BIT | grep '64'); then
           echo "64 bit system"
        else
            echo "32 bit system"
        fi

Solution 19 - Linux

I can't believe that in all this time, no one has mentioned:

sudo lshw -class cpu

to get details about the speed, quantity, size and capabilities of the CPU hardware.

Solution 20 - Linux

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

Based on the fact that 64-bit is usually x86_64 and 32-bit is i686 etc.

Solution 21 - Linux

First you have to download Virtual Box. Then select new and a 32-bit Linux. Then boot the linux using it. If it boots then it is 32 bit if it doesn't then it is a 64 bit.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSwapnonil MukherjeeView Question on Stackoverflow
Solution 1 - LinuxVonCView Answer on Stackoverflow
Solution 2 - LinuxThomas WatnedalView Answer on Stackoverflow
Solution 3 - LinuxasharmaView Answer on Stackoverflow
Solution 4 - Linuxuser3207041View Answer on Stackoverflow
Solution 5 - LinuxGreg von WinckelView Answer on Stackoverflow
Solution 6 - LinuxReed HedgesView Answer on Stackoverflow
Solution 7 - LinuxscottyView Answer on Stackoverflow
Solution 8 - LinuxDenis R.View Answer on Stackoverflow
Solution 9 - LinuxLouis GerbargView Answer on Stackoverflow
Solution 10 - LinuxysdxView Answer on Stackoverflow
Solution 11 - LinuxkaiwanView Answer on Stackoverflow
Solution 12 - LinuxLuchosteinView Answer on Stackoverflow
Solution 13 - LinuxLinuxMaintwoView Answer on Stackoverflow
Solution 14 - Linuxuser5859111View Answer on Stackoverflow
Solution 15 - LinuxSandeep GiriView Answer on Stackoverflow
Solution 16 - LinuxMichael ShigorinView Answer on Stackoverflow
Solution 17 - LinuxalexView Answer on Stackoverflow
Solution 18 - LinuxLakshmikandanView Answer on Stackoverflow
Solution 19 - LinuxRolf of SaxonyView Answer on Stackoverflow
Solution 20 - Linux79manView Answer on Stackoverflow
Solution 21 - LinuxVikram ThamanView Answer on Stackoverflow