An organization typically has two types of servers. One type has relatively static content, such as a DHCP or DNS server. The second type of server contains user files. In both cases, providing a consistent work environment for the end user is a must. Uniformity of behavior over the server environment permits tool sets and scripts to be used cross-platform. Additionally, users do not have to remember multiple command syntaxes to achieve the same goal. In the following sections, we look at how this can be achieved in SLES first by defining a consistent command interpreter and then by selecting a standard set of variables the user can use. Default ShellPreviously, we detailed the structure of the /etc/passwd file and identified that the last field on each record identifies the default, or login, shell. A login shell is a fancy name for a command interpreter and its associated environment. Simply put, a shell is a program. A shell can be run interactively, spawned off as a child process, or run in the background through cron. As with any other program, a shell expects a predetermined syntax for commands and parameters. Within the lifetime of the shell process, the program allows for interaction with the operating system, other processes, and the file system. Unlike some operating systems, Linux offers a variety of different shells such as ksh, zsh, bash, and others. Each shell supported by SLES incorporates its own syntax and command set. By specifying an initial shell for each user, you can define the type of environment a user experiences when interacting with the system. A standard should be set for each organization to identify the default shell for all accounts. Each shell type uses a different set of configuration files. To facilitate system management, restricting changes to a single set of configuration files reduces the chances of errors. Choosing a standard shell allows scripting efforts by one individual to be shared with other users. Selecting which shell to standardize on will invoke various arguments from the different camps as to which is best. For the purpose of this book, we restrict ourselves to describing the bash shell.
bash is a hybrid shell that is compatible with the original Unix sh shell but contains some features originally present only in the Korn and C shells. The following is a quick summary of what bash makes available:
Using a standard shell across the enterprise is important. It provides consistency of interface and allows for the porting of scripts from server to server. In the following section, we examine another benefit of a consistent default shell: controlling login-generated information. Login Scripts and Environment VariablesIn the preceding section, you saw that a shell is simply an instance of a program or command interpreter. This is true whether it is controlling an interactive session or running a script. A login shell is a special case of such an instance, and it triggers a number of additional events. A login shell executes a number of additional scripts when it is invoked. These additional scripts are used to tailor the environment the user will experience. You, as the system administrator, can customize these scripts to your advantage. These login scripts can modify search paths pointing to customized resources or set up environment variables specific to third-party software. The names of the logon scripts depend on the flavor of the default login shell. If all the users on a system share the same default login shell, maintenance of login scripts will be greatly simplified. In the case of the bash shell, the system-wide login script executed at login is stored as /etc/profile. SUSE LINUX suggests that you maintain a customized version of your login script in /etc/profile.local instead. This is done to prevent the overwriting of any customizations at upgrade time. At login, the /etc/profile script will be run followed by /etc/profile.local. One of the benefits of using a managed login script is allowing control over the setting of environment variables that can be used in other scripts or applications. Typical examples would be setting an environment variable ORACLE_HOME to point to the base directory for your Oracle install or a BACKUP_DIR variable to specify a target location for your backups. Within the /etc/profile script, you will also find numerous calls to other scripts. One such script, /etc/bash.bashrc, creates a number of aliases that can be used as shortcuts for other commands. Listing 5.2 shows a few of the aliases provided. If you want to add a number of system-wide aliases, SLES recommends that you place them in the local version, bash.bashrc.local. This script will be run following bash.bashrc. Listing 5.2. Some of the Aliases Generated in /etc/bash.bashrcalias +='pushd .' alias -='popd' alias ..='cd ..' alias ...='cd ../..' alias dir='ls -l' alias la='ls -la' alias ll='ls -l' The default aliases created in bash.bashrc allow users to type plus (+) to save their current working directory and minus (-) to return to that directory without the need for typing pushd and popd. Setting system-wide environment variables and aliases can make the system more comfortable for the users. A case in point would be the dir alias in Listing 5.2 that allows for DOS-type commands to appear to work in Linux. To allow user-based customizations, bash also interprets scripts in the user's directory. At login, bash runs the first of the following files it finds in the user's directory: .bash_profile, .bash_login, or .profile. These local scripts can be used to set or override variables to better suit the end user's requirements. A typical snippet of a .bash_profile would look like Listing 5.3. In this snippet, you can see that two environment variables, BACKUP_DIR and WEB, are created, as well as an alias. The alias st, short for status, yields information on the current logged-on process, disk quota usage, as well as a summary of disk space in the user's home directory. When the user is logged in, the variables have the username of the current session in the proper locations, as shown in Listing 5.4. It is important to note that an alias is not evaluated at login; it is evaluated when invoked. Listing 5.3. Content of a Typical ./bash_profileBACKUP_DIR=/backup/$USER ; export BACKUP_DIR WEB="/home/$USER/public_html" ; export WEB alias st='finger $USER ; quota $USER ; du --si -s $HOME' Listing 5.4. Verification of Environment Variables After Logineric@Athena:~> env | grep -e BACKUP BACKUP_DIR=/backup/eric eric@Athena:~> env | grep -e WEB WEB=/home/eric/public_html eric@Athena:~> alias -p | grep -e st= alias st='finger $USER ; quota $USER ; du --si -s $HOME' eric@Athena:~> When the $USER symbol is placed in the definitions in Listings 5.3 and 5.4, the .bash_profile script is more portable from machine to machine and user to user. A user with a privileged account on one machine, such as WEBMASTER, should have a regular account, such as eric on other servers. By using standard variables provided by bash, the login script becomes user-independent. Upon logout, the bash shell calls one final script: .bash_logout. If this file exists at logout time, it will be executed. Users can set up routines to back up files or force a cleanup of temporary files. Here is the content of a sample .bash_logout: eric@Athena:~> cat ./.bash_logout mv Documents.tar.gz old_Documents.tar.gz tar -zcvf Documents.tar.gz ./Documents eric@Athena:~> This script makes a copy of the current archive and then creates a backup of the content of the Documents subfolder in the account's root directory. If required during an interactive session, this routine can be invoked as ./.bash_logout. A number of automated script processes take place on login and logout. By defining environment variables and aliases, the system administrator can provide the user with a more consistent environment from server to server. |