Command line user interface

General

  • System administration requires the use of command line interface (CLI) since graphical user interface (GUI) may not be available
  • Most system administration tasks are easier and quicker to execute through CLI
  • In this course we focus only for CLI
  • All commands are run through shell in Linux

Terminal

  • Terminal is a text based command line interface
  • Computer hardware is utilized and installed programs are run by inputting text commands sequentially

CLI example


Command line management

  • Below is the list of some of the general hot keys for command line management
Key combination Action
Ctrl + a Move cursor to the beginning of the line
Ctrl + e Move cursor to the end of the line
Ctrl + p Previous command
Ctrl + n Next command
Ctrl + u Remove all characters between cursor and the beginning of the line
Ctrl + k Remove all characters between cursor and the end of the line
Ctrl + l Clear screen
  • The following table includes some of the basic keys for interacting with command line
Key Action
TAB Autocomplete the command
Previous command from command history
Next command from command history
Del Remove character from the position of cursor
Backspace Remove character from the left of the cursor

Autocompleting commands - example

  • Command can be autocompleted with TAB key
  • This feature is supported for example in bash command shell
  • Example:
    • Write the word who in command line and press TAB button twice
testuser@ubuntu-pc:~$ who 
who whoami
testuser@ubuntu-pc:~$
  • As can be seen from the example, two commands beginning with the word who are presented

Shell

  • Shell is a text based program for controlling the operating system
  • Login is required for user authentication
  • Shell starts automatically when user logs in to the operating system
  • During shell startup both common and user settings are loaded
  • After startup an empty command prompt is printed to the screen
  • User is now able to enter commands through this prompt
  • Current Linux distributions use bash as the default shell
  • Other shells can be chosen as well
  • All valid shells can be found from the file /etc/shells with the following command:
testuser@ubuntu-pc:~$ cat /etc/shells
  • Some of the most common shells are listed in the table below
Shell Description
sh (Bourne shell) Primitive shell from the 1970s
bash (Bourne again shell) Added the option for command autocompletion, command line editing and command history
csh (C shell) Shell with syntax resembling the C programming language
tcsh (Enhanced C shell) Further developed version of csh with modern features
ksh (Korn shell) Shell with command line editing and command history
zsh (Z shell) Modern shell with Bourne shell compatibility

Shell functionality

  • The following sequence describes how shell works
    1. User inputs command through command prompt and this command is transferred to the shell
    2. Shell interprets the command and calls for the corresponding program
      • If program cannot be found shell returns error message
      • If program can be found, new process is created for the execution of the program
    3. After the execution of the program shell prints new empty command prompt for the user

Shell functionality


Shell functionality - example

  • Below is the example where user enters valid and invalid command
testuser@ubuntu-pc:~$ cd ..
testuser@ubuntu-pc:/home$ cd..
cd..: command not found
testuser@ubuntu-pc:/home$
  • The description of the action above:
  • User inputs a valid command cd ..
  • User's current directory changes (changes to the parent directory)
  • User inputs invalid command cd..
  • Shell return error command not found
  • New empty prompt is printed to the screen

Command prompt

  • The structure of Linux command prompt is presented below

Linux prompt structure


User types in Linux

  • Regular user can perform tasks not requiring special permissions
  • Super user / root has rights to perform system wide tasks
    • Bypass all system security restrictions
    • Delete all files, read other user files etc.
    • Install, customize and remove programs
    • etc.
  • For the security point of view it is important to use regular user and use root user rights only if necessary
  • Below is the comparison of regular and root user

Prompt comparison between different user types


Change the user

  • Current logged in user can be printed with whoami command
testuser@ubuntu-pc:~$ whoami
testuser
  • This user is regular user without any permissions for performing system wide tasks
  • To login with superuser, use su (switch user) command (superuser password is required)
testuser@ubuntu-pc:~$ su
Password:
root@ubuntu-pc:~#
  • To change user to some other user than root, user account name will be given as a parameter for the previously presented command
testuser@ubuntu-pc:~$ su - sulo
Password:
sulo@ubuntu-pc:~$
  • To logout from current user session use exit or logout commands
testuser@ubuntu-pc:~$ su - sulo
Password:
sulo@ubuntu-pc:~$ exit
testuser@ubuntu-pc:~$ su - root
Password:
root@ubuntu-pc:~# logout
testuser@ubuntu-pc:~$

Important: If su command is used without - character, current directory is not changed and only the following environment variables will be changed: HOME and SHELL (In addition, USER and LOGNAME will also be changed if target user is not root)!


Setting / changing password for the user

  • Root user does not password by default
  • Password can be set with the following command
testuser@ubuntu-pc:~$ sudo passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Sudo

  • Sudo command (substitute user do) allows regular user to execute commands with administrative permissions
    • Regular user is a member of a group with administrative permissions
  • Sudo command can be found from most of the current Linux distributions
  • Sudo command is used for tasks that require administrative permissions
  • Below is an example where system is shutdown with sudo command
testuser@ubuntu-pc:~$ sudo /sbin/shutdown –r now
[sudo] password for testuser:
.
System is restarting...
.

Where to get help with Linux commands? - Man

  • Each command has a dedicated page in man(ual)
  • Manual can be run with man command and by giving command name as a parameter
  • Below is an example where whoami command section is searched from the manual
testuser@ubuntu-pc:~$ man whoami
WHOAMI(1)                                                 User Commands                                                      WHOAMI(1)

NAME
       whoami - print effective userid

SYNOPSIS
       whoami [OPTION]...

DESCRIPTION
       Print the user name associated with the current effective user ID.  Same as id -un.

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by Richard Mlynarik.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report whoami translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2017 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation at: <http://www.gnu.org/software/coreutils/whoami>
       or available locally via: info '(coreutils) whoami invocation'

GNU coreutils 8.28                                       January 2018                                                         WHOAMI(1)
  • Manual can be navigated with the following keys:

    • Page up / down: manual is navigated up / down
    • q: exists from the manual
  • Man includes a lot of useful information about the command, but the command needs to be known beforehand

  • If user cannot remember the command name, parts of the name or command description can be searched from the manual by using the command man -k word
  • Below is an example where the word who is searched from the manual
testuser@ubuntu-pc:~$ man –k who
at.allow (5)        - determine who can submit jobs via at or batch
at.deny (5)         - determine who can submit jobs via at or batch
bsd-from (1)        - print names of those who have sent mail
btrfs-filesystem (8)- command group other primarily does work on the whole filesystems
from (1)            - print names of those who have sent mail
w (1)               - Show who is logged on and what they are doing.
w.Procps (1)        - Show who is logged on and what they are doing.
who(1)              - Show who is logged on
whoami (1)          - print effective userid

--help option

  • Almost all commands have --help option available
  • Option is given after the command and it prints a short description of the command
testuser@ubuntu-pc:~$ whoami –help
Usage: whoami [OPTION]...
Print the user name associated with the current effective user ID.
Same as id –un.

    --help      display this help and exit
    --version   output version information and exit

GNU coreutils online help:
<http://www.gnu.org/software/coreutils/>
Full documentation at:
<http://www.gnu.org/software/coreutils/whoami>
Or available locally via: ’(coreutils) whoami invocation’
testuser@ubuntu-pc:~$

info

  • Online documentation about the command can be read with info command
testuser@ubuntu-pc:~$ info whoami
Next: groups invocation,   Prev: logname invocation,   Up: User information

20.3. ’whoami’: Print effective user ID
==============================
’whoami’ prints the user name associated with the current effective user ID. It is equivalent to the command ’id -un’.

    The only options are ’--help’ and ’--version’. *Note Common options::.

    An exit status of zero indicates success, and a nonzero value indicates failure.

Apropos and Whatis

  • Apropos offers a short description for the command that matches the keyword used in search (this is similar to man -k word command)
testuser@ubuntu-pc:~$ apropos who
at.allow (5)        - determine who can submit jobs via at or batch
at.deny (5)         - determine who can submit jobs via at or batch
bsd-from (1)        - print names of those who have sent mail
btrfs-filesystem (8)- command group other primarily does work on the whole filesystems
from (1)            - print names of those who have sent mail
w (1)               - Show who is logged on and what they are doing.
w.Procps (1)        - Show who is logged on and what they are doing.
who(1)              - Show who is logged on
whoami (1)          - print effective userid
  • The command whatis offers a short description for a given command
testuser@ubuntu-pc:~$ whatis whoami
whoami (1)          -print effective userid