Table of contents
1.
Introduction
2.
Linux Errors
2.1.
Getting Error information
3.
List of Error Codes
4.
Utility to get Error Information
4.1.
Installing the tool
4.2.
Getting Error Information
4.3.
Getting List of all available Errors
5.
Frequently asked questions
5.1.
What is the exit code in Linux?
5.2.
What are two types of Linux User Mode?
5.3.
What is the maximum length for a filename under Linux?
6.
Conclusion 
Last Updated: Mar 27, 2024
Medium

Linux - Error Codes

Author Harsh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Errors are issues or flaws in the program that cause it to behave abnormally. These mistakes can be made even by skilled coders. Without knowing the type or cause of the error, it is very hard to solve the error. That is why we have a whole bunch of error codes available to us. Error codes are the integer value that tells us what kind of error we are getting in our code or program.

Linux Error Codes

This blog will explain the many types of Linux error codes, and we will also see how we can retrieve error information using the error code.

Linux Errors

Linux is made using the C programming language. Most of the code for Linux Kernel is written in C Programming language. Error management is not directly supported by the C programming language. If an error occurs while running the C program, a number is returned that specifies the error type.

There is a variable, namely 'errorno', available in C language that can be used to identify the type of error. We can use the value of this variable to handle the error efficiently.

Let’s have a look at how we can use the value of ‘errno’ variable to get the error information.

Getting Error information

We must include the header file 'errno.h' to use the external variable errno. The "errno" variable is defined in the "error.h" header file. System calls and library routines set the errno variable when an error occurs.

In C programming, there are two different functions available perror and strerror, which can be used to print the description of the error and get the error code.

Let’s look at how we can use both available functions to get error details.

Code

#include <stdio.h>
#include <errno.h>
#include <string.h>

extern int errno;

int main () {
   FILE *file = fopen("CodingNinja.txt", "rb"); // opening the file
   if (file == NULL) {
      printf("Error code: %d\n", errno);
      perror("Error Description");
   } else {
      fclose(file);
   }
   return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Error code: 2
Error Description: No such file or directory

In the above program, we are trying to open a file that does not exist on our system. Now, without knowing the error code, it would be hard to figure out what causes the error.

The value of errno is set to 2, which represents that the file does not exist. There are more than 100 such error codes, each representing a different type of error.

Let’s have a look at some of the error code and their cause.

List of Error Codes

The below table shows some of the Linux error codes. 

List of error codes

🔺There are more than 100 Linux error codes available, and memorizing them all is not an easy task. Because of this, Linux has a utility that may be used to retrieve the details of any error code.

Utility to get Error Information

Errno is the utility available in Linux that can be used to get information about any Linux error. This utility might or might not come pre-installed on your Linux System. So let’s first see how we can install the utility if it’s not installed.

Installing the tool

“Errno” utility comes under the package moreutils. So, Depending on the type of Linux distribution you are using, you can execute the following commands to install the package.

if you are running ubuntu, run the following command:

sudo apt install moreutils

 

If you are running arch linux, run the following command:

sudo pacman -S moreutils

 

If you are running any other Linux distribution, have a look at their documentation to know how to install the “moreutils” package.

Getting Error Information

We can use errno to get information about any Linux command. All we have to do is to run the utility with the error number to get the information. For example, if we want to know what error number 2 means we run the following command in the Linux terminal.

errno 2

 

The above command will show the below output.

ENOENT 2 No such file or directory

 

Similarly, we can get the information about any other Linux error. Instead of error number, we can also use the error code to the information. For example,

errno EPERM

 

The above command will show the following output.

EPERM 1 Operation not permitted

Getting List of all available Errors

The same tool can be used to retrieve the list of all of the available Linux error codes. All we have to do is to run the tool with -l flag.

errno -l

 

The above command will return the list of all the available Linux error codes and the output will be

list of available errors

Frequently asked questions

What is the exit code in Linux?

An exit code in Linux represents the result of the execution of a command or script. It ranges from 0 to 255. We can tell whether a process ran successfully or not by looking at the exit codes.
 

What are two types of Linux User Mode?

Command Line and GUI are the two types of Linux user modes.
 

What is the maximum length for a filename under Linux?

Under Linux, a filename can only be 255 bytes long.

Conclusion 

In this blog, we have discussed Linux errors and how we can use a utility available in Linux to get information about any Linux error.

If you think this blog has helped you enhance your knowledge about the above question, and if you want to learn more, check out our articles. 

🔥 Linux - Decision Making
 

🔥 Linux File System
 

🔥 Linux- Shell Loops
 

🔥 Linux Security

And many more on our Coding Ninjas Studio.

Visit our website to read more such blogs. Make sure that you enroll in the courses we provide, take mock tests, solve problems available, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. 

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass