Linux administration

Tune the User Environment and System Environment Variables

Nguyen Hai Chau
Vietnam National University

Configuration Files

  • Many programs, including shells, read configuration files
  • Files which apply to only one user are stored in that user's home directory, usually as hidden files

    • Use ls -A in your home directory to find them
    • Hidden files have names which start with .
    • Often such files have names ending in rc, for 'run commands', for example the Vim editor uses .vimrc
  • Sometimes whole directories of configuration information are present in a home directory, for example .kde and .gnome

Shell Configuration Files

  • Bash reads ˜/.bashrc whenever it starts as an interactive shell
    • That file often sources a global file, for example:
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
  • Bash also reads a profile file if it is a login shell
    • First it reads the global configuration from /etc/profile
    • Then one of ˜/.bash_profile, ˜/.bash_login or ˜/.profile
  • Login shells also source ˜/.bash_logout when the user exits

Changing Environment Variables

  • The value of an environment variable can be set on the command line or in a configuration file as follows:
export VARIABLE=VALUE
  • To see the current value of a variable: echo $VARIABLE
  • The shell searches for programs to run in a list of directories in the variable $PATH, which are separated by the : character
    • If you want to run programs which aren't in /bin or /usr/bin then you might want to add them to your $PATH, for example:
export PATH="$PATH:/usr/local/bin:/usr/games"
  • Some other variables, such as $INFOPATH, use the same convention

Changing the Prompt

  • Setting PS1 will change the shell prompt, for example:
export PS1=':\w\$ '
  • Characters preceded by \ are given special interpretations, for example:
    • \t and \d display the time and date in the prompt
    • \w or \W show the current working directory
    • \$ shows up as either $ or #, depending on whether you are a normal user or root
    • \u displays your username
    • \h displays the hostname of the machine
  • PS2 is an alternative prompt, displayed when bash needs more input before it can run a complete command

Shell Aliases

  • It is often useful to have bash aliases for commands like ls and ls -l, perhaps adding options:
alias "l=ls --color=auto -F"
alias "ll=l -l"
  • The alias command with no arguments will show a list of currently defined aliases
  • To show what one particular alias is set to, pass the name to alias without setting it to anything:
alias l

Setting Up Home Directories for New Accounts

  • When a new user account is created, a new home directory is also made
  • Each new home directory is populated with a skeleton set of configuration files
    • These are copied from /etc/skel by the useradd command
  • Setting up these files with useful defaults can make life easier for new users
  • Linux distributions usually have a simple /etc/skel directory with a few files in

Exercise 1

  • a. Use the shell builtin alias to get a list of the aliases currently defined.
  • b. Define a new alias for changing to the parent directory (i.e., cd ..). For example, you could call it up.
  • c. Edit your .bashrc file to add the alias to it permanently, so that the alias command is run whenever a shell starts.
  • d. Login as root and look in the directory /etc/skel to find out what configuration files a new user will get.
  • e. Create a text file called .signature, which is the signature appended to emails you send.

Exercise 2

  • a. Use echo to print the current value of the $PS1 environment variable.
  • b. Try setting it to something different. You might like to try putting some of the special \ sequences in, but remember to use single quotes, so that the backslashes are not interpreted by the shell.
  • c. Decide how you would like your prompt to appear, and edit your .bashrc file to set PS1 every time you start a shell.