Sometimes it is necessary to retrieve a computer model name for various tasks. One example is applying different configurations or rice scripts in home lab settings. To achieve that, one must be able to retrieve the hardware information in terminal with a command or two. In this article, we demonstrate how to get a computer model name in Linux, OpenBSD, and FreeBSD.
On Linux
There are two approaches to get the hardware model on Linux:
- Using
dmidecode
- Cat on
/sys/devices
With dmidecode
The dmidecode approach is the simplest way to retrieve a hardware model in Linux. One can run the following command to get the hardware model:
$ sudo dmidecode -s system-version
It outputs the computer model name. For example:
ThinkPad T480
The limitation of this approach is that it requires sudo
or superuser permission. That means if the current user or script does not have that privilege, they cannot leverage dmidecode
. Hence, it is not an ideal solution to utilize in shell scripts. Instead, one can use cat /sys/devices
.
Getting hardware mode from /sys/devices
Unlike dmidecode
, this approach does not require one to be a root user or have access to it. It is possible to get the computer model with the command below:
$ cat /sys/devices/virtual/dmi/id/product_version
The downside of this solution is that it may not always work on different hardware models.
On OpenBSD
Getting hardware information on OpenBSD is simple and secure. To get the hardware version, one does not need to be a superuser nor find any workaround. We can directly run the sysctl
command as follows:
$ sysctl hw.version
It outputs something like this,
hw.version=ThinkPad T480
To get only the output of the command without the parameter name, we can tweak the command:
$ sysctl -n hw.version
On FreeBSD
The hardware information on FreeBSD is available as kernel environment variables, which are easily retrievable with the kenv
command:
$ kenv | grep smbios.system.family
The smbios.system.family
stores the computer model. The kenv should output something like below:
smbios.system.family="ThinkPad T480"
Alternatively, one can install dmidecode
on FreeBSD to retrieve computer model names similar to Linux:
$ sudo pkg install dmidecode
Then run dmidecode
as follows:
$ sudo dmidecode -s system-version
Needless to say, this approach has the same limitation as on Linux, meaning it requires elevated privilege.
Conclusion
Depending on the operating system (OS), it might be tedious or easy to get a computer model name in Linux, OpenBSD, and FreeBSD. It is easier in some than others. However, there is no bulletproof approach to successfully retrieving the computer model name, especially on different architectures such as ARM or RISC-V.
Inline/featured images credits
- Featured image by Kevin Horvat on Unsplash