Linux Basic Guide and Assignment
Linux Basic Guide and Assignment
typical Linux operating system is made up of three parts: the kernel, the shell and
A
the applications.
ernel: The kernel of Linux is the hub of the operating system: it allocates time and memory to
K
programs and handles the file storage as well as response to system calls.
hell: The shell acts as an interface between the user and the kernel. When a user logs in (into
S
command line only environment), the login program checks the username and password, and then starts
another program called the shell. The shell is a command line interpreter (CLI). It interprets the
commands, which are typed by the user, and it arranges for them to be carried out. The commands are
themselves programs: when they terminate, the shell gives the user another prompt. A graphical user
interface for the shell is called Terminal.
esktop Environment: Linux operating systems also have a graphical user interface (GUI) through
D
which users can access applications and system resources. Unlike other operating systems Linux OS
has many Desktop Environments available. Users are free to install the GUI of their choice. Some
popular ones are GNOME, KDE, XFCE, Ubuntu Unity, etc. When one installs a Linux system, it
comes with a default GUI. One can install other GUIs at any time, but only one GUI environment can
be used at a time. For example, Kali Linux comes with GNOME.
Linux Directory Structure: In Linux, files and directories (folders) are laid out on disk in hierarchical
fashion in a tree structure. The top directory is called root folder, it is denoted by “/” (slash).
A collection of all files and directories in a Linux machine is referred to as a “filesystem”.
A path (location) of a file or directory in the filesystem is written starting from the root directory (/)
followed by all intermediate directory names, which are separated by a slash with the actual file name
(or directory name) in the end of the path. Example: the file path of the file “report.doc” in the above
figure is written as “/home/its/ug1/ee51vn/report.doc”.
Linux Basics and Capabilities Lab
Notes:
1. In the path /home/its/ug1/ee51vn/report.doc, do not get confused between first slash / which
denotes the root directory and subsequent slashes, which are used to separate directories at each
level of the path.
2. There are no spaces in a file path. Example: The file path /home/its/ug1/ee51vn/report.doc is the
one single string with no spaces between slashes and subsequent folder names.
Q1: Write the path for directory staff in the above figure.
Different default directories under the / directory are used for some specific purposes.
/bin : All executable binary files (including commands) are present in this directory.
/dev : Contains device files for hardware devices (usb, cdrom etc.)
/etc : Contains application’s configuration files, startup, shutdown, start, stop script for every
individual program.
/home : Home directory of the users. Every time a new user is created, a directory with the
corresponding username is created within /home which contains other directories such as Desktop,
Downloads, Documents, etc.
/lib : Contains the kernel modules and shared library images required to boot the system and run
commands in the root file system.
/lost+found : This directory is created during installation of Linux, useful for recovering files which
may be broken due to unexpected shutdown.
/media : Temporary mount directory is created for removable devices viz., media/cdrom.
/mnt : Temporary mount directory for mounting file system.
/opt : Contains the third-party application software viz. Java, etc.
/proc : A virtual and pseudo filesystem which contains information about running processes with
a particular process id abbreviated as “pid”.
Linux Basics and Capabilities Lab
Setup VirtualBox
If you already have setup virtualbox or any other linux environment , no need to follow this . But it
should be an Ubuntu/Debian Based environment.
An OVA file is an Open Virtualization Appliance that contains a compressed, “installable” version of a
virtual machine. When you open an OVA file, it extracts the VM and imports it into whatever
virtualization software you have installed on your computer. The instructions below explain how to
install an OVA file in Oracle VirtualBox. Briefly, these steps are:
1. If it is not already on your computer, download and install VirtualBox –
https:///www.virtualbox.org/wiki/Downloads. You can find instructions on how to do this at the
Installing VirtualBox wiki page.
a. For Mac users, select “OS X hosts”. Run the file and follow the on-screen directions.
b. You can launch VirtualBox from the Applications and “Keep in dock” if desired.
Linux Basics and Capabilities Lab
d. Add further user details. These details are optional-you can just hit enter to skip for Full
Name, Number, Phone etc.) –See the below screenshot.
e. Type “y” at “Is this information correct?”
Linux Basics and Capabilities Lab
Q2: Login to your new user account, open a terminal and type “sudo su”, enter sudo password
for your account, and submit a screenshot. The screenshot should clearly show the prompt. It will
look like “root@usudont-sec:/home/euid”.
7. To change to root user from normal user (type “exit” if currently in root mode for user):
a. Type “sudo su –”.
b. If prompted, type the root user password at the prompt. After successful login, you
should observe a change in shell prompt from your user to root user.
c. Type “exit” to exit from the root user. Now you should be back to you user prompt.
8. Type “tail /etc/passwd” (note that “/etc/passwd” is one string—no spaces in between).
A root user (also referred to as a superuser) is an administrator of the system who has full privileges by
default, while normal users have limited privileges. Normal users need to use the “sudo” command in
order to temporarily gain certain high-privilege commands. Example of these commands are: software
installation, changing to the root user in a terminal, changing the system settings, and others.
Linux Basics and Capabilities Lab
Q5: What difference did you observe between the results of “ls –l” and “ls –la”?
What does the option “-a” mean?
Hint: Refer to the man page: man ls. Also, see the section on man help below.
Q6: Why do we have to use “-r” option to copy directories? (Hint: Use man cp.)
You are searching for the file with name file1 in the directory /root. This command will search for the
file in /root and in all its subdirectories. You gave /root as search location as you think file1 must be
somewhere in it. If you have no information where the file might be in your system, you should start
searching for it from the most top directory in the filesystem, that is “/”. In this later case, the command
should be find / -name file1.
18. The absolute path of a file or directory is its path written from the root of the file system. For
example, consider the directory dir44 you created earlier in Step 5. Its absolute path is
/home/euid/dir11/dir22/dir44.
19. The relative path of a file or directory is its path from the current directory. Now, you are in
your home directory which is /home/euid. The relative path of dir44 from this directory is
dir11/dir22/dir44. One writes the relative path starting from a directory one level below, i.e.,
dir11 in our case.
Linux Basics and Capabilities Lab
20. One can refer to a file or directory either using relative or absolute path (both are possible).
● Type “touch /home/euid/dir11/dir22/dir44/file44”.
(Replace username you actual username).
● Type “dir11dir22/dir44/file55”
Either way you are able can access dir44
Note: When using the relative path, one should be aware of the current directory. In the above example,
when the current directory changes to home, then the relative path to dir44 changes to
euid/dir11/dir22/dir44.
21. To compress and archive files: In Linux file compression and archiving (grouping file together)
and compression are two different activities. First, we archive multiple files with “tar”
command. A file with “.tar” extension is created. Then we zip (compress the file) with gzip
command. (gzip-stands for gunzip is compression tool in linux. Bunzip (bzip2) and zip are other
commonly used compression tools)
d. You can also do it in single step which is usually the preferred way:
type “tar –zcvf compressed2.tar.gz a b c d”.
The “man” command is used to get help for any command in Linux. man stands for manual. To pull
up a man page of a command, type man command. Ex: man ls. This command displays list of options
available with the command. Man pages explain different options and syntax of a command and are
usually difficult to understand for beginners. If you want to know usage and example for a command,
google is the best place to look.
In Linux a file has read, write and execute permissions. And permissions are assigned to users of three
categories: owner, group and others. The user who creates a file will be the owner, group is group of
users who has access. Others are all the users other than owner and users in the group.
Each file (and directory) has associated access rights, which may be found by typing ls -l. Also, ls
-lg gives additional information as to which group owns the file (beng95 in the following example):
-rwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1
In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, -, and,
occasionally, s or S. If d is present, it will be at the left hand end of the string, and indicates a
directory: otherwise - will be the starting symbol of the string.
Linux Basics and Capabilities Lab
The 9 remaining symbols indicate the permissions, or access rights, and are taken as 3 groups of 3.
● The left group of 3 gives the file permissions for the user that owns the file (or directory)
(ee51ab in the above example);
● the middle group gives the permissions for the group of people to whom the file (or
directory) belongs (eebeng95 in the above example); For every user in Linux, a group with
the same name is also created. This group will be default group on newly created files.
● the rightmost group gives the permissions for all others.
The symbols r, w, etc., have slightly different meanings depending on whether they refer to a simple
file or to a directory.
● r (or -), indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file
● w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to
change a file
● x (or -), indicates execution permission (or otherwise), that is, the permission to execute a
file, where appropriate
Some Examples:
-rwxrwxrwx a file that everyone can read, write and execute (and delete).
a file that only the owner can read and write - no-one else can read or write
-rw-------
and no-one has execution rights (e.g., your mailbox file).
u user
g group
o other
a all
r read
w write (and delete)
x execute (and access directory)
+ add permission
- take away permission
c. chown- this command is used change ownership of a file or directory. This can also be used
to change the group like chgrp.
i. Type “touch own_file”.
ii. Type “ls –l own_file”.
iii. Type “sudo chown euidnew own_file”. This change owner of the file to euidnew
from you.
iv. Type “mkdir own_dir”.
v. Type “ls –ld own_dir”. The –ld option displays properties of directory. If you only
use –l it shows properties of contents of own_dir.
vi. Type “sudo chown euidnew:euidnew own_dir”. This changes both the owner and
the group to euidnew on folder own_dir.
vii. Type “ls –ld own_dir”.
Note: The command chown only changes the owner of a directory but not its contents.
To change ownership of files and directories within a directory own_dir, along with is
Linux Basics and Capabilities Lab
2. Installing software from GUI: One can also install software using “Add or Remove” utility.
Click Applications → System Tools → Add or Remove Software.
This will download the pdf file. Note that the wget utility works any file type.
Nano:
a. To open a file in nano: Type “nano newfile”. This will open a blank file called newfile.
If there is already a file by name newfile nano will open that. Only if there exists no file by the
name supplied it opens a new blank file with the supplied name.
b. To edit and save: To write data to file, move cursor with arrow keys to location where you
want to add content and start writing. Delete content using backspace.
Press Ctrl+O, it ask for confirmation: File name to write: newfile. Hit Enter.
c. To exit nano: Press Ctrl+X.
d. To search for a string or keyword: Press Ctrl+W. A search: prompt appears at the bottom,
type keyword you want to search and press enter.
Note: You have all the above explained controls at the bottom of the nano editor with brief
description.
Linux Basics and Capabilities Lab
Vim:
VIM Editor has 2 different modes: Command Mode and Insert Mode.
a. Command mode: Type: “vim blankfile”. This opens a new file-blank file if a file doesn’t exist
by the name. A file is opened into command mode by default. In this mode you can issue
commands to edit text.
b. Insert mode:
Hit letter “i”, which moves to insert mode from command mode. You can edit, add content to
the file in this mode. Use arrow keys to navigate, backspace to delete.
To save changes to the file:
i. Press Escape key-it takes file back to command mode.
ii. Press "Shift + :” A : prompt is created at bottom of file.
iii. Type letter “w”-it saves your file
Note: If you don’t want save the file at this point but want to go back, hit escape-to go
to command mode and then “i” to go to insert mode. You always have to be in
command mode before you go to insert mode.
iv. Again Press "Shift + :”- and type “q” to exit vi editor. If file has unsaved modifications,
vim asks for confirmation. Typing q! Forcefully quits without saving.
c. More about command mode: This mode takes commands from key board, to do operation like
copy, paste, cut delete, etc. You can navigate using arrow keys.
i. dd - delete current line (in which cursor is present). ndd –deletes n number of lines.
Example: 5dd.
Linux Basics and Capabilities Lab
ii. yy - copy current line (in which cursor is present). nyy- copies n number of lines.
iii. p - paste copied line(s) below current line.
There exist many other text editors such as emacs, pico, and others, however we limit our
demonstration to nano and vim. The main reasons are that they are simple, and that they are likely to
be present (built-in) in various current and future Linux/Unix distributions. Typically, these operating
systems have GUI-based text editors available as well.
q
quota Display disk usage and limits
quotacheck Scan a file system for disk usage
quotactl Set disk quotas
r
ram ram disk device
rcp Copy files between two machines
read Read a line from standard input •
readarray Read from stdin into an array variable •
readonly Mark variables/functions as readonly
reboot Reboot the system
rename Rename files
renice Alter priority of running processes
remsync Synchronize remote files via email
return Exit a shell function
rev Reverse lines of a file
rm Remove files
rmdir Remove folder(s)
rsync Remote file copy (Synchronize file trees)
s
screen Multiplex terminal, run remote shells via ssh
scp Secure copy (remote file copy)
sdiff Merge two files interactively
sed Stream Editor
select Accept keyboard input
seq Print numeric sequences
set Manipulate shell variables and functions
sftp Secure File Transfer Program
shift Shift positional parameters
shopt Shell Options
shutdown Shutdown or restart linux
sleep Delay for a specified time
slocate Find files
sort Sort text files
source Run commands from a file '.'
split Split a file into fixed-size pieces
ssh Secure Shell client (remote login program)
strace Trace system calls and signals
su Substitute user identity
sudo Execute a command as another user
sum Print a checksum for a file
suspend Suspend execution of this shell •
sync Synchronize data on disk with memory
t
Linux Basics and Capabilities Lab
whereis Search the user's $path, man pages and source files for a program
which Search the user's $path for a program file
while Execute commands
who Print all usernames currently logged in
whoami Print the current user id and name (`id -un')
wget Retrieve web pages or files via HTTP, HTTPS or FTP
write Send a message to another user
x
xargs Execute utility, passing constructed argument list(s)
xdg-open Open a file or URL in the user's preferred application.
yes Print a string until interrupted
zip Package and compress (archive) files.
. Run a command script in the current shell
!! Run the last command again
# Comment / Remark
// Comment / Remark