How to get Linux distribution name and version?
Try:
cat /etc/lsb-release
You can also try
lsb_release -a
Or:
cat /proc/version
How to find Linux Distribution name using shell script?
$ lsb_release -i
Distributor ID: Fedora
$ lsb_release -i | cut -f 2-
Fedora
Check Linux distribution name
This works for me on Ubuntu:
('Ubuntu', '10.04', 'lucid')
I then used strace
to find out what exactly the platform module is doing to find the distribution, and it is this part:
open("/etc/lsb-release", O_RDONLY|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
fstat64(3, {st_mode=S_IFREG|0644, st_size=102, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76b1000
read(3, "DISTRIB_ID=Ubuntu\nDISTRIB_RELEAS"..., 8192) = 102
read(3, "", 4096) = 0
read(3, "", 8192) = 0
close(3) = 0
So, there is /etc/lsb-release
containing this information, which comes from Ubuntu's Debian base-files package.
Is there a way to find the linux os name and use it as an argument in an if statement?
source /etc/os-release
echo "$NAME"
Output:
Ubuntu
or
Red Hat Enterprise Linux Server
C++ get linux distribution name\version
Got it from cplusplus.com forums, a simple call GetSystemOutput("/usr/bin/lsb_release -a")
works.
char* GetSystemOutput(char* cmd){
int buff_size = 32;
char* buff = new char[buff_size];
char* ret = NULL;
string str = "";
int fd[2];
int old_fd[3];
pipe(fd);
old_fd[0] = dup(STDIN_FILENO);
old_fd[1] = dup(STDOUT_FILENO);
old_fd[2] = dup(STDERR_FILENO);
int pid = fork();
switch(pid){
case 0:
close(fd[0]);
close(STDOUT_FILENO);
close(STDERR_FILENO);
dup2(fd[1], STDOUT_FILENO);
dup2(fd[1], STDERR_FILENO);
system(cmd);
//execlp((const char*)cmd, cmd,0);
close (fd[1]);
exit(0);
break;
case -1:
cerr << "GetSystemOutput/fork() error\n" << endl;
exit(1);
default:
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
int rc = 1;
while (rc > 0){
rc = read(fd[0], buff, buff_size);
str.append(buff, rc);
//memset(buff, 0, buff_size);
}
ret = new char [strlen((char*)str.c_str())];
strcpy(ret, (char*)str.c_str());
waitpid(pid, NULL, 0);
close(fd[0]);
}
dup2(STDIN_FILENO, old_fd[0]);
dup2(STDOUT_FILENO, old_fd[1]);
dup2(STDERR_FILENO, old_fd[2]);
return ret;
}
Get OS name with C [Linux, portable for distros: Centos, Debian, Fedora, OpenSUSE, RedHat, Ubuntu]
The uname
system call gives you the generic system type (Linux
in all your cases) in the sysname
field, but it also gives you additional data in the release
, version
, and machine
fields. The release
field will give you the kernel version, and the version
field will give you the general system version, which will be different for all the various linux variants you mention.
How to get distro name and version from linux kernel code?
There's no kernel API for detecting the current OS distribution, simply because it's not really needed. The Linux kernel itself is distribution-agnostic, and it couldn't care less which distribution is being run on top of it (having the kernel depend on what's being run on top of it wouldn't make much sense).
If you really want, you can open, read and parse the file yourself from kernel space. See more in this other post for an example, and in particular this answer for modern kernels. In any case, remember that filesystem interaction from kernel space is generally discouraged, and could easily lead to bugs and compromise the security of the kernel if done wrong, so be careful.
If you are developing a kernel module, I would suggest you to parse the /etc/os-release
file from userspace when compiling/installing the module and use a set of #define
s, or even module parameters. In any case, you should ask yourself why you need this information in kernel code in the first place, as you really shouldn't.
Related Topics
Paramiko Error When Trying to Edit File: "Sudo: No Tty Present and No Askpass Program Specified"
Trying to Import a Module: Undefined Symbol: Pyunicodeucs4_Decodeutf8
How to Make a Cross-Module Variable
Python Selenium Click on Button
Passing Functions with Arguments to Another Function in Python
Find the Max of Two or More Columns with Pandas
Efficient Way to Apply Multiple Filters to Pandas Dataframe or Series
How to Keep Track of Class Instances
Can "List_Display" in a Django Modeladmin Display Attributes of Foreignkey Fields
How to Exit Linux Terminal Using Python Script
Python: Get Output of the Shell Command 'History'
Python and Regular Expression with Unicode
How to Construct a Timedelta Object from a Simple String
Can Pandas Automatically Read Dates from a CSV File