You are on page 1of 23

Linux Platform – I Chapter: 2

Chapter 2:
Basic Commands and Getting Help

- 1 -
Linux Platform – I Chapter: 2

Title Page
1. Switching between Consoles and the Graphical Environment 5

2. Creating New Users 7


2.1. useradd command 7

2.2. passwd command 8

3. Some Basic Commands 9

4. Getting Help 18

- 2 -
Linux Platform – I Chapter: 2

Learning Objectives
Upon completion this chapter, the student should be able to :
1. Switch between virtual consoles and the graphical environment ;
2. Create new user accounts and change their passwords ;
3. Execute basic commands such as: pwd, date, cal;... ,
4. Get help when needed.

Keywords
GUI, CLI, virtual console, terminal, useradd, passwd, ls, pwd, file, whoami, who am
I, w, who, id, su, sudo, cat, tac, more, less, date, cal, tree, help, man, whatis,
makewhatis, apropos.

- 3 -
Linux Platform – I Chapter: 2

Introduction
After installing CentOS correctly, you must reboot it and login to the system. You
can login with either a Graphical User Interface (GUI) or a Command Line Interface
(CLI) which is a text-based prompt. Although GUI will have installed by default
and easy to control by novice users, you should learn how to use CLI to be a good
Linux user. Note that the GUI and its tools are out of the scope of our study.

- 4 -
Linux Platform – I Chapter: 2

1. Switching between Consoles and the Graphical Environment


An operating system needs to provide a way for users to run programs and access
the file system. Linux has both graphical and text-based user interfaces. GNOME and
KDE provide graphical user interfaces, whereas shell command interpreters (such as
bash) run programs by typing commands and options.
In CentOS and most of RedHat based distros, bash is the shell command interpreter
used by default. A shell is the program that a terminal sends user input to. The shell
generates output and passes it back to a terminal for display. In addition to bash, there
are many other shells such as fish, zsh, ksh, sh, tsch, pwsh. We use the word terminal
to refer to the virtual terminal because physical terminals are no longer used. Virtual
terminal is an application that provides a text based environment to access the shell .
A virtual console is an application that simulates a physical terminal device in software
and connects it with Linux system on serial port through software configuration virtually.
It uses a single terminal to provide workspace and command prompt. It offers only CLI
interface and uses a single terminal to access that CLI interface .
Linux GUIs have become as easy to use as Windows operating system. The basic
framework for a GUI environment such as KDE (K Desktop Environment) and GNOME
(GNU Object Model Environment) is provided by the X window system that allows
users to interact with the display device using keyboard and mouse .
How you first get to a shell depends on whether your computer is configured to have
a GUI or not. While the X window system starts with a GUI, server systems often
are run entirely from a CLI .
Centos and other RedHat based distros provide six virtual consoles to users physically
present at the computer. Virtual consoles enable the user to have multiple logins
without using the X window system. They provide full screen, non-graphical access
to the system and allow the user to log into each virtual console separately. Every
virtual console keep the executed commands available on the screen and the user
can scroll back by typing Shift-PgUp and Shift-PgDn .
Virtual consoles are accessed using the CTRL+ALT+F1 through CTRL+ALT+F6 key
sequences, with one virtual console mapped to each of the first 6 function keys. When
starting a new session on a Linux using a virtual console, the screen will look like the
following:

- 5 -
Linux Platform – I Chapter: 2

CentOS Linux 8 (Core)


Kernel 4.18.0-147.el8. x86_64 on an x86_64
Activate the web console with: systemctl enable --now cockpit.socket
RaddadHost login:

If you use the graphical environment, you should know that CTRL+ALT+F1 will return
you to it, and the screen will look like the following:

When logging into Linux using a GUI, you will have to start a shell manually. Some
GUIs provide a menu option to start a program called a terminal, xterm, Konsole, or
something similar.

- 6 -
Linux Platform – I Chapter: 2

2. Creating New Users


2.1. useradd command
When installing Linux, it was optional for you to create a regular user. If you did not
create it, it is time to do that. Don’t forget, you should be superuser or privileged user
in order to create other users.
The most straightforward method for creating a new user from the shell is with the
useradd command. After opening a Terminal window with root permission, simply
invoke the useradd command at the command prompt, with details of the new account
as parameters. The only required parameter to useradd is the login name of the user,
but you will probably want to include some additional information. Each item of account
information is preceded by a single-letter option code with a dash in front of it.
Example:
The command line below shows how to add a user named newUser with default
options (more details about creating users are explained later in another chapter).

[root@StudentHost ~]# useradd newUser

To verify that the user is created, just type the following command line:

[root@StudentHost ~]# tail -1 /etc/passwd


newUser:x:1002:1002: :/home/newUser:/bin/bash

The newUser is created and its UID (User Identifier) is 1002. Now, you should enter
a password for this new user:

[root@StudentHost ~]# passwd newUser

[root@RaddadHost ~] # passwd newUser


Changing password for user newUser.
new password:
Retype new password:
passwd: all authentication tokens updated successfully.

- 7 -
Linux Platform – I Chapter: 2

2.2. passwd command


After first login, you should change your password given by the root user; you can do
such that by typing the passwd command and press Enter key. When changing your
user password, make sure that you remember it, because your password is never
stored on the system in human readable plaintext, so even your system administrator
can't know your password. Keep in mind that if you forget your password, someone
with root privileges can reset your password, and then tell you your new password.
When choosing a new password, users can get “bad password” messages. The
passwd command forces users to avoid too simple passwords. The good password
must always contain more than seven characters including a number, a capital letter,
and a special character.
You can find here some common rules for creating a good password.
You must supply your current user password before it can be changed. Remember
that the characters you enter when typing a password are never echoed back to the
screen. You will be expected to enter your new password twice to ensure that you did
not make a typing mistake.
• Choose a minimum of eight characters; more characters are better, as long as
you are comfortable remembering them and typing them;
• Don’t use your real name, login name, or variations thereof. For example, with
a login name of magy, a poor password would be m@gy;
• Don’t use formulas, such as 1+3=4;
• Don’t use a word from a dictionary;
• Don’t use easy to determine personal information, such as your birthday,
anniversary…etc.
• Choose different types of characters; use at least three of the following:
▪ lower case letters;
▪ upper case letters (Linux passwords are case sensitive);
▪ numbers;
▪ punctuation and other special characters.
• Don’t excessive complexity if it tempts you to perform such unsafe practices as
writing the password onto a notepad near your monitor.

- 8 -
Linux Platform – I Chapter: 2

3. Some Basic Commands


In this section, we present some basic commands that every Linux user, even novice
one, must be able to use.

ls command
The ls command lists the names of all (or selected) files in a directory, and tells you
about the attributes of those files. The syntax of this command is:

$ ls [option] [files or directories]

Some common options for the ls command are shown in the following table:

Option Description

-a, --all Include files that start with ‘.’ (hidden files)

List information about the directory (or file) itself, not the directory's
-d, --directory
contents.

List the files in a directory, with a different flag for each type of file:
-F
directory (/), executable (*), soft link (@)

-h Use "human readable" abbreviations when reporting file lengths.

-i, --inode List index number of each file's inode

-l Use long listing format

-n, --numeric-
Use numeric UIDs and GIDs, rather than usernames and groupnames
uid-gid

-r, --reverse Reverse sorting order

-R, --recursive List subdirectories recursively.

- 9 -
Linux Platform – I Chapter: 2

-s Print the allocated size of each file, in blocks

-S Sort by file size, largest first

Report (or sort by) time specified by WORD instead of mtime. WORD
--time=WORD
may be one of "atime", "access", "ctime", or "status".

-t Sort by modification time.

Here are some examples:


[student@StudentHost ~]$ ls
File1.txt file1.txt services stuff File3.txt classes

[student@StudentHost ~]$ ls –a
File1.txt .bash_profile file1.txt .bash_logout
. ..bash_history .bashrc services stuff .ssh File3.txt classes

[student@StudentHost ~]$ ls –l
Total 98
-rw-r--r-- 1 student student 19558 Sep 17 00:04 services
drwxr-xr-x 2 student student 4096 Sep 19 15:17 stuff
Output is truncated…

[student@StudentHost ~]$ ls –lh


Total 98
-rw-r--r-- 1 student student 20.0k Sep 17 00:04 services
drwxr-xr-x 2 student student 4.0k Sep 19 15:17 stuff
Output is truncated…

It is very important to understand every detail of the output given by the ls –l command,
for that reason, we describe the output contents in the following table:

- 10 -
Linux Platform – I Chapter: 2

Detail Description
The total size of your directory in blocks (1024 bytes) on the hard
disk, which is 98 in our example. Each file and directory is listed with
Total 98
a lot of information. (keep in mind that this does not include
subdirectories)

The file type and permissions of the file (in this example, the first
drwxr-xr-x
character ‘d’ means the file is a directory)

The file's link count, or the total number of entries (filenames) that
2 refer to this file. (keep in mind that it is always greater than 1 for
directories)

Student (first
The file's owner
word)

Student (second
The file's group owner
word)

4.0k The length of the file (here, in kilo bytes)

Sep 19 15:17 The file's mtime, or last time the file was modified

stuff The file's name

pwd command
This command allows you to know “where are you in the directory tree”; pwd stands
for “Print Working Directory”:

- 11 -
Linux Platform – I Chapter: 2

[student@StudentHost ~]$ pwd


/home/student

file command
This command determines the file type, and displays results to terminal window. Its
syntax is:

$ file [OPTIONS] [FILE...]

The contents of any given file might be ASCII (plain text, HTML, shell script, program
source code, etc.) or binary (compiled executable, compressed archive, audio, etc.).
Here are some examples:

[student@StudentHost test]$ file file.txt


file.txt: empty
[student@StudentHost test]$ file one.txt
one.txt: ASCII text
[student@StudentHost test]$ file Dir1
Dir1: directory

and here is another example, showing information about several other file types :

[student@StudentHost ~]$ file /dev*/


/dev/block: directory
/dev/console: character special)1/5(
/dev/cdrom: symbolic link to sr0
/dev/sda block special)0/8(
/dev/sda01 block special)1/8(
/dev/sda02 block special)2/8(
Output is truncated…

whoami command
This command tells you your username.

[student@StudentHost ~]$ whoami


Student

- 12 -
Linux Platform – I Chapter: 2

who command
This command gives you information about who is logged on the system.

[student@StudentHost ~]$ who


student tty1 2020-07-19 23:26
root tty2 2020-07-19 23:26

who am i command
This command displays only the line pointing to your current session.

[student@StudentHost ~]$ who am i


student tty1 2020-07-19 23:27

w command
This command shows you who is logged on and what they are doing.
[student@StudentHost ~]$ w
01:54:47 up 1 day, 9:09,2 users, load average: 0.03, 0.01. 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
student tty1 - sun23 6.00s 5.35s 0.00s w
root tt - sun23 8:32m 0.40s 0.40s -bash

id command
This command gives you your user id, primary group id, and a list of the groups that
you belong to (on RHEL/CentOS you will also get SELinux context information with
this command). To find out information about your identity, use the id command as
follows:

[student@StudentHost ~]$ id
uid=1000(student)gid=1000(student) groups=1000 (student)

Su command
This command allows a user to run a shell as another user. You can also use su
command to become root, when you know the root password.
[student@StudentHost ~]$ su C1IOS
Password:
[C1IOS@RaddadHost raddad]$

- 13 -
Linux Platform – I Chapter: 2

su - $username command
The su command maintains, by default, the same shell environment. To become
another user and get the target user's environment, issue the su - command
followed by the target username.

[student@StudentHost ~]$ su - C1IOS


Password:
[C1IOS@RaddadHost ~]$

Note: When no username is provided to su or su -, the command will assume root


is the target.

sudo command
This command allows a user to start a program with the credentials of another user.
Before this works, the system administrator has to set up the /etc/sudoers file.
This can be useful to delegate administrative tasks to another user without giving the
root password.
The example below shows the usage of sudo. User student received the right to
run useradd with the credentials of root. This allows student to create new users on
the system without becoming root and without knowing the root password.

[student@StudentHost ~]$ sudo useradd raddad

cat command
This command concatenates files to standard output. Its syntax is as follows:

$ cat [OPTION] [FILE(S)]

It can be used to show the contents of one or more files on the terminal window.
The most common options for this command is –n which numbers all output lines
and –b which numbers nonempty output lines (overrides –n). The following
command lines illustrate how to use cat command:

- 14 -
Linux Platform – I Chapter: 2

[student@StudentHost test]$ cat one.txt two.txt


Hello
World!
[student@StudentHost test]$
[student@StudentHost test]$ cat one.txt
Hello
[student@StudentHost test]$ cat two.txt
World!
[student@StudentHost test]$ cat one.txt two.txt
Hello
World!
[student@StudentHost test]$ cat -n one.txt two.txt
1 Hello
2 World!

tac command
This command concatenates and prints files in reverse. The following example
shows you the purpose of tac (cat backwards).

[student@StudentHost test]$ cat one.txt


one
two
three
four
[student@StudentHost test]$ tac one.txt
four
three
two
one

more and less commands


The more command is useful for displaying files that take up more than one screen.
It will allow you to see the contents of the file page by page. Use the space bar to
see the next page, or q to quit. Interactive commands for more are based on vi text
editor. Some people prefer the less command to more. Users should realize that
less provides more emulation plus extensive enhancements. Less allows fast
backward movement in the file as well as forward movement. Also, Less does not
have to read the entire input file before starting, so with large input files it starts up
faster than text editors like vi .

- 15 -
Linux Platform – I Chapter: 2

date command
This command displays the date, time, time zone and more.

[student@StudentHost ~]$ date


Fri Jul 24 11:45:10 EEST 2020

A date string can be customized to display the format of your choice (use the next
section to check the man page for more options).

[student@StudentHost ~]$ date +‘%m-%d-%Y at %I:%M’


07-24-2020 at 11-49

where m: month, d: day, Y: year, I: hour (01-12), M: minute.

cal command
If this command used alone (without options or arguments), it will display the current
month, with the current day highlighted. However, you can select any month in the
past or the future to be shown and any day to be highlighted. In the next command,
we choose May 24, 1977:

[student@StudentHost test]$ cal 24 5 1977


May 1977
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

tree command
With no arguments, the command tree lists the files in the current directory. When
directory arguments are given, it lists all the files or directories found in the given
directories each in turn. Upon completion of listing all files and directories found, tree
command returns the total number of files and directories listed. There are options to
change the characters used in the output, and to use color output. You can type
man tree; and press Enter key to show all available options for this command.

- 16 -
Linux Platform – I Chapter: 2

[student@StudentHost test]$ tree


Dir1
ftest
f123
file.txt
ln -> ftest
one.txt
onr.txt
tow.txt
1 directory, 7 files

- 17 -
Linux Platform – I Chapter: 2

4. Getting Help
People new to the CLI think that every command and each little argument must be
committed to memory. Linux provides so many commands and so many possible
options for each command; you can't expect to remember all of them. There are
megabytes of documentation that can answer all of your questions, if you just know
how to access it quickly.

help command
The help command displays information about shell built-in commands. For example,
the command line shows not only options, but also time formats you can use with the
date command:

[student@StudentHost ~]$ date --help | less

man command or man pages


The name man is short for ‘‘manual’’. When using Linux, the standard way to read
man pages, as they are known, is using man command followed by a component
name, from any shell. For example, to get help on the ls command, you would type:

[student@StudentHost ~]$ man ls

This command displays a help screen describing the ls command and its usage. To
learn about how the man page system itself works, type man man.
The man pages provide succinct summaries of what a command or file does. They
are formatted from special source files, and are displayed using the less pager to
display information. They are grouped into "sections". The common sections of the
Linux manual are listed in the following table:

- 18 -
Linux Platform – I Chapter: 2

1 Executable programs or shell commands


2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special device 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)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]

This table is very useful in order to find quickly the exact man page you are looking
for. Sometimes a single keyword has entries in multiple sections. For example,
“passwd” has entries under both section 1 and section 5. In most cases, man returns
the entry in the lowest-numbered section; but you can force the issue by preceding
the keyword by the section number. The user command passwd is used to change
your login password. If you type:

[student@StudentHost ~]$ man passwd

you will get the man page for this command, since it is found in section 1. However,
there is also a file called passwd that stores the account information on your system.
The format for this file is explained in a man page in section 5. To get to it, you need
to type:

[student@StudentHost ~]$ man 5 passwd

then, the man command will look in section 5 rather than starting at section 1 .
To navigate man pages, you can use arrows, PgUp, PgDn. To look for a word, you
should use forward slash (/) followed by the term you are looking for and press Enter.
To look for “word” for example, you should type /word and to go to the next/previous
match, you should use n/N, respectively. To quit the man pages, you should type q
character.

- 19 -
Linux Platform – I Chapter: 2

As any other command, you can use options with man command; the following table
contains its most common options:
Option Description

-f Give the section in which the given command is present

-a Help us to display all the available intro manual pages in succession

Search the given command as a regular expression in all the manuals and it
-k
returns the manual pages with the section number in which it is found

-w Return the location in which the manual page of a given command is present

-I Consider the command as case sensitive

No
Display the whole man pages of the command
option

Man pages are often very detailed and might tell you far more than you wanted to
know. If you just want a quick idea of what a command does, use the whatis
command.

Note: Should you be convinced that a man page exists, but you can't access it, then
try running makewhatis on CentOS/Redhat. For each page, the makewhatis program
writes a line in the whatis database.

whatis command
The whathis command provides very brief descriptions of the component name that
follows it. It searches the short descriptions in the whatis database for each keyword
provided to it as an argument (i.e., input data). This database contains just the title,
section number and description from the NAME section of each page in the man
manual that is built into most Linux systems. The whatis database is a plain text (i.e.,
human-readable characters) file that is generated automatically by the makewhatis
program .

- 20 -
Linux Platform – I Chapter: 2

The output of whatis is limited by the fact that it provides only a single line for each
keyword found in the database; thus it supplies incomplete information about even
moderately complex commands. Here are two examples of this command:

[student@StudentHost ~]$ whatis zcat


zcat (1p) – expand and concatenate data
zcat (1) – compress or expand files
[student@StudentHost ~]$
[student@StudentHost ~]$ whatis ls
ls (1p) – list directory contents
ls (1) – list directory contents
[student@StudentHost ~]$
[student@StudentHost ~]$

When you use the man command with its -f option, it produces the same output as
whatis, for example:

[student@StudentHost ~]$ man -f head

is equivalent to:

[student@StudentHost ~]$ whatis head

whatis is similar to the apropos command. However, apropos is more powerful in


that its arguments are not limited to complete words but can also be strings (i.e., any
finite sequences of characters) which comprise parts of words. Both commands are
unusual in that they have no options.

apropos command
This command produces the same output as the man command with its -k option. For
example, if you want to know about renaming files, try this command line:

[student@StudentHost ~]$ apropos rename


buildah-rename (1) – Rename a Local container.
lvrename (8) - Rename a logical volume
mmove (1) – move or rename an MSDOS file or subdirectory
mren (1) - Rename an existing MSDOS file
mv (1) – move (rename) files
mvxattr (1) – Recursively rename extended attributes
rename (1) – rename files
rename (2) – change the name or location of afile

- 21 -
Linux Platform – I Chapter: 2

rename (3p) - rename file relative to directory file descriptor


renameat (2) - change the name or location of a file
renameat2 (2) -change the name or location of a file
vfs_crossrename (8) –server side rename files across filesystem
boundaries
vgrename (8) – Rename a volume group
zipnote (1) – write the comments in zipfile to stdout, edit
comments and rename files in..
[student@StudentHost ~]$

By reading through the result, you will find multiple commands that could be used to
rename your files, you can choose the command you want.

- 22 -
Linux Platform – I Chapter: 2

Questions
1. How to display only your currently logged-on user name.
2. List all logged-on users.
3. List the contents of the root directory.
4. List a long listing of the root directory.
5. List of all logged-on users including the command they are running.
6. Display your user name and your unique user identification (userid).
7. Stay at your home directory, and list the contents of /etc.
8. Stay where you are, and list the contents of /bin and /sbin.
9. Stay where you are, and list the contents of ~.
10. List all the files (including hidden files) in your home directory.
11. List the files in /boot in a human readable format.
12. Use less command to display /etc/services.
13. Use ls command to find the biggest file in /etc.
14. Use cat command to display the contents of .bashrc followed by the contents
of /etc/passwd.
15. Use more command to display /var/log/secure*.
16. Use su to switch to another user account (unless you are root, you will need
the password of the other account), and get back to the previous account.
17. Now use su - to switch to another user and notice the difference.
18. Note that su - gets you into the home directory of Yara.
19. When using your regular user account, try to create a new user account. What
has been happened?
20. Now try the same, but with sudo before your command.

References
1. Red Hat Linux Essentials RH033-RHEL5-en-2-20070306
2. Paul Cobbaut, “Linux Fundamentals”, https://linux-
training.be/funhtml/index.html. Updated on 2015-05-24

- 23 -

You might also like