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

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?

This question is related to linux shell 32bit-64bit processor

The answer is


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


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

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

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


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.


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.


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

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

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


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

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


The command

$ arch    

is equivalent to

$ uname -m

but is twice as fast to type


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


#include <stdio.h>

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

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

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


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


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


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


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.


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.


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

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

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


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.


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


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

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.


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.


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

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

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

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.


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

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


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


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

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

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


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

The command

$ arch    

is equivalent to

$ uname -m

but is twice as fast to type


#include <stdio.h>

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

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.


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


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


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

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to 32bit-64bit

MS Access DB Engine (32-bit) with Office 64-bit Running vbscript from batch file Can't start Eclipse - Java was started but returned exit code=13 how much memory can be accessed by a 32 bit machine? CentOS 64 bit bad ELF interpreter Range of values in C Int and Long 32 - 64 bits Is it possible to install both 32bit and 64bit Java on Windows 7? How to find out if an installed Eclipse is 32 or 64 bit version? Missing include "bits/c++config.h" when cross compiling 64 bit program on 32 bit in Ubuntu Should I use Python 32bit or Python 64bit

Examples related to processor

What's the difference between a single precision and double precision floating point operation? How to determine whether a given Linux is 32 bit or 64 bit?