如何检查Linux的32位/ 64位内核

时间:2023-01-06 07:00:40

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

我需要编写一个bash脚本,我必须检查Linux内核是32位还是64位。

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.

我正在使用uname -a命令,它给了我x86_64结果。但我相信我不能以通用的方式使用它,因为如果某些人使用非x86架构,结果可能会有所不同。

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

如何检查Linux的32位/ 64位内核?

6 个解决方案

#1


15  

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.

问题是:您打算通过了解自己是32岁还是64岁来实现目标?在假设的128位环境中会产生什么后果?那个实际上正在测试N位的部分是什么? CPU可能支持以64位模式运行,但环境为32位。此外,环境本身可能是混合模式;考虑运行具有32位用户空间的64位内核(在少数经典RISC上完成)。然后,如果用户空间不是同质的比特/可执行格式怎么办?这就是为什么getconf LONG_BIT同样毫无意义,因为它取决于它是如何编译的。

$ /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

#2


5  

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

您可以在系统中查询long int的大小:

getconf LONG_BIT

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

但我不确定这是否可以完全移植到所有不同的架构。

#3


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.

在/ proc / cpuinfo中搜索lm(长模式)标志。如果是这样,则意味着您拥有64位处理器。一个简单的grep应该会给你这个信息。

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.

关于内核版本,您始终可以使用uname -a信息。更好的方法是找到uname程序的来源,以便我们可以减少由于恶意主机名引起的差异。

#4


1  

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

我想出了以下内容。它假设使用了init(一些发行版已经切换到其他加载器,但它应该很容易得到一个合理经常使用的列表)并且你使用ELF,而不是a.out或其他现在奇特的可执行格式。对于大多数系统来说,这些似乎是理智的假设,但可能它们可能在嵌入式系统中被破坏等等。但是,一般的想法应该可以适应(获取init进程或等效并使用文件检查其位数)。如果你是以root身份运行,而不是通过文件的路径,你可以使用文件$(sudo readlink -e / proc / 1 / exe)(PID 1是init可能比假设它的路径更可移植)。

if file /sbin/init | fgrep 'ELF 64-bit' &>/dev/null; then
    echo "64-bit"
else
    echo "not 64-bit"
fi

#5


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

取决于你要找的东西,我在64位proc上安装了32位机器,以上所有都会在我的情况下返回32位

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.

但是如果我看看硬件,lshw在Ubuntu(lshw -c cpu)上,cpu的描述清楚地表明我有64位CPU,所以我可以安装64位版本的Ubuntu。

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

/ proc / cpuinfo也是关于硬件的良好信息来源,例如lm标志代表长模式。

Have a nice day. Jack.

祝你今天愉快。插口。

#6


-2  

Grep for '64' in uname output

在uname输出中grep为'64'

uname -a | grep 64 

#1


15  

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.

问题是:您打算通过了解自己是32岁还是64岁来实现目标?在假设的128位环境中会产生什么后果?那个实际上正在测试N位的部分是什么? CPU可能支持以64位模式运行,但环境为32位。此外,环境本身可能是混合模式;考虑运行具有32位用户空间的64位内核(在少数经典RISC上完成)。然后,如果用户空间不是同质的比特/可执行格式怎么办?这就是为什么getconf LONG_BIT同样毫无意义,因为它取决于它是如何编译的。

$ /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

#2


5  

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

您可以在系统中查询long int的大小:

getconf LONG_BIT

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

但我不确定这是否可以完全移植到所有不同的架构。

#3


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.

在/ proc / cpuinfo中搜索lm(长模式)标志。如果是这样,则意味着您拥有64位处理器。一个简单的grep应该会给你这个信息。

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.

关于内核版本,您始终可以使用uname -a信息。更好的方法是找到uname程序的来源,以便我们可以减少由于恶意主机名引起的差异。

#4


1  

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

我想出了以下内容。它假设使用了init(一些发行版已经切换到其他加载器,但它应该很容易得到一个合理经常使用的列表)并且你使用ELF,而不是a.out或其他现在奇特的可执行格式。对于大多数系统来说,这些似乎是理智的假设,但可能它们可能在嵌入式系统中被破坏等等。但是,一般的想法应该可以适应(获取init进程或等效并使用文件检查其位数)。如果你是以root身份运行,而不是通过文件的路径,你可以使用文件$(sudo readlink -e / proc / 1 / exe)(PID 1是init可能比假设它的路径更可移植)。

if file /sbin/init | fgrep 'ELF 64-bit' &>/dev/null; then
    echo "64-bit"
else
    echo "not 64-bit"
fi

#5


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

取决于你要找的东西,我在64位proc上安装了32位机器,以上所有都会在我的情况下返回32位

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.

但是如果我看看硬件,lshw在Ubuntu(lshw -c cpu)上,cpu的描述清楚地表明我有64位CPU,所以我可以安装64位版本的Ubuntu。

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

/ proc / cpuinfo也是关于硬件的良好信息来源,例如lm标志代表长模式。

Have a nice day. Jack.

祝你今天愉快。插口。

#6


-2  

Grep for '64' in uname output

在uname输出中grep为'64'

uname -a | grep 64