9

I need to write a bash script where i have to check whether the Linux kernel is 32 bit or 64 bit.

I am using uname -a command, and it gives me x86_64 result. But I believe I can not use it in the generic way because result may differ if some one using non x86 architecture.

How to check for 32-bit / 64-bit kernel for Linux?

jww
  • 97,681
  • 90
  • 411
  • 885
VJS
  • 2,891
  • 7
  • 38
  • 70

7 Answers7

18

The question is rather: what do you intend to achieve by knowing whether you are on 32 or 64? What are the consequences of being on a hypothetical 128-bit environment? And what part actually is being tested for N-bitness? A CPU may support running in 64-bit mode, but the environment be 32-bit. Furthermore, the environment itself may be a mixed-mode; consider running a 64-bit kernel with a 32-bit userspace (done on a handful of classic RISCs). And then, what if the userspace is not of a homogenous bitness/executable format? That is why getconf LONG_BIT is equally pointless to use, because it depends on how it was compiled.

$ /rt64/usr/bin/getconf LONG_BIT
64
$ /usr/bin/getconf LONG_BIT
32
$ file /usr/bin/getconf /rt64/usr/bin/getconf
/usr/bin/getconf:      ELF 32-bit MSB executable, SPARC32PLUS, V8+ Required, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.4, not stripped
/rt64/usr/bin/getconf: ELF 64-bit MSB executable, SPARC V9, relaxed memory ordering, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.4, not stripped
$ uname -m
sparc64
jørgensen
  • 10,149
  • 2
  • 20
  • 27
7

You could query the system for the size of a long int:

getconf LONG_BIT

But I'm not sure if this is completely portable to all different architectures.

mtvec
  • 17,846
  • 5
  • 52
  • 83
2

I came up with the following. It assumes that init is used (some distros have switched to other loaders, but it should be easy to get a list of reasonably often used ones) and that you use ELF, not a.out or other nowadays exotic executable format. These seem to be sane assumptions for most systems, but probably they may be broken in embedded systems, etc. Still, the general idea should be possible to adapt (get to the init process or equivalent and check its bitness using file). If you are running as root, instead of going through the file's path, you could use file $(sudo readlink -e /proc/1/exe) (PID 1 being init is probably more portable than assuming anything about its path).

if file /sbin/init | fgrep 'ELF 64-bit' &>/dev/null; then
    echo "64-bit"
else
    echo "not 64-bit"
fi
Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
  • The problem with this approach is that it is possible to use 32-bit executables when running a 64-bit kernel. – mtvec Apr 13 '12 at 10:12
  • @Job You are right, but `init` (PID 1) is executed as the very first process, so I think it does determine the bitness of the system as a whole. – Michał Kosmulski Apr 13 '12 at 10:15
  • Well I think that technically it should be possible even for `init` to be 32-bit on a 64-bit kernel but on any "normal" system your approach should be ok I guess. – mtvec Apr 13 '12 at 10:18
  • +1. This is (in my opinion) the most viable solution of all the ones proposed here. – Daniel Kamil Kozar Apr 13 '12 at 11:36
  • This will not work on non-ELF platforms (e.g. Mach-O); also init cat be 32-bit and other userspace can be 64 bit, because bitness is per-process, it is detected while loading ELF/other binary. – osgx Apr 13 '12 at 12:42
1

Search for the lm (long mode) flag in /proc/cpuinfo. If this is true, it means you have a 64 bit processor. A simple grep should give you this information.

Regarding the kernel version, you can always grep on the uname -a information. Better would be to find the source of the uname program so that we can discount the discrepancy due to malicious hostname.

prathmesh.kallurkar
  • 5,468
  • 8
  • 39
  • 50
  • 2
    This will only work on i386/x86_64. I once worked on a POWER machine and there the format of `/proc/cpuinfo` was completely different, much more compact and didn't have flags like these. – Michał Kosmulski Apr 13 '12 at 10:21
  • @above /proc/cpuinfo gives the number of physical bits and virtual bits. Do you receive this information while working on a power machine ?? Next step for us is to make sense of this information. I am trying on my part. One more thing : I think we can use the size of int in C to determine the word size of the machine. – prathmesh.kallurkar Apr 13 '12 at 16:02
  • @prathmeshkallurkar I don't have access to that machine any more, but I remember the information was really brief - like 4 lines per CPU, only model names and hardly anything more. Even if it had info on available bits, it was certainly in a different format than on x86. – Michał Kosmulski Apr 13 '12 at 18:23
  • [Here's](http://lists.freebsd.org/pipermail/freebsd-ppc/2009-January/003461.html) an example of /proc/cpuinfo from a POWER6 machine - it looks very much like the one I had. The mailing list is about FreeBSD but the cpuinfo they cite is from Linux. – Michał Kosmulski Apr 13 '12 at 18:27
1

A quite reliable way to determine the supported executable architecture:

A featured function to do this:

# Gets the supported executable architecture
# >: 32-bit | 64-bit
getRunArch() {
  local arch
  local IFS=' '
  read -r _ arch _ <<< $(file --dereference --brief "$(which ls)")
  echo -n "${arch}"
}

Testing this:

echo "Supported run-time architecture is: $(getRunArch)"
Supported run-time architecture is: 64-bit

Slightly more reliable than:

getconf LONG_BIT
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
0

Depend what you're looking for, I have machine installed in 32 bit on 64 bit proc, all of above would return 32 bit in my case

But if I look at hardware, lshw on Ubuntu (lshw -c cpu), the description of cpu clearly show I have a 64 bit CPU, so I could have install a 64 bit version of Ubuntu.

/proc/cpuinfo is also good source of info about hardware, like the lm flags stand for long mode.

Have a nice day. Jack.

JBat
  • 21
  • 2
-2

Grep for '64' in uname output

uname -a | grep 64 
je4d
  • 7,628
  • 32
  • 46
Fedor Igumnov
  • 201
  • 3
  • 14
  • 2
    If the kernel was compiled 64 times, this will cause a false positive. Ditto if the server's name is `blade64A`. – David Schwartz Apr 13 '12 at 08:53
  • 1
    Use `uname -i | grep 64` instead. – David Schwartz Apr 13 '12 at 08:59
  • @David Schwartz : I am little bit new in this and didn't get your thought. Could you please explain a bit. Thanks a ton !! – VJS Apr 13 '12 at 09:00
  • Look in the `uname -a` output. You'll see the nodename. So if the node name is "server64A" then there will be a "64" in there. You'll also see the kernel name string, which often includes the number of times it was compiled. So if it was compiled 64 times, there'll be a "64" in there. – David Schwartz Apr 13 '12 at 09:01
  • It should be `uname -m | grep 64`, I think. – diewie Apr 13 '12 at 12:49