Shell Environment and its types

Linux Tutorial

File:Gnu-bash-logo.svg

BASH has many Shell Environment variables on which its function depends. The list of environment variables which is set for current session can be listed out using env or printenv commands.

$ env
$ printenv

The Shell Environment is set, as per the configuration distributed in different files. And the list of required bash configuration files are listed below:-

  • /etc/bashrc
  • /etc/profile
  • ~/.bashrc
  • ~/.bash_profile

 

Know about Interactive and Non-Interactive Shell

When the user starts a terminal in GUI session or when user login into CLI, Interactive Shell is started. Interactive shell is where the shell session is attached to a terminal.

Non-Interactive shell is where the shell session is not attached to a terminal, such shell starts while executing shell scripts.

 

Know about Login AND Non-Login Shell

When the user starts a shell session by authenticating, login shell is started or when user login into terminal or through SSH, login shell is started. In login shell following files are sourced.

  • /etc/bashrc
  • /etc/profile
  • ~/.bashrc
  • ~/.bash_profile

When user starts a shell session from already running session, non-login shell is started. Or When terminal is started in GUI, when we run bash in running shell session, when substituting user with su, a non-login shell is started. In non-login shell following files are sourced.

  • /etc/bashrc
  • ~/.bashrc

Login or Non-Login shell can be identified easily by executing, echo $0 If the output is ‘-bash’ the it is login shell, if the output is ‘bash’ then it is non-login shell.

$ echo $0
-bash

Also certain commands work in login shell only like logout which terminates the session, works only in login shell. Executing logoutin non-login shell will give an error.

 

What is SU in Linux?

su is used to change the identity of the user without logout and login again. su command takes username as input. If the username is not passed to su, By default root user is considered.

Invoking su starts a non-login shell.

$ su
Password:
# echo $0
bash
# logout
bash: logout: not login shell: use `exit'
# exit
$

To start a login shell with su pass ‘-‘ as argument.

$ su - root
Password:
# echo $0
-bash
# logout
$

To directly execute a command as another user pass option ‘c’ to su following the command to be executed.

$ su -c "id" -
Password:
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
$

What is SUDO in Linux?

su can be used to temporarily gain superuser privileges to perform certain tasks. The disadvantage is that users can do a lot more than they have gained the superuser privileges for. Users can gain superuser privileges for adding a new user and they can also remove complete /etc/ directory as well. Additionally, the users which are using su needs to know the password of root user account or other user accounts as they are trying to get the access of.

The sudo command allows a user to be permitted to run a command as root, or as another user. Unlike susudo requires users to enter their own password for authentication. So users do not need to know the passwords of accounts they are trying to run the command as.Access to other accounts via sudo is managed by a file /etc/sudoers.

In RHEL 7wheel group is allowed to use sudo to run commands as any user, including root. The users will be prompted for their own password.

If any regular user wants to use sudo, they must be the member of wheel group.

# usermod -G wheel student

Above command when executed as root, will make user student a member of wheel group. Then student can use sudo command to run any command as root, as given below.

$ touch /testfile
ERROR
$ sudo touch /testfile
$ ls /