5 Sem WCO-392 Linux Lab (Part 1)
5 Sem WCO-392 Linux Lab (Part 1)
2 of 73
Structure of Linux
Linux is a multiprogramming, mltiuser, time sharing and muti-
tasking operating system.
There are two major components of Linux, the kernel and the
shell.
The kernel is the core of the Linux operating system which
schedules processes and interfaces directly with the hardware.
It manages system and user I/O, processes, devices, files, and
memory.
The shell is an interface to the kernel.
Users input commands through the shell, and the kernel receives
the tasks from the shell and performs them.
The shell tends to do four jobs repeatedly:
- Displays a prompt
3 of 73
Structure of Linux (contd...)
- Reads a command
- Interprets the given command
- Then executes the command (Process the command and diaplay the
result - certainly with the help of the kernel)
- After this, it starts the process all over again.
4 of 73
Structure of Linux (contd...)
A shell this way is also called a command interpreter.
The shell also incorporates a powerful programming language.
This language referred to as "Shell Programming", enables the
user to exploit the full power and versatility of Linux.
Clearly, if we want to work with Linux, we need to access its
shell.
So, in a way we can say that accessing Linux means accessing
its shell.
5 of 73
Various Linux Installations (Environments)
Direct Access
- Linux is loaded on the server.
- Each user needs a time slot.
- Not possible with so many students.
- Remove Windows from each computer and install Linux: Not
possible..
Dual Boot Systems
- Each computer in the lab could be configured as a dual boot
Linux-windows system.
- No concept of centralized security and administration.
- May annoy non Linux users.
- Complex disk partitioning schemes.
- Cross partition security restrictions are hard to implement.
6 of 73
Various Linux Installations (Environments) (contd...)
Virtualization
- Create a virtual machine on each computer.
- This may be done using some virtualization S/W (e.g. Oracle
VM Virtualbox).
- Install Linux on the virtual machine.
- Each box this way can have both-Windows and Linux.
- Yet again, no concept of centralized security and administra-
tion.
- May annoy non Linux users.
- Virtual machine shares resources with real machine.
- This may slowdown both Linux and Windows.
Remote Login (Terminal Emulation)
- Linux is loaded on one machine (preferably server).
7 of 73
Various Linux Installations (Environments) (contd...)
- Each user emulates his/her computer into Linux mode using
some terminal emulator program (terminal emulator).
- A terminal emulator allows a computer to access another
computer, including remote ones, through either a command-
line interface or a graphical one.
- The communication is made possible using protocols such as
Telnet, Rlogin, Raw and SSH (Secure Shell).
- No need to install Linux on each computer.
- Centralized security and administration.
- Best example of centralized computing.
- Telnet, Raw, Rlogin etc., being insecure are not used with
modern setups.
- So, we shall use ssh protocol to access the Linux server.
8 of 73
Various Linux Installations (Environments) (contd...)
9 of 73
Accessing Linux from Windows using ssh
A ssh client is required.
To run GUI Linux applications a X Windows System Server (X-
Server) is also required.
A variety of ssh clients and X-Server are available:
- Putty: A pure ssh client with X11 forwarding.
- Xming: A pure X Windows System Server.
- VcXsrv: A pure X Windows System Server.
So, Putty used in combination with Xming or VcXsrv can serve
the purpose.
But, ideal solution must bring ssh client and X-Server as a single
package.
Some option in this category are:
10 of 73
Accessing Linux from Windows using ssh (contd...)
- Xmanager: X server for Windows with tabbed SSH client
(Best, but carries a cost)
- MobaXterm: Free X server for Windows with tabbed SSH
client.
- SmarTTY: Free X server for Windows with tabbed SSH client.
We shall use SmarTTY (preferred) or MobaXterm.
Next few slides demonstrate how some of them are used.
11 of 73
Accessing using Putty
Download Putty from https://www.putty.org/.
Install and Run it.
12 of 73
Accessing using Putty & Xming
No X application could be run using putty. So,
Download Xming from:
https://sourceforge.net/projects/xming/.
Install and Run it. You must notice a Xming icon on the taskbar.
Enable X11 forwarding in Putty.
(Run Putty → Under Category expand SSH → Select X11 and
on right hand side check Enable X11 forwarding button).
Enter Linux server’s IP address in putty and login in the same
way as we have previously exercised.
We can now run command line as well as X applications.
Note: VcXsrv can also be used instead of Xming for the same
results.
13 of 73
Accessing using Putty & Xming (contd...)
14 of 73
Accessing using MobaXterm
Download Home Edition of MobaXterm from:
https://mobaxterm.mobatek.net/download.html.
Install and Run it.
Create new ssh session (Click Session → Click ssh).
Enter IP address of server in Remote Host field and your user-
name in the username field.
Click OK. Enter your password when prompted and you must
get the Linux $ prompt.
Now you can execute any application X based or command line.
15 of 73
Accessing using MobaXterm (contd...)
16 of 73
Accessing using SmarTTY
Download SmarTTY from:
https://sysprogs.com/SmarTTY/download/.
Install and Run it.
Create new ssh session (Click New ssh connection).
Enter IP address of server in Host Name field and your username
ans password in intended fields.
Leave other settings as default and click Connect button.
SmarTTY prompts you to start a regular or smart terminal.
You can run X-based applications in both terminals but Smart
terminal offers you extended services over regular terminal.
Finally, you get the Linux $ prompt.
17 of 73
Accessing using SmarTTY (contd...)
18 of 73
Part 1
Basic Commands
19 of 73
Changing password
Irrespective of what method you are using to access the Linux,
you must have a valid username and password on the Linux
machine.
Using following steps you can change your current password:
1. To start, type passwd at the command prompt.
2. Enter your old password, the one you are currently using.
3. Type in your new password.
4. Always keep your password complex enough so that nobody
can guess it. But make sure, you remember it.
5. You must verify the password by typing it again.
Have a look at the following figure:
20 of 73
Changing password (contd...)
21 of 73
The cal & ncal Commands
On Unix-like operating systems, the cal and ncal commands
display a formatted calendar in the terminal.
ncal ("new cal") stands for new calendar.
It provides the same functions of cal, but it can display the
calendar vertically (with weeks in columns).
On systems with ncal installed, cal is typically a symbolic link
to ncal.
It behaves like the original cal if you we the name cal to run the
program.
22 of 73
The cal & ncal Commands (contd...)
Their general form is:
$cal [Options]
$ncal [Options]
23 of 73
The cal & ncal Commands (contd...)
Option Description
-h Don’t highlight today’s date. (works with ncal only)
-m month Specify a month to display. The month specifier can be a full
month name (e.g., Februiary), a month abbreviation of at least
three letters (e.g., Feb), or a number (e.g., 2). If you specify a
number, followed by the letter f or p, the month of the following
or previous year, respectively, will be displayed. For instance, -m
2f displays February of next year.
-y year Specify a year to display. For example, -y 1970 displays the entire
calendar of the year 1970.
-3 Display last month, this month, and next month.
-1 Display only this month. This is the default.
-A num Display num months occurring after any months already specified.
For example, -3 -A 3 displays last month, this month, and four
months after this one; and -y 1970 -A 2 displays every month in
1970, and the first two months of 1971.
-B num Display num months occurring before any months already speci-
fied. For example, -3 -B 2 displays the previous three months, this
month, and next month.
-d YYYY-MM Operate as if the current month is number MM of year YYYY.
24 of 73
The cal & ncal Commands (contd...)
- Execute Following Commands and analyze the result
carefully:
1. $ cal
2. $ ncal
3. $ ncal -h
4. $ cal -m february
5. $ cal -m sep
6. $ cal -m 5
7. $ cal -m 2f
8. $ cal -m 10p
9. $ cal -y 1970 (same as cal 1970)
10. $ cal -3
11. $ cal -1
12. $ cal -3 -A 3
13. $ cal -y 1970 -A 2
14. $ cal -3 -B 2
15. $ cal -d 2005-03
25 of 73
The echo Command
echo command on Unix-like operating systems prints text to
standard output, e.g., the terminal.
This is a built in command that is mostly used in shell scripts
to output status text to the screen or a file.
To confirm this execute following command:
$type echo
$echo [Option][String]
26 of 73
The echo Command (contd...)
Option Description
27 of 73
The echo Command (contd...)
echo Command: Execution & Examples
- echo without any option(s) prints the string passed to it on
standard output (terminal).
$echo Hello World!
OR
$echo "Hello World!"
28 of 73
The echo Command (contd...)
- echo is also used to print the values of shell/ environment vari-
ables as shown below:
$echo $PATH
$ x=5
$echo $x
29 of 73
The echo Command (contd...)
- To omit echoing trailing newline, use -n option with echo.
$echo -n "Linux for Fun"
- To print all files and folders in the present working folder use
following form of echo:
$echo *
30 of 73
The tput Command
tput command is used to query the terminfo terminal database
and check if that terminal supports a specific feature.
Long ago, when computers were centralized, interactive com-
puter users communicated with remote systems by using a phys-
ical terminal or a terminal emulator program running on some
other system (like we are using).
In their blooming days, there were many kinds of terminals.
They all used different sequences of control characters to man-
age their screens and keyboards.
When we start a terminal session on our Linux system, the
terminal emulator sets the TERM environment variable with
the name of a terminal type.
If we examine TERM, we can see this:
31 of 73
The tput Command (contd...)
$echo $TERM
In this example, we notice that our terminal type is named
"xterm" suggesting that our terminal behaves like the classic
X terminal emulator program xterm.
Other common terminal types are "linux" for the Linux console,
and "screen".
While we will encounter these 3 types most often, there are, in
fact, thousands of different terminal types.
Our Linux system contains a database called terminfo that de-
scribes them.
We can examine a typical terminfo entry using the infocmp
command followed by a terminal type name:
32 of 73
The tput Command (contd...)
$infocmp xterm
OR
$infocmp screen
33 of 73
The tput Command (contd...)
$tput cup 2 3
OR
$tput cup 5 30
2. Clear the Screen:
You can clear the screen using tput clear.
$tput clear
34 of 73
The tput Command (contd...)
4. Change the Terminal Foreground/Background Color:
To change the terminal’s foreground/ background color use tput setf/tput
setb.
$tput setb 4
$tput setf 7
If you notice reverse colors, first use the command tput rev, to reverse
the video. As emulator settings may override the defaults, it is better
to exercise these commands in a separate xterm.
Note: If you set foreground and background to the same color you
cannot see the cursor. So, to reset, execute "tput reset".
5. Turn On and Turn Off Highlighting:
tput allows us to turn on/off the text high lighting using tput bold/tput
sgr0. When we turn it on, new text in the terminal gets bold.
$tput bold
$tput sgr0
35 of 73
The tput Command (contd...)
6. Underline Text:
tput allows us to turn on/off the text underline mode using tput smul/tput
rmul.
$tput smul
$tput rmul
36 of 73
The date Command
date command on Unix-like operating systems, is used to print
or change the value of, the system’s time and date information.
Its general form is:
39 of 73
The date Command (contd...)
With -s or −−set option we can set the system date and time.
Have a look at following commands:
$date −−set="Tue Nov 13 15:23:34 PDT 2018"
OR
$date -s="Tue Nov 13 15:23:34 PDT 2018"
40 of 73
The date Command (contd...)
$cat »
dfile.txt
Sep 23 2018
Nov 03 2019
With -I option the date command displays the date in ISO 8601
format (YYYY-MM-DD).
$date -I
With -R option the date command displays the date and time
in RFC 2822 format.
41 of 73
The date Command (contd...)
Example is: Mon, 07 Aug 2006 12:34:56 -0600.
$date -R
Date Format
Format is a sequence of characters which specifies how output
will appear.
General format for specifying a format with the date command
is:
"+%[format-option]"
42 of 73
The date Command (contd...)
Sequence Description
%D Display date as mm/dd/yy.
%d Display the day of the month (01 to 31).
%a Displays the abbreviated name for weekdays (Sun to Sat).
%A Displays full weekdays (Sunday to Saturday).
%h Displays abbreviated month name (Jan to Dec).
%b Displays abbreviated month name (Jan to Dec).
%B Displays full month name(January to December).
%m Displays the month of year (01 to 12).
%y Displays last two digits of the year(00 to 99).
%Y Display four-digit year.
%T Display the time in 24 hour format as HH:MM:SS.
%H Display the hour.
%M Display the minute.
%S Display the seconds.
For a more detailed list of date command options and format sequences you may
refer to: https://www.computerhope.com/unix/udate.htm.
43 of 73
The date Command (contd...)
Execute the following commands and notice the output:
1. $date "+%D"
2. $date "+%D %T"
3. $date "+%Y-%m-%d"
4. $date "+%Y/%m/%d"
5. $date "+%A %B %d %T %y"
44 of 73
The time Command
time command on Unix-like operating systems, reports how long
it took for a command to complete execution.
It prints a summary of real-time, user CPU time and system
CPU time spent by executing a command when it terminates.
First thing to note is that every Linux command is a program.
real time is the time elapsed wall clock time taken by a command
to get executed.
user time is the amount of CPU time that the command spends
executing its own code.
For small Linux commands, which take milliseconds to execute,
this time is often shown as 0.0.
sys time is the amount of CPU time that the command spends
executing kernal oriented code.
45 of 73
The time Command (contd...)
Thus we conclude that the actual amount of CPU time spent
on executing a command is the sum of its "user" and "sys"
times.
General form of time command is:
47 of 73
Part 1
Knowing Yourself & Other Users
48 of 73
The who Command
who command is used to find out the following information:
1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.
The command is mostly used to get information about currently
logged in user on to the system.
Its general form is:
$who
$who -H
50 of 73
The who Command (contd...)
- To print the login names and total number of logged on users,
use the -q flag.
$who -q
$who -m
Note: If the arguments "am i" are specified, who assumes the
-m option.
51 of 73
The who Command (contd...)
- To add user’s message status as +, - or ?, use the -T or
--message switch option.
$who -T
OR
$who - -message
$who -bu
$who -r
53 of 73
The who Command (contd...)
$who -a
$who -d
$who -p
54 of 73
The who Command (contd...)
55 of 73
The logname Command
logname command is used to confirm your login name.
It displays the login name of the current user.
Its general form is:
$logname [Option]
56 of 73
The finger Command
finger command displays information about the users currently
logged in to a host.
The format of the output varies with the options for the infor-
mation presented.
Its general form is:
$finger
58 of 73
The finger Command (contd...)
- With -s option, finger displays the same information as it does
without any option.
$finger -s
59 of 73
The finger Command (contd...)
- One entry per user is displayed with the -l option; if a user is
logged on multiple times, terminal information is repeated once
per login.
- Mail status is shown as "No Mail." if there is no mail at all,
"Mail last read DDD MMM ## HH:MM YYYY (TZ)" if the
person has looked at their mailbox since new mail arriving
$finger -l
60 of 73
The finger Command (contd...)
$finger -lp
$finger -m jahangir
61 of 73
The finger Command (contd...)
$finger @amucc
$finger jahangir@amucc
63 of 73
The whoami Command
whoami command is used to confirm your login name. It is
similar to logname command.
It displays the login name of the current user.
Its general form is:
$whoami [Option]
64 of 73
The id Command
id command on Unix-like operating systems, is used to find user
and group names and numeric ID’s (UID or group ID) of the
current user or any other user of the system.
This command is particularly useful to determine the following
information:
- user ID
- username.
- group ID.
- group name.
- ID of other groups.
- names of other groups.
Its general form is:
$id [OPTION] ... [USER]
65 of 73
The id Command (contd...)
id Command: Important Options & Examples
- The id command reveals all the information listed above, about
the current user when run without any option.
- Execute id as follows:
$id
- To reveal same information for any other user say ’sam’, use id
as follows:
$id sam
66 of 73
The id Command (contd...)
- If you want to see the primary group id for a different user say
’sam’, specify the user’s name as follows:
$id -g sam
or
$id - -group sam
67 of 73
The id Command (contd...)
- Execute following commands:
$id -gn
or
$id - -group - -name
$id -gn sam
or
$id - -group - -name sam
- If you want to display the secondary group names and not the
id numbers for current user or for a specific user, use -Gn or
- -groups - -name option.
- Try following commands:
$id -Gn
or
$id - -groups - -name
$id -Gn sam
or
$id - -groups - -name sam
68 of 73
The id Command (contd...)
- With -u or - -user option the id command displays the user id
for the current user or for some specified user.
$id -u
or
$id - -user
$id -u sam
or
$id - -user sam
- You can display the username for the current user by typing the
following command:
$id -un
or
$id - -user - -name
69 of 73
The w Command
w command on Unix-like operating systems is a quick way to
see who is logged on and what they are doing.
This command shows the information about the users currently
on the machine and their processes (running programs).
When executed without any option the header shows, in the
order, the current time, how long the system has been running,
how many users are currently logged on, and the system load
averages for the past 1, 5, and 15 minutes.
Following entries are displayed for each user:
login name
terminal name
the user’s host
login time
idle time
70 of 73
The w Command (contd...)
JCPU, PCPU, and the command line of their current process.
The JCPU time is the time used by all processes attached to the
terminals. It does not include past background jobs but does
include currently running background jobs.
The PCPU time is the time used by the current process, named
in the "what" field.
General form of w command is:
$w [OPTIONS] [USER]
71 of 73
The w Command (contd...)
w Command: Important Options & Examples
- With -h option, w command doesn’t print the header.
- With -u option, w command, ignores the username while figur-
ing out the current process and cpu times.
(Note: To practice an example of this, switch to the root user
with "sudo su" and then run both "w" and "w -u".)
- Try following commands:
$w
$w -h
$sudo su
#w
#w -h
72 of 73
The w Command (contd...)
- With -f option, w command, doesn’t displays the FROM (host
name/ip address) field.
- With -help option, w command, displays the help.
- With -V option, w command, displays the version information.
- With user option, w command, displays the information about
specified user only.
- Try following commands:
$w -s
$w -f
$w -help
$w -V
$w sam
73 of 73