Chapter S2. Users and their Priorities.
 
Goals for this chapter: rpm packages covered in this chapter: 
  • sh (bash)
  • shadow-utils (adduser)
  

That is where the way goes now
where a new track leads
for more versatile singers
more abundant bards
among the youngsters rising
among the people growing. 
- Kalevala 50:611-620

Adding users to the system

Linux like UNIX is a multiuser Operating system. This means that different users like john, peter, mary, ... and others will have a special directory to save its own personal files.

On Linux generally the directory where users are installed is: "/home/".

Therefore we will have:

/home/mary for user mary,
/home/peter for user peter
...

To create the account there are different modes. Some procedures are inmmediate and others are graphical.

For a SysAdm purpose is more easy to use the "adduser" program.

[root@ftosx1 root-Thu Sep  6 12:39:40 EDT 2001]# adduser mary
[root@ftosx1 root-Thu Sep  6 12:39:40 EDT 2001]# passwd mary
Changing password for user mary
New password:
Retype new password:
passwd: all authentication tokens updated successfully
[root@ftosx1 root-Thu Sep  6 12:39:40 EDT 2001]#

After the installation the user will have an account, for example: if the hostname is futuretg.com, (or www.futuretg.com), the e-mail address will be: mary@futuretg.com., and therefore the user will have access to all the system services like sendmail to receive emails, ftp, telnet and any other activated system.

After the installation the user will have the following files, by default.

[root@ftosx1 orlando]# ls -al
total 67
drwx------    6 orlando  orlando       416 Jan 16 15:47 .
drwxr-xr-x    3 root     root           72 Jan 16 14:23 ..
-rw-r--r--    1 orlando  orlando        24 Jan 16 14:23 .bash_logout
-rw-r--r--    1 orlando  orlando       191 Jan 16 14:23 .bash_profile
-rw-r--r--    1 orlando  orlando       124 Jan 16 14:23 .bashrc
-rw-r--r--    1 orlando  orlando       820 Jan 16 14:23 .emacs
-rw-r--r--    1 orlando  orlando       118 Jan 16 14:23 .gtkrc
drwxr-xr-x    3 orlando  orlando        80 Jan 16 14:23 .kde
drwx--Srwx    4 orlando  orlando       152 Jan 16 15:20 Network Trash Folder
-rw-r--r--    1 orlando  orlando      3511 Jan 16 14:23 .screenrc
drwxrwsrwx    3 orlando  orlando        80 Jan 16 15:26 Temporary Items
drwxrwsrwx    3 orlando  orlando        80 Jan 16 15:20 TheVolumeSettingsFolder
-rw-------    1 orlando  orlando      6610 Jan 16 15:28 users.gif
-rw-------    1 orlando  orlando     30628 Jan 16 15:26 users.png
[root@ftosx1 orlando]#

The .bashrc and others are copied from the directory "/etc/skel".

[root@ftosx1 skel]# ls -al
total 32
drwxr-xr-x    3 root     root          240 Jan 16 07:34 .
drwxr-xr-x   70 root     root         7128 Jan 19 10:40 ..
-rw-r--r--    1 root     root           24 Jul  9  2001 .bash_logout
-rw-r--r--    1 root     root          191 Jul  9  2001 .bash_profile
-rw-r--r--    1 root     root          124 Jul  9  2001 .bashrc
-rw-r--r--    1 root     root          820 Jul 30 06:03 .emacs
-rw-r--r--    1 root     root          118 Aug  9 20:15 .gtkrc
drwxr-xr-x    3 root     root           80 Jan 16 07:26 .kde
-rw-r--r--    1 root     root         3511 Aug  3 12:53 .screenrc
[root@ftosx1 skel]#
 

Linux supports also the command "userdel" to remove an user. There are

[root@ftosx1 sbin]# ls -al *user*
lrwxrwxrwx    1 root     root            7 Jan 16 06:16 adduser -> useradd
-rwxr-xr-x    1 root     root       181932 Aug 30  2001 kuser
-rwxr-xr-x    1 root     root        16172 Aug 29  2001 lnewusers
-rwxr-xr-x    1 root     root        17168 Aug 29  2001 luseradd
-rwxr-xr-x    1 root     root        14252 Aug 29  2001 luserdel
-rwxr-xr-x    1 root     root        17140 Aug 29  2001 lusermod
-rwxr-xr-x    1 root     root        26156 Aug 27  2001 newusers
-rwxr-xr-x    1 root     root         9148 Aug  2  2001 rpc.rusersd
-rwxr-xr-x    1 root     root         4312 Aug 14  2001 sasldblistusers
-rwxr-xr-x    1 root     root        52236 Aug 27  2001 useradd
-rwxr-xr-x    1 root     root        35148 Aug 27  2001 userdel
-rws--x--x    1 root     root        20732 Aug 28  2001 userhelper
-rwxr-xr-x    1 root     root        53100 Aug 27  2001 usermod
-rwsr-xr-x    1 root     root         6340 Feb 18 07:23 usernetctl
[root@ftosx1 sbin]# userdel mike
[root@ftosx1 sbin]#
 

The file: "/etc/passwd"

UNIX, like Linux, again, list all the files in the "/etc/passwd" file.

This file haves the following look:

Each entry in the passwd file haves the following sense: If possible to change the Real name and also includes the office and home, running the command "chfn"  (Change Finger Info).

[root@ftosx1 root]# chfn orlando
Changing finger information for orlando.
Name []: Dr. Giovanni A. Orlando
Office []: Italy
Office Phone []: 0039 0434 209 107
Home Phone []: 0039 0434 XXXXXXXX

Finger information changed.
[root@ftosx1 root]# more /etc/passwd | grep orlando
orlando:x:500:500:Dr. Giovanni A. Orlando,Italy,0039 0434 209 107,0039 0434 XXXXXXXX:/home/orlando:/bin/bash
[root@ftosx1 root]#
 

We can check the info for any user running the command, "finger"

[root@ftosx1 root]# finger
Login    Name                    Tty      Idle  Login Time   Office     Office Phone
orlando  Dr. Giovanni A. Orland  tty3           Jan 16 16:42 Italy      0039 0434 209
root     root                    tty1     3:50  Jan 16 12:52
root     root                    pts/0    3:50  Jan 16 12:52
root     root                    pts/1          Jan 16 12:52
root     root                    pts/3    1:03  Jan 16 14:23
root     root                    pts/4    1:47  Jan 16 14:55
root     root                    pts/6          Jan 16 15:15
[root@ftosx1 root]#

The phone number generally is used as the extension number in the company, while the finger may be used by any user.

For example the user orlando, will be its info as follows:

Login: root                             Name: root
Directory: /root                        Shell: /bin/bash
On since Wed Jan 16 16:48 (EST) on tty1    9 minutes 42 seconds idle
On since Wed Jan 16 16:49 (EST) on pts/0   9 minutes 27 seconds idle
On since Wed Jan 16 16:49 (EST) on pts/1   29 seconds idle
No mail.
No Plan.

Login: orlando                          Name: Dr. Giovanni A. Orlando
Directory: /home/orlando                Shell: /bin/bash
Office: Italy, 0039 0434 209 107        Home Phone: 0039 0434 XXXXXXXX
On since Wed Jan 16 16:58 (EST) on tty2
No mail.
No Plan.
[root@ftosx1 root]#

Creating a file called: ".plan"

[root@ftosx1 root]# more ~orlando/.plan
Finish FTLinuxCourse Complete and release it.

Is possible to see each user and its actual role in the company.

Login: orlando                          Name: Dr. Giovanni A. Orlando
Directory: /home/orlando                Shell: /bin/bash
Office: Italy, 0039 0434 209 107        Home Phone: 0039 0434 XXXXXXXX
On since Wed Jan 16 16:58 (EST) on tty2
No mail.
Plan:
Finish FTLinuxCourse Complete and release it.
[root@ftosx1 root]#
 

The file "/etc/profile"

Linux supports the file: "/etc/profile", that list the TERMINAL setting, setup some important enviroment variables and other similar task.

These tasks are made in the "/etc/profile" file.

The profile file is included in the "/etc" directory.
 

[root@ftosx1 root]# more /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
 

# Path manipulation
if [ `id -u` = 0 ] && ! echo $PATH | /bin/grep -q "/sbin" ; then
    PATH=/sbin:$PATH
fi

if [ `id -u` = 0 ] && ! echo $PATH | /bin/grep -q "/usr/sbin" ; then
    PATH=/usr/sbin:$PATH
fi

if [ `id -u` = 0 ] && ! echo $PATH | /bin/grep -q "/usr/local/sbin" ; then
    PATH=/usr/local/sbin:$PATH
fi

if ! echo $PATH | /bin/grep -q "/usr/X11R6/bin" ; then
    PATH="$PATH:/usr/X11R6/bin"
fi

# No core files by default
ulimit -S -c 0 > /dev/null 2>&1

USER=`id -un`
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"

HOSTNAME=`/bin/hostname`
HISTSIZE=1000

if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then
    INPUTRC=/etc/inputrc
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC

for i in /etc/profile.d/*.sh ; do
    if [ -r $i ]; then
        . $i
    fi
done

unset i
[root@ftosx1 root]#

We can update the PATH to be used for any user including new variables. For example, the is common to use the variable QTDIR, used by the Qt Toolkit compilation.

For example, we can add the shell command:

export QTDIR=/root/FTGUI/qt

inside the /etc/profile and then run the command:

[root@ftosx1 root]# vi /etc/profile
[root@ftosx1 root]# echo $QTDIR
/usr/lib/qt-2.3.1
[root@ftosx1 root]# source /etc/profile
[
[root@ftosx1 root]# echo $QTDIR
/root/FTGUI/qt
[root@ftosx1 root]#

The source command is used to reload resource file, like /etc/profile, .bashrc or .twmrc files.
 

The file .bashrc

Generally users choose the BASH, as a standard shell. The BASH is the Bourne Again Shell. UNIX standard shell was the Bourne Shell, after the introduction of the C-Shell, and the T-CSH at berkeley the Bourne start to be obsolete.

The BASH, is in some sense a re-inventation of the Bourne Shell, but including a complete set of features available in the C Shell, that moves the BASH, to be used as a default shell, used my programmers, the system administrator and any other user.

The Bash, haves its resource files:

[root@ftosx1 root]# ls -al .bash
.bash_history  .bash_logout   .bash_profile  .bashrc
[root@ftosx1 root]# ls -al .bash (ESC) (ESC)

The most important file in the previous list is the .bashrc

[root@ftosx1 root]# more .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias f='fgrep -n'
alias h='history'
alias h1='h | grep'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@ftosx1 root]#

Note that this file, load and run the "/etc/bashrc" file if present.

[root@ftosx1 root]# cd /etc/
[root@ftosx1 etc]# more bashrc
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# by default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ `id -gn` = `id -un` -a `id -u` -gt 99 ]; then
        umask 002
else
        umask 022
fi

# are we an interactive shell?
if [ "$PS1" ]; then
    if [ -x /usr/bin/tput ]; then
      if [ "x`tput kbs`" != "x" ]; then # We can't do this with "dumb" terminal
        stty erase `tput kbs`
      elif [ -x /usr/bin/wc ]; then
        if [ "`tput kbs|wc -c `" -gt 0 ]; then # We can't do this with "dumb" terminal
          stty erase `tput kbs`
        fi
      fi
    fi
    case $TERM in
        xterm*)
                if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
                        PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
                else
                PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'
                fi
            ;;
        *)
                [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
            ;;
    esac
    [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

    if [ "x$SHLVL" != "x1" ]; then # We're not a login shell
        for i in /etc/profile.d/*.sh; do
            if [ -x $i ]; then
                . $i
            fi
        done
    fi
fi
# vim:ts=4:sw=4
[root@ftosx1 etc]#

This file defines the PS1 (Default Prompt Shell).

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
 

Note also that the ".bashrc" file includes different alias, that may be listed with the command: "alias"

[root@ftosx1 root]# alias
alias cp='cp -i'
alias f='fgrep -n'
alias h='history'
alias h1='h | grep'
alias l.='ls -d .[a-zA-Z]* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@ftosx1 root]#

We can also set the new alias, running the command:

alias gospec='cd /usr/src/ftosx/SPECS'

If we want that the alias become fixed, we need to includes it in the ".bashrc"

[root@ftosx1 root]# vi .bashrc
 

After the update we can

[root@ftosx1 root]# source !$
source .bashrc
[root@ftosx1 root]# gospec
[root@ftosx1 SPECS]# pwd
/usr/src/ftosx/SPECS
[root@ftosx1 SPECS]#
 

On WebServer is a good move, trap the init command with a simple "echo" command. This avoid simple reboot, or shutdown the machine, while is connected to the Web.

For example, the following commands avoid innecessary shutdowns.

alias reboot='echo Are you sure to use this command?'
alias shutdown='echo Are you sure to use this command?'
alias init='echo Are you sure to use this command?'

Note that these commands, does not have the parameter. Therefore if we run:

[root@www /root]# init 0
Are you sure to use this command? 0

To shutdown the machine using "init" we can run:

[root@www /root]# unalias init
[root@www /root]# init 0

We can also run alias with parameters, for example suppose you want to know all the commands that includes a particular parameter.

For example, suppose that you want to list all previous command where we use the command: "mount".

A very simple mode is run the command:

[root@ftosx1 root]# history | grep mount

How we can run this as an alias ?

Well, the answer in elementary. We can simply run the alias:

alias h1='h | grep'

where h=history.

Therefore, after the set inside the .bashrc file (see above) or running the command

[root@ftosx1 root]# alias h1
alias h1='h | grep'
[root@ftosx1 root]#
 

We will have:

[root@ftosx1 root]# h1 mount
   13  mount -t reiserfs /dev/hda5 /mnt/redhat71
   15  mount -t reiserfs /dev/hda5 /mnt/redhat71
   18  mount -t reiserfs /dev/hda8 /mnt/redhat62
   19  umount /mnt/redhat62
   20  mount -t reiserfs /dev/hda8 /mnt/redhat72
   22  mount -t reiserfs /dev/hda8 /mnt/redhat72
   26  mount -t reiserfs /dev/hda6 /mnt/drake
   28  mount -t reiserfs /dev/hda6 /mnt/drake
   48  mount
   52  mount -t vfat /dev/hda3 /mnt/win2000/
   82  mount /mnt/cdrom/
  119  h1 mount
  143  mount /mnt/redhat62/
  144  mount -t reiserfs /dev/hda7 /mnt/redhat62
  222  h1 mount
  230  h1 mount
  233  h1 mount
  238  h1 mount
  240  mount -t vfat /dev/hda3 /mnt/win2000/
  358  h1 mount
  575  mount -t reiserfs /dev/hda5 /mnt/redhat
  925  mount -t /dev/hda5 /mnt/redhat
  926  mount -t reiserfs /dev/hda5 /mnt/redhat
 1002  history | grep mount
 1007  h1 mount
[root@ftosx1 root]#

The command history presents the history of commands, or present a list of all commands we run in the shell session.

[root@ftosx1 root]# history
    3  ping thunder
    4  vi /etc/hosts
    5  ping thunder
    6  ping thunder
    7  cd fax1
    8  telnet thunder
    9  find . -name oo*
   10  startx -- -depth 16
...

We can also run the command "fc" (file command)

The "fc" may be runned with different options, for example: "fc -l", list the latest 10 commands.

[root@ftosx1 root]# fc -l
986      cd
987      rpm -ql kdeutils
988      rpm -ql kdeutils > kdeutils.content
989      cd -
990      rpm -e --nodeps kdeutils
991      rpm -i  objprelink-1.0-1.i386.rpm FTGUI-* FTLiquid-0.8-1.i386.rpm
992      rpm -e --nodeps koffice
993      rpm -e --nodeps abiword
994      rpm -e --nodeps nautilus
995      rpm -e --nodeps nautilus-devel
996      rpm -e --nodeps gmc
997      df -h
998      kscd
999      bg
1000     netscape &
1001     telnet www
1002     history
[root@ftosx1 root]#

We can also "edit" a command

[root@ftosx1 root]# fc 1000

For example, the latest command edit the command #1000 and the exectute it.

The editor to open these previous commands may be intialized with the enviroment variable: FCEDIT.

Using the Shell

In the previous sections we introduce how to create users, and its resource files, "/etc/profile" and "/etc/bashrc". In the past, an also today, users continue to use the shell prompt, ($), and generally using the shell prompt is the only mode to interact with the system, and run commands, that make the difference and introduce UNIX and its real opensource flavor, as the better Operating System ever made.

The first element that we note is the Prompt Shell.

[root@ftosx1 root]#

or for the user orlando:

[gorlando@ftosx1 gorlando]$

The displayed information (the prompt) resides in the PS1 enviroment variable

[gorlando@ftosx1 gorlando]$ echo $PS1
[\u@\h \W]\$
[gorlando@ftosx1 gorlando]$

Suppose for example, that you want now to display at any time the date.

We can update the information inside the file bashrc (global in "etc") to be more effective. We can change the info as follows:
 

Before the change After  the change
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ " [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W-`date`]\\$ "

You can run for example a program that evaluate the shell like "sg" (or other similar program) and you will have the prompt including the date!

[gorlando@ftosx1 gorlando-Fri Sep  7 17:05:47 EDT 2001]$

To back to the default simply run the command

The graphical enviroment may be configured using the relative programs. For example with KDE the user karl will have the interface in German, the user giovanni will have the interface in italian and the user mary will have the interface in English, on the same system.

Running your application

The shell is the most common mode to run applications.

For example if we want to run the konqueror, we may run:

[root@ftosx1 root]# konqueror

This is possible because the directory where the binary konqueror is installed, belong to the PATH.

[root@ftosx1 root]# which konqueror
/usr/bin/konqueror
[root@ftosx1 root]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@ftosx1 root]#
 

Is also possible to add new directories in the PATH to run other commands.

For example, we can run applications that are installed in internal dirs, including the complete path:

[root@ftosx1 root]# /root/FTLinuxCourse_Complete-2002/FTContribs/Shells/myfirstlogin
The time of your first login in this session was: 12:08pm
[root@ftosx1 root]#

If we includes this path in the PATH variable the program will run using its single name:

In fact,

[root@ftosx1 root]# export PATH=$PATH:/root/FTLinuxCourse_Complete-2002/FTContribs/Shells
[root@ftosx1 root]#
[root@ftosx1 root]# myfirstlogin
The time of your first login in this session was: 12:08pm
[root@ftosx1 root]#

Note that we don't includes the PATH with the binary name.

If we run a program from a shell and

[root@ftosx1 root]# konqueror

The shell will be "locked"; this means that we cannot send other commands to the shell because the program (konqueror) in this case lock the shell prompt.

We can however, stop the program, running the sequence: Ctrl-C

[root@ftosx1 root]# konqueror
(Ctrl-C)
[root@ftosx1 root]#

and the shell will be available again ... or we can also suspend the program and run in the background.

To do that we can run Ctrl-Z and the the command "bg"

[root@ftosx1 root]# konqueror
(Ctrl-Z)
[1]+  Stopped                 konqueror
[root@ftosx1 root]# bg
[1]+ konqueror &
[root@ftosx1 root]#
 

We can take the control again, running the command "fg".

[root@ftosx1 root]# fg
konqueror

and re-send again to the background with the procedure (Ctrl-Z) and "bg".

Of course we can also run the program with "&" that send the program directly to the background:

[root@ftosx1 root]# kworldclock &
[3] 2544
[root@ftosx1 root]#

In this case we get the backround id [3] and also the PID, the process ID.

In each shell we can run the command: "jobs" to list all the background commands:

[root@ftosx1 root]# jobs
[1]   Running                 netscape &
[2]-  Running                 konqueror &
[3]+  Done                    kworldclock
[root@ftosx1 root]#
 

The command "jobs" is valid only to list the background that we send from the local shell. In other words, if we run the netscape from the ttyp2 and konqueror from ttyp3, "jobs" from ttyp2 will list only "netscape".

Using "jobs" we can run different commands like:

[root@ftosx1 root]#kill %2

To kill the second program we send to the background

[root@ftosx1 root]# fg %1
netscape

and other similars.

Linux like UNIX, supports also the possibility to send multiple commands to the shell.

For example, we can send commands:

[root@ftosx1 root]# cd /usr/src/linux-2.4
[root@ftosx1 linux-2.4]# make dep; make clean
 

Note the ";" between the copmmands.

We can also run simple commands like:

[root@ftosx1 root]# less /etc/passwd ; less /etc/group
 

The "nohup" command

Now, suppose we run a program, like "konqueror" from an xterm or "konsole".

If, we close or "kill" the shell ... inmmediately will be closed or killed also the "konqueror".

However, Linux offers the possibility to continues to run the program "konqueror" also if the original shell (xterm or konsole) is closed.

Therefore, we open a konsole and run inside "who am i"

[root@ftosx1 root]# who am i
ftosx1.futuretg.com!root     pts/10   Jan 21 15:23
[root@ftosx1 root]#
 

Now, we run "konqueror"  in background mode.

[root@ftosx1 root]# konqueror &
[1] 3327
[root@ftosx1 root]#

Now we close the shell, and also the konqueror will die.

Again, we run the "konsole"  (... if we does not open any other konsole, the next one will be the same or the last one we close).

Now, we run the command:

[root@ftosx1 root]# nohup konqueror &
[2] 3448

... and we close the shell (or konsole)

Therefore will have that the konqueror continues to run, without any problem.
 

The Jolly commands

UNIX, like DOS supports the jolly or (wild commands):


[root@ftosx1 root]# cd /usr/doc/PyQt-examples-2.4/
[root@ftosx1 PyQt-examples-2.4]# ls
aclock.py       application.py   dclock.py   dirview.py  README       table.py   tut10.py  tut12.py  tut14.py  tut2.py  tut4.py  tut6.py  tut8.py  widgets.py
addressbook.py  buttongroups.py  desktop.py  menu.py     splitter.py  themes.py  tut11.py  tut13.py  tut1.py   tut3.py  tut5.py  tut7.py  tut9.py
[root@ftosx1 PyQt-examples-2.4]#
[root@ftosx1 PyQt-examples-2.4]#
[root@ftosx1 PyQt-examples-2.4]#

For example, if we want to list the programs that start with "tut" but haves a single character, we will have:

[root@ftosx1 PyQt-examples-2.4]# ls tut?.py
tut1.py  tut2.py  tut3.py  tut4.py  tut5.py  tut6.py  tut7.py  tut8.py  tut9.py
[root@ftosx1 PyQt-examples-2.4]#
[root@ftosx1 PyQt-examples-2.4]#
 

Or, we can list the programs with two characters after the "tut".

[root@ftosx1 PyQt-examples-2.4]# ls tut??.py
tut10.py  tut11.py  tut12.py  tut13.py  tut14.py
[root@ftosx1 PyQt-examples-2.4]#
 

Note that the last command does not list the programs with "one" ... but only with "two" characters.

We can also list also all the programs that start with "tut":

[root@ftosx1 PyQt-examples-2.4]# ls tut*.py
tut10.py  tut11.py  tut12.py  tut13.py  tut14.py  tut1.py  tut2.py  tut3.py  tut4.py  tut5.py  tut6.py  tut7.py  tut8.py  tut9.py
[root@ftosx1 PyQt-examples-2.4]#
 

The range commands

The previous commands are available also in DOS, but UNIX offers also the range commands:


for example we can list files that start with 'a' or 'b',

[root@ftosx1 PyQt-examples-2.4]# ls [a-b]*
aclock.py  addressbook.py  application.py  buttongroups.py
[root@ftosx1 PyQt-examples-2.4]#

we can also list files that start with 'a', 'b' and 't'.

[root@ftosx1 PyQt-examples-2.4]# ls [a-b,t]*
aclock.py       application.py   table.py   tut10.py  tut12.py  tut14.py  tut2.py  tut4.py  tut6.py  tut8.py
addressbook.py  buttongroups.py  themes.py  tut11.py  tut13.py  tut1.py   tut3.py  tut5.py  tut7.py  tut9.py
[root@ftosx1 PyQt-examples-2.4]#

In the same mode we can list all the files that start with 'a' and also the file: tut4.py.

[root@ftosx1 PyQt-examples-2.4]#  ls {a*,tut4.py}
aclock.py  addressbook.py  application.py  tut4.py
[root@ftosx1 PyQt-examples-2.4]#
 

The pipe: "|"

In this chapter we run the command:

[root@ftosx1 root]# h1 mount
   58  h1 mount
   66  h1 mount
   69  h1 mount
   74  h1 mount
   76  mount -t vfat /dev/hda3 /mnt/win2000/
  194  h1 mount
  411  mount -t reiserfs /dev/hda5 /mnt/redhat
  761  mount -t /dev/hda5 /mnt/redhat
  762  mount -t reiserfs /dev/hda5 /mnt/redhat
  838  history | grep mount
  843  h1 mount
  851  h1 mount
  852  mount -t reiserfs /dev/hda5 /mnt/redhat
 1000  h1 mount
[root@ftosx1 root]#

This simple example, introduce one of the most common examples:

For example, if we want to know the number of files in the local directory we can run the command:

[root@ftosx1 SPECS]# ls -al | wc -l
    727
[root@ftosx1 SPECS]#
 

In C programming, the popen(), pclose() and pipe() are the system calls used

Command redirection

Unix and Linux offers different modes to re-direct the output.

The ">" and ">>" operators

UNIX offers also different mode of redirection:

For example we can redirect the output of the command: "ls" using ">",
 

[root@ftosx1 PyQt-examples-2.4]#  ls > 22
[root@ftosx1 PyQt-examples-2.4]# more 22
22
aclock.py
addressbook.py
application.py
buttongroups.py
dclock.py
desktop.py
dirview.py
menu.py
README
splitter.py
table.py
themes.py
tut10.py
tut11.py
tut12.py
tut13.py
tut14.py
tut1.py
tut2.py
tut3.py
tut4.py
tut5.py
tut6.py
tut7.py
tut8.py
tut9.py
widgets.py
[root@ftosx1 PyQt-examples-2.4]#

The explanation is easy. UNIX haves three standard from the shell.

When we run a program that generate output this output may be redirected to a file, using the ">" or the ">>".

However, if we run a program that generate errors like:

[root@ftosx1 PyQt-examples-2.4]# who am i ? john
who: too many arguments
Try `who --help' for more information.
[root@ftosx1 PyQt-examples-2.4]#

The output may be not send to a shell.

[root@ftosx1 PyQt-examples-2.4]# who am i ? john > probably_error
who: too many arguments
Try `who --help' for more information.
[root@ftosx1 PyQt-examples-2.4]# more probably_error
[root@ftosx1 PyQt-examples-2.4]# ls -al probably_error
-rw-r--r--    1 root     root            0 Jan 21 13:52 probably_error
[root@ftosx1 PyQt-examples-2.4]#
 

However if we add the number "2" that represent the standard error, we will move the error to the file.

[root@ftosx1 root]# who am i john? 2> probably_error
[root@ftosx1 root]# more probably_error
who: too many arguments
Try `who --help' for more information.
[root@ftosx1 root]#
 

Is also possible redirect errors using ">" . The command is a little more complex

[root@ftosx1 root]# who am i john? > probably_error.bak 2>&1
[root@ftosx1 root]# more probably_error.bak
who: too many arguments
Try `who --help' for more information.
[root@ftosx1 root]#

Is possible also to disable  the use of jolly characters as well the possibility to overwrite files, with the re-director ">".

To disable the jolly chars "?" and "*", we can run:

[root@ftosx1 PyQt-examples-2.4]# ls tut?.py
tut1.py  tut2.py  tut3.py  tut4.py  tut5.py  tut6.py  tut7.py  tut8.py  tut9.py
[root@ftosx1 PyQt-examples-2.4]# set -o noglob
[root@ftosx1 PyQt-examples-2.4]# ls tut?.py
ls: tut?.py: No such file or directory
[root@ftosx1 PyQt-examples-2.4]#

We can also avoid to overwrite files:

[root@ftosx1 PyQt-examples-2.4]# ls
aclock.py       buttongroups.py  dirview.py  splitter.py  tut10.py  tut13.py  tut2.py  tut5.py  tut8.py
addressbook.py  dclock.py        menu.py     table.py     tut11.py  tut14.py  tut3.py  tut6.py  tut9.py
application.py  desktop.py       README      themes.py    tut12.py  tut1.py   tut4.py  tut7.py  widgets.py
[root@ftosx1 PyQt-examples-2.4]# ls > 22
[root@ftosx1 PyQt-examples-2.4]# set -o noclobber
[root@ftosx1 PyQt-examples-2.4]# ls > 22
bash: 22: cannot overwrite existing file
[root@ftosx1 PyQt-examples-2.4]#
 

The redirector "<"

In the same mode we send output to files, we can read from standard input or from files using the re-director: "<".

For example suppose we have a file, in UPPERCASE:

$more UPPFILE
THIS FILE INCLUDES ONLY WORDS IN UPPCASE.
NO WORDS IN LOWER CASE CAN BELONG TO THIS FILE.
THE NUMBERS: 1234567890 CAN BELONG BECAUSE ARE NUMBERS!.
$
$

There are UNIX/Linux commands like: "tr" that read the standard input and transform the files in lower case or support similar commands:

$tr A-Z a-z < UPPFILE
this file includes only words in uppcase.
no words in lower case can belong to this file.
the numbers: 1234567890 can belong because are numbers!.
$

Of course, these operations are possible also in shell scripts:

$more UPP2low
tr A-Z a-z < $1 > $1.low
$

The redirector "<<"

The "<<" operator generally is used in shell programming, however we prefer to introduce it, here together with standard operators like: "<", ">", and ">>".

Run, for example the command:

$cat << ^D
> echo "Hello Bash"
> ^D
echo "Hello Bash"
$

Note the ^D (or Ctrl-D) is inserted using the normal mode we use in "vi". Therefore, to insert a Ctrl-D, we write Ctrl-V + Ctrl-D.

The operator "<<", is also used to print "usage" in shell scripts like "grub-install".

[root@ftosx1 Shells]# more /sbin/grub-install
...
# Usage: usage
# Print the usage.
usage () {
    cat <<EOF
Usage: grub-install [OPTION] install_device
Install GRUB on your drive.

  -h, --help              print this message and exit
  -v, --version           print the version information and exit
  --root-directory=DIR    install GRUB images under the directory DIR
                          instead of the root directory
  --grub-shell=FILE       use FILE as the grub shell
  --force-lba             force GRUB to use LBA mode even for a buggy
                          BIOS
  --recheck               probe a device map even if it already exists

INSTALL_DEVICE can be a GRUB device name or a system device filename.

Report bugs to <bug-grub@gnu.org>.
EOF
}
...
 
 

Monitoring the users on the System

There are different commands to monitor the system. Monitor the system, means who is logged, what users does, what is its role, etc.

The command "w" is very usefull to print a list of the users and theirs commands:

[root@ftosx1 root]# w
  2:51pm  up  4:17, 10 users,  load average: 0.20, 0.15, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT
root     tty1     -                 3:36pm   ?     0.93s  0.01s  /bin/sh /usr/X11R6/bin/startx -- -depth 16
root     pts/0    -                 5:46pm   ?     0.01s  0.01s  /bin/cat
root     pts/1    -                 5:47pm  1:04m  1.71s  1.62s  /usr/bin/mc -P
root     pts/3    -                 1:03pm  1:42m  0.07s  0.03s  /usr/bin/at -q b now
root     pts/5    -                 1:44pm 56:06  58.96s  0.05s  /bin/bash
root     pts/6    -                 1:46pm  1:04m  0.08s  0.03s  telnet thunder
root     pts/7    -                 1:47pm  1:04m  0.06s  0.06s  /bin/bash
root     pts/8    -                 1:56pm  0.00s  0.13s  0.02s  w
[root@ftosx1 root]#
 

We can also runs the command: "who"

[root@ftosx1 root]# who
root     tty1     Jan 21 15:36
root     pts/0    Jan 21 17:46
root     pts/1    Jan 21 17:47
root     pts/3    Jan 21 13:03
root     pts/4    Jan 21 16:10
root     pts/5    Jan 21 13:44
root     pts/6    Jan 21 13:46
root     pts/2    Jan 21 12:49
root     pts/7    Jan 21 13:47
root     pts/8    Jan 21 13:56
[root@ftosx1 root]#

and its famous "who am i"

[root@ftosx1 root]# who am i
ftosx1.futuretg.com!root     pts/8    Jan 21 13:56
[root@ftosx1 root]#

We can list the mission or plan of each user.

[root@ftosx1 root]# finger -l
Login: root                             Name: root
Directory: /root                        Shell: /bin/bash
On since Mon Jan 21 15:36 (CET) on tty1
On since Mon Jan 21 17:46 (CET) on pts/0
On since Mon Jan 21 17:47 (CET) on pts/1   1 hour 21 minutes idle
On since Mon Jan 21 13:03 (CET) on pts/3   1 hour 59 minutes idle
On since Mon Jan 21 16:10 (CET) on pts/4   1 hour 24 minutes idle
On since Mon Jan 21 13:44 (CET) on pts/5   1 hour 12 minutes idle
On since Mon Jan 21 13:46 (CET) on pts/6   1 hour 20 minutes idle
On since Mon Jan 21 12:49 (CET) on pts/2   20 days 14 hours idle
     (messages off)
On since Mon Jan 21 13:47 (CET) on pts/7   1 hour 20 minutes idle
On since Mon Jan 21 13:56 (CET) on pts/8
No mail.
No Plan.

Login: orlando                          Name: Dr. Giovanni A. Orlando
Directory: /home/orlando                Shell: /bin/bash
Office: Future Technologies - Italy
Office Phone: 001 39 0434 209 107       Home Phone: XXXXX
On since Mon Jan 21 15:07 (CET) on tty2   10 seconds idle
No mail.
Plan:
Finish FTLinuxCourse Complete and release it.
[root@ftosx1 root]#
 
 

Changing users priority in daily workload with "nice"

Let's suppose that the system is running the compilation of several components of a big project. The result of a report program for marketing has a higher priority than the program about the stock. Using the program "nice" it's possible to change the priority to the marketing task

In fact, the program "nice" without any parameters prints us the actual priority user level :

[root@ftosx1 /root]# nice
0
[root@ftosx1 /root]#

The nice priority ranges from the highest "-20" to the lowest priority of "19".

Observe how "nice" changes the priority of a program, allowing a higher or a lower workload depending on the request:

[root@ftosx1 /root]# time nice --20 cat /etc/passwd /dev/null
0.00user 0.01system 0:00.00elapsed 166%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (150major+15minor)pagefaults 0swaps
[root@ftosx1 /root]#
[root@ftosx1 /root]#
[root@ftosx1 /root]# time nice -19 cat /etc/passwd /dev/null
0.00user 0.00system 0:00.07elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (150major+15minor)pagefaults 0swaps
[root@ftosx1 /root]#

As we can see, with priority "-20" the CPU has a workload of 166% and uses only 0.01 of the system CPU, while with the lower priority the system workload is constant and the command takes only 0.07 seconds.

The parameter "--20" moves the nice priority to "-20" and therefore changes the priority to the highest.
 

Exercises

  1. Change the shell to print the date. (This chapter includes the answer).
Tests
  1. What is the program to create user accounts ?
  2. What directory contains the original bashrc that will be copied to the user home ?
  3. What does the command Ctrl-U ?
  4. What represent the sequence Ctrl-D ?
  5. What is the Linux command to reload the .bashrc file ?
  6. What shell resource file includes the PS1 Prompt Shell definition ?
  7. What is an alias ? How we can set an alias ? How we can unset an alias ?
  8. How we can write the EOF character in the shell prompt ?
  9. Is possible to redirect the standard error to file, using the redirector ">" ?
  10. What does the command: "tr A-Z a-z < file > file.bak ?
  11. Is possible to list all the jobs that are running in the background ? ... Is possible, for example to kill the job #3 ?
  12. What is the output of the command: ls {a-b*,tut4.py} ? Have sense ?
  13. What command will list all the files that start with 'a', 'b' , 'c', 'd' and 'H' ?
  14. What command print the plans for each user logged ?
  15. List three commands to monitor the users in the system.
  16. What command list the actual users logged in the system ?
  17. Are there some command to know the process ID ?
  18. If we set the enviroment variable noglob. What happens ?
  19. What is the default "nice" level ?
  20. What does the nohup command? Make an example.
  21. What does the command "fc" ?
  22. How we can count the files present in a dir using the pipe "|" ?
  23. Is there are some command to remove a user ?
  24. What is the difference between "adduser" and "useradd" ?
  25. Where are located the files "adduser" and "userdel" ?
Consult the answers

Check the Interactive Exam Cram SysAdm: Try the interactive cram ...

Internet Resources for this Chapter.