LPI-Learning-Material-102-500-en.pdf
Document Details
Uploaded by GuiltlessAshcanSchool
Tags
Full Transcript
Table of Contents TOPIC 105: SHELLS AND SHELL SCRIPTING................................................ 1 105.1 Customize and use the shell environment........................................ 2 105.1 Lesson 1...................................................................... 4 Introduction..........
Table of Contents TOPIC 105: SHELLS AND SHELL SCRIPTING................................................ 1 105.1 Customize and use the shell environment........................................ 2 105.1 Lesson 1...................................................................... 4 Introduction...................................................................... 4 Shell Types: Interactive vs. Non-Interactive and Login vs. Non-Login................... 5 Guided Exercises................................................................. 18 Explorational Exercises........................................................... 20 Summary....................................................................... 22 Answers to Guided Exercises...................................................... 24 Answers to Explorational Exercises................................................. 26 105.1 Lesson 2..................................................................... 28 Introduction..................................................................... 28 Variables: Assignment and Reference............................................. 28 Local or Shell Variables......................................................... 32 Global or Environment Variables................................................. 34 Guided Exercises................................................................. 45 Explorational Exercises........................................................... 48 Summary....................................................................... 50 Answers to Guided Exercises...................................................... 52 Answers to Explorational Exercises................................................. 56 105.1 Lesson 3..................................................................... 58 Introduction..................................................................... 58 Creating Aliases............................................................... 58 Creating Functions............................................................. 62 Guided Exercises................................................................. 73 Explorational Exercises........................................................... 76 Summary....................................................................... 77 Answers to Guided Exercises...................................................... 79 Answers to Explorational Exercises................................................. 84 105.2 Customize or write simple scripts............................................... 85 105.2 Lesson 1..................................................................... 87 Introduction..................................................................... 87 Script Structure and Execution................................................... 88 Variables...................................................................... 90 Arithmetic Expressions......................................................... 93 Conditional Execution.......................................................... 94 Script Output.................................................................. 95 Guided Exercises................................................................. 98 Explorational Exercises........................................................... 99 Summary...................................................................... 100 Answers to Guided Exercises..................................................... 101 Answers to Explorational Exercises................................................ 102 105.2 Lesson 2.................................................................... 103 Introduction.................................................................... 103 Extended Tests............................................................... 103 Loop Constructs.............................................................. 108 A More Elaborate Example..................................................... 111 Guided Exercises................................................................ 115 Explorational Exercises.......................................................... 117 Summary...................................................................... 118 Answers to Guided Exercises..................................................... 119 Answers to Explorational Exercises................................................ 121 TOPIC 106: USER INTERFACES AND DESKTOPS........................................... 122 106.1 Install and configure X11...................................................... 123 106.1 Lesson 1.................................................................... 124 Introduction.................................................................... 124 X Window System Architecture................................................. 125 X Server Configuration......................................................... 128 Wayland..................................................................... 133 Guided Exercises................................................................ 135 Explorational Exercises.......................................................... 136 Summary...................................................................... 137 Answers to Guided Exercises..................................................... 138 Answers to Explorational Exercises................................................ 139 106.2 Graphical Desktops........................................................... 140 106.2 Lesson 1.................................................................... 141 Introduction.................................................................... 141 X Window System............................................................. 142 Desktop Environment......................................................... 142 Popular Desktop Environments................................................. 144 Desktop Interoperability....................................................... 146 Non-Local Access............................................................. 147 Guided Exercises................................................................ 149 Explorational Exercises.......................................................... 150 Summary...................................................................... 151 Answers to Guided Exercises..................................................... 152 Answers to Explorational Exercises................................................ 153 106.3 Accessibility.................................................................. 154 106.3 Lesson 1.................................................................... Introduction.................................................................... Accessibility Settings.......................................................... Keyboard and Mouse Assist.................................................... Visual Impairments........................................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ TOPIC 107: ADMINISTRATIVE TASKS.................................................... 107.1 Manage user and group accounts and related system files....................... 107.1 Lesson 1.................................................................... Introduction.................................................................... Adding User Accounts......................................................... Modifying User Accounts...................................................... Deleting User Accounts........................................................ Adding, Modifying and Deleting Groups......................................... The Skeleton Directory........................................................ The /etc/login.defs File.................................................... The passwd Command........................................................ The chage Command......................................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 107.1 Lesson 2.................................................................... Introduction.................................................................... /etc/passwd.................................................................. /etc/group................................................................... /etc/shadow.................................................................. /etc/gshadow................................................................ Filter the Password and Group Databases........................................ Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 107.2 Automate system administration tasks by scheduling jobs....................... 155 155 155 156 158 160 161 162 163 164 165 166 168 168 168 170 172 172 173 173 174 175 177 178 179 181 183 185 185 186 186 187 188 188 190 192 193 194 196 198 107.2 Lesson 1.................................................................... Introduction.................................................................... Schedule Jobs with Cron....................................................... User Crontabs................................................................ System Crontabs.............................................................. Particular Time Specifications................................................... Crontab Variables............................................................. Creating User Cron Jobs....................................................... Creating System Cron Jobs..................................................... Configure Access to Job Scheduling............................................. An Alternative to Cron......................................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 107.2 Lesson 2.................................................................... Introduction.................................................................... Schedule Jobs with at......................................................... List Scheduled Jobs with atq................................................... Delete Jobs with atrm......................................................... Configure Access to Job Scheduling............................................. Time Specifications............................................................ An Alternative to at........................................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 107.3 Localisation and internationalisation.......................................... 107.3 Lesson 1.................................................................... Introduction.................................................................... Time Zones.................................................................. Daylight Savings Time......................................................... Language and Character Encoding.............................................. Encoding Conversion.......................................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... 200 200 200 201 202 203 203 204 205 206 206 209 211 212 213 215 217 217 217 218 219 219 220 220 221 222 223 224 225 227 229 229 230 234 234 238 239 240 241 242 Answers to Explorational Exercises................................................ TOPIC 108: ESSENTIAL SYSTEM SERVICES................................................ 108.1 Maintain system time......................................................... 108.1 Lesson 1.................................................................... Introduction.................................................................... Local Versus Universal Time.................................................... Date........................................................................ Hardware Clock............................................................... timedatectl................................................................... Setting Time Zone Without timedatectl.......................................... Setting Date and Time Without timedatectl....................................... Guided Exercise................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 108.1 Lesson 2.................................................................... Introduction.................................................................... timedatectl................................................................... NTP Daemon................................................................. NTP Configuration............................................................ pool.ntp.org.................................................................. ntpdate...................................................................... ntpq........................................................................ chrony...................................................................... Guided Exercise................................................................ Explorational Exercise........................................................... Summary...................................................................... Answers Guided Exercise........................................................ Answers to Explorational Exercises................................................ 108.2 System logging............................................................... 108.2 Lesson 1.................................................................... Introduction.................................................................... System Logging.............................................................. Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 108.2 Lesson 2.................................................................... 243 244 245 247 247 248 248 250 250 252 253 255 257 258 260 262 263 263 264 266 266 267 268 268 269 274 276 277 278 280 281 283 283 283 304 306 307 308 311 312 Introduction.................................................................... Basics of systemd............................................................ The System Journal: systemd-journald........................................ Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 108.3 Mail Transfer Agent (MTA) basics.............................................. 108.3 Lesson 1.................................................................... Introduction.................................................................... Local and Remote MTA........................................................ Linux MTAs.................................................................. The mail Command and Mail User Agents (MUA)................................. Delivery Customization........................................................ Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 108.4 Manage printers and printing................................................. 108.4 Lesson 1.................................................................... Introduction.................................................................... The CUPS Service............................................................. Installing a Printer............................................................ Managing Printers............................................................ Submitting Print Jobs.......................................................... Managing Print Jobs........................................................... Removing Printers............................................................ Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ TOPIC 109: NETWORKING FUNDAMENTALS............................................. 109.1 Fundamentals of internet protocols............................................ 109.1 Lesson 1.................................................................... Introduction.................................................................... IP (Internet Protocol).......................................................... Guided Exercises................................................................ 312 312 313 331 333 334 335 337 338 339 339 339 341 346 347 349 350 351 352 353 354 355 355 356 360 362 363 366 367 368 369 370 372 373 375 376 377 377 377 386 Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 109.1 Lesson 2.................................................................... Introduction.................................................................... Transmission Control Protocol (TCP)............................................. User Datagram Protocol (UDP).................................................. Internet Control Message Protocol (ICMP)........................................ IPv6......................................................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 109.2 Persistent network configuration.............................................. 109.2 Lesson 1.................................................................... Introduction.................................................................... The Network Interface......................................................... Interface Names.............................................................. Interface Management........................................................ Local and Remote Names...................................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 109.2 Lesson 2.................................................................... Introduction.................................................................... NetworkManager............................................................. systemd-networkd............................................................ Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 109.3 Basic network troubleshooting................................................ 109.3 Lesson 1.................................................................... Introduction.................................................................... About the ip Command....................................................... 387 388 389 390 391 391 393 393 393 393 397 398 399 400 401 402 403 403 403 405 406 408 412 413 414 415 416 417 417 417 421 425 426 427 428 429 430 432 432 432 Netmask and Routing Review.................................................. Configuring an Interface....................................................... The Routing Table............................................................. Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 109.3 Lesson 2.................................................................... Introduction.................................................................... Testing Connections With ping................................................. Tracing Routes............................................................... Finding MTUs With tracepath................................................. Creating Arbitrary Connections................................................. Viewing Current Connections and Listeners...................................... Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 109.4 Configure client side DNS..................................................... 109.4 Lesson 1.................................................................... Introduction.................................................................... Name Resolution Process...................................................... DNS Classes.................................................................. Name Resolution Tools........................................................ Guided Exercises.............................................................. Explorational Exercises........................................................ Summary...................................................................... Answers to Guided Exercises................................................... Answers to Explorational Exercises.............................................. TOPIC 110: SECURITY................................................................. 110.1 Perform security administration tasks......................................... 110.1 Lesson 1.................................................................... Introduction.................................................................... Checking for Files with the SUID and SGID Set.................................... Password Management and Aging.............................................. Discovering Open Ports........................................................ Limits on Users Logins, Processes and Memory Usage............................. Dealing with Logged in Users................................................... 433 434 437 440 441 442 443 445 447 447 447 448 450 451 453 455 456 457 458 460 462 463 463 463 464 467 473 474 475 476 477 478 479 481 481 481 484 487 494 496 Basic sudo Configuration and Usage............................................ Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 110.2 Setup host security........................................................... 110.2 Lesson 1.................................................................... Introduction.................................................................... Improve Authentication Security with Shadow Passwords.......................... How to Use a Superdaemon to Listen for Incoming Network Connections............ Checking Services for Unnecessary Daemons..................................... TCP Wrappers as Sort of a Simple Firewall........................................ Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 110.3 Securing data with encryption................................................. 110.3 Lesson 1.................................................................... Introduction.................................................................... Basic OpenSSH Client Configuration and Usage................................... The Role of OpenSSH Server Host Keys.......................................... SSH Port Tunnels............................................................. Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ 110.3 Lesson 2.................................................................... Introduction.................................................................... Perform Basic GnuPG Configuration, Usage and Revocation........................ Use GPG to Encrypt, Decrypt, Sign and Verify Files................................. Guided Exercises................................................................ Explorational Exercises.......................................................... Summary...................................................................... Answers to Guided Exercises..................................................... Answers to Explorational Exercises................................................ Imprint............................................................................ 499 504 507 508 510 514 515 516 516 516 518 523 525 526 527 528 530 531 532 534 534 535 540 542 546 548 549 550 552 553 553 553 559 564 566 567 568 570 571 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 1 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting 105.1 Customize and use the shell environment Reference to LPI objectives LPIC-1 version 5.0, Exam 102, Objective 105.1 Weight 4 Key knowledge areas Set environment variables (e.g. PATH) at login or when spawning a new shell. Write Bash functions for frequently used sequences of commands. Maintain skeleton directories for new user accounts. Set command search path with the proper directory. Partial list of the used files, terms and utilities. source /etc/bash.bashrc /etc/profile env export set unset ~/.bash_profile ~/.bash_login 2 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment ~/.profile ~/.bashrc ~/.bash_logout function alias Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 3 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting 105.1 Lesson 1 Certificate: LPIC-1 Version: 5.0 Topic: 105 Shells and Shell Scripting Objective: 105.1 Customize and use the shell environment Lesson: 1 of 3 Introduction The shell is arguably the most powerful tool in a Linux system and can be defined as an interface between the user and the kernel of the operating system. It interprets commands entered by the user. Therefore, all system administrators must be skilled in using the shell. As we may certainly know by now, the Bourne Again Shell (Bash) is the de facto shell for the vast majority of Linux distributions. Once started, the first thing Bash — or any other shell for that matter — does is executing a series of startup scripts. These scripts customize the session’s environment. There are both system wide and user specific scripts. We can put our personal preferences or settings that best fit our users' needs in these scripts in the form of variables, aliases and functions. The exact series of startup files depends on a very important parameter: the type of shell. Let us have a look at the variety of shells that exist. 4 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment Shell Types: Interactive vs. Non-Interactive and Login vs. NonLogin To start with, let us clarify the concepts of interactive and login in the context of shells: Interactive / Non-interactive Shells This kind of shell refers to the interaction that takes place between the user and the shell: The user provides input by typing commands into the terminal using the keyboard; the shell provides output by printing messages on the screen. Login / Non-login Shells This kind of shell refers to the event of a user accessing a computer system providing its credentials, such as username and password. Both interactive and non-interactive shells can be either login or non-login and any possible combination of these types has its specific uses. Interactive login shells are executed when users log into the system and are used to customize users' configurations according to their needs. A good example of this type of shell would be that of a group of users belonging to the same department who need a particular variable set in their sessions. By interactive non-login shells we refer to any other shells opened by the user after logging into the system. Users use these shells during sessions to carry out maintenance and administrative tasks such as setting variables, the time, copying files, writing scripts, etc. On the other hand, non-interactive shells do not require any kind of human interaction. Thus, these shells do not ask the user for input and their output — if any — is in most cases written to a log. Non-interactive login shells are quite rare and impractical. Their uses are virtually non-existent and we will only comment on them for the sake of insight into shell behaviour. Some odd examples include forcing a script to be run from a login shell with /bin/bash --login or piping the standard output (stdout) of a command into the standard input (stdin) of an ssh connection: | ssh @ As for non-interactive non-login shell there is neither interaction nor login on behalf of the user, so we are referring here to the use of automated scripts. These scripts are mostly used to carry out Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 5 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting repetitive administrative and maintenance tasks such as those included in cronjobs. In such cases, bash does not read any startup files. Opening a Terminal When we are in a desktop environment, we can either open a terminal application or switch to one of the system consoles. Therefore, a new shell is either a pts shell when opened from a terminal emulator in the GUI or a tty shell when run from a system console. In the first case we are not dealing with a terminal but with a terminal emulator. As part of graphical sessions, terminal emulators like gnome-terminal or konsole are very feature-rich and user-friendly as compared to text-based user interface terminals. Less feature-rich terminal emulators include — amongst others — XTerm and sakura. Using the Ctrl + Alt + F1 - F6 combos we can go to the console logins which open an interactive textbased login shell. Ctrl + Alt + F7 will take the session back into the Desktop. NOTE tty stands for teletypewritter; pts stands for pseudo terminal slave. For more information: man tty and man pts. Launching Shells with bash After logging in, type bash into a terminal to open a new shell. Technically, this shell is a child process of the current shell. While starting the bash child process, we can specify various switches to define which kind of shell we want to start. Here some important bash invocation options: bash -l or bash --login will invoke a login shell. bash -i will invoke an interactive shell. bash --noprofile with login shells will ignore both the system-wide startup file /etc/profile and the user-level startup files ~/.bash_profile, ~/.bash_login and ~/.profile. bash --norc with interactive shells will ignore both the system-wide startup file /etc/bash.bashrc and the user-level startup file ~/.bashrc. 6 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment bash --rcfile with interactive shells will take as the startup file ignoring system wide /etc/bash.bashrc and user-level ~/.bashrc. We will discus the various startup files below. Launching Shells with su and sudo Through the use of these two similar programs we can obtain specific types of shells: su Change user ID or become superuser (root). With this command we can invoke both login and non-login shells: su - user2, su -l user2 or su --login user2 will start an interactive login shell as user2. su user2 will start an interactive non-login shell as user2. su - root or su - will start an interactive login shell as root. su root or su will start an interactive non-login shell as root. sudo Execute commands as another user (including the supersuser). Because this command is mainly used to gain root privileges temporarily, the user using it must be in the sudoers file. To add users to sudoers we need to become root and then run: root@debian:~# usermod -aG sudo user2 Just as su, sudo allows us to invoke both login and non-login shells: sudo su - user2, sudo su -l user2 or sudo su --login user2 will start an interactive login shell as user2. sudo su user2 will start an interactive non-login shell as user2. sudo -u user2 -s will start an interactive non-login shell as user2. sudo su - root or sudo su - will start an interactive login shell as root. sudo -i will start an interactive login shell as root. sudo -i will start an interactive login shell as root, run the command and return to the original user. Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 7 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting sudo su root or sudo su will start an interactive non-login shell as root. sudo -s or sudo -u root -s will start a non-login shell as root. When using either su or sudo, it is important to consider our particular case scenario for starting a new shell: Do we need the target user’s environment or not? If so, we would use the options which invoke login shells; if not, those which invoke non-login shells. What Shell Type Do We Have? In order to find out what type of shell we are working at, we can type echo $0 into the terminal and get the following output: Interactive login -bash or -su Interactive non-login bash or /bin/bash Non-interactive non-login (scripts) How Many Shells Do We Have? To see how many bash shells we have up and running in the system, we can use the command ps aux | grep bash: user2@debian:~$ ps aux | grep bash user2 5270 0.1 0.1 25532 5664 pts/0 Ss 23:03 0:00 bash user2 5411 0.3 0.1 25608 5268 tty1 S+ 23:03 0:00 -bash user2 5452 0.0 0.0 16760 S+ 23:04 0:00 grep --color=auto bash 940 pts/0 user2 at debian has logged into a GUI (or X Window System) session and opened gnome-terminal, then she has pressed Ctrl + Alt + F1 to go into a tty terminal session. Finally, she has gone back to the GUI session by pressing Ctrl + Alt + F7 and typed in the command ps aux | grep bash. Thus, the output shows an interactive non-login shell via the terminal emulator (pts/0) and an interactive login shell via the proper text-based terminal (tty1). Note also how the last field of each line (the command) is bash for the former and -bash for the latter. 8 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment Where Shells Get their Configuration From: Startup Files Well, now that we know the shell types that we can find in a Linux system, it is high time that we saw what startup files get executed by what shell. Note that system wide or global scripts are placed in the /etc/ directory, whereas local or user-level ones are found in the user’s home (~). Also, when there is more than one file to be searched, once one is found and run the others are ignored. Explore and study these files yourself with your favorite text editor or by typing less. NOTE Startup files can be divided into Bash specific (those limited only to bash configurations and commands) and general ones (relating to most shells). Interactive Login Shell Global Level /etc/profile This is the system-wide.profile file for the Bourne shell and Bourne compatible shells (bash included). Through a series of if statements this file sets a number of variables such as PATH and PS1 accordingly as well as sourcing — if they exist — both the file /etc/bash.bashrc and those in the directory /etc/profile.d. /etc/profile.dcopyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Tue Nov 27 19:57:19 2018 from 192.168.1.10 Hello from /etc/profile Hello from /home/user2/.profile As the last two lines show, it worked. Also, note three things: The global file was run first. There were no.bash_profile or.bash_login files in he home directory of user2. The tilde (~) expanded to the absolute path of the file (/home/user2/.profile). 10 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment Interactive Non-Login Shell Global Level /etc/bash.bashrc This is the system-wide.bashrc file for interactive bash shells. Through its execution bash makes sure it is being run interactively, checks the window size after each command (updating the values of LINES and COLUMNS if necessary) and sets some variables. Local Level ~/.bashrc In addition to carrying out similar tasks to those described for /etc/bash.bashrc at a user level (such as checking the window size or if being run interactively), this Bash specific file usually sets some history variables and sources ~/.bash_aliases if it exists. Apart from that, this file is normally used to store users' specific aliases and functions. Likewise, it is also worthwhile noting that ~/.bashrc is read if bash detects its is a network connection (as it was the case with the Secure Shell (SSH) connection in the example above). Exploring Interactive Non-Login Shell Configuration Files Let us now modify /etc/bash.bashrc and /home/user2/.bashrc: root@debian:~# echo 'echo Hello from /etc/bash.bashrc' >> /etc/bash.bashrc root@debian:~# echo 'echo Hello from ~/.bashrc' >> ~/.bashrc And this is what happens when user2 starts a new shell: user2@debian:~$ bash Hello from /etc/bash.bashrc Hello from /home/user2/.bashrc Again, the two files were read and executed. WARNING Remember, because of the order in which files are run, local files take precedence over global ones. Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 11 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment the exact distribution terms for each program are described in the individual files in /usr/share/doc Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 45 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting Wrong Command Right Command Variable Reference win_path=C:\path\t o\dir\ Expected Output C:\path\to\dir\ 4. Consider the purpose and write the appropriate command: Purpose Command Set the language of the current shell to Spanish UTF-8 (es_ES.UTF-8). Print the name of the current working directory. Reference the environment variable which stores the information about ssh connections. Set PATH to include /home/carol/scripts as the last directory to search for executables. Set the value of my_path to PATH. Set the value of my_path to that of PATH. 5. Create a local variable named mammal and assign it the value gnu: 6. Using variable substitution, create another local variable named var_sub with the appropriate value so that when referenced via echo $var_sub we obtain: The value of mammal is gnu: 7. Turn mammal into an environment variable: 8. Search for it with set and grep: 9. Search for it with env and grep: 10. Create, in two consecutive commands, an environment variable named BIRD whose value is penguin: 46 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment 11. Create, in a single command, an environment variable named NEW_BIRD whose value is yellow-eyed penguin: 12. Assuming you are user2, create a folder named bin in your home directory: 13. Type the command to add the ~/bin folder to your PATH so that it is the first directory bash searches for binaries: 14. To guarantee the value of PATH remains unaltered across reboots, what piece of code — in the form of an if statement — would you put into ~/.profile? Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 47 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting Explorational Exercises 1. let: more than arithmetic expression evaluation: ◦ Do a manpage or web search for let and its implications when setting variables and create a new local variable named my_val whose value is 10 — as a result of adding 5 and 5: ◦ Now create another variable named your_val whose value is 5 — as a result of dividing the value of my_val into 2: 2. The result of a command in a variable? Of course, that is possible; it is called command substitution. Investigate it and study the following function named music_info: music_info(){ latest_music=`ls -l1t ~/Music | head -n 6` echo -e "Your latest 5 music files:\n$latest_music" } The result of the command ls -l1t ~/Music | head -n 6 becomes the value of the variable latest_music. Then the variable latest_music is referenced in the echo command (which outputs the total number of bytes occupied by the Music folder and the latest five music files stored in the Music folder — one per line). Which of the following is a valid synonym for latest_music=`ls -l1t ~/Music | head -n 6` Option A: latest_music=$(ls -l1t ~/Music| head -n 6) Option B: latest_music="(ls -l1t ~/Music| head -n 6)" Option C: 48 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment latest_music=((ls -l1t ~/Music| head -n 6)) Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 49 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting Summary In this lesson we learned: Variables are a very important part of the shell environment as they are used by the shell itself as well as by other programs. How to assign and reference variables. The differences between local and global (or environment) variables. How to make variables readonly. How to turn a local variable into an environment variable with the export command. How to list all environment variables. How to run a program in a modified environment. How to make variables persistent with the help of startup scripts. Some common environment variables: DISPLAY, HISTCONTROL, HISTSIZE, HISTFILESIZE, HISTFILE, HOME, HOSTNAME, HOSTTYPE, LANG, LD_LIBRARY_PATH, MAIL, MAILCHECK, PATH, PS1 (and other prompt variables), SHELL and USER. The meaning of the tilde (~). The very basics of if statements. Commands used in this lesson: echo Reference a variable. ls List directory contents. readonly Make variables immutable. List all readonly variables in current session. set List all variables and functions in current session. grep Print lines matching a pattern. 50 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment bash Launch a new shell unset Unset variables. export Turn a local variable into an environment variable. List environment variables. env List environment variables. Run a program in a modified environment. printenv List environment variables. Reference a variable. chmod Change mode bits of a file, for example make it executable. history List previous commands. su Change user ID or become superuser. id Print user ID. Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 51 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting Answers to Guided Exercises 1. Observe the variable assignment under the “Command(s)” column and indicate if the resulting variable is “Local” or “Global”: Command(s) Local Global debian=mother Yes No ubuntu=deb-based Yes No mint=ubuntu-based; export mint No Yes export suse=rpm-based No Yes zorin=ubuntu-based Yes No 2. Study the “Command” and the “Output” and explain the meaning: Command Output Meaning echo $HISTCONTROL ignoreboth Both duplicate commands and those starting with a space will not be saved in history. echo ~ /home/carol The HOME of carol is /home/carol. echo $DISPLAY reptilium:0:2 reptilium machine has a X server running and we are using the second screen of the display. echo $MAILCHECK 60 Mail will be checked every minute. echo $HISTFILE /home/carol/.bash_histor history will be saved in y /home/carol/.bash_histor y. 3. Variables are being set incorrectly under the “Wrong Command” column. Provide the missing information under “Right Command” and “Variable Reference” so that we get the “Expected Output”: 52 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment Wrong Command Right Command Variable Reference Expected Output lizard =chameleon lizard=chameleon echo $lizard chameleon cool lizard=chameleon cool_lizard=chamel echo $cool_lizard eon (for example) chameleon lizard=cha|me|leon lizard="cha|me|leo echo $lizard n" or lizard='cha|me|leo n' cha|me|leon lizard= lizard="" or lizard='' echo "$lizard" win_path=C:\path\t win_path=C:\\path\ echo $win_path o\dir\ \to\\dir\\ C:\path\to\dir\ 4. Consider the purpose and write the appropriate command: Purpose Command Set the language of the current shell to Spanish UTF-8 (es_ES.UTF-8). LANG=es_ES.UTF-8 Print the name of the current working directory echo $PWD or pwd Reference the environment variable which stores the information about ssh connections echo $SSH_CONNECTION Set PATH to include /home/carol/scripts as PATH=$PATH:/home/carol/scripts the last directory to search for executables. Set the value of my_path to PATH. my_path=PATH Set the value of my_path to that of PATH. my_path=$PATH 5. Create a local variable named mammal and assign it the value gnu: mammal=gnu 6. Using variable substitution, create another local variable named var_sub with the appropriate value so that when referenced via echo $var_sub we obtain The value of mammal is gnu: Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 53 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting var_sub="The value of mammal is $mammal" 7. Turn mammal into an environment variable: export mammal 8. Search for it with set and grep: set | grep mammal 9. Search for it with env and grep: env | grep mammal 10. Create, in two consecutive commands, an environment variable named BIRD whose value is penguin: BIRD=penguin; export BIRD 11. Create, in a single command, an environment variable named NEW_BIRD whose value is yellow-eyed penguin: export NEW_BIRD="yellow-eyed penguin" or export NEW_BIRD='yellow-eyed penguin' 12. Assuming you are user2, use mkdir to create a folder named bin in your home directory: mkdir ~/bin or mkdir /home/user2/bin 54 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment or mkdir $HOME/bin 13. Type the command to add the ~/bin folder to your PATH so that it is the first directory bash searches for binaries: PATH="$HOME/bin:$PATH" PATH=~/bin:$PATH or PATH=/home/user2/bin:$PATH are equally valid. 14. To guarantee the value of PATH remains unaltered across reboots, what piece of code — in the form of an if statement — would you put into ~/.profile? if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 55 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting Answers to Explorational Exercises 1. let: more than arithmetic expression evaluation: ◦ Do a manpage or web search for let and its implications when setting variables and create a new local variable named my_val whose value is 10 — as a result of adding 5 and 5: let "my_val = 5 + 5" or let 'my_val = 5 + 5' ◦ Now create another variable named your_val whose value is 5 — as a result of dividing the value of my_val into 2: let "your_val = $my_val / 2" or let 'your_val = $my_val / 2' 2. The result of a command in a variable? Of course, that is possible; it is called command substitution. Investigate it and study the following function named music_info: music_info(){ latest_music=`ls -l1t ~/Music | head -n 6` echo -e "Your latest 5 music files:\n$latest_music" } The result of the command ls -l1t ~/Music | head -n 6 becomes the value of the variable latest_music. Then the variable latest_music is referenced in the echo command (which outputs the total number of bytes occupied by the Music folder and the latest five music files stored in the Music folder — one per line). Which of the following is a valid synonym for 56 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment latest_music=`ls -l1t ~/Music | head -n 6` It is option A: latest_music=$(ls -l1t ~/Music| head -n 6) Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 57 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting 105.1 Lesson 3 Certificate: LPIC-1 Version: 5.0 Topic: 105 Shells and Shell Scripting Objective: 105.1 Customize and use the shell environment Lesson: 3 of 3 Introduction After going through shells, startup scripts and variables in the previous lessons, we will round the whole topic of customizing the shell off by having a look at two very interesting shell elements: aliases and functions. In fact the entire group of variables, aliases and functions — and their influence on one another — is what makes up the shell environment. The main strength of these two flexible and time-saving shell facilities has to do with the concept of encapsulation: they offer the possibility of putting together — under a single command — a series of repetitive or recurrent commands. Creating Aliases An alias is a substitute name for another command(s). It can run like a regular command, but instead executes another command according to the alias definition. The syntax for declaring aliases is quite straightforward. Aliases are declared by writing the keyword alias followed by the alias assignment. In turn, the alias assignment consists of the alias name, an equal sign and one or more commands: 58 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment alias alias_name=command(s) For example: $ alias oldshell=sh This awkward alias will start an instance of the original sh shell when the user types oldshell into the terminal: $ oldshell $ The power of aliases lies in that they allow us to write short versions of long commands: $ alias ls='ls --color=auto' NOTE For information about ls and its colors, type man dir_colors into the terminal. Likewise, we can create aliases for a series of concatenated commands — the semicolon (;) is used as a delimiter. We can, for instance, have an alias that gives us information about the location of the git executable and its version: $ alias git_info='which git;git --version' To invoke an alias, we type its name into the terminal: $ git_info /usr/bin/git git version 2.7.4 The alias command will produce a listing of all available aliases in the system: $ alias alias git-info='which git;git --version' alias ls='ls --color=auto' alias oldshell='sh' Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 59 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting The unalias command removes aliases. We can, for instance, unalias git-info and see how it disappears from the listing: $ unalias git-info $ alias alias ls='ls --color=auto' alias oldshell='sh' As we saw with the alias hi='echo We salute you.' in a previous lesson, we must enclose commands in quotes (either single or double) when — as a result of having arguments or parameters — they contain spaces: $ alias greet='echo Hello world!' $ greet Hello world! Commands with spaces include also those with options: $ alias ll='ls -al' Now ll will list all files — including the hidden ones (a) — in the long format (l). We can reference variables in aliases: $ reptile=uromastyx $ alias greet='echo Hello $reptile!' $ greet Hello uromastyx! The variable can also be assigned within the alias: $ alias greet='reptile=tortoise; echo Hello $reptile!' $ greet Hello tortoise! We can escape an alias with \: $ alias where?='echo $PWD' 60 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment $ where? /home/user2 $ \where? -bash: where?: command not found Escaping an alias is useful when an alias has the same name as a regular command. In this case, the alias takes precedence over the original command, which, however, is still be accessible by escaping the alias. Likewise, we can put an alias inside another alias: $ where? /home/user2 $ alias my_home=where? $ my_home /home/user2 On top of that, we can also put a function inside an alias as you will be shown below. Expansion and Evaluation of Quotes in Aliases When using quotes with environment variables, single quotes make the expansion dynamic: $ alias where?='echo $PWD' $ where? /home/user2 $ cd Music $ where? /home/user2/Music However, with double quotes the expansion is done statically: $ alias where?="echo $PWD" $ where? /home/user2 $ cd Music $ where? /home/user2 Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 61 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting Persistence of Aliases: Startup Scripts Just as with variables, for our aliases to gain persistence, we must put them into initialization scripts that are sourced at startup. As we already know, a good file for users to put their personal aliases in is ~/.bashrc. You will probably find some aliases there already (most of them commented out and ready to be used by removing the leading #): $ grep alias.bashrc # enable color support of ls and also add handy aliases alias ls='ls --color=auto' #alias dir='dir --color= #alias vdir='vdir --color= #alias grep='grep --color= #alias fgrep='fgrep --color' #alias egrep='egrep --color= # some more ls aliases #ll='ls -al' #alias la='ls -A' #alias l='ls -CF' # ~/.bash_aliases, instead of adding them here directly. if [ -f ~/.bash_aliases ]; then. ~/.bash_aliases As you can read in the last three lines, we are offered the possibility of having our own aliasdedicated file — ~/.bash_aliases — and have it sourced by.bashrc with every system start. So we can go for that option and create and populate such file: ########### #.bash_aliases: # a file to be populated by the user's personal aliases (and sourced by ~/.bashrc). ########### alias git_info='which git;git --version' alias greet='echo Hello world!' alias ll='ls -al' alias where?='echo $PWD' Creating Functions Compared to aliases, functions are more programmatic and flexible, specially when it comes to exploiting the full potential of Bash special built-in variables and positional parameters. They are also great to work with flow control structures such as loops or conditionals. We can think of a 62 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment function as a command which includes logic through blocks or collections of other commands. Two Syntaxes for Creating Functions There are two valid syntaxes to define functions. Using the keyword function On the one hand, we can use the keyword function, followed by the name of the function and the commands between curly brackets: function function_name { command #1 command #2 command #3... command #n } Using () On the other, we can leave out the keyword function and use two brackets right after the name of the function instead: function_name() { command #1 command #2 command #3... command #n } It is commonplace to put functions in files or scripts. However, they can also be written directly into the shell prompt with each command on a different line — note PS2(>) indicating a new line after a line break: $ greet() { > greeting="Hello world!" Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 63 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting > echo $greeting > } Whatever the case — and irrespective of the syntax we choose — , if we decide to skip line breaks and write a function in just one line, commands must be separated by semicolons (note the semicolon after the last command too): $ greet() { greeting="Hello world!"; echo $greeting; } bash did not complain when we pressed Enter, so our function is ready to be invoked. To invoke a function, we must type its name into the terminal: $ greet Hello world! Just as with variables and aliases, if we want functions to be persistent across system reboots we have to put them into shell initialization scripts such as /etc/bash.bashrc (global) or ~/.bashrc (local). WARNING After adding aliases or functions to any startup script file, you should source such files with either. or source for the changes to take effect if you do not want to logout and back in again or reboot the system. Bash Special Built-in Variables The Bourne Again Shell comes with a set of special variables which are particularly useful for functions and scripts. These are special because they can only be referenced — not assigned. Here is a list of the most relevant ones: $? This variable’s reference expands to the result of the last command run. A value of 0 means success: $ ps aux | grep bash user2 420 0.0 0.4 21156 5012 pts/0 Ss 17:10 0:00 -bash user2 640 0.0 0.0 12784 936 pts/0 S+ 18:04 0:00 grep bash $ echo $? 0 64 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment A value other than 0 means error: user1@debian:~$ ps aux |rep bash -bash: rep: command not found user1@debian:~$ echo $? 127 $$ It expands to the shell PID (process ID): $ ps aux | grep bash user2 420 0.0 0.4 21156 5012 pts/0 Ss 17:10 0:00 -bash user2 640 0.0 0.0 12784 936 pts/0 S+ 18:04 0:00 grep bash $ echo $$ 420 $! It expands to the PID of the last background job: $ ps aux | grep bash & 663 $ user2 user2 420 663 0.0 0.0 0.4 0.0 21156 12784 5012 pts/0 972 pts/0 Ss+ S 17:10 18:08 0:00 -bash 0:00 grep bash ^C + Done ps aux | grep bash $ echo $! 663 NOTE Remember, the ampersand (&) is used to start processes in the background. Positional parameters $0 through $9 They expand to the parameters or arguments being passed to the function (alias or script) — $0 expanding to the name of the script or shell. Let us create a function to demonstrate positional parameters — note PS2 (>) indicating new lines after line breaks: $ special_vars() { > echo $0 > echo $1 Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 65 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting > echo $2 > echo $3 } Now, we will invoke the function (special_vars) passing three parameters to it (debian, ubuntu, zorin): $ special_vars debian ubuntu zorin -bash debian ubuntu zorin It worked as expected. Although passing positional parameters to aliases is technically possible, it is not at all functional since — with aliases — positional parameters are always passed at the end: WARNING $ alias great_editor='echo $1 is a great text editor' $ great_editor emacs is a great text editor emacs Other Bash special built-in variables include: $# It expands to the number of arguments passed to the command. $@, $* They expand to the arguments passed to the command. $_ It expands to the last parameter or the name of the script (amongst other things; see man bash to find out more!): Variables in Functions Of course, variables can be used within functions. To prove it, this time we will create a new empty file called funed and put the following function 66 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment into it: editors() { editor=emacs echo "My editor is: $editor. $editor is a fun text editor." } As you may have guessed by now, we must source the file first to be able to invoke the function: $. funed And now we can test it: $ editors My editor is emacs. emacs is a fun text editor. As you can appreciate, for the editors function to work properly, the editor variable must first be set. The scope of that variable is local to the current shell and we can reference it as long as the session lasts: $ echo $editor emacs Together with local variables we can also include environment variables in our function: editors() { editor=emacs echo "The text editor of $USER is: $editor." } editors Note how this time we decided to call the function from within the file itself (editors in the last line). That way, when we source the file, the function will also be invoked — all at once: Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 67 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting $. funed The text editor of user2 is: emacs. Positional Parameters in Functions Something similar occurs with positional parameters. We can pass them to functions from within the file or script (note the last line: editors tortoise): editors() { editor=emacs echo "The text editor of $USER is: $editor." echo "Bash is not a $1 shell." } editors tortoise We source the file and prove it works: $. funed The text editor of user2 is: emacs. Bash is not a tortoise shell. And we can also pass positional parameters to functions at the command line. To prove it, we get rid of the last line of the file: editors() { editor=emacs echo "The text editor of $USER is: $editor." echo "Bash is not a $1 shell." } Then, we have to source the file: 68 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment $. funed Finally, we invoke the function with tortoise as the positional parameter $1 at the command line: $ editors tortoise The text editor of user2 is: emacs. Bash is not a tortoise shell. Functions in Scripts Functions are mostly found in Bash scripts. Turning our funed file into a script (we will name it funed.sh) is really a piece of cake: #!/bin/bash editors() { editor=emacs echo "The text editor of $USER is: $editor." echo "Bash is not a $1 shell." } editors tortoise That is it! We only added two lines: The first line is the shebang and defines what program is going to interpret the script: #!/bin/bash. Curiously enough, that program is bash itself. The last line is simply the invocation of the function. Now there is only one thing left — we have to make the script executable: $ chmod +x funed.sh And now it is ready to be executed: Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 69 LPIC-1 (102) (Version 5.0) | Topic 105: Shells and Shell Scripting $./funed.sh The text editor of user2 is: emacs. Bash is not a tortoise shell. NOTE You will learn all about shell scripting in the next few lessons. A Function within an Alias As said above, we can put a function inside an alias: $ alias great_editor='gr8_ed() { echo $1 is a great text editor; unset -f gr8_ed; }; gr8_ed' This long alias value deserves an explanation. Let us break it down: First there is the function itself: gr8_ed() { echo $1 is a great text editor; unset -f gr8_ed; } The last command in the function — unset -f gr8_ed — unsets the function so that it does not remain in the present bash session after the alias is called. Last but not least, to have a successful alias invocation, we must first invoke the function too: gr8_ed. Let us invoke the alias and prove it works: $ great_editor emacs emacs is a great text editor As shown in unset -f gr8_ed above, the unset command is not only used to unset variables, but also functions. In fact, there are specific switches or options: unset -v for variables unset -f for functions If used without switches, unset will try to unset a variable first and — if it fails — then it will try to unset a function. 70 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 105.1 Customize and use the shell environment A Function within a Function Now say we want to communicate two things to user2 every time she logs into the system: Say hello and recommend/praise a text editor. Since she is starting to put a lot of Matroska video files in its $HOME/Video folder, we also want to give her a warning. To accomplish that purpose, /home/user2/.bashrc: we have put the following two functions into The first function (check_vids) does the checking on.mkv files and the warning: check_vids() { ls -1 ~/Video20 * * * * 4. Explain the meaning of the following time specifications used in the OnCalendar option of a timer file: *-*-* 08:30:00 Sat,Sun *-*-* 05:00:00 Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 209 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks *-*-01 13:15,30,45:00 Fri *-09..12-* 16:20:00 Mon,Tue *-*-1,15 08:30:00 *-*-* *:00/05:00 210 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs Explorational Exercises 1. Assuming that you are authorized to schedule jobs with cron as an ordinary user, what command would you use to to create your own crontab file? 2. Create a simple scheduled job that executes the date command every Friday at 01:00 pm. Where can you see the output of this job? 3. Create another scheduled job that executes the foobar.sh script every minute, redirecting the output to the output.log file in your home directory so that only standard error is sent to you by e-mail. 4. Look at the crontab entry of the newly created scheduled job. Why is it not necessary to specify the absolute path of the file in which the standard output is saved? And why can you use the./foobar.sh command to execute the script? 5. Edit the previous crontab entry by removing the output redirection and disable the first cron job you have created. 6. How can you send the output and errors of your scheduled job to the emma user account via email? And how can you avoid sending the standard output and error via e-mail? 7. Execute the command ls -l /usr/bin/crontab. Which special bit is set and what is its meaning? Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 211 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks Summary In this lesson, you learned: Use cron to run jobs at regular intervals. Manage cron jobs. Configure user access to cron job scheduling. Understand the role of systemd timer units as an alternative to cron. The following commands and files were discussed in this lesson: crontab Maintain crontab files for individual users. /etc/cron.allow and /etc/cron.deny Particular files used to set crontab restrictions. /etc/crontab System crontab file. /etc/cron.d The directory that contains system crontab files. systemctl Control the systemd system and service manager. In relation to timers, it can be used to enable and start them. 212 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs Answers to Guided Exercises 1. For each of the following crontab shortcuts, indicate the corresponding time specification (the first five columns in a crontab file): @hourly 0 * * * * @daily 0 0 * * * @weekly 0 0 * * 0 @monthly 0 0 1 * * @annually 0 0 1 1 * 2. For each of the following OnCalendar shortcuts, indicate the corresponding time specification (the longer normalized form): hourly *-*-* *:00:00 daily *-*-* 00:00:00 weekly Mon *-*-* 00:00:00 monthly *-*-01 00:00:00 yearly *-01-01 00:00:00 3. Explain the meaning of the following time specifications for a crontab file: 30 13 * * 1-5 At 01:30 pm every day of the week from Monday to Friday 00 09-18 * * * Every day and every hour from 09 am to 06 pm 30 08 1 1 * At 08:30 am on the first day of January 0,20,40 11 * * Sun Every Sunday at 11:00 am, 11:20 am and 11:40 am 00 09 10-20 1-3 * At 09:00 am from the 10th to the 20th of January, February and March */20 * * * * Every twenty minutes 4. Explain the meaning of the following time specifications used in the OnCalendar option of a timer file: Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 213 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks 214 *-*-* 08:30:00 Every day at 08:30 am Sat,Sun *-*-* 05:00:00 At 05:00 am on Saturday and Sunday *-*-01 13:15,30,45:00 At 01:15 pm, 01:30 pm and 01:45 pm on the first day of the month Fri *-09..12-* 16:20:00 At 04:20 pm every Friday in September, October, November and December Mon,Tue *-*-1,15 08:30:00 At 08.30 am on the first or fifteenth day of each month only if the day is a Monday or Tuesday *-*-* *:00/05:00 Every five minutes | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs Answers to Explorational Exercises 1. Assuming that you are authorized to schedule jobs with cron as an ordinary user, what command would you use to to create your own crontab file? dave@hostname ~ $ crontab -e no crontab for dave - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano < ---- easiest 3. /usr/bin/emacs24 4. /usr/bin/vim.tiny Choose 1-4 : 2. Create a simple scheduled job that executes the date command every Friday at 01:00 pm. Where can you see the output of this job? 00 13 * * 5 date The output is mailed to the user; to view it, you can use the mail command. 3. Create another scheduled job that executes the foobar.sh script every minute, redirecting the output to the output.log file in your home directory so that only standard error is sent to you by e-mail. */1 * * * *./foobar.sh >> output.log 4. Look at the crontab entry of the newly created scheduled job. Why is it not necessary to specify the absolute path of the file in which the standard output is saved? And why can you use the./foobar.sh command to execute the script? cron invokes the commands from the user’s home directory, unless another location is specified by the HOME environment variable within the crontab file. For this reason, you can use the relative path of the output file and run the script with./foobar.sh. 5. Edit the previous crontab entry by removing the output redirection and disable the first cron job you have created. Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 215 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks #00 13 * * 5 date */1 * * * *./foobar.sh To disable a cron job, you can simply comment the corresponding line within the crontab file. 6. How can you send the output and errors of your scheduled job to the emma user account via email? And how can you avoid sending the standard output and error via e-mail? To send the standard output and error to emma, you must set the MAILTO environment variable in your crontab file as follows: MAILTO="emma" To tell cron that no mail should be sent, you can assign an empty value to the MAILTO environment variable. MAILTO="" 7. Execute the command ls -l /usr/bin/crontab. Which special bit is set and what is its meaning? $ ls -l /usr/bin/crontab -rwxr-sr-x 1 root crontab 25104 feb 10 2015 /usr/bin/crontab The crontab command has the SGID bit set (the s character instead of the executable flag for the group), which means that it is executed with the privileges of the group (thus crontab). This is why ordinary users can edit their crontab file using the crontab command. Note that many distributions have file permissions set such that crontab files can only be edited via the crontab command. 216 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs 107.2 Lesson 2 Certificate: LPIC-1 Version: 5.0 Topic: 107 Administrative Tasks Objective: 107.2 Automate system administration tasks by scheduling jobs Lesson: 2 of 2 Introduction As you learned in the previous lesson, you can schedule regular jobs using cron or systemd timers, but sometimes you may need to run a job at a specific time in the future only once. To do this, you can use another powerful utility: the at command. Schedule Jobs with at The at command is used for one-time task scheduling and only requires that you specify when the job should be run in the future. After entering at on the command line followed by the time specification, you will enter the at prompt where you can define the commands to be executed. You can exit the prompt with the Ctrl + D key-sequence. $ at now +5 minutes warning: commands will be executed using /bin/sh at> date at> Ctrl+D Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 217 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks job 12 at Sat Sep 14 09:15:00 2019 The at job in the above example simply executes the date command after five minutes. Similar to cron, the standard output and error is sent to you via e-mail. Note that the atd daemon will need to be running on the system in order for you to use at job scheduling. NOTE In Linux, the batch command is similar to at, however batch jobs are executed only when the system load is low enough to allow it. The most important options which apply to the at command are: -c Print the commands of a specific job ID to the standard output. -d Delete jobs based on their job ID. It is an alias for atrm. -f Read the job from a file instead of the standard input. -l List the pending jobs of the user. If the user is root, all jobs of all users are listed. It is an alias for atq. -m Send mail to the user at the end of the job even if there was no output. -q Specify a queue in the form of a single letter from a to z and from A to Z (by default a for at and b for batch). Jobs in the queues with the highest letters are executed with increased niceness. Jobs submitted to a queue with a capital letter are treated as batch jobs. -v Show the time at which the job will run before reading the job. List Scheduled Jobs with atq Now let us schedule two more at jobs: the first executes the foo.sh script at 09:30 am, while the second executes the bar.sh script after one hour. 218 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs $ at 09:30 AM warning: commands will be executed using /bin/sh at>./foo.sh at> Ctrl+D job 13 at Sat Sep 14 09:30:00 2019 $ at now +2 hours warning: commands will be executed using /bin/sh at>./bar.sh at> Ctrl+D job 14 at Sat Sep 14 11:10:00 2019 To list your pending jobs, you can use the atq command which shows the following information for each job: job ID, job execution date, job execution time, queue, and username. $ atq 14 Sat Sep 14 11:10:00 2019 a frank 13 Sat Sep 14 09:30:00 2019 a frank 12 Sat Sep 14 09:15:00 2019 a frank Remember that the at -l command is an alias for atq. NOTE If you run atq as root, it will display the queued jobs for all users. Delete Jobs with atrm If you want to delete an at job, you can use the atrm command followed by the job ID. For example, to delete the job with ID 14, you can run the following: $ atrm 14 You can delete multiple jobs with atrm by specifying multiple IDs separated by spaces. Remember that the at -d command is an alias for atrm. NOTE If you run atrm as root you can delete the jobs of all users. Configure Access to Job Scheduling Authorization for ordinary users to schedule at jobs is determined by the /etc/at.allow and /etc/at.deny files. If /etc/at.allow exists, only non-root users listed within it can schedule at jobs. If /etc/at.allow does not exist but /etc/at.deny exists, only non-root users listed within Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 219 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks it cannot schedule at jobs (in this case an empty /etc/at.deny file means that each user is allowed to schedule at jobs). If neither of these files exist, the user’s access to at job scheduling depends on the distribution used. Time Specifications You can specify when to execute a particular at job using the form HH:MM, optionally followed by AM or PM in case of 12-hour format. If the specified time has already passed, the next day is assumed. If you want to schedule a particular date on which the job will run, you must add the date information after the time using one of the following forms: month-name day-of-month, month-name day-of-month year, MMDDYY, MM/DD/YY, DD.MM.YY and YYYY-MM-DD). The following keywords are also accepted: midnight, noon, teatime (4 pm) and now followed by a plus sign (+) and a time period (minutes, hours, days and weeks). Finally, you can tell at to run the job today or tomorrow by suffixing the time with the words today or tomorrow. For example, you can use at 07:15 AM Jan 01 to execute a job at 07:15 am on 01 January and at now +5 minutes to execute a job five minutes from now. You can read the timespec file under the /usr/share tree for more information about the exact definition of time specifications. An Alternative to at Using systemd as the system and service manager, you can also schedule one-time tasks with the systemd-run command. It is typically used to create a transient timer unit so that a command will be executed at a specific time without the need to create a service file. For example, acting as root, you can run the date command at 11:30 AM on 2019/10/06 using the following: # systemd-run --on-calendar='2019-10-06 11:30' date If you want to run the foo.sh script, located in your current working directory, after two minutes you can use: # systemd-run --on-active="2m"./foo.sh Consult the manual pages to learn all possible uses of systemd-run with systemd-run(1). NOTE 220 | Remember that timers are logged to the systemd journal and you can review the logs of the different units using the journalctl command. Also remember that if you are acting as an ordinary user, you need to use the --user option of the systemd-run and journalctl commands. learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs Guided Exercises 1. For each of the following time specifications, indicate which is valid and which is invalid for at: at 08:30 AM next week at midday at 01-01-2020 07:30 PM at 21:50 01.01.20 at now +4 days at 10:15 PM 31/03/2021 at tomorrow 08:30 AM 2. Once you have scheduled a job with at, how can you review its commands? 3. Which commands can you use to review your pending at jobs? Which commands would you use to delete them? 4. With systemd, which command is used as an alternative to at? Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 221 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks Explorational Exercises 1. Create an at job that runs the foo.sh script, located in your home directory, at 10:30 am on coming October 31st. Assume you are acting as an ordinary user. 2. Login to the system as another ordinary user and create another at job that runs the bar.sh script tomorrow at 10:00 am. Assume the script is located in the user’s home directory. 3. Login to the system as another ordinary user and create another at job that runs the foobar.sh script just after 30 minutes. Assume the script is located in the user’s home directory. 4. Now as root, run the atq command to review the scheduled at jobs of all users. What happens if an ordinary user executes this command? 5. As root, delete all these pending at jobs using a single command. 6. Run the ls -l /usr/bin/at command and examine its permissions. 222 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs Summary In this lesson, you learned: Use at to run one-time jobs at a specific time. Manage at jobs. Configure user access to at job scheduling. Use systemd-run as an alternative to at. The following commands and files were discussed in this lesson: at Execute commands at a specified time. atq List the user’s pending at jobs, unless the user is the superuser. atrm Delete at jobs, identified by their job number. /etc/at.allow and /etc/at.deny Particular files used to set at restrictions. systemd-run Create and start a transient timer unit as an alternative to at for one-time scheduling. Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 223 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks Answers to Guided Exercises 1. For each of the following time specifications, indicate which is valid and which is invalid for at: at 08:30 AM next week Valid at midday Invalid at 01-01-2020 07:30 PM Invalid at 21:50 01.01.20 Valid at now +4 days Valid at 10:15 PM 31/03/2021 Invalid at tomorrow 08:30 AM monotonic Invalid 2. Once you have scheduled a job with at, how can you review its commands? You can use the at -c command followed by the ID of the job whose commands you want to review. Note that the output also contains most of the environment that was active at the time the job was scheduled. Remember that root can review the jobs of all users. 3. Which commands can you use to review your pending at jobs? Which commands would you use to delete them? You can use the at -l command to review your pending jobs, and you can use the at -d command to delete your jobs. at -l is an alias for atq and at -d is an alias for atrm. Remember that root can list and delete the jobs of all users. 4. With systemd, which command is used as an alternative to at? The systemd-run command can be used as an alternative to at to schedule one-time jobs. For example, you can use it to run commands at a specific time, defining a calendar timer or a monotonic timer relative to different starting points. 224 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.2 Automate system administration tasks by scheduling jobs Answers to Explorational Exercises 1. Create an at job that runs the foo.sh script, located in your home directory, at 10:30 am on coming October 31st. Assume you are acting as an ordinary user. $ at 10:30 AM October 31 warning: commands will be executed using /bin/sh at>./foo.sh at> Ctrl+D job 50 at Thu Oct 31 10:30:00 2019 2. Login to the system as another ordinary user and create another at job that runs the bar.sh script tomorrow at 10:00 am. Assume the script is located in the user’s home directory. $ at 10:00 AM tomorrow warning: commands will be executed using /bin/sh at>./bar.sh at> Ctrl+D job 51 at Sun Oct 6 10:00:00 2019 3. Login to the system as another ordinary user and create another at job that runs the foobar.sh script just after 30 minutes. Assume the script is located in the user’s home directory. $ at now +30 minutes warning: commands will be executed using /bin/sh at>./foobar.sh at> Ctrl+D job 52 at Sat Oct 5 10:19:00 2019 4. Now as root, run the atq command to review the scheduled at jobs of all users. What happens if an ordinary user executes this command? # atq 52 Sat Oct 5 10:19:00 2019 a dave 50 Thu Oct 31 10:30:00 2019 a frank 51 Sun Oct 6 10:00:00 2019 a emma If you run the atq command as root, all pending at jobs of all users are listed. If you run it as Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 225 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks an ordinary user, only your own pending at jobs are listed. 5. As root, delete all of these pending at jobs using a single command. # atrm 50 51 52 6. As root, run the ls -l /usr/bin/at command and examine its permissions. # ls -l /usr/bin/at -rwsr-sr-x 1 daemon daemon 43762 Dec 1 2015 /usr/bin/at In this distribution, the at command has both the SUID (the s character instead of the executable flag for the owner) and SGID (the s character instead of the executable flag for the group) bits set, which means that it is executed with the privileges of the owner and group of the file (daemon for both). This is why ordinary users are able to schedule jobs with at. 226 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.3 Localisation and internationalisation 107.3 Localisation and internationalisation Reference to LPI objectives LPIC-1 version 5.0, Exam 102, Objective 107.3 Weight 3 Key knowledge areas Configure locale settings and environment variables. Configure timezone settings and environment variables. Partial list of the used files, terms and utilities /etc/timezone /etc/localtime /usr/share/zoneinfo/ LC_* LC_ALL LANG TZ /usr/bin/locale tzselect timedatectl date iconv Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 227 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks UTF-8 ISO-8859 ASCII Unicode 228 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.3 Localisation and internationalisation 107.3 Lesson 1 Certificate: LPIC-1 Version: 5.0 Topic: 107 Administrative Tasks Objective: 107.3 Localisation and internationalisation Lesson: 1 of 1 Introduction All major Linux distributions can be configured to use custom localisation settings These settings include region and language related definitions such as the time zone, interface language as well as character encoding can be modified during the installation of the operating system or anytime after that. Applications rely on environment variables, system configuration files and commands to decide the proper time and language to use; hence most Linux distribution share a standardized way to adjust time and localisation settings. These adjustments are important not only to improve user experience, but also to ensure that the timing of system events — important, for example, for reporting security related issues — are correctly calculated. To be able to represent any written text, regardless of the spoken language, modern operating systems need a reference character encoding standard, and Linux systems are no different. As computers are only able to deal with numbers, a text character is nothing more than a number associated with a graphic symbol. Distinct computer platforms may associate distinct number values to the same character, so a common character encoding standard is necessary to make them compatible. A text document created in one system will be readable in another system only Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 229 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks if both agree on the encoding format and on what number is associated to what character, or at least if they know how to convert between the two standards. The heterogeneous nature of the localisation settings in Linux-based systems result in subtle differences between the distributions. Despite these differences, all distributions share the same basic tools and concepts to setup the internationalisation aspects of a system. Time Zones Time zones are roughly proportional discrete bands of Earth’s surface spanning the equivalent to one hour, that is, regions of the world experiencing the same hour of the day at any given time. Since there is not a single longitude that can be considered as the beginning of the day for the whole world, time zones are relative to the prime meridian, where the Earth’s longitude angle is defined to be 0. The time at the prime meridian is called the Coordinated Universal Time, by convention abbreviated to UTC. Due to practical reasons, time zones do not follow the exact longitudinal distance from the reference point (the prime meridian). Instead, time zones are artificially adapted to follow the borders of countries or other significant subdivisions. The political subdivisions are so relevant that time zones are named after some major geographical agent in that particular area, usually based on the name of a large country or city inside the zone. Yet, time zones are divided according to their time offset relative to UTC and this offset can also be used to indicate the zone in question. The time zone GMT-5, for example, indicates a region for which UTC time is five hours ahead, i.e. that region is 5 hours behind UTC. Likewise, the time zone GMT+3 indicates a region for which UTC time is three hours behind. The term GMT — from Greenwich Mean Time — is used as a synonym for UTC in the offset based zone names. A connected machine can be accessed from different parts of the world, so it is a good practice to set the hardware clock to UTC (the GMT+0 time zone) and leave the choice of the time zone to each particular case. Cloud services, for example, are commonly configured to use UTC, as it can help to mitigate occasional inconsistencies between local time and time at clients or at other servers. In contrast, users who open a remote session on the server may want to use their local time zone. Thus, it will be up to the operating system to set up the correct time zone according to each case. In addition to the current date and time, command date will also print the currently configured time zone: $ date Mon Oct 21 10:45:21 -03 2019 230 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.3 Localisation and internationalisation The offset relative to UTC is given by the -03 value, meaning that the displayed time is three hours less than UTC. Therefore, UTC time is three hours ahead, making GMT-3 the corresponding time zone for the given time. Command timedatectl, which is available in distributions using systemd, shows more details about the system time and date: $ timedatectl Local time: Sat 2019-10-19 17:53:18 -03 Universal time: Sat 2019-10-19 20:53:18 UTC RTC time: Sat 2019-10-19 20:53:18 Time zone: America/Sao_Paulo (-03, -0300) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: no As shown in the Time zone entry, time zone names based on localities — like America/Sao_Paulo — are also accepted. The default time zone for the system is kept in the file /etc/timezone, either by the zone’s full descriptive name or offset. Generic time zone names given by the UTC offset must include Etc as the first part of the name. So, to set the default time zone to GMT+3, the name of the time zone must be Etc/GMT+3: $ cat /etc/timezone Etc/GMT+3 Although time zone names based on localities do not require the time offset to work, they are not so straight forward to choose from. The same zone can have more than one name, which can make it difficult to remember. In order to ease this issue, command tzselect offers an interactive method which will guide the user towards the correct time zone definition. Command tzselect should be available by default in all Linux distributions, as it is provided by the package that contains necessary utility programs related to the GNU C Library. Command tzselect will be useful, for example, for a user who wants to identify the time zone for “São Paulo City” in “Brazil”. tzselect starts by asking the macro region of the desired location: $ tzselect Please identify a location so that time zone rules can be set correctly. Please select a continent, ocean, "coord", or "TZ". 1) Africa 2) Americas 3) Antarctica Version: 2023-07-13 | Licensed under CC BY-NC-ND 4.0. | learning.lpi.org | 231 LPIC-1 (102) (Version 5.0) | Topic 107: Administrative Tasks 4) Asia 5) Atlantic Ocean 6) Australia 7) Europe 8) Indian Ocean 9) Pacific Ocean 10) coord - I want to use geographical coordinates. 11) TZ - I want to specify the time zone using the Posix TZ format. #? 2 Option 2 is for (North and South) American locations, not necessarily in the same time zone. It is also possible to specify the time zone with geographical coordinates or with the offset notation, also known as the Posix TZ format. The next step is to choose the country: Please select a country whose clocks agree with yours. 1) Anguilla 19) Dominican Republic 37) Peru 2) Antigua & Barbuda 20) Ecuador 38) Puerto Rico 3) Argentina 21) El Salvador 39) St Barthelemy 4) Aruba 22) French Guiana 40) St Kitts & Nevis 5) Bahamas 23) Greenland 41) St Lucia 6) Barbados 24) Grenada 42) St Maarten (Dutch) 7) Belize 25) Guadeloupe 43) St Martin (French) 8) Bolivia 26) Guatemala 44) St Pierre & Miquelon 9) Brazil 27) Guyana 45) St Vincent 10) Canada 28) Haiti 46) Suriname 11) Caribbean NL 29) Honduras 47) Trinidad & Tobago 12) Cayman Islands 30) Jamaica 48) Turks & Caicos Is 13) Chile 31) Martinique 49) United States 14) Colombia 32) Mexico 50) Uruguay 15) Costa Rica 33) Montserrat 51) Venezuela 16) Cuba 34) Nicaragua 52) Virgin Islands (UK) 17) Curaçao 35) Panama 53) Virgin Islands (US) 18) Dominica 36) Paraguay #? 9 Brazil’s territory spans four time zones, so the country information alone is not enough to set the time zone. In the next step tzselect will require the user to specify the local region: Please select one of the following time zone regions. 1) Atlantic islands 2) Pará (east); Amapá 3) Brazil (northeast: MA, PI, CE, RN, PB) 232 | learning.lpi.org | Licensed under CC BY-NC-ND 4.0. | Version: 2023-07-13 LPIC-1 (102) (Version 5.0) | 107.3 Localisation and internationalisation 4) Pernambuco 5) Tocantins 6) Alagoas, Sergipe 7) Bahia 8) Brazil (southeast: GO, DF, MG, ES, RJ, SP, PR, SC, RS) 9) Mato Grosso do Sul 10) Mato Grosso 11) Pará (west) 12) Rondônia 13) Roraima 14) Amazonas (east) 15) Amazonas (west) 16) Acre #? 8 Not all locality names are available, but choosing the closest region will be good enough. The given information will then be used by tzselect to display the corresponding time zone: The following information has been given: Brazil Brazil (southeast: GO, DF, MG, ES, RJ, SP, PR, SC, RS) Therefore TZ='America/Sao_Paulo' will be used. Selected time is now: sex out 18 18:47:07 -03 2019. Universal Time is now: sex out 18 21:47:07 UTC 2019. Is the above information OK? 1) Yes 2) No #? 1 You can make this change permanent for yourself by appending the line