Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Changing the Default Shell in Linux
Changing the default shell in Linux is a common administrative task that gives you the flexibility to use different command-line interfaces. The default shell of most Linux systems is bash, but you can replace it with alternatives such as sh, fish, dash, or zsh. There are several reasons why you might need to change your default shell:
To disable or block normal user logins using a nologin shell.
Change the default shell on a shared network to meet specific user requirements.
Implement shell wrapper programs that control command execution until proper authentication.
This guide covers all the available methods for changing the default shell in Linux systems.
Checking Available and Current Shell
Before changing the default shell, you need to know which shells are installed on your system. The /etc/shells file contains all available shells:
cat /etc/shells
# /etc/shells: valid login shells /bin/sh /bin/bash /usr/bin/bash /bin/rbash /usr/bin/rbash /usr/bin/sh /bin/dash /usr/bin/dash
To find your current shell, use any of these commands:
# Method 1: Check user entry in passwd file
grep "^${USER}" /etc/passwd
# Method 2: Using whoami
grep `whoami` /etc/passwd
# Method 3: Environment variable
echo $SHELL
# Method 4: Current running shell
ps -p $$
prateek:x:1000:1000:Prateek Jangid,,,:/home/prateek:/bin/bash /bin/bash PID TTY TIME CMD 2357 pts/0 00:00:00 bash
Method 1: Using the chsh Command
The chsh (change shell) command is the standard way to change your default shell. It doesn't require root privileges for changing your own shell:
chsh -s /path/to/new/shell
Example changing from bash to sh:
chsh -s /bin/sh
To change another user's shell (requires sudo):
chsh -s /bin/sh username
Method 2: Using the usermod Command
The usermod command modifies user account information, including the login shell. This method requires root privileges:
sudo usermod --shell /path/to/new/shell username # or using short option sudo usermod -s /path/to/new/shell username
Example changing the current user's shell to sh:
sudo usermod --shell /bin/sh $USER
Method 3: Manual Editing of /etc/passwd
You can directly edit the /etc/passwd file to change the default shell. This method allows changing multiple users' shells simultaneously:
sudo nano /etc/passwd
The file structure shows user information in this format:
username:password:UID:GID:GECOS:home_directory:login_shell
Example changing root's shell from bash to sh:
| Before | After |
|---|---|
| root:x:0:0:root:/root:/bin/bash | root:x:0:0:root:/root:/bin/sh |
Warning: When manually editing /etc/passwd, ensure the shell path is valid. An invalid shell path can prevent user login.
Verification and Best Practices
After changing the shell, verify the change by:
Logging out and back in to activate the new shell
Running
echo $SHELLto confirm the changeTesting that the new shell functions correctly
Comparison of Methods
| Method | Privileges Required | Multiple Users | Validation |
|---|---|---|---|
| chsh | User (own shell) / Sudo (others) | One at a time | Automatic |
| usermod | Sudo required | One at a time | Automatic |
| Manual editing | Sudo required | Multiple users | Manual |
Conclusion
Changing the default shell in Linux can be accomplished using chsh, usermod, or manual editing of /etc/passwd. The chsh command is the safest and most user-friendly option, while usermod provides administrative flexibility. Manual editing should be used cautiously but allows bulk changes for multiple users simultaneously.
