Skip to content

CLI Basics

  • Use the command line to perform operations in Linux.
  • Search for files.
  • Create and manage files.
  • Install and update software.

The command line interface provides the following advantages:

  • No GUI overhead is incurred.
  • Virtually any and every task can be accomplished while sitting at the command line.
  • You can implement scripts for often-used (or easy-to-forget) tasks and series of procedures.
  • You can sign into remote machines anywhere on the Internet.
  • You can initiate graphical applications directly from the command line instead of hunting through menus.
  • While graphical tools may vary among Linux distributions, the command line interface does not.

Using a Text Terminal on the Graphical Desktop

Section titled “Using a Text Terminal on the Graphical Desktop”
  • A terminal emulator program emulates (simulates) a standalone terminal within a window on the desktop.

  • By default, on GNOME desktop environments, the gnome-terminal application is used to emulate a text-mode terminal in a window. Other available terminal programs include:

  • Most input lines entered at the shell prompt have 3 basic elements:

    • Command
    • Options
    • Arguments
  • The command is the name of the program you are executing. It may be followed by one or more options (or switches) that modify what the command may do.

  • Options usually start with one or two dashes, for example, -p or --print, in order to differentiate them from arguments, which represent what the command operates on.

However, plenty of commands have no options, no arguments, or neither. In addition, other elements (such as setting environment variables) can also appear on the command line when launching a task.

  • sudo allows users to run programs using the security privileges of another user, generally root (superuser).

Steps for Setting Up and Running sudo (If Sudo is not enabled)

  • Make modifications as the administrative or superuser, root. While sudo will become the preferred method of doing this, we do not have it set up yet, so we will use su instead.
  • At the command line prompt, type su and press Enter. You will then be prompted for the root password, so enter it and press Enter.
  • Now, you will notice that nothing is printed; this is so others cannot see the password on the screen. You should end up with a different looking prompt, often ending with #. For example:
Terminal window
su
Password:
#
  • Now, you need to create a configuration file to enable your user account to use sudo. Typically, this file is created in the /etc/sudoers.d/ directory with the name of the file the same as your username.
    • For example, for this demo, let’s say your username is student. After doing step 1, you would then create the configuration file for student by doing this:
Terminal window
# echo "student ALL=(ALL) ALL" > /etc/sudoers.d/student
  • Finally, some Linux distributions will complain if you do not also change permissions on the file by doing:
Terminal window
# chmod 440 /etc/sudoers.d/student
  • When using sudo, by default you will be prompted to give a password (your own user password) at least the first time you do it within a specified time interval.
  • It is possible (though very insecure) to configure sudo to not require a password or change the time window in which the password does not have to be repeated with every sudo command.

Switching Between the GUI and the Command Line

Section titled “Switching Between the GUI and the Command Line”
  • The customizable nature of Linux allows you to drop the graphical interface (temporarily or permanently) or to start it up after the system has been running.
  • Linux production servers are usually installed without the GUI, and even if it is installed, usually do not launch it during system startup. Removing the graphical interface from a production server can be very helpful in maintaining a lean system, which can be easier to support and keep secure.

virtual_terminals

  • Virtual Terminals (VT) are console sessions that use the entire display and keyboard outside of a graphical environment. Such terminals are considered “virtual” because, although there can be multiple active terminals, only one terminal remains visible at a time.
  • A VT is not quite the same as a command line terminal window; you can have many of those visible at once on a graphical desktop.
  • One virtual terminal (usually number one or seven) is reserved for the graphical environment, and text logins are enabled on the unused VT’s.

Ubuntu uses VT 7, but CentOS/RHEL and openSUSE use VT 1 for the graphical display.

  • An example of a situation where using VT is helpful is when you run into problems with the graphical desktop. In this situation, you can switch to one of the text VT’s and troubleshoot.
  • To switch between VT’s, press CTRL-ALT-function key for the VT.
    • For example, press CTRL-ALT-F6 for VT 6. Actually, you only have to press the ALT-F6 key combination if you are in a VT and want to switch to another VT.
  • Linux distributions can start and stop the graphical desktop in various ways. The exact method differs from distribution and among distribution versions.
  • For the newer systemd-based distributions, the display manager is run as a service, you can stop the GUI desktop with the systemctl utility and most distributions will also work with the telinit command, as in:
Terminal window
sudo systemctl stop gdm
#or
sudo telinit 3
#and restart it (after logging into the console) with:
sudo systemctl start gdm
#or
sudo telinit 5

On Ubuntu versions before 18.04 LTS, substitute lightdm for gdm.

basic cli commands

  • An available text terminal will prompt for a username (with the string login:) and password. When typing your password, nothing is displayed on the terminal (not even a ***** to indicate that you typed in something), to prevent others from seeing your password. After you have logged into the system, you can perform basic operations.
  • The preferred method to shut down or reboot the system is to use the shutdown command. This sends a warning message, and then prevents further users from logging in.
  • The init process will then control shutting down or rebooting the system. It is important to always shut down properly; failure to do so can result in damage to the system and/or loss of data.
  • The halt and poweroff commands issue shutdown -h to halt the system;
  • reboot issues shutdown -r and causes the machine to reboot instead of just shutting down.
  • Both rebooting and shutting down from the command line requires superuser (root) access.
  • When administering a multi-user system, you have the option of notifying all users prior to shutdown, as in:
Terminal window
sudo shutdown -h 10:00 "Shutting down for scheduled maintenance."
Terminal window
man man
NAME
man - an interface to the system reference manuals
SYNOPSIS
man [man options] [[section] page ...] ...
man -k [apropos options] regexp ...
man -K [man options] [section] term ...
man -f [whatis options] page ...
man -l [man options] file ...
man -w|-W [man options] page ...
DESCRIPTION
man is the system's manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and
displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections following a pre-defined order (see
DEFAULTS), and to show only the first page found, even if page exists in several sections.
The table below shows the section numbers of the manual followed by the types of pages they contain.
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions, e.g. /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7), man-pages(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]

How to check, what sections/pages we have available?

Terminal window
man -f ls
ls (1) - list directory contents

we see, ls has 1 page only.

Let’s use intro. This command in the introduction to Linux commands.

Terminal window
man -f intro
intro (1) - introduction to user commands
intro (2) - introduction to system calls
intro (3) - introduction to library functions
intro (4) - introduction to special files
intro (5) - introduction to file formats and filesystems
intro (6) - introduction to games
intro (7) - introduction to overview and miscellany section
intro (8) - introduction to administration and privileged commands

shows us multiple sections available.

Let’s play with them, and please remember, if you wish to quit, press q.

Terminal window
man 1 intro
man 8 intro

To quickly check what a command does

Terminal window
whatis ls
ls (1) - list directory contents

shows short description of the functionality.

The same is available with man:

Terminal window
man -f ls
ls (1) - list directory contents
man -w ls #returns the location of the file from where the page is rendered
/usr/share/man/man1/ls.1.gz
man -k ls #searches for the given command through all man pages, and returns all of them as output.
  • The PS1 variable is the character string that is displayed as the prompt on the command line.

  • Most distributions set PS1 to a known default value, which is suitable in most cases. However, users may want custom information to show on the command line.

    • For example, some system administrators require the user and the host system name to show up on the command line as in:

      Terminal window
      student@c8 $
  • This could prove useful if you are working in multiple roles and want to be always reminded of who you are and what machine you are on. The prompt above could be implemented by setting the PS1 variable to: \u@\h \$

    • For example:
    Terminal window
    $ echo $PS1
    $
    $ PS1="\u@\h \$ "
    student@c8 $ echo $PS1
    \u@\h \$
    student@c8 $
  • By convention, most systems are set up so that the root user has a pound sign (#) as their prompt.

When commands are executed, by default there are three standard file streams (or descriptors) always open for use:

  • standard input (standard in or stdin),
  • standard output (standard out or stdout) and
  • standard error (or stderr)

standard_file_streams

NameSymbolic NameValueExample
standard inputstdin0keyboard
standard outputstdout1terminal
standard errorstderr2terminal
  • Usually, stdin is your keyboard, and stdout and stderr are printed on your terminal.

  • stderr is often redirected to an error logging file, while stdin is supplied by directing input to come from a file or from the output of a previous command through a pipe.

  • stdout is also often redirected into a file. Since stderr is where error messages are written, usually nothing will go there.

  • In Linux, all open files are represented internally by what are called file descriptors. Simply put, these are represented by numbers starting at zero.

    • stdin is file descriptor 0,
    • stdout is file descriptor 1, and
    • stderr is file descriptor 2.
  • Typically, if other files are opened in addition to these three, which are opened by default, they will start at file descriptor 3 and increase from there.

  • Through the command shell, we can redirect the three standard file streams so that we can get input from either a file or another command, instead of from our keyboard, and we can write output and errors to files or use them to provide input for subsequent commands.

    • For example, if we have a program called do_something that reads from stdin and writes to stdout and stderr, we can change its input source by using the less-than sign (<) followed by the name of the file to be consumed for input data:

      Terminal window
      do_something < input-file
  • If you want to send the output to a file, use the greater-than sign (>) as in:

    Terminal window
    do_something > output-file
  • Because stderr is not the same as stdout, error messages will still be seen on the terminal windows in the above example.

  • If you want to redirect stderr to a separate file, you use stderr’s file descriptor number (2), the greater-than sign (>), followed by the name of the file you want to hold everything the running command writes to stderr:

    Terminal window
    do_something 2> error-file
  • A special shorthand notation can send anything written to file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout): 2>&1.

    Terminal window
    do_something > all-output-file 2>&1
    # bash permits an easier syntax for the above:
    do_something >& all-output-file
  • The UNIX/Linux philosophy is to have many simple and short programs (or commands) cooperate together to produce quite complex results, rather than have one complex program with many possible options and modes of operation.

  • In order to accomplish this, extensive use of pipes is made. You can pipe the output of one command or program into another as its input.

  • In order to do this, we use the vertical-bar, pipe symbol (|), between commands as in:

    Terminal window
    command1 | command2 | command3
  • The above represents what we often call a pipeline, and allows Linux to combine the actions of several commands into one. This is extraordinarily efficient because command2 and command3 do not have to wait for the previous pipeline commands to complete before they can begin hacking at the data in their input streams; on multiple CPU or core systems, the available computing power is much better utilized and things get done quicker.

  • Furthermore, there is no need to save output in (temporary) files between the stages in the pipeline, which saves disk space and reduces reading and writing from disk, which is often the slowest bottleneck in getting something done.