Professional Documents
Culture Documents
HAMIRPUR
PRACTICAL FILE
Of
LINUX ADMINISTRATION
CSL 303
02 24/08/18
Study the basic Linux commands
03 File System and working with 07/09/18
files in Linux Operating System.
Using a DVD?
It’s easy to install Ubuntu from a DVD. Here’s what you need to do:
Restart your computer. You should see a welcome screen prompting you to choose your
language and giving you the option to install Ubuntu or try it from the DVD.
If you don’t get this menu, read the booting from the DVD guide for more information.
Use the checkboxes to choose whether you’d like to Install Ubuntu alongside another operating
system, delete your existing operating system and replace it with Ubuntu, or — if you’re an
advanced user — choose the ’Something else’ option.
Depending on your previous selections, you can now verify that you have chosen the way in
which you would like to install Ubuntu. The installation process will begin when you click the
Install Now button.
Ubuntu needs about 4.5 GB to install, so add a few extra GB to allow for your files.
If you are connected to the internet, this should be done automatically. Check your location is
correct and click ’Forward’ to proceed. If you’re unsure of your time zone, type the name of the
town you’re in or click on the map and we’ll help you find it.
6.Select your preferred keyboard layout
Click on the language option you need. If you’re not sure, click the ’Detect Keyboard Layout’
button for help.
Your name:
Choose a password:
9.That’s it.
All that’s left is to restart your computer and start enjoying Ubuntu!
PROGRAM: 2
To Handle file
Usage
Options
Mandatory arguments to long options are mandatory for short options too.
cd - change directories
Use cd to change directories. Type cd followed by the name of a directory to access that
directory. Keep in mind that you are always in a directory and can navigate to directories
hierarchically above or below.
mv- change the name of a directory
Type mv followed by the current name of a directory and the new name of the directory.
will show you the full path to the directory you are currently in. This is very handy to use,
especially when performing some of the other commands on this page
rm -r
Usage
Options
Change the owner and/or group of each FILE to OWNER and/or GROUP. With --reference,
change the owner and group of each FILE to those of RFILE.
-c changes like verbose but report only when a change is made
-dereference affect the referent of each symbolic link, rather than the symbolic link itself
-h no-dereference affect each symbolic link instead of any referenced file (useful only on
systems that can change the ownership of a symlink)
-from=CURRENT_OWNER:CURRENT_GROUP
change the owner and/or group of each file only if its current owner and/or group match those
specified here. Either may be omitted, in which case a match is not required for the omitted
attribute.
-reference=RFILE use RFILE's owner and group rather than the specifying OWNER: GROUP
values
Usage
r - Change the permission on files that are in the subdirectories of the directory that you are
currently in. permission Specifies the rights that are being granted. Below are the different
rights that you can grant in an alphanumeric format. Filenames File or directory that you are
associating the rights with Permissions
o - Other.
a - All.
cp - Copy files
cp myfileyourfile
Copy the files "myfile" to the file "yourfile" in the current working directory. This command will
create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it
exists.
With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.
cp -i /data/my file
Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt before
overwriting the file.
cp -dprsrcdirdestdir
Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-poption),
file attributes (-p option), and copy recursively (-r option). With these options, a directory and all
it contents can be copied to another dir
ln - Creates a symbolic link to a file.
ln -s test symlink
Creates a symbolic link named symlink that points to the file test Typing "ls -i test symlink" will
show the two files are different with different inodes. Typing "ls -l test symlink" will show that
symlink points to the file test.
slocate -u
This command builds the slocate database. It will take several minutes to complete this
command. This command must be used before searching for files, however cron runs this
command periodically on most systems. Locate whereis Lists all files whose names contain the
string "whereis". directory.
more - Allows file contents or piped output to be sent to the screen one page at a time
cat - Sends file contents to standard output. This is a way to list the contents of short files to the
screen. It works well with piping.
bg - bg jobs Places the current job (or, by using the alternative form, the specified jobs) in the
background, suspending its execution so that a new user prompt appears immediately. Use
the jobs command to discover the identities of background jobs.
cal month year - Prints a calendar for the specified month of the specified year.
diff file1 file2 - Compares two files, reporting all discrepancies. Similar to the cmp command,
though the output format differs.
dmesg - Prints the messages resulting from the most recent system boot.
fg jobs - Brings the current job (or the specified jobs) to the foreground.
file files - Determines and prints a description of the type of each specified file.
Searches the specified path for files with names matching the specified pattern (usually enclosed
in single quotes) and prints their names. The find command has many other arguments and
functions.
ftp hostname
Opens an FTP connection to the specified host, allowing files to be transferred. The FTP
program provides subcommands for accomplishing file transfers.
2)Process Handling
kill process_ids
kill -l
Kills the specified processes, sends the specified processes the specified signal (given as a
number or name), or prints a list of available signals.
killall program
Kills all processes that are instances of the specified program or sends the specified signal to all
processes that are instances of the specified program.
mail - Launches a simple mail client that permits sending and receiving email messages.
ping host - Sends an echo request via TCP/IP to the specified host. A response confirms that the
host is operational.
shutdown minutes
shutdown -r minutes
Shuts down the system after the specified number of minutes’ elapses (requires root privileges).
The -r option causes the system to be rebooted once it has shut down.
sleep time - Causes the command interpreter to pause for the specified number of seconds.
sort files - Sorts the specified files. The command has many useful arguments; see the online
documentation.
split file - Splits a file into several smaller files. The command has many arguments; see the
online documentation
top - Prints a display of system processes that's continually updated until the user presses
the q key.
traceroute host - Uses echo requests to determine and print a network path to the host.
Aim: File System and working with files in Linux Operating System.
For convenience, the Linux file system is usually thought of in a tree structure.
Description of directories:
/bin Common programs, shared by the system, the system administrator and the users.
/bootThestartup files and the kernel, vmlinuz. In some recent distributions also grub data. Grub
is the GRand Unified Boot loader and is an attempt to get rid of the many different boot-loaders
we know today.
/devContains references to all the CPU peripheral hardware, which are represented asFiles with
special properties.
/etcMost important system configuration files are in /etc, this directory contains data similar to
those in the Control Panel in Windows
/libLibrary files, includes files for all kinds of programs needed by the system and the users.
/lost+foundEvery partition has a lost+found in its upper directory. Files that were saved during
failures are here.
/mntStandard mount point for external file systems, e.g. a CD-ROM or a digital camera.
/rootThe administrative user's home directory. Mind the difference between /, the root
directoryand /root, the home directory of the root user.
/sbin Programs for use by the system and the system administrator.
/tmpTemporary space for use by the system, cleaned upon reboot, so don't use this for saving
any work!
/varStorage for all variable files and temporary files created by users, such as log files, the mail
queue, the print spooler area, space for temporary storage of files downloaded from the Internet,
or to keep an image of a CD before burning it.
mkdir: Used For create directories in Linux operating system. With -p option you
cancreate subdirectories.
rmdir: Helps to remove directories with different options like -r for remove
directoriesforcefully if root directory contains other sub directories.
rm: Used for remove file. With -i option it will prompt for yes or no while removing file
cp:Used for copy files.
du: Display how much space a file has been occupied. With -c option you can see total
space occupied by a file.
cat: Used to display the contents of file. You can use cat command for create a file and
useCTRL+d for save that file.
mv: Used for move files from one location to another location.
df: It displays the file fragmentations in your system.
gzip: Used for compress files.
#/bin/bash
OUTPUT:-
PROGRAM: 5
Aim: Write script to display current date, time, username and current
directory.
mkdir student
cd student
touch record
cat record
echo
echo user:$(whoami)
sh record
OUTPUT:-
PROGRAM: 6
Aim: Write a shell script to read and check the directory exists or not, if not
make directory.
#!\bin\bash
FILE=””
DIR=”/tmp”
#intt
If [“$(ls –A Srecord)”];then
else
echo”$DIR is empty”
fi
OUTPUT:-
PROGRAM: 7
#!/bin/ksh
Str=”*”
For I in 1 2 3 4 5
Do
Echo “$str”
Str =”$str *”
done
OUTPUT:-
PROGRAM :8
Command Description
vi filename Creates a new file if it already does not exist, otherwise opens existing file.
You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line. If
a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some
other nonviewable character present.
Operation Modes
While working with vi editor you would come across following two modes −
Command mode − This mode enables you to perform administrative tasks such as
savingfiles, executing commands, moving the cursor, cutting (yanking) and pasting lines
or words,and finding and replacing. In this mode, whatever you type is interpreted as a
command.
Insert mode − This mode enables you to insert text into the file. Everything that's typed
inthis mode is interpreted as input and finally it is put in the file.
The vi always starts in command mode. To enter text, you must be in insert mode. To come in
insert mode you simply type i. To get out of insert mode, press the Esc key, which will put you
back into command mode.
Getting Out of vi
The command to quit out of vi is :q. Once in command mode, type colon, and 'q', followed by
return. If your file has been modified in any way, the editor will warn you of this, and not let you
quit. To ignore this message, the command to quit out of vi without saving is :q!. This lets you
exit vi without saving any of the changes.
The command to save the contents of the editor is :w. You can combine the above
command with the quit command, or :wq and return.
The easiest way to save your changes and exit out of vi is the ZZ command. When you
are incommand mode, type ZZ and it will do the equivalent of :wq.
Moving within a File
To move around within a file without affecting your text, you must be in command mode (press
Esc twice). Here are some of the commands you can use to move around one character at a time
–
CommandDescription
Editing Files
To edit the file, you need to be in the insert mode. There are many ways to enter insert mode
from the command mode −
CommandDescription
Control Commands
There are following useful command which you can use along with Control Key –
Command Description
Linux is most powerful operating system, and networking is an essential part of servers. it offers
many inbuilt commands to diagnose for network issues. In this article, I will show you useful
Linux networking commands, which will help you in troubleshooting.
Connectivity:
ping<host> —- sends an ICMP echo message (one packet) to a host. This may go
continually until you hit Control-C. Ping means a packet was sent from your machine
via ICMP, and echoed at the IP level. ping tells you if the other Host is Up.
telnet host <port> —- talk to “hosts” at the given port number. By default, the telnet
port is port 23. Few other famous ports are:
7 - echo port,
25 – SMTP, use to send mail
79 – Finger, provides information on other users of the network
Arp:
Arp is used to translate IP addresses into Ethernet addresses. Root can add and delete
arp entries. Deleting them can be useful if an arp entry is malformed or just wrong.
Arp entries explicitly added by root are permanent — they can also be by proxy. The
arp table is stored in the kernel and manipulated dynamically. Arp entries are cached
and will time out and are deleted normally in 20 minutes.
netstat –r —- Print routing tables. The routing tables are stored in the kernel and used
by ip to route packets to non-local networks.
route add —- The route command is used for setting a static (non-dynamic by hand
route) route path in the route tables. All the traffic from this PC to that IP/SubNet will
go through the given Gateway IP. It can also be used for setting a default route; i.e.,
send all packets to a particular gateway, by using 0.0.0.0 in the pace of IP/SubNet.
routed —– The BSD daemon that does dynamic routing. Started at boot. This runs the
RIP routing protocol. ROOT ONLY. You won’t be able to run this without root
access.
gated —– Gated is an alternative routing daemon to RIP. It uses the OSPF, EGP, and
RIP protocols in one place. ROOT ONLY.
traceroute —- Useful for tracing the route of IP packets. The packet causes messages
to be sent back from all gateways in between the source and destination by increasing
the number of hopes by 1 each time.
netstat –rnfinet : it displays the routing tables of IPv4
sysctlnet.inet.ip.forwarding=1 : to enable packets forwarding (to turn a host into a
router)
routeadd|delete [-net|-host] <destination><gateway> (ex. route add 192.168.20.0/24
192.168.30.4) to add a route
route flush : it removes all the routes
route add -net 0.0.0.0 192.168.10.2 : to add a default route
routed -Pripv2 –Pno_rdisc –d [-s|-q] to execute routed daemon with RIPv2 protocol,
without ICMP auto-discovery, in foreground, in supply or in quiet mode
route add 224.0.0.0/4 127.0.0.1 : it defines the route used from RIPv2
rtquery –n : to query the RIP daemon on a specific host (manually update the routing
table)
Others:
nslookup —- Makes queries to the DNS server to translate IP to a name, or vice versa.
eg. nslookup facebook.com will gives you the IP of facebook.com
ftp<host>water —– Transfer files to host. Often can use login=“anonymous” ,
p/w=“guest”
rlogin -l —– Logs into the host with a virtual terminal like telnet
Important Files:
Switching:
VLAN:
NAT/Firewall
rm /etc/resolv.conf – it prevent address resolution and make sure your filtering and
firewall rules works properly.
ipnat –f file_name – it writes filtering rules into file_name
ipnat –l – it gives the list of active rules
ipnat –C –F – it re-initialize the rules table
map em0 192.168.1.0/24 -> 195.221.227.57/32 em0 : mapping IP addresses to the
interface
map em0 192.168.1.0/24 -> 195.221.227.57/32 portmaptcp/udp 20000:50000 :
mapping with port.
ipf –f file_name : it writes filtering rules into file_name.
ipf –F –a : it resets the rule table.
ipfstat –I : it grants access to a few information on filtered packets, as well as active
filtering rules.
PROGRAM: 10
Mail client: If you want to send or receive an email, you should have a mail server capable of
sending and receiving mail across the Internet. In this post, we will discuss the Linux mail server
and how SMTP protocol (the standard protocol for mail transport across the internet) works, as
well as other mail-related protocols like Post Office Protocol (POP) and Internet Message Access
Protocol (IMAP) and the relationship between them.
SMTP (Simple Mail Transfer Protocol) defines how mail is sent from one host to another. It does
not define how the mail should be stored or how it is displayed. It is also system-independent,
which means that the sender and receiver could have different operating systems.
SMTP requires only that a server is able to send straight ASCII text to another server, and this is
done by connecting to the server on port 25which is the standard SMTP port.
Most Linux distros today are shipped with two of the most common implementation of
SMTP: sendmail and postfix.
Sendmail is a popular open-source mail server implementation used by many Linux distros, but
it has a little complex design and less secured. The Postfix took mail server implementation one
step further — it was developed with security in mind.
1. Mail user agent (MUA). This is a component that the user sees and interacts with (like
Thunderbird and Microsoft Outlook). These user agents are responsible for reading mail
and allowing you to compose mail.
2. Mail transport agent (MTA). This component is responsible for getting the mail from
one site to another (like Send mail and Postfix).
3. Mail delivery agent (MDA). This component is responsible for distributing received
messages on the local machine to the appropriate user mailbox (like postfix-
maildrop and Procmail).
Mail client: If you want to send or receive an email, you should have a mail server capable of
sending and receiving mail across the Internet. In this post, we will discuss the Linux mail server
and how SMTP protocol (the standard protocol for mail transport across the internet) works, as
well as other mail-related protocols like Post Office Protocol (POP) and Internet Message Access
Protocol (IMAP) and the relationship between them.
SMTP (Simple Mail Transfer Protocol) defines how mail is sent from one host to another. It does
not define how the mail should be stored or how it is displayed. It is also system-independent,
which means that the sender and receiver could have different operating systems.
SMTP requires only that a server is able to send straight ASCII text to another server, and this is
done by connecting to the server on port 25which is the standard SMTP port.
Most Linux distros today are shipped with two of the most common implementation of
SMTP: sendmail and postfix.
Sendmail is a popular open-source mail server implementation used by many Linux distros, but
it has a little complex design and less secured. The Postfix took mail server implementation one
step further — it was developed with security in mind.
1. Mail user agent (MUA). This is a component that the user sees and interacts with (like
Thunderbird and Microsoft Outlook). These user agents are responsible for reading mail
and allowing you to compose mail.
2. Mail transport agent (MTA). This component is responsible for getting the mail from
one site to another (like Send mail and Postfix).
3. Mail delivery agent (MDA). This component is responsible for distributing received
messages on the local machine to the appropriate user mailbox (like postfix-
maildrop and Procmail).