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

21

476

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?

Mullein answered 29/10, 2008 at 6:59 Comment(0)
T
748

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

Tsarina answered 29/10, 2008 at 7:6 Comment(12)
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".Rigby
@Kim: true. I have update my answer to adequately reflect the difference between the kernel and the CPUTsarina
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.Arizona
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?Humoresque
If I have a 32-bit kernel running on a 64-bit machine/ processor, what would uname -i, uname -p and uname -m show?Vardhamana
what if tm and lm are both present?Breuer
@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.Sedulous
@IssamT. good point. I have edited the answer accordingly, and added a link to unix.stackexchange.com/questions/43539/…. Your edit was a bit too extensive, and was rejected.Tsarina
@Tsarina 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 ;)Sedulous
@IssamT. what would you use for the kernel then?Tsarina
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.Maziar
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".Nesta
R
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

Refugiorefulgence answered 29/10, 2008 at 6:59 Comment(6)
uname -m outputs x86_64 getconf LONG_BIT outputs 32 Which one is correct ?? :\Faires
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.Cheapjack
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)Fourpence
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.Maziar
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).Nesta
uname -m outputs x86_64 getconf LONG_BIT outputs 64 What is the meaning of that ?? :\ –Backsight
R
45

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

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
...
Rhett answered 29/10, 2008 at 6:59 Comment(2)
Outputs the physical capabilities of the CPU, useful, but not reliable for the current userspace of the current OS.Maziar
Totaly wrong. The question is what OS is running. 42 upvotes? I would spend a down vote but it would drown.Himation
M
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
Michail answered 29/10, 2008 at 6:59 Comment(2)
Not true in the case of HP-UX 11.31i on Itanium 64 : this command returns 32.Impulsion
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.Miler
D
12

The command

$ arch    

is equivalent to

$ uname -m

but is twice as fast to type

Despiteful answered 29/10, 2008 at 6:59 Comment(3)
Agreed, but I am sure the typing speed is not an issue for most developers.Gyro
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.Maziar
The arch command is not available on every unix system, while uname should be.Impulsion
M
11
#include <stdio.h>

int main(void)
{
    printf("%d\n", __WORDSIZE);
    return 0;
}
Mismanage answered 29/10, 2008 at 6:59 Comment(1)
Works but appears to be an implementation detail of stdio.h on Linux, better solutions exist, eg: limits.h, DO NOT USE.Maziar
F
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).

Further answered 29/10, 2008 at 6:59 Comment(3)
You can just use: dpkg --architecture to get the host system architecture, which doesn't require the dpkg-dev package to be installed.Rabid
This produces dpkg: error: unknown option --architecture for dpkg 1.17.5ubuntu of 14.04. dpkg-architecture (with dpkg-dev installed) works fine though.Semple
The command dpkg --print-architecture has worked on Debian since forever. This one works but is limited to Debian and it's derivatives.Maziar
P
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).

Parimutuel answered 29/10, 2008 at 6:59 Comment(2)
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.Maziar
There are other values in uname output that indicate 64-bit OS. Not all the world is an x86 or Itanium...Miler
N
6

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

Nonesuch answered 29/10, 2008 at 6:59 Comment(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.Maziar
N
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
Nesta answered 29/10, 2008 at 6:59 Comment(2)
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.Stefaniastefanie
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).Miler
O
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
Overmatter answered 29/10, 2008 at 6:59 Comment(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.Maziar
J
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'!

Jarboe answered 29/10, 2008 at 6:59 Comment(5)
Silly idea! Use CHAR_BIT*sizeof(void*) or __WORDSIZE in C.Chart
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.Nahshon
Gets the default length of a long in the GNU-C library .. this one works!Maziar
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.Goulette
It is strongly suggested to not use fgets: #16323685Chart
C
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.

Chalaza answered 29/10, 2008 at 6:59 Comment(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"Maziar
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.Suzannesuzerain
A
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
Anya answered 29/10, 2008 at 6:59 Comment(0)
R
2

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

Rahman answered 29/10, 2008 at 6:59 Comment(0)
K
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.

Kunstlied answered 29/10, 2008 at 6:59 Comment(2)
Same problem with bash using "long longs".Maziar
This worked for me, because if it returns 32 bit then it's 100% confirmed running 32. If it returns 64 may be bash was compiled with long longs.Favors
N
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.

Nutgall answered 29/10, 2008 at 6:59 Comment(2)
Totaly wrong. The question is what OS is running. I could spend my total merits on down-voting all wrong answers to this question.Himation
@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.Nutgall
C
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
Cephalic answered 29/10, 2008 at 6:59 Comment(0)
F
1
$ grep "CONFIG_64" /lib/modules/*/build/.config
# CONFIG_64BIT is not set
Faintheart answered 29/10, 2008 at 6:59 Comment(1)
I got two lines, one with it set one without.Maziar
A
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.

Amelioration answered 29/10, 2008 at 6:59 Comment(2)
I got THREE of those, one for 32 one for 64 and one for mx32.Maziar
Yeah, if it doesn't return tons of 64-bit libraries, then it's running 32 bit OS: ls -aR | grep 64 | grep -v procFavors
N
-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.

Nevillenevin answered 29/10, 2008 at 6:59 Comment(3)
This is a really far-fetched way to determine whether the system is 32 or 64 bit.Algid
But it is truly a way to solve this problem. So I will mark it up.Haulage
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... figuresAmelioration

© 2022 - 2024 — McMap. All rights reserved.