RHCSA Howto ... Tips and Tricks - Episode 2

Introduction

In this episode of my journey to the desired certification (RHCSA), we are going to provide some information about one of the topic of the chapter "Understand and use essential tools":
  • access a shell prompt and issue commands with correct syntax
There is no "official" list of commands which Red Hat suggest you to know to pass the certification as it does not really matter how you solve the question; so every command or option you use, which is available in the distribution ISO and you are familiar with, will be good.
Penguin alla console

Feel free to add your comments and recommendation; we will be happy to reply and or update the post!
Let's get started !

access a shell prompt and issue commands with correct syntax

There are 3 type of commands in Linux:
  • internal
  • external
  • aliases
An internal command is part of the shell itself. An example of internal command is echo which print to the standard output whatever is the argument.
An external command is an executable file located somewhere in the fylesystem. An example of external command is /bin/hostname which print the name of your system.
To know if you are calling an internal or external command you can use the command type, while with which you will know the exact command the shell will use:

$ type ls
ls is aliased to `ls --color=auto'

$ which ls
alias ls='ls --color=auto'
        /usr/bin/ls

The output shows that there is an alias called ls and an executable file called /usr/bin/ls. Aliases have precedence on both internal and external commands so Bash will use the alias when we will use the ls command (see below for more info on the aliases).

If you want to create a custom command, you can use an alias. If for example you frequently use a specific command with some option (like ls -al or df -h) you can use the Bash command alias to create a "custom command" (alias). There are some useful aliases available by default in most distribution. To check which are available for your user just type alias at the Bash prompt:

# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .*'
alias ll='ls -l'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

To create a new alias you just have to type "alias" followed by the command name you want to use, equal the command that it will execute (as you can see in the output of the raw alias command). Let's look at an alias example:

# df
Filesystem              1K-blocks     Used Available Use% Mounted on
/dev/mapper/centos-root 385867688 58806536 327061152  16% /
devtmpfs                  3885068        0   3885068   0% /dev
tmpfs                     3901000      320   3900680   1% /dev/shm
tmpfs                     3901000    42304   3858696   2% /run
tmpfs                     3901000        0   3901000   0% /sys/fs/cgroup
/dev/sda2                84907480 30177444  50393860  38% /home
/dev/sda1                  508588   363028    145560  72% /boot
tmpfs                      780204       16    780188   1% /run/user/1001
tmpfs                      780204       24    780180   1% /run/user/1000

# alias df='df -h'

# df
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  368G   57G  312G  16% /
devtmpfs                 3.8G     0  3.8G   0% /dev
tmpfs                    3.8G  320K  3.8G   1% /dev/shm
tmpfs                    3.8G   42M  3.7G   2% /run
tmpfs                    3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/sda2                 81G   29G   49G  38% /home
/dev/sda1                497M  355M  143M  72% /boot
tmpfs                    762M   16K  762M   1% /run/user/1001
tmpfs                    762M   24K  762M   1% /run/user/1000

As you can see from now on, typing df I will get the output of "df -h" which is much more easy to read (human readable format) than the raw command.

The issue here is that whenever you close the shell or restart your server, the alias you manually enforced will disappear. To make the change persistend and customize many other shell behaviour there are 4 files to consider:

  • ~/.bashrc    -   user specific config, read by a non-login shell
  • ~/.bash_profile   -   user specific config, read by a login shell
  • /etc/bashrc   -   system config, read by a non-login shell
  • /etc/profile   -   system config, read by a login shell

I suggest you to have a look at the content of each of the mentioned files to discover which configuration are applied in each of the situation. Be aware that: when you open a login shell, Bash read the /etc/profile file first and then the ~/.bash_profile. The user specific settings have priority and override the system settings. The same for the "non login" shell.

Some more alias-stuff ! If you just created an alias for the current session, you can remove it by typing unalias followed by the command name (unalias df). You can also force Bash to use the internal or external command instead of the alias by unticipating the command with a backslash (\df).

Other interensting topic about the shell is the environment variables. A variable is a way to assign a value (which can be a string or a number) to a label (i.e. a name) which can only start with a letter. As a style rule it is better to use capital letters for variable names. Variables that are "available" within a working shell are called environmental variable. You can get a list executing the command env.
When you create a Bash script and you need to use multiple times the same value, it is a good idea to create a variable and call it within the script. As the variable has been created in the script, it will be available just within it (you need to export it to make it available within the running shell, outside the scope of the script).
You can permanently define environment variable by adding or modifying them within the Bash script we have been reviewing above. You can create or assign a value to a variable for the current session in the following way:

$ TEST="This is a test"
$ echo $TEST
This is a test

As you can see, you can use echo to print to the value of a variable to the standard output (just precede the variable name with a $).

To make your life easier you can recall commands that you have executed before by using the Bash internal command history. The command history is stored in RAM until the session is closed; at that point the list is appended to the ~/.bash_history text file.
So typing history you can get the list printed in your shell. Each entry start with a number to make it easy for you to execute the same command again:

$ history
   .........
   88  su
   89  df
   90  su
   91  alias
   92  su
   93  env
   94  TEST="This is a test"
   95  echo $TEST
   96  which ls
   97  type ls
   98  type /usr/bin/ls
   99  type ls
  100  history

$ !99
type ls
ls is aliased to `ls --color=auto'

You can scroll backward the list using CTRL-R. With CTRL-R you enter into an interactive session... type part of a command you already entered and see what happen!!
You can also call a command by typing ! followed by part of the command.
A good security measure is to clear the root history when closing the session. In this way, if an cracker succeed to compromise your system, there will be no traces of any possible "clear" password you typed within your commands (in example when you login to a database by providing the password while starting the CLI ... dangerous!!). It is also something crackers do to remove their footprints. Clearing the history is a 2 steps process: you need to clear the commands in memory (history -c) and you need to clear the content of the file ~/.bash_history (history -w).

No comments:

Post a Comment