- Who Is Root? Why Does Root Exist?
- What is root?
- Gaining Access to root Permissions
- What is the difference between ‘su -‘ , ‘sudo bash’ and ‘sudo sh’?
- 2 Answers 2
- 8 Types of Linux Shells
- What is a Linux Shell and Why is it Important?
- Types of Linux Shells
- 1. Bourne Shell (sh)
- 2. C Shell (csh)
- 3. TENEX C Shell (tcsh)
- 4. KornShell (ksh)
- 5. Debian Almquist Shell (dash)
- 6. Bourne Again Shell (bash)
- 7. Z Shell (zsh)
- 8. Friendly Interactive Shell (fish)
Who Is Root? Why Does Root Exist?
Have you ever wondered why there is a special account named root in Linux? Do you know what are the recommended best practices to use this account? Are you aware of the scenarios where it must be used and those where it doesn’t? If you answered “yes” to one or more of these questions, keep reading.
In this post we will provide a reference with information about the root account that you will want to keep handy.
What is root?
To begin, let us keep in mind that the hierarchy of directories in Unix-like operating systems has been designed as a tree-like structure. The starting point is a special directory represented by a forward slash (/) with all other directories initially branching off from it. Since this is analogous to an actual tree, / is called the root directory.
In the following image we can see the output of:
which illustrates the analogy between / and the root of a tree.
Although the reasons behind the naming of the root account are not quite clear, it is likely due to the fact that root is the only account having write permissions inside / .
Additionally, root has access to all files and commands in any Unix-like operating system and it is often referred to as the superuser for that reason.
On a side note, the root directory (/) must not be confused with /root , which is the home directory of the root user. In fact, /root is a subdirectory of / .
Gaining Access to root Permissions
When we talk about root (or superuser) privileges, we refer to the permissions that such account has on the system. These privileges include (but are not limited to) the ability to modify the system and to grant other users certain access permissions to its resources.
The reckless use of this power can lead to system corruption at best and total failure at worst. That is why the following guidelines are accepted as best practices when it comes to accessing the privileges of the root account:
Initially, use the root account to run visudo. Use that command to edit /etc/sudoers to grant the minimum superuser privileges that a given account (e.g. supervisor) needs.
Moving forward, login as supervisor and use sudo to perform user management tasks. You will notice that attempting to perform other tasks that require superuser permissions (removing packages, for example) should fail.
Repeat the above two steps whenever needed, and once done, use the exit command to return to your unprivileged account immediately.
At this point you should ask yourself, Are the any other tasks that pop up on a periodic basis that need superuser privileges? If so, grant the necessary permissions in /etc/sudoers either for a given account or group, and continue avoiding the use of the root account at the extent possible.
Summary
This post can serve as a reference for the proper use of the root account in a Unix-like operating system. Feel free to add it to your bookmarks and return as many times as you want!
As always, drop us a note using the comment form below if you have any questions or suggestions about this article. We look forward to hearing from you!
What is the difference between ‘su -‘ , ‘sudo bash’ and ‘sudo sh’?
I cannot login as root while doing su — because I do not have the root password — I mean I have my user password with which I can sudo «command» but this password does not work for su — I can log in as root with sudo bash with my user password and I can login while doing sudo sh but then I get a command prompt in this form #
2 Answers 2
In Linux (and Unix in general), there is a SuperUser named Root. The SuperUser can do anything and everything, and thus doing daily work as the SuperUser can be dangerous. You could type a command incorrectly and destroy the system. Ideally, you run as a user that has only the privileges needed for the task at hand. In some cases, this is necessarily Root, but most of the time it is a regular user.
This command is used to login at root account.
By default, the Root account password is locked in Ubuntu. This means that you cannot login as Root directly or use the su command to become the Root user. However, since the Root account physically exists it is still possible to run programs with root-level privileges. This is where sudo comes in — it allows authorized users (normally «Administrative» users) to run certain programs as Root without having to know the root password.
This command runs «sh» as a super user.
The sh utility is a command language interpreter that shall execute commands read from a command line string, the standard input, or a specified file.
This command runs «bash» as a super user.
Bash is the shell, or command language interpreter. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.
8 Types of Linux Shells
Linux comes with several different shells. Although a distribution has a default shell, users can change to another type or install a new one.
If you’re looking to change your default shell, knowing the different available types helps provide a clear overview of the possible options.
This article showcases the importance and features of eight different Linux shells.
What is a Linux Shell and Why is it Important?
A shell is a command-line interpreter program that parses and sends commands to the operating system. This program represents an operating system’s interactive interface and the kernel’s outermost layer (or shell). It allows users and programs to send signals and expose an operating system’s low-level utilities.
The terminal program (or terminal emulator) enables interaction with the system’s utilities. When we run any command in the terminal, such as ls or cat, the shell parses, evaluates, searches for, and executes the corresponding program, if found.
Types of Linux Shells
Linux offers different shell types for addressing various problems through unique features. The shells developed alongside Unix often borrowed features from one another as development progressed.
Below is a brief overview of different shell types and their features.
1. Bourne Shell (sh)
The Bourne shell was the first default shell on Unix systems, released in 1979. The shell program name is sh, and the traditional location is /bin/sh. The prompt switches to $, while the root prompt is #.
The Bourne shell quickly became popular because it is compact and fast. However, sh lacks some standard features, such as:
- Logical and arithmetic expansion.
- Command history.
- Other comprehensive features, such as autocomplete.
Modern Unix-like systems have the /bin/sh executable file. The program does not start the Bourne shell but acts as an executable file pointing to the default system shell.
For most systems, the hard or symbolic link points to bash, while on Ubuntu and Debian, the link is to dash. In both cases, the link mimics the Bourne shell as much as possible.
2. C Shell (csh)
The C shell (csh) is a Linux shell from the late 1970s whose main objective was to improve interactive use and mimic the C language. Since the Linux kernel is predominantly written in C, the shell aims to provide stylistic consistency across the system.
The path to the C shell executable is /bin/csh. The prompt uses % for regular users and # for the root user.
New interactive features included:
- History of the previous command.
- User-defined aliases for programs.
- Relative home directory (~).
- Built-in expression grammar.
The main drawbacks of the C shell are:
- Syntax inconsistencies.
- No support for standard input/output (stdio) file handles or functions.
- Not fully recursive, which limits complex command handling.
The C shell improved readability and performance compared to the Bourne shell. The interactive features and innovations in csh influenced all subsequent Unix shells.
3. TENEX C Shell (tcsh)
The TENEX C shell (tcsh) is an extension of the C shell (csh) merged in the early 1980s. The shell is backward compatible with csh, with additional features and concepts borrowed from the TENEX OS.
The TENEX C shell executable path is in /bin/tcsh. The user prompt is hostname:directory> while the root prompt is hostname:directory# . Early versions of Mac OS and the default root shell of FreeBSD use tcsh.
Additional features of the shell include:
- Advanced command history.
- Programmable autocomplete.
- Wildcard matching.
- Job control.
- Built-in where command.
Since tcsh is an extension of the C shell, many drawbacks persist in the extended version.
4. KornShell (ksh)
The KornShell (ksh) is a Unix shell and language based on the Bourne shell (sh) developed in the early 1980s. The location is in /bin/ksh or /bin/ksh93, while the prompt is the same as the Bourne shell ( $ for a user and # for root).
The shell implements features from the C shell and Bourne shell, aiming to focus on both interactive commands and programming features. The KornShell adds new features of its own, such as:
- Built-in mathematical functions and floating-point arithmetic.
- Object-oriented programming.
- Extensibility of built-in commands.
- Compatible with the Bourne shell.
The shell is faster than both the C shell and the Bourne shell.
5. Debian Almquist Shell (dash)
The Debian Almquist Shell (dash) is a Unix shell developed in the late 1990s from the Almquist shell (ash), which was ported to Debian and renamed.
Dash is famous for being the default shell for Ubuntu and Debian. The shell is minimal and POSIX compliant, making it convenient for OS startup scripts.
The executable path is /bin/dash, in addition to /bin/sh pointing to /bin/dash on Ubuntu and Debian. The default and root user prompt is the same as in the Bourne shell.
- Execution speeds up to 4x faster than bash and other shells.
- Requires minimal disk space, CPU, and RAM compared to alternatives.
The main drawback is that dash is not bash-compatible. The features not included in dash are known as «bashisms.» Therefore, bash scripts require additional reworkings of bashisms to run succesfully.
6. Bourne Again Shell (bash)
The Bourne Again shell is a Unix shell and command language created as an extension of the Bourne shell (sh) in 1989. The shell program is the default login shell for many Linux distributions and earlier versions of macOS.
The shell name shortens to bash, and the location is /bin/bash. Like the Bourne shell, the bash prompt is $ for a regular user and # for root.
Bash introduces features not found in the Bourne shell, some of which include:
- Brace expansion.
- Command completion.
- Basic debugging and signal handling.
- Command history.
- Conditional commands, such as the bash if and bash case statements.
- Heredoc support.
Note: Some features are not unique to Bash, but rather borrowed from other shells.
Since bash is a superset of the Bourne shell, most sh scripts execute in bash without any additional changes.
7. Z Shell (zsh)
The Z shell (zsh) is a Unix shell created as an extension for the Bourne shell in the early 1990s. The feature-rich shell borrows ideas from ksh and tcsh to create a well-built and usable alternative.
The executable location is in /bin/zsh. The prompt is user@hostname location % for regular users and hostname# for the root user. The Z shell is the default shell of Kali Linux and Mac OS.
Some new features added to the zsh include:
- Shared history among all running shell sessions.
- Improved array and variable handling.
- Spelling corrections and command name autofill.
- Various compatibility modes.
- Extensibility through plugins.
The shell is highly configurable and customizable due to the community-driven support through the Oh My Zsh framework.
8. Friendly Interactive Shell (fish)
The Friendly Interactive Shell (fish) is a Unix shell released in the mid-2000s with a focus on usability. The feature-rich shell does not require additional configuration, which makes it user-friendly from the start.
The default executable path is /usr/bin/fish. The user prompt is user@hostname location> , while the root prompt is root@hostname location# .
Features in the shell include:
- Advanced suggestions/tab completion based on the current directory history.
- Helpful syntax highlighting and descriptive error messages.
- Web-based configuration.
- Command history with search options.
The main drawback of fish is non-POSIX compliance. However, the developers aim to improve flawed designs from POSIX.
After reading about several different shell types in this article, you have a better overview of the available Linux shells. Other shells exist for specific use cases, and every shell type addresses different problems.