Beruflich Dokumente
Kultur Dokumente
Linux history
Definition of Linux
Basic features of Linux
Architecture of Linux
Popular shells
Linux distributions
Ubuntu Linux
Installing Ubuntu
Linux History
Linux looks like UNIX system. Linux is developed by Linux Torvalds. Linux development
began in 1991. Linux source code is available for free on the internet. The basic Linux system is
standard environment for applications and user programming.
Definition
Linux is a modern, free operating system based on UNIX standards. It has designed to run
efficiently and reliably on common PC hardware; it also runs on a variety of other platforms. It
provide a programming interface and user interface compatible with standard UNIX systems and
can run a large number of UNIX applications, including an increasing number of commercially
supported applications.
Components of a Linux System
Any Linux system consists of three components.
1. Kernel
2. System libraries
3. System utilities
Kernel:
The Kernel is responsible for maintaining all the important construct of the operating system. It
includes virtual memory and processes. Kernel is most important component of the Linux
system.
System libraries:
It contains standard set of functions through which application can interact with the Kernel.
System library implements much of the operating system functionality that does not need the full
privileges of Kernel code.
System utilities:
System utilities are programs that perform individual, specialized management tasks
1
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
2
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
1. Hardware: The hardware at the centre of the structure provides the operating system with
basic services. The hardware constitutes all the peripherals like memory (RAM, HDD, FDD, CD
etc), processor, mouse and other input devices, terminal (i.e. VDUs), printers etc.
2. The Kernel: The operating system interacts directly with the hardware, providing common
services to programs and insulating user program complexities of hardware. The operating
system is also called as Kernel. This kernel interacts directly with the hardware. Because of this
hardware isolation of user program, it is easy to move the user programs easily between UNIX
systems running on different hardware.
3. Shell: Shell is actually the interface between the user and the Kernel that isolates the user from
knowledge of Kernel functions. The shell accepts the commands keyed in by the users and
checks for their syntax and gives out error messages if something goes
Popular shells :
C shell - csh : resembles that of the C programming language
Bourne shell sh : used on Unix and Linux
Korne shell ksh : standard configuration
Tc shell - Tcsh : similar to the C shell
Bash shell the basic shell available on all linux
Flavors of UNIX
3
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Ubuntu Linux
Ubuntu Philosophy
The term Ubuntu is a traditional African concept originating from the Bantu languages of
southern Africa. It can be described as a way of connecting with othersliving in a global
community where your actions affect all of humanity. Ubuntu is more than just an operating
system: it is a community of people coming together voluntarily to collaborate on an
international software project that aims to deliver the best possible user experience
4
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
There are many reasons to use Ubuntu, but here are some of the most important ones:
Its free and open source: shared code, shared efforts, shared principles, no cost.
Its easy to use, trial and install: you dont have to be an expert.
Its beautiful, sleek, and stylish: learn more about the Unity desktop environmen
Its stable and fast: usually loads in less than a minute on modern computers.
Its up-to-date: Canonical releases new versions of Ubuntu every six months and also brings
you regular updates for free.
It is supported: you can get all the support and advice you need from the global FLOSS
community and Canonical.
Canonical releases new versions of Ubuntu every six months, in April and October. Each Ubuntu
release has a version number that contains the year and month of its release. This guide, for
example, discusses the latest version of Ubuntu 11.10 which was released in October of 2011.
The next scheduled release of Ubuntu, version 12.04, will be in April of 2012; the one after that
will be 12.10 in October of 2012, and so on.
For a complete listing of Ubuntu versions and code names, see the table below.
One of the great features of Ubuntu (and there are many) is that it is supported within a
structured time frame. New versions of the operating system are released every six months and
are normally supported by Canonical for 18 months thereafter. These versions are referred to as
normal releases.
5
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
In addition to normal releases, Canonical also develops Long Term Support (LTS) releases
which are versions of Ubuntu that are released approximately every two years (if on schedule)
and are supported for three years thereafter. The upcoming version of Ubuntu, 12.04, will be a
Long Term Support release.
Ubuntu 14.10 - the current stable support version, released in late October 2014, codenamed
Utopic Unicorn.
Ubuntu 14.04 LTS - the current long term support version, released in mid April 2014,
codenamed Trusty Tahr.
Ubuntu 12.04 LTS - a previous long term support version, released in April 2012, codenamed
Precise Pangolin.
Ubuntu 10.04 LTS - a previous long term support version, released in April 2010, codenamed
Lucid Lynx
6
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Installing Ubuntu
There are three different ways you can install Ubuntu on to your computer:
This option will replace your current operating system with Ubuntu. A complete installation will
run the fastest and smoothest on your computer, but requires a full commitment to leave behind
your old operating system.
http://www.ubuntu.com/download/ubuntu/download
Note that this method can also be used to create a dual-boot setup, but requires some knowledge
of partitioning. Keep reading for an easier method.
This option will allow you to maintain your current operating system and also run Ubuntu from
an independent storage device. This installation requires the least commitment from you and
your computer, but will probably affect the quality and speed of Ubuntus performance.
http://www.ubuntu.com/download/ubuntu/download
This option, also known as a dual boot installation, will install Ubuntu on your computer
alongside your current Windows or Mac operating system. Whenever you start your computer,
you will have the option of choosing to boot into Ubuntu or Windows/Mac OS.
This is the installation option that we will be discussing in this guide as it is the easiest way for
beginners to transition to Ubuntu. Detailed instructions will be provided for a Windows 7 dual
boot installation, but for those using Mac OS,
7
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
8
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
9
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
10
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
11
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
10. Click on CD/DVD , choose ISO Image file and select image location
11. Click on Ok
12. Click Power on Virtual machine and wait
12
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
13. You will see two options : Try Ubuntu , and Install Ubuntu
14. Select Install Ubuntu option and follow instruction until finish
13
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
What is a shell
Managing the System
Navigating
Exploring the system
Manipulating files and directories
Working with commands
When we speak of the command line, we are really referring to the shell. The shell is a program
that takes keyboard commands and passes them to the operating system to carry out. Almost all
Linux distributions supply a shell program from the GNU Project called bash. The name bash
is an acronym for Bourne Again SHell, a reference to the fact bash is an enhanced replacement
for sh, the original Unix shell program written by Steve Bourne.
Terminal Emulators
When using a graphical user interface, we need another program called a terminal emulator
to interact with the shell. If we look through our desktop menus, we will probably find
one. KDE uses konsole and GNOME uses gnome-terminal, though it's likely called simply
terminal on our menu. There are a number of other terminal emulators available for Linux, but
they all basically do the same thing; give us access to the shell.
You will probably develop a preference for one or another based on the number of bells and
whistles it has. So let's get started. Launch the terminal emulator! Once it comes up, we should
see something like this:
This is called a shell prompt and it will appear whenever the shell is ready to accept input. While
it may vary in appearance somewhat depending on the distribution, it will usually include your
username@machinename, followed by the current working directory (more about that in a little
bit) and a dollar sign
Open command line console in graphical interface , windows named Terminal windows is provided in
linux . or press CRT+ALT+T
14
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Once the windows is clearly visible , you see first Username followed by @ symbol then computer name
that describes the directory you are working in
To change your default shell , type the name of the shell you want to use on the command line for
example , type csh at command prompt
Command syntax
The command of linux is uniform . it consist of three parts , in the order specified below
The top command provides a means of watching the currently running processes, with those
processes sorted by CPU usage or memory you can also use top to watch your memory usage in
a screen-oriented way. Here is an example:
$ top
For a more detailed view of your virtual memory statistics, use the vmstat command. With
vmstat you can view memory use over a given time period The following example shows vmstat
redisplaying statistics every three seconds:
$ vmstat 3
To exit vmstat, press Ctrl+c.
The dstat command (dstat package) is available as an alternative to iostat for viewing
information about your CPU usage (as well as other performance-related items)
Here is an example of dstat for displaying CPU information:
$ dstat -t -c 3 View CPU usage continuously with time stamps
you can use the top command. Type top, then press Shift+p to sort by CPU usage (this is the
default sorting order):
$ top Display running processes and sort by CPU usage
17
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
If you want information about the processor itself, you can view information directly from the
/proc/cpuinfo file. Here is an example:
$ cat /proc/cpuinfo View CPU information from /proc
Here is an example:
$ vmstat -p sda1 Display read/write stats for a disk partition
the cal command is a quick way to display dates by month. Here are
examples:
$ cal Show current month calendar (today is highlighted)
$ cal 2007 Show whole years calendar
The date command can also be used to change the system date and time. For example:
$ sudo date 081215212008 Set date/time to Aug. 12, 2:21PM, 2008
Tue Aug 12 11:42:00 CDT 2008
$ sudo date --set=+7 minutes Set time to 7 minutes later
Sun Aug 12 11:49:33 CDT 2008
$ sudo date --set=-1 month Set date/time to one month earlier
Straight to the Kernel
In general, when the kernel starts up on your Linux system
To find out what kernel is currently running on your system, type the following:
$ uname -r Display name of kernel release
2.6.20-16-generic
$ uname -a Display all available kernel info
Navigation
The first thing we need to learn (besides just typing) is how to navigate the file system on our
Linux system. In this section we will introduce the following commands:
pwd - Print name of current working directory
cd - Change directory
ls - List directory contents
18
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
To display the current working directory, we use the pwd (print working directory) command.
19
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Now that we know how to move around the file system, it's time for a guided tour of our
Linux system. Before we start however, were going to learn some more commands that will be
useful along the way:
ls List directory contents
file Determine file type
less View file contents
Besides the current working directory, we can specify the directory to list, like so:
The ls command has a large number of possible options. The most common are listed in Table 3-
1.
20
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
21
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
22
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
23
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
24
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
25
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Then type the text that you want to store in file and press CRL+D to down new line .
Appending contents using cat command
26
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
The mv command performs both file moving and file renaming, depending on how it is used. In
either case, the original filename no longer exists after the operation. mv is used in much the
same way as cp:
mv item1 item2
to move or rename file or directory item1 to item2 or:
mv item... directory
to move one or more items from one directory to another.
Useful Options And Examples
mv shares many of the same options as cp:
27
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
28
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
29
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Packing systems
How package system works
Common package management tasks
Package Management
Package management is a method of installing and maintaining software on the system.
Today, most people can satisfy all of their software needs by installing packages from
their Linux distributor. This contrasts with the early days of Linux, when one had to download
and compile source code in order to install software.
In this chapter, we will look at some of the command line tools used for package management.
While all of the major distributions provide powerful and sophisticated graphical programs for
maintaining the system, it is important to learn about the command line programs,
Packaging Systems
Different distributions use different packaging systems and as a general rule, a package intended
for one distribution is not compatible with another distribution. Most distributions fall into one of
two camps of packaging technologies: the Debian .deb camp and the Red Hat .rpm camp
Package Files
The basic unit of software in a packaging system is the package file. A package file is a
compressed collection of files that comprise the software package. A package may consist of
numerous programs and data files that support the programs
Common Package Management Tasks
There are many operations that can be performed with the command line package management
tools Several other Linux distributions also use packaging systems similar to APT. Red Hat
based/derived distributions (including CentOS, Fedora, and Mandriva) have tools such as yum,
rpm, urpmi, and smart to manage software. Although these tools
are quite different from the ones Ubuntu uses, the ideas are similar; a configuration file is set up
to tell the packaging tool where online to find the latest software packages.
The packaging tool then works in conjunction with an installer to get the software on the system.
This system of having an online package fetcher (so to speak) and a back-end packaging tool is a
very powerful combination to resolve dependency issues, digitally authenticate software
integrity, easily keep a system up to date, and allow distribution maintainers to distribute changes
simply and on a large scale
30
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
31
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
32
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
33
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
of doing more than one thing at once by rapidly switching from one executing program to
another. The Linux kernel manages this through the use of processes. Processes are how Linux
organizes the different programs waiting for their turn at the CPU. Sometimes a computer will
become sluggish or an application will stop responding. In this chapter, we will look at some of
the tools available at the command line that let us examine what programs are doing, and how to
terminate processes that are misbehaving.
This chapter will introduce the following commands:
ps Report a snapshot of current processes
top Display tasks
jobs List active jobs
kill Send a signal to a process
renice - changes the nice value of an already running process
killall Kill processes by name
shutdown Shutdown or reboot the system
The kernel maintains information about each process to help keep things organized. For
example, each process is assigned a number called a process ID or PID. PIDs are assigned in
ascending order, with init always getting PID 1. The kernel also keeps track of the memory
assigned to each process, as well as the processes' readiness to resume execution.
ps
The ps command lists running processes. The following command lists all processes running on
your system:
ps -A
34
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
This may be too many processes to read at one time, so you can pipe the output through the less
command to scroll through them at your own pace:
ps -A | less
You could also pipe the output through grep to search for a specific process without using any
other commands. The following command would search for the Firefox process:
ps -A | grep firefox
pstree
The pstree command is another way of visualizing processes. It displays them in tree format. So,
for example, your X server and graphical environment would appear under the display manager
that spawned them.
top
The top command is the traditional way to view your systems resource usage and see the
processes that are taking up the most system resources. Top displays a list of processes, with the
ones using the most CPU at the top.
To exit top or htop, use the Ctrl-C keyboard shortcut. This keyboard shortcut usually kills the
currently running process in the terminal.
htop
The htop command is an improved top. Its not installed by default on most Linux distributions
heres the command youll need to install it on Ubuntu:
htop displays the same information with an easier-to-understand layout. It also lets you select
processes with the arrow keys and perform actions, such as killing them or changing their
priority, with the F keys.
kill
The kill command can kill a process, given its process ID. You can get this information from the
ps -A, top or pgrep commands.
kill PID
35
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Technically speaking, the kill command can send any signal to a process. You can use kill -
KILL or kill -9 instead to kill a stubborn process.
pgrep
Given a search term, pgrep returns the process IDs that match it. For example, you could use the
following command to find Firefoxs PID:
pgrep firefox
The pkill and killall commands can kill a process, given its name. Use either command to kill
Firefox:
pkill firefox
killall firefox
renice
The renice command changes the nice value of an already running process. The nice value
determines what priority the process runs with. A value of -19 is very high priority, while a value
of 19 is very low priority. A value of 0 is the default priority.
The renice command requires a processs PID. The following command makes a process run
with very low priority:
renice 19 PID
You can use the pgrep trick above with renice, too.
If youre making a process run at a higher priority, youll require root permissions. On Ubuntu,
use sudo for that:
xkill
The xkill command is a way of easily killing graphical programs. Run it and your cursor will
turn into an x sign. Click a programs window to kill that program. If you dont want to kill a
program, you can back out of xkill by right-clicking instead.
You dont have to run this command from a terminal you can also press Alt-F2, type xkill and
press Enter to use it from a graphical desktop.
36
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Storage Media
In previous chapters weve looked at manipulating data at the file level. In this chapter, we will
consider data at the device level. Linux has amazing capabilities for handling storage devices,
whether physical storage, such as hard disks, or network storage, We will look at the following
commands:
mount Mount a file system
umount Unmount a file system
fsck Check and repair a file system
fdisk Partition table manipulator
mkfs Create a file system
(mkisofs) Create an ISO 9660 image file
wodim (cdrecord) Write data to optical storage media
Mounting And Unmounting Storage Devices
Recent advances in the Linux desktop have made storage device management extremely easy for
desktop users. The first step in managing a storage device is attaching the device to the file
system tree. This process, called mounting, allows the device to participate with the operating
system.
Viewing A List Of Mounted File Systems
The mount command is used to mount file systems. Entering the command without arguments
will display a list of the file systems currently mounted:
[me@linuxbox ~]$ mount
we become the superuser
(using the command appropriate for our system) and unmount the disc with the umount
(notice the spelling) command:
[me@linuxbox ~]$ su
[root@linuxbox ~]# umount /dev/hdc
The next step is to create a new mount point for the disk. A mount point is simply a directory
somewhere on the file system tree.
we will create a new directory:
[root@linuxbox ~]# mkdir /mnt/cdrom
Finally, we mount the CD-ROM at the new mount point. The -t option is used to specify
the file system type:
[root@linuxbox ~]# mount -t iso9660 /dev/hdc /mnt/cdrom
we can examine the contents of the CD-ROM via the new mount point:
37
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
We can easily remedy the issue by changing the working directory to something other than the
mount point:
[root@linuxbox cdrom]# cd
[root@linuxbox ~]# umount /dev/hdc
Command line tools such as mkisofs (for creating CD images) and cdrecord (for burning images
to CD or DVD) once provided the most popular interfaces for making backups to CD or DVD.
Creating Backup Images with mkisofs
Most data CDs and DVDs can be accessed on both Windows and Linux systems because they
are created using the ISO9660 standard for formatting the information on those discs.
Here are some examples of using mkisofs to create an ISO image from all files and directories
under the /home directory:
$ cd /tmp
$ sudo mkisofs -o home.iso /home Create basic ISO9660 image
$ sudo mkisofs -o home2.iso -J -R /home Add Joliet Rock Ridge extensions
$ sudo mkisofs -o home3.iso -J -R -hfs /home Also add HFS extensions
You can see a lot of the information entered on the mkisofs command line when the image was
created you can next try accessing files on the ISO image by mounting it:
$ sudo mkdir /mnt/myimage Create a mount point
$ sudo mount -o loop home.iso /mnt/myimage Mount the ISO in loopback
$ ls -l /mnt/myimage Check the ISO contents
$ sudo umount /mnt/myimage Unmount the image when done
Burning Backup Images with cdrecord
The cdrecord command is the most popular Linux command line tool for burning CD and DVD
images. After you have created an ISO image (as described earlier) or obtained one otherwise
(such as downloading an install CD or live CD from the
Internet), cdrecord makes it easy to put that image on a disc.
check that your drive supports CD/DVD
burning and determine the address of the drive. Use the --scanbus option to cdrecord to do that:
$ cdrecord --scanbus Shows a drive that cannot do burning
Writing An Image
To write an image, we again use wodim, specifying the name of the optical media writer device
and the name of the image file:
wodim dev=/dev/cdrw image.iso
39
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Sorting data
One popular function that comes in handy when working with large amounts of data is the sort
command. The sort command does what it says it sorts data.
By default, the sort command sorts the data lines in a text file using standard sorting rules for
the language you specify as the default for the session.
$ cat file1
There are several other handy sort parameters to use, shown in Table 4-7.
The -k and -t parameters are handy when sorting data that uses fields, such as the /etc/passwd
file. Use the -t parameter to specify the field separator character, and the -k parameter to specify
which field to sort on. For example, to sort the password file based on numerical userid, just
do this:
$ sort -t : -k 3 -n /etc/passwd
The -n parameter is great for sorting numerical outputs, such as the output of the du command:
$ du -sh * | sort nr
$ grep -e t -e f file1
Compressing data
Linux contains several file compression utilities. While this may sound great, it often leads to
confusion and chaos when trying to download files. Table 4-8 lists the file compression utilities
available for Linux.
The compress file compression utility is not often found on Linux systems. If you download a
file with a .Z extension, you can usually install the compress package (called ncompress in
many Linux distributions) and then uncompress the file with the uncompress command.
The bzip2 utility
The bzip2 utility is a relatively new compression package that is gaining popularity, especially
when compressing large binary files. The utilities in the bzip2 package are:
bzip2 for compressing files
bzcat for displaying the contents of compressed text files
bunzip2 for uncompressing compressed .bz2 files
bzip2recover for attempting to recover damaged compressed files
By default, the bzip2 command attempts to compress the original file, and replaces it with the
compressed file, using the same filename with a .bz2 extension:
$ ls -l myprog
-rwxrwxr-x 1 rich rich 4882 2007-09-13 11:29 myprog
$ bzip2 myprog
$ ls -l my*
-rwxrwxr-x 1 rich rich 2378 2007-09-13 11:29 myprog.bz2
To uncompress the file, just use the bunzip2 command:
$ bunzip2 myprog.bz2
$ ls -l myprog
The gzip utility
By far the most popular file compression utility in Linux is the gzip utility
This package includes the files:
gzip for compressing files
gzcat for displaying the contents of compressed text files
gunzip for uncompressing files
These utilities work the same way as the bzip2 utilities:
$ gzip myprog
41
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
$ ls -l my*
-rwxrwxr-x 1 rich rich 2197 2007-09-13 11:29 myprog.gz
The zip utility
The zip utility is compatible with the popular PKZIP package created by Phil Katz for MS-DOS
and Windows. There are four utilities in the Linux zip package:
zip creates a compressed file containing listed files and directories.
zipcloak creates an encrypted compress file containing listed files and directories.
zipnote extracts the comments from a zip file.
zipsplit splits a zip file into smaller files of a set size (used for copying large zip files to floppy
disks).
unzip extracts files and directories from a compressed zip file.
To see all of the options available for the zip utility, just enter it by itself on the command line:
$ zip
Archiving data
While the zip command works great for compressing and archiving data into a single file, its not
the standard utility used in the Unix and Linux worlds. By far the most popular archiving tool
used in Unix and Linux is the tar command.
The tar command was originally used to write files to a tape device for archiving. However, it
can also write the output to a file, which has become a popular way to archive data in Linux.
The format of the tar command is:
tar function [options] object1 object2 ...
The function parameter defines what the tar command should do, as shown in Table 4-9.
Each function uses options to define a specific behavior for the tar archive file.
This creates an archive file called test.tar containing the contents of both the test directory
and the test2 directory.
tar -tf test.tar
This lists (but doesnt extract) the contents of the tar file: test.tar.
tar -xvf test.tar
42
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Chapter 7: Networking
After completing this chapter you will be able to:
Linux is used to build all sorts of networking systems and appliances, including firewalls,routers,
name servers, NAS (Network Attached Storage) boxes and on and on.
Just as the subject of networking is vast, so are the number of commands that can be used to
configure and control it. We will focus our attention on just a few of the most frequently used
ones. The commands chosen for examination include those used to monitor networks and those
used to transfer files. In addition, we are going to explore the ssh program that is used to perform
remote logins. This chapter will cover:
ping - Send an ICMP ECHO_REQUEST to network hosts
traceroute - Print the route packets trace to a network host
netstat - Print network connections, routing tables, interface statistics, masquerade
connections, and multicast memberships
ftp - Internet file transfer program
wget - Non-interactive network downloader
ssh - OpenSSH SSH client (remote login program)
ping
ping sends ECHO_REQUEST packets to the address you specify. Its a great way to see whether
your computer can communicate with the Internet or a specific IP address. Bear in mind that
many systems are configured not to respond to pings, however.
Unlike the ping command on Windows, the Linux ping command will keep sending packets until
you terminate it. You can specify a finite amount of packets with the -c switch.
ping -c 4 google.com
The tracepath command is similar to traceroute, but it doesnt require root privileges. Its also
installed by default on Ubuntu, while traceroute isnt. tracepath traces the network path to a
destination you specify and reports each hop along the path. If youre having network
problems or slowness, tracepath can show you where the network is failing or where the
slowness is occurring.
tracepath example.com
43
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
mtr
The mtr command combines ping and tracepath into a single command. mtr will continue to
send packets, showing you the ping time to each hop. This will also show you any problems
in this case, we can see that hop 6 is losing over 20% of the packets.
mtr howtogeek.com
host
The host command performs DNS lookups. Give it a domain name and youll see the associated
IP address. Give it an IP address and youll see the associated domain name.
host howtogeek.com
host 208.43.115.82
whois
The whois command will show you a websites whois records, so you can view more
information about who registered and owns a specific website.
whois example.com
ifplugstatus
The ifplugstatus command will tell you whether a cable is plugged into a network interface or
not. It isnt installed by default on Ubuntu. Use the following command to install it:
Run the command to see the status of all interfaces or specify a specific interface to view its
status.
ifplugstatus
ifplugstatus eth0
Link beat detected means the cable is plugged in. Youll see unplugged if it isnt.
ifconfig
The ifconfig command has a variety of options to configure, tune, and debug your systems
network interfaces. Its also a quick way to view IP addresses and other network interface
information. Type ifconfig to view the status of all currently active network interfaces, including
44
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
their names. You can also specify an interfaces name to view only information about that
interface.
ifconfig
ifconfig eth0
The ifdown and ifup commands are the same thing as running ifconfig up or ifconfig down.
Given an interfaces name, they take the interface down or bring it up. This requires root
permissions, so you have to use sudo on Ubuntu.
Try this on a Linux desktop system and youll probably get an error message. Linux desktops
usually use NetworkManager, which manages network interfaces for you. These commands will
still work on servers without NetworkManager, though.
If you really need to configure NetworkManager from the command line, use the nmcli
command.
dhclient
The dhclient command can release your computers IP address and get a new one from your
DHCP server. This requires root permissions, so use sudo on Ubuntu. Run dhclient with no
options to get a new IP address or use the -r switch to release your current IP address.
sudo dhclient -r
sudo dhclient
netstat
The netstat command can show a lot of different interface statistics, including open sockets and
routing tables. Run the netstat command with no options and youll see a list of open sockets.
Theres a lot more you can do with this command. For example, use the netstat -p command to
view the programs associated with open sockets.
Transferring Files
Commands in Linux for downloading files from remote servers (HTTP, HTTPS, FTP, or SSH)
are plentiful and powerful. You might choose one command over another because of the specific
options you need. For example, you may want to perform a download over an encrypted
connection, resume an aborted download, or do recursive downloads. This section describes how
to use wget, ftp, lftp, scp, and scftp.
45
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
The wget command can download files from web servers (HTTP and HTTPS) and
FTP servers. With a server that doesnt require authentication, a wget command can
be as simple as the wget command and the location of the download file:
$ wget https://help.ubuntu.com/7.04/common/img/headerlogo.png
If, for example, an FTP server requires a login and password, you can enter that information on
the wget command line in the following forms:
$ wget ftp://user:password@ftp.example.com/path/to/file
$ wget --user=user --password=password ftp://ftp.example.com/path/to/file
For example:
$ wget ftp://chris:mykuulpwd@ftp.linuxtoys.net/home/chris/image.jpg
$ wget -user=chris -password=mykuulpwd \
ftp://ftp.linuxtoys.net/home/chris/image.jpg
You can use wget to download a single web page as follows:
$ wget http://www.wiley.com Download only the Web page
The following sample lftp session illustrates useful commands when downloading:
$ lftp mirrors.kernel.org
lftp mirrors.kernel.org:~> pwd Check current directory
ftp://mirrors.kernel.org
lftp mirrors.kernel.org:~> ls List current directory
Here are some useful commands during an authenticated lftp upload session. This assumes you
have the necessary file permissions on the server:
46
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
$ lftp chris@example.com
Password: *******
The lftp session illustrates how you can use shell command names to operate on remote
directories (provided you have permission)
Using SSH Tools to Transfer Files
Because SSH utilities are among the most important tools in a system administrators arsenal of
communications commands In particular, the scp command will do most of what you need to get
a file from one computer to another, while making that communication safe by encrypting both
the password stage and data transfer stage of the process. The ssh command replaces the rcp
command as the most popular tool for host-to-host file copies.
Copying Remote Files with scp
To use scp to transfer files, the SSH service (usually the sshd server daemon) must be running on
the remote system. Here are some examples of useful scp commands:
$ scp myfile francois@server1:/tmp/ Copy myfile to server1
Password: ******
$ scp server1:/tmp/myfile . Copy remote myfile to local working dir
Password: ******
Use the -p option to preserve permissions and timestamps on the copied files:
$ scp -p myfile server1:/tmp/
If the SSH service is configured to listen on a port other than the default port 22, use
-P to indicate that port on the scp command line:
$ scp -P 12345 myfile server1:/tmp/ Connect to a particular port
To do recursive copies, from a particular point in the remote file system, use the
-r option:
$ scp -r mydir francois@server1:/tmp/ Copies all mydir to remote /tmp
Copying Remote Files in sftp and lftp Sessions
The sftp command lets you use an FTP-like interface to find and copy files over SSH protocols.
Heres an example of how to start an sftp session:
$ sftp chris@server1
chris@server1s password: *****
sftp>
Use sftp in the same manner as you use regular FTP clients. Type ? for a list of commands.
You can change remote directories (cd), change local directories (lcd), check current remote and
local directories (pwd and lpwd), and list remote and local contents (ls and lls).
To add an existing Linux user as a Samba user, use the smbpasswd command:
$ sudo smbpasswd -a francois
New SMB password: ******
To list services offered by a server to an anonymous user, type the following:
$ smbclient -L server
Password: ******
To connect to a Samba share FTP-style, type the following:
$ smbclient //192.168.1.1/myshare -U francois
Password:
Managing E-mail with mutt
If you want to use a command line mail client on an ongoing basis, we recommend you use mutt
instead of mail. The mail command has many limitations, such as not being able to send
attachments without encoding them in advance (such as with the uuencode command), while
mutt has many features for handling modern e-mail needs. The mutt command is part of the mutt
package, which you need to install to use this command. Configure mutt by editing /etc/Muttrc.
You also need to configure sendmail to allow for sending e-mail. Like mail, mutt can also be
used to pop off a message from a script. mutt also adds the capability to send attachments. For
example:
$ mutt -s My Linux Version -a /etc/lsb-release \
chris@example.com < email-body.txt
$ mutt -s My Linux Version -a /etc/lsb-release \
chris@example.com < /dev/null
You can begin your mutt mail session (assuming your default mailbox is $MAIL) by simply
typing mutt:
$ mutt
/home/chris/Mail does not exist. Create it? ([yes]/no): y
Configuring SSH
Nowadays, the Swiss Army knife of remote system administration is Secure Shell (SSH).
SSH commands and services replace all the old remote tools and add strong encryption, public
keys, and many other features.
To install the OpenSSH server, run the following command:
$ sudo apt-get install openssh-server
48
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
For security purposes, a remote host may have its SSH service listening a different port than the
default port number 22. If thats the case, use -p option to ssh to contact that service:
$ ssh -p 12345 francois@turbosphere.com Connect to SSH on port 12345
The following sequence of commands illustrates starting an SSH session, then starting a few X
applications so they appear on the local desktop:
$ ssh francois@myserver Start ssh connection to myserver
francois@myserver's password: *******
[francois@myserver ~}$ echo $DISPLAY Show the current X display entry
localhost:10.0 SSH sets display to localhost:10.0
[francois@myserver ~}$ xeyes& Show moving desktop eyes
[francois@myserver ~}$ gnome-cups-manager& Configure remote printers
[francois@myserver ~}$ gksu services-admin& Change system services
49
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Linux Security
The core of the Linux security system is the user account. Each individual who accesses a Linux
system should have a unique user account assigned .The permissions users have to objects on the
system depend on the user account they log in with. User permissions are tracked using a user ID
(often called a UID), which is assigned to an account when its created. The UID is a numerical
value, unique for each user. However, you dont log in to a Linux system using your UID.
Instead, you use a login name. The login name is an alphanumeric text string of eight characters
or fewer that the user uses to log in to the system (along with an associated password).
The Linux system uses special files and utilities to track and manage user accounts on the
system. Before we can discuss file permissions, we need to iscuss how Linux handles user
accounts. This section describes the files and utilities required for user accounts so that you can
understand how to use them when working with file permissions.
The /etc/passwd file
The Linux system uses a special file to match the login name to a corresponding UID value. This
file is the /etc/passwd file. The /etc/passwd file contains several pieces of information about the
user. Heres what the /etc/passwd file looks like on my Linux system:
$ cat /etc/passwd
The root user account is the administrator for the Linux system and is always assigned UID 0. As
you can see, the Linux system creates lots of user accounts for various functions that arent
actual users. These are called system accounts.
You probably noticed that the /etc/passwd file contains lots more than just the login name and
UID for the user. The fields of the /etc/passwd file contain the following information:
The login username
The password for the user
The numerical UID of the user account
The numerical group ID (GID) of the user account
50
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
You can test this by creating a new user account using the default system parameters and then
looking at the HOME directory for the new user:
# /usr/sbin/useradd test
# ls -al /home/test
Removing a user
If you want to remove a user from the system, the userdel command is what you need. By
default, the userdel command only removes the user information from the /etc/passwd file. It
doesnt remove any files the account owns on the system.
If you use the -r parameter, userdel will remove the users HOME directory, along with the
users mail directory.
Heres an example of using the userdel command to remove an existing user account:
# /usr/sbin/userdel -r test
# ls -al /home/test
Modifying a user
Linux provides a few different utilities for modifying the information for existing user accounts.
Table 6-3 shows these utilities.
51
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Each of these utilities provides a specific function for changing information about user accounts.
The following sections describe each of these utilities.
usermod
The usermod command is the most robust of the user account modification utilities. It provides
options for changing most of the fields in the /etc/passwd file.
there are a couple of additional parameters that might come in handy:
-l to change the login name of the user account
-L to lock the account so the user cant log in
-p to change the password for the account
-U to unlock the account so that the user can log in
The -e option is a handy way to force a user to change the password on the next log in
The chfn command provides a standard method for storing information in the comments field in
the /etc/passwd file.
# finger rich
Login: rich Name: Rich Blum
Directory: /
52
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
# chfn test
One neat feature of the chage command is that it allows you to set an expiration date for an
account. Using this feature, you can create temporary user accounts that automatically expire on
a set date,
The chage Command Parameters
Parameter Description
-d Set the number of days since the password was last changed.
-E Set the date the password will expire.
-I Set the number of days of inactivity after the password expires to lock the account.
-m Set the minimum number of days between password changes.
-W Set the number of days before the password expires that a warning message appears.
Using Linux Groups
User accounts are great for controlling security for individual users, but they arent so good at
allowing groups of users to share resources. To accomplish this, the Linux system uses another
security concept, called groups. Group permissions allow multiple users to share a common set
of permissions for an object on the system, such as a file, directory, or device (more on that later
in the Decoding File Permissions section).
Each group has a unique GID, which, like UIDs, is a unique numerical value on the system.
Along with the GID, each group has a unique group name. There are a few group utilities you
can use to create and manage your own groups on the Linux system.
The /etc/group file
Just like user accounts, group information is stored in a file on the system. The /etc/group file
contains information about each group used on the system. Here are a few examples from the
/etc/group file on my Linux system:
The /etc/group file uses four fields:
The group name
The group password
The GID
The list of user accounts that belong to the group
Modifying groups
As you can see from the /etc/group file, there isnt too much information about a group for you
to modify. The groupmod command allows you to change the GID (using the -g parameter) or
the group name (using the -n parameter) of an existing group:
53
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
With the groupmod command, you can change the name or group ID of an existing group.
Here are examples:
$ sudo groupmod -g 491 myadmin Modify myadmin to use GID 491
$ sudo groupmod -n myad myadmin Change name of myadmin group to myad
Checking on Users
After you have created user accounts, and let those users loose on your computer,
there are several different commands you can use to keep track of how they are using
your computer.
Here are examples of commands for getting information
about people logging into your system:
$ last List the most recent successful logins
Here are some commands for finding out more about individual users on your system:
$ id Your identity (UID, GID and group for current shell)
After that, there are three sets of three characters. Each set of three characters defines an access
permission triplet:
r for read permission for the object
w for write permission for the object
x for execute permission for the object
54
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
The touch command created the file using the default permissions assigned to my user account.
The umask command shows and sets the default permissions:
$ umask
0022
You can specify a different default umask setting using the umask command:
$ umask 026
$ touch newfile2
$ ls -l newfile2
55
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Changing permissions
The chmod command allows you to change the security settings for files and directories. The
format of the chmod command is:
chmod options mode file
The first group of characters defines to whom the new permissions apply:
u for the user
g for the group
o for others (everyone else)
a for all of the above
Changing ownership
Sometimes you need to change the owner of a file, such as when someone leaves an organization
or a developer creates an application that needs to be owned by a system account when its in
production. Linux provides two commands for doing that. The chown command to makes it easy
to change the owner of a file, and the chgrp command allows you to change the default group of
a file.
The format of the chown command is:
chown options owner[.group] file
You can specify either the login name or the numeric UID for the new owner of the file:
# chown dan newfile
# ls -l newfile
Simple. The chown command also allows you to change both the user and group of a file:
# chown dan.dan newfile
# ls -l newfile
-rw-rw-r-- 1 dan dan 0 Sep 20 19:16 newfile*
The chgrp command provides an easy way to change just the default group for a file or directory:
$ chgrp shared newfile
$ ls -l newfile
56
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
Chapter 9: printing
After completing this chapter you will be able to:
After spending the last couple of chapters manipulating text, its time to put that text on paper. In
this chapter, well look at the command-line tools that are used to print files and control printer
operation. We wont be looking at how to configure printing, as that varies from distribution to
distribution and is usually set up automatically during installation. Note that we will need a
working printer configuration to perform the exercises in this chapter. We will discuss the
following commands:
prConvert text files for printing.
lprPrint files.
lpPrint files (System V).
a2psFormat files for printing on a PostScript printer.
lpstatShow printer status information.
lpqShow printer queue status.
lprmCancel print jobs.
cancelCancel print jobs (System V).
57
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
58
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
59
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali
60
Bosaso University Faculty of Computer Science