You are on page 1of 24

Linux Ubuntu Assist. Lect.

ICCI Estabraq Hazim



1 | P a g e

Installing Ubuntu
Before installing Ubuntu, some preparatory work within Windows is
advisable.
Ensuring enough disk space is free
Regardless of what installation method you choose, youll need at least 5GB
of free disk space for Ubuntu.
Getting Ubuntu
The Ubuntu install CD also includes a live mode that runs Ubuntu straight
from the CD. This lets you tryout Ubuntu but its too slow to be used
permanently.
Choosing a version
Its possible to switch to a different version of Ubuntu once its been
installed, so making this choice now is not critical.
Downloading Ubuntu
To download Ubuntu, simply head off to www.ubuntu.com and click the
relevant link to download the ISO image of Ubuntu.
Select the desktop edition for 32-bit computers.
1. Insert the Ubuntu CD and reboot your computer. At the BIOS
startup screen, choose boot from CD/DVD-ROM
2. Select your preferred language from the list and hit Enter.
3. Eventually the Ubuntu installation program window will appear
4. After some time, the disk repartitioning choices will appear.
5. You should take a small part of memory and assign it as Swap Area
in order to ease the processing operating system
6. Assign a partition for Ubuntu on which to be installed
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

2 | P a g e

7. After making your partitioning choices, click FORWARD. Youll be
asked to confirm your choices. Once repartitioning has finished, youll be
invited to create a user account.
Users and File System
Each user on the system is given their own folder within the /home folder in
which to save personal data. For example, the user Jane is given the folder
/home/jane. The /home folder is akin to C:\Documents and Settings under
Windows.
Perhaps the most important locations for the majority of users are /home,
as described above; /usr/bin, where practically all software is located; and
/etc, where system configuration files are found.
The table below gives an idea of what each of the folders mean.


Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

3 | P a g e

Understanding Users
When you installed Ubuntu, a personal user account was created for you. As
part of this process, a folder named after your username was created in /home,
as mentioned previously.
Root User
On most Linux systems, two login accounts are created during installation
a standard user, and the root user.
The root user is a special user account gifted with the ability to do anything,
such as delete system files, or install software. Its username is always root.
The owner of a file can set three separate sets of permissions:
firstly for himself
secondly for the group
and lastly for all others.
In setting file permissions for himself, the files owner could mark a file as
read-only, for example, to avoid accidentally changing it.
All users are members of a group, and each file is assigned to a particular
group, in addition to being owned by a user. So, a separate set of file
permissions can be granted for members of that group.
A third and final set of permissions can be configured for others, which is to
say, users on the computer who arent the files owner, and who arent in the
group.
Below is a window in Linux with its exhibition.

Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

4 | P a g e


1. Toolbar: Here you can move backwards and forwards within your
file browsing history, or click the UP button to move to the parent folder
of the one youre currently browsing. The RELOAD button will refresh the
file listing to show files that have recently been added or deleted, while
the Home and Computer buttons provide shortcuts to those browsing
locations.
2. Location bar: The location bar displays the currently browsed
folder, and works in one of two modes: button, and text.
3. Side Pane: The side pane shows information relevant to the files or
folders being browsed. It can be closed by clicking the X button at the top
right of the pane
4. Filesystem locations: If the Places mode of the side bar is selected
(it is by default), youll find listed shortcuts to filesystem locations, or
attached storage devices. This list is identical to the entries found on the
Places main menu.
5. Bookmarks: If the Places mode is selected in the side pane, here
youll find shortcuts to bookmarked filesystem locations. This list is
effectively a mirror of the Bookmarks menu.
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

5 | P a g e

6. Status bar: If no files are selected, the status bar shows the number
of files in the current folder, along with the free space on the disk. If files
are selected, youll see the number of files in the selection, along with
their combined total size.
7. Zoom controls: Here you can set the zoom level of the current
view, effectively making the icons bigger or smaller.
8. Emblems: As mentioned previously, all files and folders can have
small icons attached to them. These are called emblems, and its
important to note theyre specific user.
9. File area: Here is where the files/folders in the currently browsed
folder are listed.
10. Icon/list view selector: Here you can switch between three
separate file listing view modes: icon, list, and compact.

What is Shell?
When we talk about the command-line, were talking about issuing typed
commands directly to Linux.
The command-line utilized in Ubuntu is known as bashthe Bourne Again
SHell.
Command-line programs are sometimes known as shells.
Starting a command-line session:
- Open a desktop terminal to access the command line
- When the terminal program appears, youll see something like this:
keir@keirdesktop:~$
- The first part of the prompt shows your username. In this example,
taken from my test PC, the user is keir. After the @ sign is the name of
the computer, commonly referred to as the hostname. This was set
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

6 | P a g e

during installation of Ubuntu, on the same configuration screen where
you chose your username.
- A colon (:). This separates the physical location part of the prompt
from the rest, that tells us the location in the filesystemwhich folder
were currently browsing.
- A ~ symbol (known as a tilde). This is shorthand that always
indicates the users /home folder. When you see a tilde, imagine the path
to your home folder instead.
So, reading from left to right, the prompt tells us that the user keir,
logged in at the computer called keirdesktop, is currently browsing his
/home folderin this case, thats /home/keir.
- The final component in the line is a dollar sign. This indicates that
youre logged in as an ordinary user. If you ever log in directly as root, the
prompt changes to a pound sign (#; also called a hash sign).

NOTE Remember: upper and lower case letters matter in Ubuntu! You
must type Documents and not documents or DOCUMENTS!
How Commands Work:
Some commands work on their own. An example is (ls).
Opening a Terminal will start a command prompt in which commands are
input. (ls) will list the contents of a folder.
ls
Desktop Documents Examples Music Pictures Public
Templates Videos
This is a listing of the files and folders in my /home folder.
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

7 | P a g e

While some commands are good to go on their own, most commands take
arguments. In other words, they need to be told what file(s) or folder(s) to
work with.
For example to change directories (cd) is used followed by the folders
name:
Cd Documents
keir@keirdesktop:~/Documents$
Command Options
Heres the ls command used on its own, and then with the l command option
to show who owns each file listed in the directory:
ls
Desktop Documents Examples Music Pictures Public
Templates Videos
ls l
total 28
drwxrxrx 2 keir keir 4096 20081024 10:07 Desktop
drwxrxrx 2 keir keir 4096 20081024 10:07 Documents
lrwxrwxrwx 1 keir keir 26 20081024 09:51 Examples >
/usr/share/examplecontent etc.
Another useful command option for ls is a, which causes all files to be
listedeven hidden ones.
Command options can be combined together. To do so, simply list them one
after the other. To see all details about files, and also any hidden files, we
would type ls la.
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

8 | P a g e

ls la is a very common command used by many Linux users daily. Command
options are usually letters, but sometimes words are used too. In fact, to view a
quick list of options, you can use the help command option (thats two
hyphens, rather than one). For example, ls help, or cp help.
Relative and absolute paths
A path is simply the part of a file or folder listing describing where it is
located in the filesystem/home/keir/Documents/report.doc, for example.
Paths can be expressed in two ways: relative and absolute.
- Cd /home/keir/Documents/pictures (obsolete path)
Or you can use a relative path and enter the same folder in two steps:
- Cd Documents
Cd Pictures
To revert to a previous folder (parent folder):
- Cd ..
Useful everyday commands:

Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

9 | P a g e



Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

10 | P a g e

Working with root powers
Using sudo
At the command-line, you can force a command to run with root powers by
preceding it with sudo (short for super-user do). For example, to install
software using the dpkg command, root powers are needed, so to use dpkg
you would type something similar to the following:
- sudo dpkg i package.deb
Youll be prompted for your password. Once this is entered, the command
will complete.
Temporarily Switch to Root
To switch to the root user temporarily, type the following:
- sudo su
After typing your password, youll see that the command-prompt changes to
a hash symbol, to indicate that you have root powers.
When youve finished your work and want to return to your ordinary user
account, just type exit, or hit Ctrl+D.
Add a User
To add a user besides the administrator, you must enter sudo:
- sudo adduser Ali (adds a user named Ali)
Change Owner
In order to show each files owner and view their permissions, enter the
following:
ls -l
An example of what would appear is
total 28
drwxrxrx 2 estabraq estabraq 4096 20081024 10:07 Desktop
drwxrxrx 2 estabraq estabraq 4096 20081024 10:07 Documents
lrwxrwxrwx 1 estabraq estabraq 26 20081024 09:51 Examples
/usr/share/examplecontent
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

11 | P a g e

drwxrxrx 2 estabraq estabraq 4096 20081024 10:07 Music
drwxrxrx 2 estabraq estabraq 4096 20081024 10:07 Pictures
drwxrxrx 2 estabraq estabraq 4096 20081024 10:07 Public
drwxrxrx 2 estabraq estabraq 4096 20081024 10:07 Templates
drwxrxrx 2 estabraq estabraq 4096 20081024 10:07 Videos
where letters at the beginning of line indicates permission for the
administrator (estabraq), as follows:

Where (d) indicates (Directory), (r), (w) and (x) are for (Read), (write) and
(execute), respectively.
Now, to change the owner of one of the above files,
Cd Documents
Sudo chown Ali aa

Some keyboard shortcuts are helpful in writing bash:

Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

12 | P a g e


Working with Compressed Files
Zip files
The following will create a new zip file called report.zip, and add report.doc
to it:
odt
To zip a folder full of files, add the r command option. The following will
create reports.zip, containing the contents of the reports folder:
r reports.zip reports
NOTE If any files or folders have spaces or unusual characters in them,
enclose them in quotation marks.
To unzip files, the unzip command is used. The following will extract the files
from archive.zip:

To list files in an archive prior to unzipping, use the l command option:
l archive.zip
You can subsequently unzip a single file from the archive by specifying its
filename (and path, if its contained within a subfolder within the zip!) after
specifying the archive name. The following will extract only report.doc from
archive.zip:
odt

tar archives
The tar command is both powerful and multi-faceted. It was originally
designed for backup purposes, but works equally well for individual file/folder
archiving.

Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

13 | P a g e

Creating a tar archive
The following will create a simple tar archive called archive.tar, containing
the contents of the reports folder:
NOTE Remember that tar archives are not automatically compressed. They
are simply container files. A tar files size reflects almost exactly the combined
size of the files it contains:

The c command option tells tar to create an archive, and the f command
option tells the tar command that the filename immediately follows. The f
option should always be added at the end of the range of command-options,
immediately before the archives filename.
To additionally compress the archive, the j or z options can be added in for
bzip2 or gzip compression, respectively. Bzip2 compression is considered most
efficient and is arguably most common.
The following will create a bzip2 tar archive of the reports folder:
cjvf archive.tar.bz2 reports
The v command option has also been added above. This provides verbose
feedback, explaining what tar is doing. Without it, tar provides no feedback at
all unless something goes wrong.

The following will extract the contents of archive.tar.bz2:

Again, the v option has been added so that the user is provided with
verbose feedback.
The following will extract the contents of archive.tar.gz:

To list files in an archive, use the t option:

To extract a single file, specify it after the archive name. The following will
extract report.doc from the archive.tar.bz2 archive:
odt
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

14 | P a g e

Advanced bash Techniques
Redirection
Sometimes its useful to send the output of a command into a file, rather
than displaying it on screen. This is the principle of output redirection.
Lets say you wanted to make a permanent record of a file listing. To do this
using redirection, type the following:

The right angle symbol (>) tells bash to redirect output of the ls command
into the file listing.txt, rather than send it to the screen.
If listing.txt doesnt exist, it will be created. If a file of that name already
exists, its contents will be overwritten.
Using two angle brackets (>>) instead of one will cause the output to be
added to the bottom of an existing file.
You can view your new file in a text editor, or by using the less text viewer:


To redirect the contents of a file into a particular command, the reverse
angle bracket is used (<).
sort < shoppinglist.txt

Brace expansion
Lets say youve come back from a European tour with a digital camera full
of photos, and want to create four folders on your PC, named photos_germany,
photos_france, photos_england, and photos_spain.
You could create each manually using the mkdir command, but brace
expansion lets you to create all four in one fell swoop, as follows:

If we view a file listing, well find we have four new folders:

photos_england photos_france photos_germany photos_spain

Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

15 | P a g e

If you subsequently wanted to delete the photos_spain and photos_france
folders, rather than type two separate rm commands, you could use brace
expansion again:
r photos_{france,spain}
If you wanted to create 10 folders named photos (i.e.photos1, photos2,
etc.), the following will do the trick:

Letters can be specified instead of letters. The following will create photosA,
photosB etc., all the way to Z:


Wildcards
If you wanted to delete all files in a folder, you could type the following:

To return to the vacation folder example used above, the photos_spain,
photos_germany etc. folders could be deleted like this:
r photos_*
This will delete any file whose name begins with photos_, but has any
number of characters following (should there be a folder called photos_, with
nothing after it, that too will be deleted).
A question mark (?) can be used to indicate any single character. The
following will delete photos1, photosA, photos!, and so on:
r photos?

APT and dpkg
APT (Advanced Packaging Tool) and dpkg (Debian Package System) are used
for installing or removing software.
Some Typical software administration commands are:
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

16 | P a g e






Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

17 | P a g e

Firewall using IPTables
In order to make a server more secure after the initial set up, Ubuntu ships
with Iptables which is the distributions default firewall.
If you type in the following, you can see the current rules in the virtual
server's IP Table:
sudo iptables -L

They should look like this:
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

If you have another set of rules in place or want to start fresh, you can
always set the rules back to the default by flushing and deleting all of them:
sudo iptables -F

Additionally, if you want speed up your work with IP Table, you can include -n
in the command. This option disables DNS lookups and prevents the command
from trying to find the reverse of each IP in the ruleset. You could use this to
list rules, as an example:
iptables -L -n

To start off, lets be sure to allow all current connections, all of the
connections.
sudo iptables -A INPUT -j ACCEPT

-A tells the IP table to append a rule to the table.
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

18 | P a g e

INPUT designates this rule as part of the Input chain.
-j ACCEPT tells the packet to JUMP to accept and the connections are still in
place.
Lets assume that we want to allow all incoming traffic, except for those
coming in on 2 common ports: 22 for SSH and 80 for web traffic. We proceed
by allowing all traffic and DROP traffic with the specific ports with the following
commands:

Sudo iptables A INPUT j ACCEPT

sudo iptables -A INPUT -p tcp --dport ssh -j DROP
sudo iptables -A INPUT -p tcp --dport 80 -j DROP

In both of these commands, the -p option stands for the protocol with which
the connection is being made, in this case tcp, while the --dport specifies the
destination port through which the packet is being transmitted.

Lets make a rule to block all of the remaining traffic:
sudo iptables -A INPUT -j DROP
and in order we want to only accept the traffic of TELNET that take port
number 22
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
or
sudo iptables -A INPUT -p tcp --dport telnet -j ACCEPT

With that, we can see what our updated rules look like:

sudo iptables -L


Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

19 | P a g e

Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
DROP tcp -- anywhere anywhere tcp dpt:ssh
DROP tcp -- anywhere anywhere tcp dpt:http
DROP all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:telnet


We are almost finished. However, we are missing one more rule. We need
to provide our server with loopback access. If we were to add the rule now
without further qualifiers, it would go to the end of the list. In order to make
this rule first in the list, using the INPUT option:

sudo iptables -I INPUT 1 -i lo -j ACCEPT

I INPUT 1 places this rule at the beginning of the table
lo refers to the loopback interface
-j ACCEPT then guarantees that the loopback traffic will be accepted
Now we have finished creating a basic firewall. Your rules should look like
this (we can see the details of the iptable by typing -v):
sudo iptables -L -v

Although the IP tables are effective, they will automatically be deleted if the
server reboots. To make sure that they remain in effect, we can use a package
called IP-Tables persistent.
We can install it using apt-get:
sudo apt-get install iptables-persistent

Once the installation is complete, start iptables-persistent running:
sudo service iptables-persistent start

Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

20 | P a g e

Starting a Shell
In most Linux systems, your default shell is the bash shell. To fi nd out what
your default login shell is, type the following command:
$ echo $SHELL
/bin/bash

In this example, its the bash shell. There are many other shells, and you can
activate a different one by simply typing the new shells command (ksh, tcsh,
csh, sh, bash, and so forth) from thecurrent shell. For example, to change
temporarily to the C shell, type the following command:
$ csh

Checking your login session
To find out information about your identity, use the id command as follows:
$ id
uid=501(chris)gid=105(sales)groups=105(sales),4(adm),7(lp)

In this example, the username is chris, which is represented by the numeric
user ID (uid) 501. The primary group for chris is called sales, which has a group
ID (gid) of 105. The user chris also belongs to other groups called adm (gid 4)
and lp (gid 7).

You can see information about your current login session by using the who
command. In the following example, the -u option says to add information
about idle time and the process ID and H asks that a header be printed:
$ who -uH
NAME LINE TIME IDLE PID COMMENT
chris tty1 Jan 13 20:57 . 2013

To find out the name of your home directory, type the echo command,
followed by the $HOME variable:
$ echo $HOME
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

21 | P a g e

Or:
$ echo ~
/home/estabraq

To find the processes running at a time:
$ ps u

To see your current path, type the
following:
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/chris/bin
The results show a common default path for a regular Linux user.

Expanding arithmetic expressions
There are two forms you can use to expand an arithmetic expression and
pass it to the shell: $[expression] or $(expression). Here is an example:
$ echo I am $[2013 - 1987] years old.
I am 26 years old.

$ echo There are $(ls | wc -w) files in this directory.
There are 14 files in this directory.

Try out some of these file-matching metacharacters by first going to an
empty directory (such as the Desktop) and creating some empty files:

$ touch apple banana grape grapefruit watermelon

The touch command creates empty files. The next few commands show you
how to use shell metacharacters with the ls command to match filenames. Try
the following commands to see whether you get the same responses:
$ ls a*
apple
$ ls g*
grape
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

22 | P a g e

grapefruit
$ ls g*t
grapefruit
$ ls *e*
apple grape grapefruit watermelon
$ ls *n*
banana watermelon

Here are a few examples of pattern matching with the question mark (?):
$ ls ????e
apple grape
$ ls g???e*
grape grapefruit


Making your Ubuntu a Web Server

1. Update you system:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade

Once all updates are complete, restart your system.

2. Set Up LAMP Server with APC and PHPMyAdmin. Installing the LAMP
(Linux, Apache, MySql, PHP) stack is quick and painless with apt. Simply use the
following commands to get everything installed.
$ sudo apt-get install lamp-server^ php-apc phpmyadmin

3. We will also need a mail server to handle outgoing email requests. Insatll
Postfix Mail Server:
$ sudo apt-get install postfix
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

23 | P a g e

When installing postfix youll go through some configuration screens. The
defaults should be fine for a basic web server setup.

4. Set Web Directory User and Permissions. Now that you have everything
installed and configured youll need to set up some file permissions to allow
Apache to read from, and you to write to, the web directory.

Create a new group:
$ sudo addgroup webdev

Change the group of your web directory:
$ sudo chgrp -R webdev /var/www/
$ sudo chmod -R g+rw /var/www/

Add Apache to the webdev group:
$ sudo usermod -a -G webdev www-data

Add your user to the webdev group:
$ sudo usermod -a -G webdev <user_name>

If everything has gone well you can nos open a web browser and enter URL
http://localhost . If everything went well, you should now see a page saying (It
Works). This is a default web page for the Apache2 server.

5. Add a web page to your website

- Create a directory at (home) named (~/public_html)
$ mkdir ~/public_html

- Next, change the permissions on the directory with the command:
$ chmod 755 ~/public_html .
Linux Ubuntu Assist. Lect.
ICCI Estabraq Hazim

24 | P a g e

This makes this directory accessible to the web server.
- Execute this command to configure the web server to look in user
directories.
$ sudo a2enmod userdir
$ sudo service apache2 restart
to restart the web server.

- create a simple web page named test1.html in the directory /public_html
like this:
<html>
<body>
<h1> Welcome to my Web Page </h1>
</body>
</html>

You can view the page by entering the URL:
http://localhost/~estabraq/test1.html .

Congratulations, you have created your first web page.

You might also like