how to change the default shell of an user in linux?
There are several different command shells available to you in Linux. bash, csh, tcsh, ksh, zsh, sh and fish are just a few of the popular ones that can be used by an user from the command line. As a system administrator, you can specify a default shell for the user. The user can however choose the run another shell of his choice later by overriding the default shell.
For this post, we will assume that your current default shell is csh and you want to change it to bash. The steps will however work just the same for any shell that you want to change to.
First thing to do is to make sure that you have the required shell installed, in this case the bash shell. You need to find the path to the shell executable or where the shell is installed. The shells are usually installed in the bin folder unless you have a distro which installs in other locations such usr/bin or usr/local/bin.
find location of the shell
You can find where the shell is installed by using either which or whereis command. The which command will show you exactly where in the path the shell executable is.
$ which bash
If which cannot find the shell you want to change to, and you are sure that it is installed then you might want to double check the environment path and make sure that the installed folder is in the path. The location of the shell executable is all you need to change the default shell.
Unlike the which command, the whereis command will show you the location of the binary/executable, source and documentation of the command. The first path in the output is the location of the shell binary.
$ whereis bash
You can see the list all the shells that are installed on your system in the /etc/shells file. Use the cat command to print out the contents of this file. It lists the binary file location of the installed shells. You can change to any of the shell listed here.
$ cat /etc/shells
change the default shell
Once you have the location of the new shell, you can change the default for any user as long as you have the root or super user credentials. You can use either the usermod or the chsh command to do it. You can also do it manually by editing the passwd file.
$ usermod -s /bin/bash <username>
usermod is the command used to modify user accounts. The -s or –shell option is used specify the default shell for the user. Substitute <username> in the above example with the actual username of the user.
Another command you can use is chsh. It changes the login shell or the default command shell for any user. The syntax of this command is just about the same as the usermod command above.
$ chsh -s /bin/bash <username>
The -s or –shell option is for the path of the new shell followed by the actual username for the user.
You can also change the shell directly in the /etc/passwd file. This is probably more riskier than using the commands above. The default shell of the user is the last field on the line where the user is listed. Use your favorite text editor to open the /etc/passwd file and search and find the user name in the file. You will see something like this :
tom:x:1000:100::/home/tom:/bin/csh
Now change the "/bin/csh" part of the line to "/bin/bash" and save the file. The modified line will look like this:
tom:x:1000:100::/home/tom:/bin/bash
change shell at time of use
If you do not have root access or the permission to modify the /etc/passwd file, then you are left with the option of executing the shell after you have logged in. This does not change the default shell but still allows you use the shell of your choice.
First, you need to find the location of the shell as described above. Now, find the file that configures your current shell. You will see that there are several files that fits, such as .profile, .bashrc, .bash_profile, .login etc etc. The differences between these files are beyond the scope of this post.
I recommend that you use the rc file, for example .bashrc or .cshrc. The reason for using it is that it is read by the interactive, login and non-login prompts. You can use any of the configuration files depending on your requirements.
So, if your default shell is csh then open the .cshrc file in your favorite text editor and add the following to it…
setenv SHELL /bin/bash exec /bin/bash --login
If your current default shell is something other than csh, then you will use the corresponding file. You can just use the .login or .profile as well, which are usually read by most shells. Be sure to use the correct syntax for setting the environment variable SHELL for the shell.
Also, you will need to add any personalization and shell specific configuration in the config file of the new executing shell. That means you can set aliases and prompt format etc in the .bashrc file once you have changed to the bash shell.