Swapnonil Mukherjee
Swapnonil Mukherjee

Reputation: 2342

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?

Upvotes: 478

Views: 525466

Answers (21)

LinuxMaintwo
LinuxMaintwo

Reputation: 1

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

Upvotes: 6

79man
79man

Reputation: 33

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

Upvotes: 2

Lakshmikandan
Lakshmikandan

Reputation: 4619

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

Upvotes: 1

VonC
VonC

Reputation: 1323045

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

Upvotes: 749

user5859111
user5859111

Reputation:

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

Upvotes: 3

Rolf of Saxony
Rolf of Saxony

Reputation: 22433

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.

Upvotes: 1

ysdx
ysdx

Reputation: 9315

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

Upvotes: 6

Sandeep Giri
Sandeep Giri

Reputation: 855

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.

Upvotes: 2

Luchostein
Luchostein

Reputation: 2444

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.

Upvotes: 4

Vikram Thaman
Vikram Thaman

Reputation: 5

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.

Upvotes: -5

Greg von Winckel
Greg von Winckel

Reputation: 2261

The command

$ arch    

is equivalent to

$ uname -m

but is twice as fast to type

Upvotes: 12

user3207041
user3207041

Reputation: 1

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

Upvotes: 33

alex
alex

Reputation: 111

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

Upvotes: 1

Michael Shigorin
Michael Shigorin

Reputation: 1051

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.

Upvotes: 1

scotty
scotty

Reputation: 1

#include <stdio.h>

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

Upvotes: 11

asharma
asharma

Reputation: 1

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

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

Upvotes: 45

Reed Hedges
Reed Hedges

Reputation: 1650

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

Upvotes: 12

Denis R.
Denis R.

Reputation: 818

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

Upvotes: 10

kaiwan
kaiwan

Reputation: 2144

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

Upvotes: 5

Louis Gerbarg
Louis Gerbarg

Reputation: 43452

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

Upvotes: 6

Thomas Watnedal
Thomas Watnedal

Reputation: 4951

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

Upvotes: 155

Related Questions