⬆️ ⬇️

Linux basics from the founder of Gentoo. Part 3 (4/4): Setting Up the User Environment

The final chapter of the third installment of the Linux beginner tutorial series. Basics of configuring the working shell, setting environment variables, as well as summing up the entire third part.



Navigating Linux basics from the founder of Gentoo:



Part I:
  1. BASH, navigation basics
  2. File and Directory Management
  3. Links, and deleting files and directories
  4. Glob substitutions
Part II:
  1. Regular expressions
  2. Folder Assignments, File Search
  3. Process management
  4. Text processing and redirection
  5. Kernel modules
Part III
  1. Documentation
  2. Access rights model
  3. Account Management
  4. Setting up the environment


Custom environment setting



Meet “fortune”



Your environment has many useful options that you can change to your liking. However, until now, we have not discussed how to restore these settings every time you log in, except that each time you type them again. In this section, we will look at setting up your environment by editing the starting configuration files.



To begin with, let's show a friendly message when you log in. To see an example of such a message, run fortune :



$ fortune

No amount of careful planning will ever replace dumb luck.



(the fortune application may not be installed, run the installation in the package manager of your distribution, for example apt-get install fortune - note.)

')

.bash_profile



Now let's do this so that fortune runs on every login. Using your favorite text editor, edit the .bash_profile file in your home directory. If the file does not exist, create it. Paste it at the beginning:



fortune



Try logging out and log back. Before launching a display manager, such as xdm, gdm or kdm, you will see a cheerful greeting when you enter:



mycroft.flatmonk.org login: chouser

Password:

Freedom from incrustations of grime is contiguous to rectitude.

$



Login shell



When you run bash, it passes the .bash_profile file in your home directory, starting each line as if typing it on the command line. This is called file sourcing.



Bash can work in different ways depending on how it is started. If it is running as a login shell, it will work as described above — first processing the system-wide / etc / profile, and then your personal ~ / .bash_profile.



There are two ways to run bash as a login shell. The first is used when you first log into the system: bash is launched with the process name -bash. You can see this in the list of processes:



$ ps u

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

chouser 404 0.0 0.0 2508 156 tty2 S 2001 0:00 -bash



You will probably see a longer list, but there will be at least one line with a line in front of the name of your shell, like -bash in the example above. This feature is used by the shell to determine whether it was launched during authorization.



Understanding --login



The second way to run bash as a login shell is with the -login option. From time to time, this feature is used by terminal emulators (like xterm) to make their bash session look like when initially logged in.



After logging in, you can run many copies of your shell. For those that are running without the --login option or do not have a dash in front of the process name, the behavior will be different than when started with authorization. They provide you with a command line, however, they are called interactive shells. If bash is running interactively, without authorization, it will ignore / etc / profile and ~ / .bash_profile will process ~ / .bashrc instead.



  interactive login profile rc 
 yes yes source ignore
 yes no ignore source
 no yes source ignore
 no no ignore ignore


Check for interactivity



Sometimes bash processes your ~ / .bashrc without being run interactively, for example when using commands like rsh or scp. This is important to remember because text output, as in the example with the fortune command above, can spoil these non-interactive bash sessions. It is a good idea to use the PS1 variable to check the fact of interactivity of the current session before outputting the text:



if [ -n "$PS1" ]; then

fortune

fi



/ etc / profile and / etc / skel



As a system administrator, you are responsible for the / etc / profile file. Since they are guided by all who enter the system for the first time, it is important to keep it operational. It is also a very powerful tool used to make things work correctly for new users right after they sign in using their new account.



However, there is an option in which, on the one hand, the settings can be set to default values ​​for new users, and on the other, they can be easily changed by them if necessary. For this purpose there is a directory / etc / skel. When using the useradd command to create a new account, all files from / etc / skel are copied to the new user's home directory. This means that you can put, for example, .bash_profile and .bashrc in / etc / skel for a more comfortable start for a new user.



export



Variables in bash can be labeled in such a way that they will be set in all newly launched shells. This means that they are designated as external. You can force bash to display a list of all external variables thus defined in your bash session:



$ export

declare -x EDITOR="vim"

declare -x HOME="/home/chouser"

declare -x MAIL="/var/spool/mail/chouser"

declare -x PAGER="/usr/bin/less"

declare -x PATH="/bin:/usr/bin:/usr/local/bin:/home/chouser/bin"

declare -x PWD="/home/chouser"

declare -x TERM="xterm"

declare -x USER="chouser"



Exporting variables



If a variable is not marked for export, its value will not be set for new launched shells. But you can mark a variable for export by passing it to the export built-in command:



$ FOO=foo

$ BAR=bar

$ export BAR

$ echo $FOO $BAR

foo bar

$ bash

$ echo $FOO $BAR

bar



In this example, both the FOO and BAR variables were set, but only BAR was marked for export. When a new bash was launched, it lost the value of the FOO variable. If you exit this new bash, you will see that the original values ​​of both the FOO and BAR variables have not changed.



$ exit

$ echo $FOO $BAR

foo bar



Export and set -x



In connection with the behavior described above, variables can be specified in ~ / .bash_profile or / etc / profile and marked for export, so that later there is no need to specify them again. But there are several options that cannot be exported, and therefore they must be specified in ~ / .bashrc and in your profile sequentially. These options are configured using the set builtin command:



$ set -x



The -x option causes bash to display every command it intends to execute:



$ echo $FOO

$ echo foo

foo



This can be very useful for understanding the unexpected behavior of commands when using quotes or similar oddities. To disable the -x option, use set + x . Refer to the man documentation page for all the options in the set built-in command.



Setting variables with "set"



The set command can also be used to set the values ​​of variables, but it is not necessary to specify this command itself. The bash command “set FOO = foo” does the same thing as “FOO = foo”. The reset of the variable value is performed by the built-in unset:



$ FOO=bar

$ echo $FOO

bar

$ unset FOO

$ echo $FOO



Unset vs. FOO =



This is not the same as setting a variable to an empty value, although it is sometimes difficult to explain. One way to notice this difference is to call the set command without parameters in order to list all current variables:



$ FOO=bar

$ set | grep ^FOO

FOO=bar

$ FOO=

$ set | grep ^FOO

FOO=

$ unset FOO

$ set | grep ^FOO



Using set with no parameters is similar to using the export builtin command, except that set displays all variables, not just those that are designated as external.



Exporting variables to change program behavior.



Often the behavior of commands can be changed by setting environment variables. Just as in the case of new bash sessions, programs launched from your command line will only see environment variables marked for export. For example, the man command checks the PAGER variable to find out which program to use for paging text.



$ PAGER=less

$ export PAGER

$ man man



When the PAGER variable is set to less, you will see one page first, and pressing the space bar will move you to the next page. If you change the PAGER variable in cat, all the text will be displayed immediately, without any page stops.



$ PAGER=cat

$ man man



Using "env"



Unfortunately, if you forget to set PAGER back to less, the man program (like some other programs) will continue to output all the requested text non-stop. If you wanted to set PAGER to cat only once, you could use the env command:



$ PAGER=less

$ env PAGER=cat man man

$ echo $PAGER

less



In this example, the PAGER variable was used with the cat value in the man program, but the PAGER environment variable itself remained unchanged in the bash session.



Totals and links



Results



Well, now is the time to congratulate you on the completion of the 3rd part of the guide. You should already know how to find information in the system and online documentation, as well as have a good understanding of the model of access rights in Linux, management of user accounts and the environment.



Links



Do not forget to look at the resources with Linux documentation used in this guide, especially the Linux Documentation Project , where you can find various guides, FAQs, as well as invaluable mana pages. Do not forget about Linux Weekly News .



The Linux System Administrators guide is a good addition to this guide. You can also find Eric Raymond’s very useful article " Unix and Internet Fundamentals HOWTO " on the Internet .



Daniel Robbins, using the example of a series of articles, “Bash in Examples,” shows how to use programming constructs to write your own scripts. This series (especially parts 1 and 2) is an excellent preparation for the LPIC Level 1 exam; in addition, it will help to consolidate the assimilated concepts covered by the “Setting Up a User Environment” guide:

If you are not familiar with the Vi text editor, read Daniel’s introduction in the " Vi intro - the cheat sheet method tutorial " manual. It will allow you to easily and at the same time quickly master the full power of Vi. Consider this material to be readable if you do not know how to use Vi.



For an introduction to Emacs, see the developerWorks, Living in Emacs guide.



The translation was performed by collective intelligence using notabenoid.com . Thanks to the following benoid-users (in alphabetical order): kindacute , nekjine , Rich . Special thanks to Alexey Blazhko (blazhkoa@gmail.com), as well as to the initiator of the entire translation series, VBart .



To be continued...




About the authors



Daniel Robbins



Daniel Robbins is the founder of the Gentoo community and the creator of the Gentoo Linux operating system. Daniel lives in New Mexico with his wife, Mary, and two energetic daughters. He is also the founder and head of Funtoo , has written many technical articles for IBM developerWorks , Intel Developer Services and the C / C ++ Users Journal.



Chris Houser



Chris Hauser was a UNIX supporter since 1994 when he joined the team of administrators at Taylor University (Indiana, USA), where he received a bachelor's degree in computer science and mathematics. After that, he worked in many areas, including web applications, video editing, drivers for UNIX, and cryptographic protection. Currently working in Sentry Data Systems. Chris also contributed to many free projects, such as Gentoo Linux and Clojure, co-authored The Joy of Clojure .



Aron griffis



Airon Griffis lives in Boston, where he spent the last decade working with Hewlett-Packard on projects such as UNIX network drivers for Tru64, Linux security certification, Xen and KVM virtualization, and most recently, the HP ePrint platform. In his spare time, Airon prefers to reflect on the problems of programming while riding his bike, juggling bits, or cheering on the Boston professional Red Sox baseball team.

Source: https://habr.com/ru/post/110697/



All Articles