'How to distinguish between different operating system distros in node.js?
In node you have access to process.os()
and process.platform
, which give you one of 5 values:
'darwin'
'freebsd'
'linux'
'sunos'
'win32'
But how do you print out the specific distro, such as one of these?
http://en.wikipedia.org/wiki/List_of_Linux_distributions
'ubuntu'
'gentoo'
'fedora'
'SUSE Linux'
'CentOS'
dozens/hundreds more
If there is no standard approach, is there any current preferred solution out there? Mainly this would be useful for being able to tell the difference (programmatically) between ubuntu and some of the other popular EC2 image oses.
Solution 1:[1]
To summarize all the responses: There is no easy way to determine the current Linux distribution.
However here are a few thing you could start with.
uname -v
usually gives you a long string with some informations. (E.g.:#66-Ubuntu SMP Thu Apr 25 03:27:11 UTC 2013
)- Debian
/etc/debian_version
, but this is set in Ubuntu, too! - Ubuntu:
/etc/lsb-release
and/etc/os-release
- SuSe:
/etc/SuSE-release
- Many distros also write to
/etc/issue
If you have a list of possible distributions that could happen, you should be able to get that information somehow. If you want a generic approach I'll guess there will be no absolute answer.
Solution 2:[2]
The linux_distribution() function in Python's platform module should work with most of the more common and many of the less common Linux distributions:
python -c "import platform; print platform.linux_distribution()"
Executing a shell command from node.js was answered here: node.js shell command execution
Solution 3:[3]
Reading through the accepted answer it wasn't clear on a 'single standard' way to detect the Distribution. The majority of Linux (and some Unix) distributions released in the last 5 years support the /etc/os-release
file, which seems to be accepted as a standard amongst distributions. Distributions that support this are:
- RHEL, CentOS, Fedora, Amazon Linux, Oracle Linux, Scientific Linux
- Debian, Ubuntu, Raspbian, Mint
- SuSE, OpenSuSE
- Arch
- FreeBSD
- Others that I have not been able to find but likely exist.
More details on the format of the file can be found here: os-release Man Page. A really great repository of user submitted exports of os-release info for various Linux releases can be found here: GitHub List of User Submitted Data.
An example of the os-release file looks like this:
drew@ubuntu20:~$ sudo cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
I believe you would be looking for the ID=ubuntu
field.
On the subject of using Node.js to detect this information. I would access the file using the following code:
import { readFile } from 'fs'
readFile('/etc/os-release', 'utf8', (err, data) => {
if (err) throw err
const lines = data.split('\n')
const releaseDetails = {}
lines.forEach((line, index) => {
// Split the line into an array of words delimited by '='
const words = line.split('=')
releaseDetails[words[0].trim().toLowerCase()] = words[1].trim()
})
console.dir(releaseDetails)
console.log('Distribution:', releaseDetails.id)
});
Output of this code would be:
{
name: '"Ubuntu"',
version: '"20.04 LTS (Focal Fossa)"',
id: 'ubuntu',
id_like: 'debian',
pretty_name: '"Ubuntu 20.04 LTS"',
version_id: '"20.04"',
home_url: '"https://www.ubuntu.com/"',
support_url: '"https://help.ubuntu.com/"',
bug_report_url: '"https://bugs.launchpad.net/ubuntu/"',
privacy_policy_url: '"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"',
version_codename: 'focal',
ubuntu_codename: 'focal'
}
Distribution: ubuntu
Solution 4:[4]
On node.js,
const os = require('os');
console.log(os.version());
should do the trick.
On my machine it prints #29~20.04.1-Ubuntu SMP Wed Aug 11 15:58:17 UTC 2021
on console
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | HoldOffHunger |
Solution 2 | Community |
Solution 3 | |
Solution 4 | Abhijay Mitra |