How to View and Browse the Linux Kernel Source?
This article briefly explains the Linux kernel is and how to browse its source code.
This tutorial is optimized for inexperienced Linux users and programmers who want to read the kernel source code or become Linux kernel developers. The Linux kernel is universal; therefore, the present document is useful for all Linux distributions.
By reading this article, you will understand what the kernel is, how it is structured and how to browse through its components. This article was initially written in 2019 and updated in 2022.
What Is the Linux Kernel and How To Browse It
The Linux kernel is the main component allowing interaction between the software and the hardware with the help of device drivers. You can think about the Linux kernel as a translator allowing communication between programs, the hardware, and the user interface.
Learning how to browse the Linux kernel is the first step for any programmer who wants to become a kernel developer. It is also useful for programmers or users who want to read its source code or modify it.
The Linux Kernel Structure
The Linux kernel is structured with directories and subdirectories known as “kernel tree”, users can browse to access specific sections of the whole code. This allows users and developers to focus on specific parts of the code.
Except for the Slackware Linux distribution, whose kernel is maintained by a single person, the rest of the Linux distributions’ huge developers’ community divide tasks based on the kernel tree. In contrast, several developers focus on drivers, others focus on security, and others on virtualization.
The kernel source tree is the following structure described through which users can browse the source code:
Viewing and Browsing the Linux Kernel Source Code
Usually, when we install Linux, we don’t include the source code. We can install Linux and keep the source code in our system. But most users discard it to download a smaller ISO image. After all, you can download the source code later if you need it.
If the kernel source code is present in your system, you can find it under the /usr/src/linux- directory, where must be replaced with the source code kernel version you are dealing with.
You also can find the last kernel source code at https://github.com/torvalds/linux.
As you can see in the previous screenshot or at the provided URL, within the source code tree, we can find the following main directories and files:
- arch: This directory contains source codes for different system architectures supported by the Linux kernel.
- block: The block I/O layer directory contains code for the management of block devices (such as hard disks, DVDs, floppy disks, etc.) and their requests.
- certs: The certs directory contains certificates and signs files to allow module signatures for the kernel to load signed modules. This can be useful to prevent malicious code from running within kernel modules like rootkits.
- COPYING: This file has information about licenses for the source code
- CREDITS: A partial listing of people who have contributed to the linux kernel source code
- Crypto: This directory contains cryptographic ciphers, which handle cryptographic and compression tasks.
- Documentation: As its name suggests, this directory includes all documentation about all kernel components.
- drivers: The drivers directory contains source code for device drivers to support hardware. Inside this directory, you’ll find subdirectories for each hardware, such as video, Bluetooth, etc. (any supported hardware).
- Fs: This directory contains sources for file systems, including virtual file systems, plus read and write file systems.
- include: This directory contains C headers for kernel files, such as functions to compile code.
- init: The init directory contains source code related to the boot process and the initialization of the Linux kernel. The source code is stored in a file named mainc.c within the /init directory. The code initializes the kernel and some initial processes.
- ipc: Inter-Process Communication, such as signals and pipes.
- Kbuild: The Kbuild file indicates object files for vmlinuz.
- Kconfig: The Kconfig file is used to disable or enable kernel features. Users deal with running the “make menuconfig” command.
- kernel: The kernel directory includes core subsystems, such as scheduler signal handling code.
- lib: This directory includes library routines, common string operations, hardware-dependent operations, debugging routines, and command-line parsing code.
- LICENSES: It contains the licenses to be applied to the kernel source and individual source files, which may have different licenses. Inside this directory, you will find four main subdirectories containing supported licenses like LGPL-X, Apache license, etc.
- MANTAINERS: The MAINTAINERS file shows the kernel maintainers’ list and instructions to submit kernel changes.
- Makefile: This file reads the .config file and builds modules and vmlinux.
- mm: The Linux kernel manages hardware and virtual memory (swap). This is the directory where the source code for memory management is stored.
- net: The net directory contains the network stack. Here, you can find code related to communication protocols, such as IP, TCP, and UP.
- README: The README file refers to guides for kernel developers in the Documentation directory previously described.
- Samples: The samples directory includes kernel and configuration files sample code.
- scripts: Here are stored the necessary scripts to build the Linux kernel.
- security: The security subdirectory contains the Security-Enhanced Linux (SELinux) security framework.
- sound: Here, you can find the sound subsystem, including sound drivers, driver architectures, and code related to sound, such as ALSA.
- tools: Here are tools for compressed kernel development, such as ACPI, cgroup, USB testing tools, vhost test module, GPIO, IIO and SPI tools, and Inter energy policy tool.
- usr: here, usr stores the initramfs, which roots the filesystem and init in the kernel memory cache. Code included allows you to execute code in user space in the boot process when the kernel isn’t fully loaded.
- virt: The virtualization directory contains the KVM (Kernel Virtual Machine) module for hypervisors, including architectures.
These are all top files and directories included in the main kernel source directory. You can browse each section depending on your needs. Once you understand the purpose of all files and directories listed above, subdirectories are pretty intuitive.
Conclusion
As you can see, this article was written for novel and medium users to understand the basics of the Linux kernel. Yet, some knowledge on development under Linux, hardware, and operating system characteristics is necessary for practical use. The previous lecture is excellent as an introduction to the Linux kernel structure and Linux distribution. Keep in mind that the Linux kernel evolves periodically. That’s why we keep the previously described content up-to-date.
We hope you found this article useful. Keep reading LinuxHint for more Linux professional articles.
Where do you download Linux source code? [closed]
Say I’m interested in the source for one particular Linux utility, like factor . Where can I find the source code for that utility?
8 Answers 8
You can also find out which package the binary comes from an download that packages source code.
On Debian (and Ubuntu and anything else that’s based on Debian) you do that like this:
$ dpkg -S /usr/bin/factor coreutils: /usr/bin/factor $ apt-get source coreutils
The first command will check which package contains the file you are searching for (use » which factor » to find out which binary is executed when you just type » factor «).
The second command will download and unpack the sources (including the patches applied to build the package) to the current directory, so it should be executed in a dedicated or temporary directory.
I’m pretty sure rpm -based distributions have a similar mechanism, but I don’t know their commands.
Thanks, that’s a good, non-Google option. I know Google will always be there, but for some reason I still like knowing how to live without it.
and went to the bottom and found ‘GNU coreutils 6.10’. So I googled ‘coreutils’ and. found the site joschi just linked to.
Thanks, this (a general explanation of how to find the source for a given program) is what I was after.
That may not be a good idea for all things. Some distributions may have modified the original for their distribution. It may be a better idea to use the package management software that comes with you system (Package management can get source as well as binaries) See ‘Saua’ below.
To find the package a binary comes from, in rpm based system, you might type:
which will print the package name. Instead, with:
you will get also information on a package, including it’s home page in many cases.
Source rpms also exist, but how to get them depends on the high-level package manager used on top of RPM (yum, urpmi, apt-get4 for rpm, . ).
On most systems, also, /usr/share/doc/ contains some documentation on the program, and the website link is pretty often found somewhere there, maybe in the README.
Usually you’ll find the source code on the website of the program if it is open source. In this case here since factor is part of coreutils.
On Gentoo, simply look in the ebuild you compiled the package with :D.
# which factor /usr/bin/factor # grep '/usr/bin/factor' /var/db/pkg/*/*/CONTENTS /var/db/pkg/sys-apps/coreutils-6.12-r2/CONTENTS:obj /usr/bin/factor 5aaf903daa4345efb11618b3cb47e9a5 1224224574 /var/db/pkg/sys-apps/coreutils-6.12-r2/CONTENTS:obj /usr/lib64/debug/usr/bin/factor.debug 517d965636850633e9b15926dde8c222 1224224575 # cat /var/db/pkg/sys-apps/coreutils-6.12-r2/SRC_URI ftp://alpha.gnu.org/gnu/coreutils/coreutils-6.12.tar.lzma mirror://gnu/coreutils/coreutils-6.12.tar.lzma mirror://gentoo/coreutils-6.12.tar.lzma mirror://gentoo/coreutils-6.12-patches-1.0.tar.lzma http://dev.gentoo.org/~vapier/dist/coreutils-6.12-patches-1.0.tar.lzma # cat /var/db/pkg/sys-apps/coreutils-6.12-r2/HOMEPAGE http://www.gnu.org/software/coreutils/
But of course, the source code is probably still available in /usr/portage/distfiles .
How do I get the Ubuntu source code? [closed]
Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Where can I find the source code for the latest Ubuntu release? Also, how would I view the code? Would it just be lots of .cpp and .h files I could view in Visual Studio?
Thanks, was i right going for the dists subfolder? At this point i was presented with a massive number of sub-folders? Are each of these part of the OS?
3 Answers 3
The source code for ubuntu is divided up by package — from a running ubuntu system you can easily retreive the source for any package by doing:
apt-get source (package name)
Otherwise, go to launchpad, and search up the package in question. For example, here’s the download page for the source code for a specific version of curl: https://launchpad.net/ubuntu/+source/curl/7.21.2-4ubuntu1
That said, it’s a lot easier if you’re on a Linux system already — the package sources are divided into an original source tarball plus ubuntu patches, so if you don’t use apt-get source , you’ll need to manually apply the patch to the source code. And new-style packages are even divided into multiple packages.
What’s more, the packages are generally not designed to be cross-compiled from a non-Linux system. Even if you download them and open them in VS, you won’t be able to build them from a Windows system.
Finally, note that not everything is in C and C++ — there are packages in just about any language you can imagine. But I suppose most of them could be opened in VS as text files 🙂
Note: If you really, really want all of it, and I can’t stress enough how silly it would be to download everything just to start learning about the system, you can use the debmirror tool, available in ubuntu, to do this:
debmirror -a none \ --source \ -s main \ -d lucid,lucid-security,lucid-updates \ -r /ubuntu \ --progress \ -e http \ -h archive.ubuntu.com \ ## or other ubuntu archive mirror destpath
This will be an absolutely huge download. Have several tens of GBs of space available. Note that this downloads only core packages — replace -s main with -s main,universe,multiverse,restricted to get everything.
Once you have the package files, you can extract the source by running dpkg-source -x on a .dsc file of interest.