LINUX-9
USER ENV & SHELL (RHEL-7)
- · USER ENVIRONMENT
- · SHELL STARTUP FILES (SYSTEM)
- · SHELL STARTUP FILES (USER)
- · LOGIN PROMPT
- · LOGIN SHELLS
- · HOW TO SET VARIABLES
USER ENVIRONMENT…………………………………..
Whenever a user logged in to system, environment comes in picture.
It’s like a room decorated according to need.
Env. Can be created as per specific user basis as well as global
basis means system wide.
What env consists of ?
Path to run programs
Login prompt style
Terminal type
How above mentioned features are implemented ?
Via “VARIABLE”
So what is variable ?
A name which contains some value. It can be user defined as well as
pre system defined.
Environment variable:
1. Local
Environment Variable
2. User Environment
Variable
3. System
wide Environment Variables
1. Local
Environment Variable
Defined and applicable only for current session. Use it till session
is active and forget it after termination of session. These variables are not
stored anywhere. Defined for special purpose which is required & valid for
that session only.
2. User
Environment Variable
These settings belong to user and defined in files residing in users
home dir.
Its like I have 20 contacts in my mobile, I set different ring tone
and different profile photo for 7 users, so whenever anyone of 7 call me, I get
different ring tone and photo. And rest 13 will follow common setting, what
system has decided for them.
From above example one more thing is clear, that per user settings
are overriding system setting.
Where I can define user variables … ?
.bashrc, .bash_profile, .bash_login, .profile (at users home dir)
3. System
wide Environment Variables
Common for all users at the time of their login, or the variables
available system wide. Whenever a system is up or a user login to system, these
system wide variables are activated automatically.
/etc/bashrc, /etc/profile, /etc/profile.d
ENVIRONMENT VARIABLE FILES DESCRIPTION:
/etc/profile
Used to set system wide environmental variables on users shells.
Provides common environment like “PATH” “USER” “LOGNAME” “HOSTNAME” … for users
and startup programs. Any / All alteration in this file will apply for the
entire environment on system.
/etc/profile contains Linux system wide environment and startup
programs. It is used by all users with bash, ksh, sh shell. It only runs for
login shell.
/etc/profile.d
/etc/profile will execute the scripts within /etc/profile.d/*.sh, if
we want to define our own system variables then we need to announce them here.
/etc/bashrc
Defines functions and aliases, “umask” value set here. It includes
settings from the /etc/profile.d dir.
The /etc/bashrc is the system wide version of .bashrc.
The /etc/bashrc also executes the shell scripts within /etc/profile.d
but
only if the users shell is a Interactive Shell / Login Shell
~/.bashrc
This file sources /etc/bashrc, every time a new terminal is opened by
user all environment variables defined in this file are would be in action.
~/.bash_profile
This file is user specific remote login file. Environment variables
listed in this file are invoked every time the user is logged in remotely i.e.
using ssh session.
If this file is not present, system looks for either .bash_login or .profile
files.
/etc/environment
Systems wide file for creating, editing or removing any environment
variables, for all users, local and remote login.
Environment variables created in this file are accessible by all throughout
the system.
SEQUENCE OF EXECUTION……………..
1st /etc/profile
2nd ~/.bash_profile
3rd ~/.bashrc
4th /etc/bashrc
PROMPT SETTING………………………..
PS1 Primary prompt
string
[root@rhel7-server ~]# cat /etc/bashrc |grep -i ps1
if [ "$PS1" ]; then
[ "$PS1" =
"\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
# if [ "$PS1" ];
then
# PS1="[\u@\h:\l \W]\\$ "
if [
"$PS1" ]; then
[root@rhel7-server ~]# echo $PS1
[\u@\h \W]\$
\u username
\h hostname
\W current dir, base dir or
final component only
\$ for regular users
\w the current working
directory, with $HOME abbreviated with a tilde
PS2 Secondary prompt
string
LOGIN SHELLS…………………………..
A terminal helps us to get logged in to system by a program called
/bin/login.
Here my login shell is bash.
root@rhel7-server:~ # echo $0
-bash
First character “-“ means the shell is login shell.
When bash runs as a login shell, first it looks for
ð /etc/profile
ð Then
for a user’s .bash_profile, .bash_login and .profile file
ð Running
only the first one that it found/sees first.
Check the environment variables,
root@rhel7-server:~ # printenv |more
XDG_SESSION_ID=173
HOSTNAME=rhel7-server
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.234.1 1739 22
SELINUX_USE_CURRENT_RANGE=
SSH_TTY=/dev/pts/1
USER=root
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
** “env” and “set” command can also be used.
VARIABLE SETTING…………………………………..
[raman@rhel7-server ~]$ myvar="i am new to linux"
[raman@rhel7-server ~]$ echo $myvar
i am new to linux
[raman@rhel7-server ~]$ echo $SHELL
/bin/bash
CHANGING THE SHELL………..
[raman@rhel7-server ~]$ csh
[raman@rhel7-server ~]$ echo $myvar
myvar: Undefined variable.
[raman@rhel7-server ~]$ exit
Exit
BACK TO OLD SHELL………..
[raman@rhel7-server ~]$ echo $myvar
i am new to linux
[raman@rhel7-server ~]$ exit
Logout
EXIT FROM USER RAMAN…….
[root@rhel7-server ~]# id
uid=0(root) gid=0(root) groups=0(root)
context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
AGAIN LOGIN TO USER RAMAN….
[root@rhel7-server ~]# su - raman
Last login: Sun Jan 1 15:11:13
IST 2017 on pts/1
[raman@rhel7-server ~]$ echo $myvar
[raman@rhel7-server ~]$
NOTHING IN O/P, MEANS THAT VARIABLE WAS ONLY VALID FOR THAT SESSION……
LET’S CHECK IT FOR OTHER USER ALSO,
[root@rhel7-server ~]# su - user1
Last login: Sun Jan 1 15:19:57
IST 2017 on pts/1
[user1@rhel7-server ~]$ echo $myvar
[user1@rhel7-server ~]$
NOTHING IN O/P, MEANS THAT VARIABLE WAS ONLY VALID FOR THAT SESSION
AS WELL AS THAT USER ONLY……
LET’S MAKE IT GLOBAL…………..
[root@rhel7-server ~]# vi /etc/profile
Added this entry at last,
myvar="i am new to linux"
system is not booted after change so we need to source the file to
make it applicable from now.
[root@rhel7-server ~]# . /etc/profile
[root@rhel7-server ~]# su - user1
Last login: Sun Jan 1 15:21:04
IST 2017 on pts/1
[user1@rhel7-server ~]$ echo $myvar
i am new to linux
[user1@rhel7-server ~]$ exit
Logout
[root@rhel7-server ~]# su - user3
Last login: Wed Dec 28 17:16:52 IST 2016 on pts/0
[user3@rhel7-server ~]$ echo $myvar
i am new to linux
[user3@rhel7-server ~]$
[root@rhel7-server ~]# su - user1
Last login: Sun Jan 1 15:30:57
IST 2017 on pts/2
Created a script at different path……..
[user1@rhel7-server ~]$ /opt/myscript/script1
/home/user1
rhel7-server
total 0
Sun Jan 1 15:35:32 IST 2017
[user1@rhel7-server ~]$ . script1
-bash: script1: No such file or directory
[user1@rhel7-server ~]$
[root@rhel7-server ~]# cd /home/user1/
[root@rhel7-server user1]# vi .bashrc
Added following entries at end
PATH=$PATH:/opt/myscript
export PATH
[user1@rhel7-server ~]$ . script1
-bash: script1: No such file or directory
Why ??
Still the old settings are in effect…………..
[user1@rhel7-server ~]$ . .bashrc
[user1@rhel7-server ~]$ . script1
/home/user1
rhel7-server
total 0
Sun Jan 1 15:56:59 IST 2017
No comments:
Post a Comment