475

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?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Swapnonil Mukherjee
  • 2,312
  • 5
  • 22
  • 32

21 Answers21

746

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

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 4
    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
  • @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
  • 3
    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 04 '09 at 20:33
  • `grep flags /proc/cpuinfo | grep -qo ' lm\( \|$\)'` succeeds (exit code 0), but `getconf LONG_BIT` prints `32`, `uname -p` prints `i686`, and `dpkg-architecture` prints `DEB_HOST_ARCH_BITS=32`. This seems to be contradictory. Is there an authoritative source for this answer? – l0b0 Jul 26 '12 at 12:41
  • 1
    If I have a 32-bit kernel running on a 64-bit machine/ processor, what would `uname -i`, `uname -p` and `uname -m` show? – ruben2020 Mar 12 '13 at 02:15
  • 4
    what if tm and lm are both present? – Javier Novoa C. Oct 27 '13 at 16:55
  • 4
    @JavierNovoaC. tm (Thermal Monitor) indicates Automatic clock control. It has nothing to do with distinguishing a 32-bit processor. In fact, lm (long mode) is present if and only if you have a 64-bit CPU. So that's why you should only rely on lm. otherwise the answer given by Thomas Watnedal is the best. This answer is just wrong and has misled many people plz moderators do something about it. – Issam T. May 07 '14 at 10:59
  • @IssamT. good point. I have edited the answer accordingly, and added a link to http://unix.stackexchange.com/questions/43539/what-do-the-flags-in-proc-cpuinfo-mean. Your edit was a bit too extensive, and was rejected. – VonC May 07 '14 at 11:17
  • @VonC I am sorry for my aggressive answer. It's just that I am one the guys who were misled by that and wasted 2 hours lol. However, I am still not happy with the current answer as it still implies that uname -m is for the Linux kernel and what follows is for the cpu. That's maybe the reason why my edit looked for you a bit too extensive. I am not going to edit again. I let you do it in the leat extensive way ;) – Issam T. May 07 '14 at 11:38
  • @IssamT. what would you use for the kernel then? – VonC May 07 '14 at 11:47
  • This ONLY detects the kernel, not the current userspace of the system or a `chroot` nor the target of a specific `CC` variable pointing at a compiler. – user3710044 May 31 '15 at 10:28
  • `uname -m` may not be reliably used to get the type of kernel. On my machine, `uname -m` gives "x86_64" but `setarch i386 uname -m` gives "'i686". – ysdx Sep 19 '15 at 07:03
154

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

Thomas Watnedal
  • 4,903
  • 4
  • 24
  • 23
  • 4
    `uname -m` outputs `x86_64` `getconf LONG_BIT` outputs `32` Which one is correct ?? :\ – Stephan Nov 19 '11 at 21:13
  • 11
    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. –  Nov 28 '11 at 00: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) – jww Jun 17 '13 at 22:00
  • 1
    The `uname -m` is not useful for the QP's `configure` as it can give the wrong result. The `getconf LONG_BIT` get the default bit size of the C library which may not be the correct size for a specified, by `CC`, compiler. – user3710044 May 31 '15 at 10:25
  • 2
    `getconf LONG_BIT` may provide 32 ig it has been built as a 32 bit application (typically 64 bit kernel running a 32 bit userland). – ysdx Sep 19 '15 at 07:04
  • uname -m outputs x86_64 getconf LONG_BIT outputs 64 What is the meaning of that ?? :\ – – Jain Nidhi Aug 28 '18 at 13:31
45

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

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
...
Kijewski
  • 25,517
  • 12
  • 101
  • 143
asharma
  • 1
  • 2
  • 2
33

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
icedwater
  • 4,701
  • 3
  • 35
  • 50
user3207041
  • 1
  • 2
  • 2
  • 3
    Not true in the case of HP-UX 11.31i on Itanium 64 : this command returns 32. – Gabriel Hautclocq May 16 '17 at 07:27
  • I guess it all depends on what the questioner means by "64-bit" - it used to mean the natural size of integers, but it's now often used to mean the addressing size instead. – Toby Speight Aug 15 '17 at 10:19
12

The command

$ arch    

is equivalent to

$ uname -m

but is twice as fast to type

Greg von Winckel
  • 2,261
  • 2
  • 16
  • 14
11
#include <stdio.h>

int main(void)
{
    printf("%d\n", __WORDSIZE);
    return 0;
}
scotty
  • 1
  • 1
  • 2
  • 2
    Works but appears to be an implementation detail of stdio.h on Linux, better solutions exist, eg: limits.h, DO NOT USE. – user3710044 May 31 '15 at 10:19
11

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Reed Hedges
  • 1,590
  • 2
  • 15
  • 17
  • 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
  • This produces `dpkg: error: unknown option --architecture` for dpkg 1.17.5ubuntu of 14.04. dpkg-architecture (with dpkg-dev installed) works fine though. – timurb Jun 16 '14 at 18:34
  • 2
    The command `dpkg --print-architecture` has worked on Debian since forever. This one works but is limited to Debian and it's derivatives. – user3710044 May 31 '15 at 10:09
10

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Denis R.
  • 818
  • 1
  • 9
  • 15
  • 1
    This returns the process types that the kernel can support. It is possible and even reasonable to run a 32 bit userspace on a 64bit kernel. – user3710044 May 31 '15 at 10:10
  • There are other values in `uname` output that indicate 64-bit OS. Not all the world is an x86 or Itanium... – Toby Speight Aug 15 '17 at 10:21
6

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
  • 1
    This returns the process types that the kernel can support. It is possible and even reasonable to run a 32 bit userspace on a 64bit kernel. – user3710044 May 31 '15 at 10:08
6

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
ysdx
  • 8,889
  • 1
  • 38
  • 51
  • 1
    Since `cc -v | grep …` is compiler-specific anyway, one can just use `cc -dumpmachine`, which does not require grepping and is supported not only by GCC. – Anton Samsonov Jul 06 '16 at 12:25
  • Given that the question suggests it's for a configure script, this is probably the most useful and relevant answer here. It will do what you want in all the cases that matter (including a 32-bit user chroot on a 64-bit OS, cross-compiling for a foreign architecture, and the rest). – Toby Speight Aug 15 '17 at 10:23
5

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

pevik
  • 4,523
  • 3
  • 33
  • 44
LinuxMaintwo
  • 1
  • 1
  • 1
  • 1
    This is a built in variable for `/bin/bash` it is not an environment variable. If you are already dependent on Bash this works fine. However, the result can be `i386`, `i486`, `i586`, `i686` and others so be careful. – user3710044 Apr 30 '16 at 09:00
5

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

Community
  • 1
  • 1
kaiwan
  • 2,114
  • 1
  • 18
  • 23
  • 5
    Silly idea! Use `CHAR_BIT*sizeof(void*)` or `__WORDSIZE` in C. – ceving Jul 11 '12 at 10:46
  • 3
    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 08:33
  • Gets the **default** length of a long in the GNU-C library .. this one works! – user3710044 May 31 '15 at 10:06
  • 1
    It's in fact wrong, because if you're running 32 bit userspace on 64 bit kernel, or even X32 userspace, it'll say that the OS is 32 bit. – Ruslan Sep 18 '15 at 19:19
  • It is strongly suggested to not use `fgets`: http://stackoverflow.com/questions/16323185/why-is-the-fgets-function-deprecated – ceving Mar 24 '17 at 10:39
4

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.

Luchostein
  • 2,314
  • 20
  • 24
  • 2
    Bash is (can be?) compiled to use 64bit ints if that type is available, it usually is nowadays and so 32bit systems will normally use the type "long long" – user3710044 May 31 '15 at 10:03
  • 2
    bash in Debian has been compiled to use 64bit arithmetic since 2008 at the latest, probably earlier than that. This answer has been broken since before stackoverflow existed. – Peter Cordes Feb 07 '16 at 13:14
3

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

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Sandeep Giri
  • 821
  • 1
  • 7
  • 14
1

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.

Michael Shigorin
  • 982
  • 10
  • 11
1
$ grep "CONFIG_64" /lib/modules/*/build/.config
# CONFIG_64BIT is not set
alex
  • 111
  • 6
1

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
pevik
  • 4,523
  • 3
  • 33
  • 44
Lakshmikandan
  • 4,301
  • 3
  • 28
  • 37
1

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.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Totaly wrong. The question is what OS is running. I could spend my total merits on down-voting all wrong answers to this question. – Albert van der Horst Oct 18 '20 at 16:07
  • @AlbertvanderHorst You are of course, `totally` correct, but given that I did attach a large caveat, concerning `CPU hardware`, I don't believe that this answer is misleading. It simply adds a small information snippet to the subject. I notice that you do not offer an answer of your own! Precise, correct or otherwise. – Rolf of Saxony Oct 19 '20 at 10:41
1

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

pevik
  • 4,523
  • 3
  • 33
  • 44
79man
  • 31
  • 5
-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.

  • 2
    This is a really far-fetched way to determine whether the system is 32 or 64 bit. – marlar Mar 20 '16 at 14:16
  • 1
    But it is truly a way to solve this problem. So I will mark it up. – firo Jun 17 '16 at 03:20
  • This reminds me of a way to tell even numbers from odd: a mathematition would look at the remainder after dividing it by two; a programmer would look at the least significant bit; a MS SQL specialist would create two tables, one for even numbers and one for the odd, and look where the input ends up... *figures* – Michael Shigorin Nov 14 '18 at 17:18