Introduction
The shell is a command-line interpreter that provides a user interface for interacting with the operating system. In Linux, several shells are available, such as Bash, Zsh, Fish, and Tcsh, each with its own features and syntax. When you log in to a Linux system, a specific shell is launched as your default. This tutorial explains how to view and change your default login shell.
Understanding the Default Shell
Your default login shell is the shell that automatically starts when you log in to the system, either through a terminal emulator, SSH, or the console. The shell interprets your commands and executes the corresponding actions. While you can always launch a different shell within a session (e.g., typing bash in a Tcsh session), changing the default shell alters the shell that is started automatically for new sessions.
Viewing Your Current Default Shell
You can determine your current default shell using the echo command combined with the $SHELL environment variable:
echo $SHELL
This will output the path to your currently configured default shell, for example, /bin/tcsh or /bin/bash. You can also use the getent command:
getent passwd $USER | cut -d: -f7
This command retrieves user information from system databases, extracts the shell field, and displays it.
Changing Your Default Shell
The primary tool for changing your default login shell is the chsh (change shell) command.
Basic Usage:
chsh -s /path/to/new/shell
Replace /path/to/new/shell with the absolute path to the shell you want to set as the default. For example, to change your shell to Bash, you would use:
chsh -s /bin/bash
Important Considerations:
- Permissions: You can only change the default shell for your own user account unless you have root (administrator) privileges.
- Valid Shells: The shell you specify must be listed in the
/etc/shellsfile. This file contains a list of valid shells allowed on the system. If the shell is not in this file,chshwill likely refuse to make the change. You can view the contents of/etc/shellswith:cat /etc/shells. - Root User: To change the shell for another user, you must use
sudo chsh -s /path/to/shell username. - Logging Out and Back In: The most critical step is to log out and then log back in. The shell change will not take effect in your current session. You must start a new session for the changes to be applied.
Example:
Let’s say you want to switch from Tcsh to Bash. First, verify Bash is in /etc/shells. Then, run:
chsh -s /bin/bash
You’ll be prompted for your password. After entering it, log out of your current session and log back in. When you open a new terminal, you should now be using Bash as your default shell.
Alternative Methods (Less Common)
While chsh is the recommended method, some older systems or specific configurations might involve directly editing user account information. However, these methods are generally discouraged due to the potential for errors and system instability. They are described here for informational purposes only.
- Editing
/etc/passwd(Not Recommended): The/etc/passwdfile stores user account information, including the default shell. Directly editing this file is risky and should be avoided unless you are an experienced system administrator. Any errors in this file can prevent users from logging in. - Profile Scripts (
~/.profile,~/.bashrc, etc.): While not a direct way to change the default login shell, you can influence the shell environment by modifying profile scripts. These scripts are executed when you log in or start a new interactive shell. You can add commands to these files to customize your shell environment, but this does not change the default shell itself.