0% found this document useful (0 votes)
17 views

Chapter2 - Using Essential Tools

Uploaded by

Ulil Amri Kamili
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter2 - Using Essential Tools

Uploaded by

Ulil Amri Kamili
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Chapter 2

Using Essential Tools


Basic Shell Skills

• The shell is the default working environment for a Linux administrator.


• It is the environment where users and administrators enter commands that
are executed by the operating system.
• Different shells for Linux are available, but bash is the common shell.
Executing Commands

• The purpose of the Linux shell is that it provides an environment in which


commands can be executed.
• The shell takes care of interpreting the command that a user has entered correctly.
• Shell makes a difference between three kinds of commands:
• Aliases
• Internal commands
• External commands
Aliases

• An alias is a command that a user can define as needed. Some aliases are
provided by default; type alias on the command line to get an overview. To
define an alias.
• Use alias newcommand=’oldcommand’ , as in the default alias ll=’ls -l --
color=auto’ .
Internal Command

• An internal command is a command that is a part of the shell itself.


• It is available when the shell is loaded and can be executed from memory
without any lookup from disk.
External Command
• An external command is a command that exists as an executable file on disk of the
computer.
• Becauseit has to be read from disk, it is a bit slower. When a user executes a
command,
• The shell first looks to determine whether it is an internal command; if it is not, it looks
for an executable file with a name that matches the command on disk.
• To find out whether a command is a bash internal, or an executable file on disk, you
can use the type command.
• To look up external commands, the $PATH variable is used. This variable defines a list
of directories that is searched for a matching filename when a user enters a command.
Using Internal and External Commands from
the Shell
I/O Redirection
Common Bash Redirectors
Redirecting Output to a File Examples

This command generates output and errors


when run as non-root:
$ find /etc -name passwd
Operators can be used to store output and errors:
$ find /etc -name passwd > find.out
$ find /etc -name passwd 2> /dev/null
$ find /etc -name passwd > find.out 2> find.err
Using Pipes

Pipes (the | character) can connect commands:


command1 | command2
• Sends STDOUT of command1 to STDIN of command2 instead of the screen.
• STDERR is not forwarded across pipes
Used to combine the functionality of multiple tools
command1 | command2 | command3... etc
Using I/O Redirection and Pipes
History

• A convenient feature of the bash shell is the bash history.


• Bash is configured to keep the last 1,000 commands you have used (and if
shell session is never closed, the exact number can grow even much beyond
that).
• When a shell session is closed, the history of that session is updated to the
history file. The name of this file is .bash_history, and it is created in the
home directory of the user who started a specific shell session.
Continue

There are several ways of working with history :


• Type history to show a list of all commands in the bash history.
• Use Ctrl+R to open the prompt from which you can do backward searches in
commands that you have previously used.
• Type !number to execute a command with a specific number from history.
• Type !sometext to execute the last command that starts with sometext.
Working with History
Bash Completion

• Another useful feature of the bash shell is automatic completion.


• This feature helps you in finding the command you need.
• Bash completion is used most on commands. Just type the beginning of a command
and press the Tab key on your computer’s keyboard.
• If there is only one option for completion, bash will complete the command
automatically for you.
• If there are several options, you need to press the Tab key once more to get an overview
of all the available options.
Using Bash Completion
Editing Files with vim

• Managing Linux often means working with files. Most of the things that are configured on Linux are
configured through files. To get things done, you often need to change the contents of a configuration
file with a text editor.
• One editor really matters, though, and that is vi
• That is why as a Linux administrator need to know how to work with vi. Only one alternative is
permitted, and that is vim. Vim is “vi improved;” enhancements that make working with vi easier, such
as syntax highlighting for many configuration files which makes it easy to recognize typing errors that
you have made.
• An important concept when working with vim is that it uses different modes. Two of them are
particularly important: command mode and input mode.
Vim Essential Commands
Vim Practice
Understanding the Shell Environment

• When you are working from a shell, an environment is created to ensure


that all that is happening is happening the right way. This environment
consists of variables that define the user environment, such as the $PATH
Understanding Variables

• The Linux shell environment consists of many variables.


• Variables are fixed names that can be assigned dynamic values.
• An example of a variable is LANG, which in my shell is set to en_US.UTF-8.
• Because the needs for different users are different, the variables that are set in
a user environment will differ.
• To get an overview of the current variables defined in your shell environment,
type the env command.
Displaying the Current Environment

• To read the value of a


variable, a user can use
the echo command,
followed by the name of
the variable, as in echo
$PATH , which reads the
current value of the PATH
variable and prints that on
the STDOUT.
Environment Configuration Files

When a user logs in, an environment is created for that user automatically. This
happens based on four different files where some script code can be specified
and where variables can be defined for use by one specific user:
• /etc/profile: This is the generic file that is processed by all users upon login.
• /etc/bashrc: This file is processed when subshells are started.
• ~/.bash_profile: In this file, user-specific login shell variables can be defined.
• ~/.bashrc: In this user-specific file, subshell variables can be defined.
login shell & Subshell

• A login shell is the first shell that is opened for a user after the user has
logged in. From the login shell, a user may run scripts, which will start a
subshell of that login shell.
• Bash allows for the creation of a different environment in the login shell and
in the subshell but to synchronize settings; by default the subshell settings
are included when entering a login shell.
Using /etc/motd and /etc/issue

• Bash offers an option to include messages in the /etc/motd and the /etc/issue files.
• Messages in /etc/motd display after a user has successfully logged in to a shell.
• (Notice that users in a graphical environment do not see its contents after a graphical
login.)
• Using /etc/motd can be a convenient way for system administrators to inform users.
• Another way to send information to users is by using /etc/issue. The contents of this
file display before the user logs in.
Managing the Shell Environment
Finding Help
• Using –help
The quickest way to get an overview of how to use a command is by running the
command with the --help option.
• Using man
When using the Linux command line, you will at some point consult man pages. Man is what
makes working from the command line doable. If you do not know how a command is used, the
man page of that command will provide valuable insight.
• Finding the Right man Page
To find information in man pages, you can search the mandb database by using apropos or
man -k . If the database is current, getting access to the information you need is easy. Just type
man -k , followed by the keyword you want to search for.
Example of man page
Searching man Pages with man -k
Man pages are categorized in different
sections.
The most relevant sections for system administrators are as follows
• 1: Executable programs or shell commands
• 5: File formats and conventions
• 8: System administration commands

• So, if you are looking for the configuration file that has something to do with passwords, use
man -k password | grep 5
• If you are looking for the command that an administrator would use to create partitions, use
man -k partition | grep 8
• Another useful man option is -f . The command man -f <somecommand> displays a short
description of the item as found in the man database.
Updating mandb

• When using the man -k command, the mandb database is consulted. It can
sometimes happen that you are looking for something that should obviously
be documented but you cannot find anything anyway and all you get is
“nothing appropriate.” If that happens, you might need to update the man
database.
• Just run the mandb command as root without any arguments. It will see
whether new man pages have been installed and update the man database
accordingly.
Exercise Using man -k
Using info

• Some commands are documented only in the info system.


• If that is the case, the “see also” section of the man page of that command
will tell you that “The full documentation for ... is maintained as a Texinfo
manual.” If that happens, you can read the Info page using the command
pinfo or info.
• Both commands work, but in pinfo , special items such as menu items are
clearly indicated, which is why using pinfo is recommended.
Getting more command usage information
using pinfo
Exercise Using info
Using /usr/share/doc Documentation Files

• A third source of information consists of files that are sometimes copied to


the /usr/share/doc directory.
• This happens in particular for services and larger systems that are a bit more
complicated.
• Some services store very useful information in this directory, like rsyslog,
bind, Kerberos, and OpenSSL.
Summary

• In this chapter, you read about essential Linux administration tasks. You
learned about some of the important shell basics, such as I/O redirection,
working with history, and management of the environment.
• You also learned how to edit text files with the vim editor. In the last part of
this chapter, you learned how to find information using man and related
commands.

You might also like