How to Determine Whether a Given Linux Is 32 Bit or 64 Bit

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

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

How do I check if I have a 32-bit or a 64-bit OS?

I know at least 2 ways. Open a terminal(Ctrl+Alt+T) and type:

  1. uname -a

    Result for 32-bit Ubuntu:

    Linux discworld 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux

    whereas the 64-bit Ubuntu will show:

    Linux discworld 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

    Shorter version:

    $ uname -i
    x86_64

    or

  2. file /sbin/init

    Result for 32-bit Ubuntu:

    /sbin/init: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

    whereas for the 64-bit version it would look like:

    /sbin/init: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

    Same for systems using systemd (16.04):

    file /lib/systemd/systemd

    Result for 64-bit:

    /lib/systemd/systemd: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=54cc7ae53727d3ab67d7ff5d66620c0c589d62f9, stripped

Shell script to detect if system is a 32-bit or 64-bit

if $(uname -m | grep '64'); then
echo "ARCH: 64-bit"
else
echo "ARCH: 32-bit"
fi

How to determine whether a linux kernel is 32bit or 64bit from a running kernel module

You can check for CONFIG_X86_64 and CONFIG_X86_32 - If the arch is x86. For other architectures there are similiar CONFIG options.

How do I determine whether the program was built as 32 bit or 64 bit in Go?

Use GOARCH for arm: arm (ARM) and arm64 (AArch64),

Optional environment variables

$GOOS and $GOARCH

The name of the target operating system and compilation architecture.
These default to the values of $GOHOSTOS and $GOHOSTARCH respectively
(described below).

Choices for $GOOS are

$GOOS     $GOARCH
darwin 386
darwin amd64
darwin arm
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips64
linux mips64le
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
plan9 386
plan9 amd64
solaris amd64
windows 386
windows amd64

Is it possible to detect 32 bit vs 64 bit in a bash script?

Does

uname -a

give you anything you can use? I don't have a 64-bit machine to test on.


Note from Mike Stone: This works, though specifically

uname -m

Will give "x86_64" for 64 bit, and something else for other 32 bit types (in my 32 bit VM, it's "i686").

How to test if a shared object is 32-bit or 64-bit?

.so files use the ELF format. The ELF header comes in two variants for 32bit and 64bit platforms. Which of the two the file contains is determined by byte 0x04 in the file. It is 1 for 32bit format and 2 for the 64bit format.

You can simply read and test this byte.

The actual instruction set that the machine code is compiled for can also be determined from bytes 0x12 and 0x13, e.g. 0x03 for x86 and 0x3E for x86_64. Note that the endianess for the two bytes is determined by byte 0x05, which is either 1 for little endian or 2 for big endian.

See also the wikipedia article on the ELF format.

Can I rely on 'sizeof' in order to determine whether I'm using 32-bit OS or 64-bit OS

In theory, you can have a C implementation where char are 32 bits, and sizeof(char) = sizeof(int) = sizeof(long) = 1, and probably sizeof(long long) = 2; IIRC some experimental implementation of C in Common Lisp (or maybe SBCL only) is doing similar stuff. Sadly, I forgot the details.

In practice, better use <stdint.h> giving int32_t, int64_t and an intptr_t such that sizeof(intptr_t) = sizeof(void*).

So the answer is no.

You might use <limits.h> and INT_MAX etc...

You could have a cross-compiler.... (e.g. compiling on a Linux x86-64 desktop for an 32 bits ARM android tablet with a cross GCC).

You might want to use things like autoconf. Read about GCC common predefined macros. Perhaps you want __LP64__

And you might have a Linux OS with 64 bits support on x86-64, but running in a chroot-ed environment (or container à la docker) providing a 32 bits environment (with 32 bits libc, 32 bits compiler, etc...). This is actually useful (e.g. to test on a 64 bits Linux laptop that your app can be compiled and executed on 32 bits). See e.g. schroot. And most Linux x86-64 systems are able to run 32 bits x86 ELF binaries (at least if they are statically linked). BTW, on my Linux/Debian/x86-64 system gcc -m32 produces 32 bits object files and executables, but gcc -m64 or just gcc gives 64 bits ...

On POSIX systems, to find out about your machine, use uname(2). On Linux, you can even read and parse /proc/cpuinfo (and some other files under /proc/, see proc(5)).

I know nothing about Windows. If you are using it, you should give Linux a try.



Related Topics



Leave a reply



Submit