Example code for OS detection - Amazon Linux 2

Example code for OS detection

The following examples demonstrate how to programmatically detect the operating system and version using the /etc/os-release (or /usr/lib/os-release if /etc/os-release does not exist) file. These examples show how to distinguish between Amazon Linux and other distributions, as well as how to use the ID_LIKE field to determine distribution families.

The script below is implemented in several different programming languages, and each implementation will produce the same output.

Shell
#!/bin/bash # Function to get a specific field from os-release file get_os_release_field() { local field="$1" local os_release_file # Find the os-release file if [ -f /etc/os-release ]; then os_release_file='/etc/os-release' elif [ -f /usr/lib/os-release ]; then os_release_file='/usr/lib/os-release' else echo "Error: os-release file not found" >&2 return 1 fi # Source the file in a subshell and return the requested field. # # A subshell means that variables from os-release are only available # within the subshell, and the main script environment remains clean. ( . "$os_release_file" eval "echo \"\$$field\"" ) } is_amazon_linux() { [ "$(get_os_release_field ID)" = "amzn" ] } is_fedora() { [ "$(get_os_release_field ID)" = "fedora" ] } is_ubuntu() { [ "$(get_os_release_field ID)" = "ubuntu" ] } is_debian() { [ "$(get_os_release_field ID)" = "debian" ] } # Function to check if this is like Fedora (includes Amazon Linux, CentOS, RHEL, etc.) is_like_fedora() { local id="$(get_os_release_field ID)" local id_like="$(get_os_release_field ID_LIKE)" [ "$id" = "fedora" ] || [[ "$id_like" == *"fedora"* ]] } # Function to check if this is like Debian (includes Ubuntu and derivatives) is_like_debian() { local id="$(get_os_release_field ID)" local id_like="$(get_os_release_field ID_LIKE)" [ "$id" = "debian" ] || [[ "$id_like" == *"debian"* ]] } # Get the main fields we'll use multiple times ID="$(get_os_release_field ID)" VERSION_ID="$(get_os_release_field VERSION_ID)" PRETTY_NAME="$(get_os_release_field PRETTY_NAME)" ID_LIKE="$(get_os_release_field ID_LIKE)" echo "Operating System Detection Results:" echo "==================================" echo "Is Amazon Linux: $(is_amazon_linux && echo YES || echo NO)" echo "Is Fedora: $(is_fedora && echo YES || echo NO)" echo "Is Ubuntu: $(is_ubuntu && echo YES || echo NO)" echo "Is Debian: $(is_debian && echo YES || echo NO)" echo "Is like Fedora: $(is_like_fedora && echo YES || echo NO)" echo "Is like Debian: $(is_like_debian && echo YES || echo NO)" echo echo "Detailed OS Information:" echo "=======================" echo "ID: $ID" echo "VERSION_ID: $VERSION_ID" echo "PRETTY_NAME: $PRETTY_NAME" [ -n "$ID_LIKE" ] && echo "ID_LIKE: $ID_LIKE" # Amazon Linux specific information if is_amazon_linux; then echo "" echo "Amazon Linux Version Details:" echo "=============================" case "$VERSION_ID" in 2018.03) echo "Amazon Linux AMI (version 1)" ;; 2) echo "Amazon Linux 2" ;; 2023) echo "Amazon Linux 2023" ;; *) echo "Unknown Amazon Linux version: $VERSION_ID" ;; esac # Check for Amazon Linux specific files [ -f /etc/image-id ] && echo "Amazon Linux image-id file present" fi
Python 3.7-3.9
#!/usr/bin/env python3 import os import sys def parse_os_release(): """Parse the os-release file and return a dictionary of key-value pairs.""" os_release_data = {} # Try /etc/os-release first, then /usr/lib/os-release for path in ['/etc/os-release', '/usr/lib/os-release']: if os.path.exists(path): try: with open(path, 'r') as f: for line in f: line = line.strip() if line and not line.startswith('#') and '=' in line: key, value = line.split('=', 1) # Remove quotes if present value = value.strip('"\'') os_release_data[key] = value return os_release_data except IOError: continue print("Error: os-release file not found") sys.exit(1) def is_amazon_linux(os_data): """Check if this is Amazon Linux.""" return os_data.get('ID') == 'amzn' def is_fedora(os_data): """Check if this is Fedora.""" return os_data.get('ID') == 'fedora' def is_ubuntu(os_data): """Check if this is Ubuntu.""" return os_data.get('ID') == 'ubuntu' def is_debian(os_data): """Check if this is Debian.""" return os_data.get('ID') == 'debian' def is_like_fedora(os_data): """Check if this is like Fedora (includes Amazon Linux, CentOS, RHEL, etc.).""" if os_data.get('ID') == 'fedora': return True id_like = os_data.get('ID_LIKE', '') return 'fedora' in id_like def is_like_debian(os_data): """Check if this is like Debian (includes Ubuntu and derivatives).""" if os_data.get('ID') == 'debian': return True id_like = os_data.get('ID_LIKE', '') return 'debian' in id_like def main(): # Parse os-release file os_data = parse_os_release() # Display results print("Operating System Detection Results:") print("==================================") print(f"Is Amazon Linux: {'YES' if is_amazon_linux(os_data) else 'NO'}") print(f"Is Fedora: {'YES' if is_fedora(os_data) else 'NO'}") print(f"Is Ubuntu: {'YES' if is_ubuntu(os_data) else 'NO'}") print(f"Is Debian: {'YES' if is_debian(os_data) else 'NO'}") print(f"Is like Fedora: {'YES' if is_like_fedora(os_data) else 'NO'}") print(f"Is like Debian: {'YES' if is_like_debian(os_data) else 'NO'}") # Additional information print() print("Detailed OS Information:") print("=======================") print(f"ID: {os_data.get('ID', '')}") print(f"VERSION_ID: {os_data.get('VERSION_ID', '')}") print(f"PRETTY_NAME: {os_data.get('PRETTY_NAME', '')}") if os_data.get('ID_LIKE'): print(f"ID_LIKE: {os_data.get('ID_LIKE')}") # Amazon Linux specific information if is_amazon_linux(os_data): print() print("Amazon Linux Version Details:") print("=============================") version_id = os_data.get('VERSION_ID', '') if version_id == '2018.03': print("Amazon Linux AMI (version 1)") elif version_id == '2': print("Amazon Linux 2") elif version_id == '2023': print("Amazon Linux 2023") else: print(f"Unknown Amazon Linux version: {version_id}") # Check for Amazon Linux specific files if os.path.exists('/etc/image-id'): print("Amazon Linux image-id file present") if __name__ == '__main__': main()
Python 3.10+
#!/usr/bin/env python3 import os import sys import platform def is_amazon_linux(os_data): """Check if this is Amazon Linux.""" return os_data.get('ID') == 'amzn' def is_fedora(os_data): """Check if this is Fedora.""" return os_data.get('ID') == 'fedora' def is_ubuntu(os_data): """Check if this is Ubuntu.""" return os_data.get('ID') == 'ubuntu' def is_debian(os_data): """Check if this is Debian.""" return os_data.get('ID') == 'debian' def is_like_fedora(os_data): """Check if this is like Fedora (includes Amazon Linux, CentOS, RHEL, etc.).""" if os_data.get('ID') == 'fedora': return True id_like = os_data.get('ID_LIKE', '') return 'fedora' in id_like def is_like_debian(os_data): """Check if this is like Debian (includes Ubuntu and derivatives).""" if os_data.get('ID') == 'debian': return True id_like = os_data.get('ID_LIKE', '') return 'debian' in id_like def main(): # Parse os-release file using the standard library function (Python 3.10+) try: os_data = platform.freedesktop_os_release() except OSError: print("Error: os-release file not found") sys.exit(1) # Display results print("Operating System Detection Results:") print("==================================") print(f"Is Amazon Linux: {'YES' if is_amazon_linux(os_data) else 'NO'}") print(f"Is Fedora: {'YES' if is_fedora(os_data) else 'NO'}") print(f"Is Ubuntu: {'YES' if is_ubuntu(os_data) else 'NO'}") print(f"Is Debian: {'YES' if is_debian(os_data) else 'NO'}") print(f"Is like Fedora: {'YES' if is_like_fedora(os_data) else 'NO'}") print(f"Is like Debian: {'YES' if is_like_debian(os_data) else 'NO'}") # Additional information print() print("Detailed OS Information:") print("=======================") print(f"ID: {os_data.get('ID', '')}") print(f"VERSION_ID: {os_data.get('VERSION_ID', '')}") print(f"PRETTY_NAME: {os_data.get('PRETTY_NAME', '')}") if os_data.get('ID_LIKE'): print(f"ID_LIKE: {os_data.get('ID_LIKE')}") # Amazon Linux specific information if is_amazon_linux(os_data): print() print("Amazon Linux Version Details:") print("=============================") version_id = os_data.get('VERSION_ID', '') if version_id == '2018.03': print("Amazon Linux AMI (version 1)") elif version_id == '2': print("Amazon Linux 2") elif version_id == '2023': print("Amazon Linux 2023") else: print(f"Unknown Amazon Linux version: {version_id}") # Check for Amazon Linux specific files if os.path.exists('/etc/image-id'): print("Amazon Linux image-id file present") if __name__ == '__main__': main()
Perl
#!/usr/bin/env perl use strict; use warnings; # Function to parse the os-release file and return a hash of key-value pairs sub parse_os_release { my %os_release_data; # Try /etc/os-release first, then /usr/lib/os-release my @paths = ('/etc/os-release', '/usr/lib/os-release'); for my $path (@paths) { if (-f $path) { if (open(my $fh, '<', $path)) { while (my $line = <$fh>) { chomp $line; next if $line =~ /^\s*$/ || $line =~ /^\s*#/; if ($line =~ /^([^=]+)=(.*)$/) { my ($key, $value) = ($1, $2); # Remove quotes if present $value =~ s/^["']|["']$//g; $os_release_data{$key} = $value; } } close($fh); return %os_release_data; } } } die "Error: os-release file not found\n"; } # Function to check if this is Amazon Linux sub is_amazon_linux { my %os_data = @_; return ($os_data{ID} // '') eq 'amzn'; } # Function to check if this is Fedora sub is_fedora { my %os_data = @_; return ($os_data{ID} // '') eq 'fedora'; } # Function to check if this is Ubuntu sub is_ubuntu { my %os_data = @_; return ($os_data{ID} // '') eq 'ubuntu'; } # Function to check if this is Debian sub is_debian { my %os_data = @_; return ($os_data{ID} // '') eq 'debian'; } # Function to check if this is like Fedora (includes Amazon Linux, CentOS, RHEL, etc.) sub is_like_fedora { my %os_data = @_; return 1 if ($os_data{ID} // '') eq 'fedora'; my $id_like = $os_data{ID_LIKE} // ''; return $id_like =~ /fedora/; } # Function to check if this is like Debian (includes Ubuntu and derivatives) sub is_like_debian { my %os_data = @_; return 1 if ($os_data{ID} // '') eq 'debian'; my $id_like = $os_data{ID_LIKE} // ''; return $id_like =~ /debian/; } # Main execution my %os_data = parse_os_release(); # Display results print "Operating System Detection Results:\n"; print "==================================\n"; print "Is Amazon Linux: " . (is_amazon_linux(%os_data) ? "YES" : "NO") . "\n"; print "Is Fedora: " . (is_fedora(%os_data) ? "YES" : "NO") . "\n"; print "Is Ubuntu: " . (is_ubuntu(%os_data) ? "YES" : "NO") . "\n"; print "Is Debian: " . (is_debian(%os_data) ? "YES" : "NO") . "\n"; print "Is like Fedora: " . (is_like_fedora(%os_data) ? "YES" : "NO") . "\n"; print "Is like Debian: " . (is_like_debian(%os_data) ? "YES" : "NO") . "\n"; print "\n"; # Additional information print "Detailed OS Information:\n"; print "=======================\n"; print "ID: " . ($os_data{ID} // '') . "\n"; print "VERSION_ID: " . ($os_data{VERSION_ID} // '') . "\n"; print "PRETTY_NAME: " . ($os_data{PRETTY_NAME} // '') . "\n"; print "ID_LIKE: " . ($os_data{ID_LIKE} // '') . "\n" if $os_data{ID_LIKE}; # Amazon Linux specific information if (is_amazon_linux(%os_data)) { print "\n"; print "Amazon Linux Version Details:\n"; print "=============================\n"; my $version_id = $os_data{VERSION_ID} // ''; if ($version_id eq '2018.03') { print "Amazon Linux AMI (version 1)\n"; } elsif ($version_id eq '2') { print "Amazon Linux 2\n"; } elsif ($version_id eq '2023') { print "Amazon Linux 2023\n"; } else { print "Unknown Amazon Linux version: $version_id\n"; } # Check for Amazon Linux specific files if (-f '/etc/image-id') { print "Amazon Linux image-id file present\n"; } }

When run on different systems, the script will produce the following output:

AL2023
Operating System Detection Results:
==================================
Is Amazon Linux: YES
Is Fedora: NO
Is Ubuntu: NO
Is Debian: NO
Is like Fedora: YES
Is like Debian: NO

Detailed OS Information:
=======================
ID: amzn
VERSION_ID: 2023
PRETTY_NAME: Amazon Linux 2023.8.20250721
ID_LIKE: fedora

Amazon Linux Version Details:
=============================
Amazon Linux 2023
Amazon Linux image-id file present
AL2
Operating System Detection Results:
==================================
Is Amazon Linux: YES
Is Fedora: NO
Is Ubuntu: NO
Is Debian: NO
Is like Fedora: YES
Is like Debian: NO

Detailed OS Information:
=======================
ID: amzn
VERSION_ID: 2
PRETTY_NAME: Amazon Linux 2
ID_LIKE: centos rhel fedora

Amazon Linux Version Details:
=============================
Amazon Linux 2
Amazon Linux image-id file present
Amazon Linux AMI
Operating System Detection Results:
==================================
Is Amazon Linux: YES
Is Fedora: NO
Is Ubuntu: NO
Is Debian: NO
Is like Fedora: YES
Is like Debian: NO

Detailed OS Information:
=======================
ID: amzn
VERSION_ID: 2018.03
PRETTY_NAME: Amazon Linux AMI 2018.03
ID_LIKE: rhel fedora

Amazon Linux Version Details:
=============================
Amazon Linux AMI (version 1)
Amazon Linux image-id file present
Ubuntu
Operating System Detection Results:
==================================
Is Amazon Linux: NO
Is Fedora: NO
Is Ubuntu: YES
Is Debian: NO
Is like Fedora: NO
Is like Debian: YES

Detailed OS Information:
=======================
ID: ubuntu
VERSION_ID: 24.04
PRETTY_NAME: Ubuntu 24.04.2 LTS
ID_LIKE: debian
Debian
Operating System Detection Results:
==================================
Is Amazon Linux: NO
Is Fedora: NO
Is Ubuntu: NO
Is Debian: YES
Is like Fedora: NO
Is like Debian: YES

Detailed OS Information:
=======================
ID: debian
VERSION_ID: 12
PRETTY_NAME: Debian GNU/Linux 12 (bookworm)
Fedora
Operating System Detection Results:
==================================
Is Amazon Linux: NO
Is Fedora: YES
Is Ubuntu: NO
Is Debian: NO
Is like Fedora: YES
Is like Debian: NO

Detailed OS Information:
=======================
ID: fedora
VERSION_ID: 42
PRETTY_NAME: Fedora Linux 42 (Container Image)