21

I am writing a bash script to set up the all the software I need after installing Ubuntu. If the computer is a laptop, I want to install tlp for power management. If it is a desktop, tlp is not necessary.

Is there a command with which I can figure out whether the computer is a desktop or a laptop/tablet?

Perhaps, detecting the presence of a battery is the signature I should look for.

2 Answers2

31

I don't think checking the battery is a good solution: A desktop may have UPS that shows up as battery.

You can use the command:

$ hostnamectl chassis
laptop

The output will be

"desktop", "laptop", "convertible", "server", "tablet", "handset", "watch", "embedded", as well as the special chassis types "vm" and "container" for virtualized systems that lack an immediate physical chassis.

See man hostnamectl.


Another option would be to use:

$ sudo dmidecode -s chassis-type
Notebook

Note that in this case, the output may be something like "Laptop", "Notebook", "Portable", "Hand held" or "Sub Notebook" depending on the manufacturer's designation.


There is also a script named:

$ laptop-detect -v
We're a notebook (chassis_type is 10)

You can inspect its (/bin/laptop-detect) contents for a more detailed approach.

FedKad
  • 13,900
  • 3
    Cool - I did not know that. :D – Konrad Gajewski May 31 '24 at 14:41
  • I have a highly unusual edge case setup. It is an old Chromebook where the ChromeOS was wiped. I ran Ubuntu 24.04 Live USB using the "Try" option. All the commands in this answer (and the other one here) tells me the computer is a "Desktop" or "Computer-Desktop". There was one exception, the latop-detect -v. The output was We're a laptop (non device ACPI battries found). – user68186 May 31 '24 at 15:52
  • @user68186 You can trace the script laptop-detect and see which command is the one that gives this result. I presume it is if ( grep -q Battery $sysfs_power_supply/$power_supply/type 2>/dev/null ) && ( ! grep -q Device $sysfs_power_supply/$power_supply/scope 2>/dev/null ). – FedKad May 31 '24 at 16:07
  • You are right: grep -q Device /sys/class/power_supply/BAT0/scope fails with no such file or directory and that results in the output. – user68186 May 31 '24 at 20:55
  • Here are the commands I ran in the terminal and their output. – user68186 May 31 '24 at 21:13
  • yes, a desktop can have UPS, and a laptop can have detachable battery. I used to remove the worn-out battery and ran my laptop from AC only – phuclv Jun 01 '24 at 02:20
  • I wonder what is the difference between laptop and notebook in the chassis type. – Archisman Panigrahi Jun 01 '24 at 11:30
  • It seems that at least in this instance dmidecode is more accurate: running hostnamectl and dmidecode on an Intel NUC, the first reports "desktop" while the second reports "Mini PC"; I mean they're both fair takes, but dmidecode just nails it. – kos Jun 01 '24 at 12:32
  • For my (Mint 20) Linux system 'hostnamectl' doesn't think chassis is a valid option... dmidecode and laptop-detect work as you specify. hostnamectl status does contain a Chassis line in its output (as detailed by Ajay) – matt Jun 01 '24 at 13:42
  • @matt I think you need Ubuntu 22.04 or above: https://manpages.ubuntu.com/manpages/focal/en/man1/hostnamectl.1.html – FedKad Jun 01 '24 at 13:46
  • 2
    I used to have a laptop that would report itself as a pizzabox many years ago :D – Journeyman Geek Jun 02 '24 at 06:05
  • 1
    On this system (Ubuntu 20.04) hostnamectl chassis returns Unknown operation chassis. Instead, hostnamectl status | grep Chassis can be used, giving Chassis: desktop (just noticed Matt said something similar, not deleting because of the use or grep to sort it out). adding | cut -d ":" -f 2 may be helpful – Chris H Jun 03 '24 at 09:54
8

Checking on whether or not the system has a battery is not reliable - a UPS connected to the system may show up as a battery (which showed up when I checked).

So one way is to use dmidecode to get the chassis type of the system:

dmidecode --string chassis-type

But this command requires sudo. To avoid using sudo, you can print the contents of /sys/class/dmi/id/chassis_type, which will return the decimal value of the chassis type.

cat /sys/class/dmi/id/chassis_type

But when I checked, both of the commands gave output where my desktop was considered as "Rack Mount Chassis" (decimal value 23) and my laptop as "Notebook" (decimal value 10). So it might vary according to the system that we are handling.

That's when I used the hostnamectl command and got better values from the Chassis and Icon name:

hostnamectl status | grep Chassis | cut -f2 -d ":" | tr -d ' '

Since my desktop machine didn't have any Chassis option, I used the Icon name value:

hostnamectl status | grep "Icon name" | cut -f2 -d ":" | tr -d ' '

So on my laptop the result was "computer-laptop", on desktop it's just "computer" and on my server, it is "computer-vm". You can use these values to install the tlp on the system.

Ajay
  • 2,283
  • Note that either DMI approach requires a computer with DMI. Ubuntu can be installed on a Raspberry Pi, and the Pi doesn't have DMI. (It's also possible that a given Pi is being used as a desktop, a laptop, a tablet, or something else entirely.) – Mark May 31 '24 at 21:33
  • 2
    But if your computer runs on an UPS, don't you also want some power management tool installed? – Paŭlo Ebermann Jun 01 '24 at 00:30
  • @PaŭloEbermann Yes, but you want something different (probably NUT or APCUPSD), because managing a UPS is completely different from managing a laptop. – Austin Hemmelgarn Jun 01 '24 at 11:33