Sie sind auf Seite 1von 60

Lecturer notes Eng.

Roble Mohamed Ali

Chapter 1: Introductory of Linux


After completing this chapter you will be able to:

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

Linux BASIC FEATURES


Linux has a number of features, mostly good and some bad, but it is necessary to know at least
some of them:
1. UNIX is Portable: Portability is the ability of the software that operates on one machine to
operate on another, different machine.
2. Open system: The system that more than one system can access the same data at the same
time. Or distributed by network.
3. open source : free and available on the internet
4. Multi-user Capability: This means that more than one system can access the same data at the
same time. Several people involved in a common project can conveniently access each others
data. Apart from data, other resources like memory(RAM), the
CPU(the chip), or the hard disk can be used by many users simultaneously.
4. multi-users : one or more users can access at the same time
5. Multi-tasking Capability: Multi-tasking means that a user can perform more than one tasks
at the same time. A user can do this by placing some tasks in the background while he works on
a task in the foreground. An ampersand(&) placed at the end of the command line sends the
process in background

6. Reliability: biggest servers on this planet running on Linux .


.
7. Linux has built in networking: The Linux has various built in programs and utilities like
UUCP, mail, write, etc. With these utilities one can communicate with other user or one server to
another.
8. Security: Computer system security means protecting hardware and the information contained
within the system. Security means to avoid:
a) Unauthorized access to the computer system.
b) Unauthorized examination of output.
9. Software Development Tools: Linux offers an excellent variety of tools for software
development for all phases, from program editing to maintenance of software.

BASIC ARCHITECTURE OF UNIX/LINUX SYSTEM

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

AIX- IBM : uses mainframe computers


BSD/- : commercial version
HP UX : for business servers

3
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Solaries ; sun Microsystems for web servers

The most popular Linux Distributions


1. Ubuntu : is the most well-known linux distribution based on Debian .
2. Linux mint : is a Linux distribution built on top of Ubuntu
3. Debian is Os composed only of free , open source
4. Federa is responsored by Redhat
5. Cent Os : is community project and available for free
6. Open use /SUSE Enterprise ::is community created Linux distribution sponsored by
Novell

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

A brief history of Ubuntu


Ubuntu was conceived in 2004 by Mark, a successful South African entrepreneur, and his
company Canonical. Shuttle worth recognized the power of Linux and open source, but was also
aware of weaknesses that prevented mainstream use Shuttleworth set out with clear intentions to
address these weaknesses and create a system that was easy to use, completely free , and could
compete with other mainstream operating systems. With the Debian system as a base, Shuttle
worth began to build Ubuntu. Using his own funds at first.
installation CDs were pressed and shipped worldwide at no cost to the recipients. Ubuntu spread
quickly, its community grew rapidly, and soon Ubuntu became the most popular Linux
distribution available.
With more people working on the project than ever before, its core features
and hardware support continue to improve, and Ubuntu has gained the attention of large
organizations worldwide. One of IBMs open source operating systems is based on Ubuntu. In
2005, the French Police began to transition their entire computer infrastructure to a variant of
Ubuntua process which has reportedly saved them millions of euros in licensing fees for
Microsoft Windows. By the end of 2012, the French Police anticipates that all of their computers
will be running Ubuntu. Canonical profitsfrom this arrangement by providing technical support
and custom-built software .While large organizations often find it useful to pay for support
services, Shuleworth has promised that the Ubuntu desktop operating system will always be
free, As of 2012, Ubuntu is installed on an estimated 2% of the worlds computers. this equates
to tens of millions of users worldwide, and is growing each year. As there is no compulsory
registration, the percentage of Ubuntu users should be treated as an estimate.

4
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Why Use Ubuntu?

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.

It has no major viruses! Ubuntu is immune to computer-crashing Windows viruses. Say


goodbye to Blue Screens of Death!

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.

Ubuntu Version Numbers

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.

Normal Releases vs. Long Term Support (LTS) Releases

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

Different Ways To Install Ubuntu

There are three different ways you can install Ubuntu on to your computer:

1.Download & Install It

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.

For step-by-step instructions on how to do this, please visit:

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.

2.Try It On A CD Or USB Stick

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.

For step-by-step instructions on how to do this, please visit:

http://www.ubuntu.com/download/ubuntu/download

3.Run It alongside Windows Or Mac OS

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

Installing Ubuntu Alongside Windows 7 using Workstation

1. Download Ubuntu from official website


2. Install VM machine on your computer
3. Start the program

4. Choose Typical option

8
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

5. Select last option

6. Choose the operating system you want to install from list .

9
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

7. You will see Ubuntu name and its location

8. Specify disk capacity ( optional )

10
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

9. Click on Edit Virtual machine settings from left side

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

Chapter 2: Starting with Linux Shells


After completing this chapter you will be able to:

What is a shell
Managing the System
Navigating
Exploring the system
Manipulating files and directories
Working with commands

What Is The Shell?

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

Start up the Terminal

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

Listing the shells available on the system

Determining the default shell

Changing current shell

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

1. Name : It is command name eg: ls , echo


15
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

2. Options : it is possible to alter the behavior of the command


3. Argument : a long with options user can also provide argument .

Ending A Terminal Session


We can end a terminal session by either closing the terminal emulator window, or by entering the
exit command at the shell prompt.
Working with Commands
Up to this point, we have seen a series of mysterious commands, each with its own mysterious
options and arguments. In this section , we will attempt to remove some of that mystery and even
create some of our own commands. The commands introduced in this section are:
type Indicate how a command name is interpreted
which Display which executable program will be executed
help Get help for shell builtins
man Display a command's manual page
apropos Display a list of appropriate commands
whatis Display a very brief description of a command
type Display A Command's Type
The type command is a shell builtin that displays the kind of command the shell will
execute, given a particular command name. It works like this:
type command where command is the name of the command you want to examine. Here are
some examples:
[me@linuxbox ~]$ type type
type is a shell builtin
[me@linuxbox ~]$ type ls
ls is aliased to `ls --color=tty'
[me@linuxbox ~]$ type cp
cp is /bin/cp
which Display An Executable's Location
Sometimes there is more than one version of an executable program installed on a system.
[me@linuxbox ~]$ which ls
/bin/ls
help Get Help For Shell Builtins
bash has a built-in help facility available for each of the shell builtins. To use it, type
help followed by the name of the shell builtin. For example:
[me@linuxbox ~]$ help cd
man Display A Program's Manual Page
Most executable programs intended for command line use provide a formal piece of
documentation called a manual or man page. A special paging program called man is used to
view them. It is used like this:
man program
As an example, let's try viewing the man page for the ls command:
[me@linuxbox ~]$ man ls
apropos Display Appropriate Commands
It is also possible to search the list of man pages for possible matches based on a search term. It's
very crude but sometimes helpful. Here is an example of a search for man pages using the search
term floppy:
[me@linuxbox ~]$ apropos floppy
16
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

whatis Display A Very Brief Description Of A Command


The whatis program displays the name and a one line description of a man page matching a
specified keyword:
[me@linuxbox ~]$ whatis ls ls (1) - list directory contents
Monitoring Memory Use
The free command provides the quickest way to see how much memory is being used on your
system.
$ free List memory usage in kilobytes (-k default)
$ free -m List memory usage in megabytes
$ free -b List memory usage in blocks
$ free -mt List memory usage with totals displayed (Swap + Mem)
free -g List memory usage in gigabytes

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.

Here are some other options for using vmstat:


$ vmstat -S m Display output in 1000k megabytes
$ vmstat -S M Display output in 1024k megabytes
$ vmstat -S k Display output in 1000-byte kilobytes
$ vmstat -S K Display output in 1024-byte kilobytes
$ vmstat -n 2 10 Output every two seconds, repeat 10 times
$ vmstat -s | less Display event counters and memory statistics
$ vmstat -S M -s | less Display statistics in megabytes

Monitoring CPU Usage


Here are two examples of using iostat to display a CPU utilization report:
$ iostat -c 3 CPU stats every 3 seconds (starting apps)
iostat -c 3 CPU stats every 3 seconds (copying files)

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

Monitoring Storage Devices


Basic information about storage space available to your Linux file systems can be seen using
commands such as du and df Some of the same kind of output from the iostat command shown
earlier can be used to tell if bottlenecks occur while doing disk reads and writes. Heres an
example:
$ iostat 3 Check disk reads and writes per disk
The vmstat command can also list statistics about your disks. Heres an example of using vmstat
to list information about disk reads and writes:
$ vmstat d

Here is an example:
$ vmstat -p sda1 Display read/write stats for a disk partition

Displaying and Setting Your System Clock


Here are examples of date commands for displaying dates and times in different ways:

date Display current date, time and time zone


Sun Aug 12 01:26:50 CDT 2007
$ date +%A %B %d %G Display day, month, day of month, year

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

Understanding the File System Tree


Like Windows, a Unix-like operating system such as Linux organizes its files in what is called a
hierarchical directory structure. This means that they are organized in a tree-like pattern of
directories (sometimes called folders in other systems), which may contain files and other
directories. The first directory in the file system is called the root directory. The root directory
contains files and subdirectories, which contain more files and subdirectories and so on and so
on.

To display the current working directory, we use the pwd (print working directory) command.

Listing The Contents Of A Directory


To list the files and directories in the current working directory, we use the ls command.

Changing The Current Working Directory


To change your working directory (where we are standing in our tree-shaped maze) we use the
cd command. To do this, type cd followed by the pathname of the desired working directory.

Some Helpful Shortcuts


In Table 2-1 we see some useful ways the current working directory can be quickly changed.

19
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Exploring The System

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

Determining A File's Type With file


As we explore the system it will be useful to know what files contain. To do this we will
use the file command to determine a file's type

Viewing File Contents With less


The less command is a program to view text files. Throughout our Linux system, there
are many files that contain human-readable text. The less program provides a convenient way to
examine them.
The less command is used like this:

21
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Summary of Exploring The System


1. cd into a given directory
2. List the directory contents with ls -l
3. If you see an interesting file, determine its contents with file
4. If it looks like it might be text, try viewing it with less

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

Manipulating Files and Directories


At this point, we are ready for some real work! This section will introduce the following
commands:
cp Copy files and directories
mv Move/rename files and directories
mkdir Create directories
Cat create files
rm Remove files and directories
ln Create hard and symbolic links
These five commands are among the most frequently used Linux commands. They are
used for manipulating both files and directories.
Manipulating Files And Directories
mkdir Create Directories
The mkdir command is used to create directories. It works like this:

Cat create files


Type the command cat followed by greater than > symbol and name of the file as show :

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

Concatenating multiple files using cat command

Display file contents or open file


$ cat file name
Example : $ cat introduction .text
cp Copy Files And Directories
The cp command copies files or directories. It can be used two different ways:
cp item1 item2
to copy the single file or directory item1 to file or directory item2 and:
cp item... directory
to copy multiple items (either files or directories) into a directory.

Useful Options And Examples


Here are some of the commonly used options (the short option and the equivalent long
option) for cp:

26
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

mv Move And Rename Files

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

rm Remove Files And Directories


The rm command is used to remove (delete) files and directories:
rm item... where item is one or more files or directories.
Useful Examples

28
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Moving And Renaming Files


to move it from dir1 to dir2, then:
[me@linuxbox playground]$ mv dir1/fun dir2

29
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Chapter 3: Package management


After completing this chapter you will be able to:

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

Managing Software with APT


Although dpkg and APT work in conjunction with each other, most of the time, APT will suffice
for any software you need to install, download, upgrade, check, or search for on any Debian
system. Table 2-4 shows how to perform different tasks using the apt commands. For a quick
command line reference of apt capabilities, use the h option on the command line.

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

Update Packages with APT


Next, update the APT package cache to refresh the new repository. This is done using sudo and
running
apt-get update. Make sure to check for the Google repository as it scrolls by:
$ sudo apt-get update
Finding Packages with APT
Now that a new repository is set up, you can query for new software you can add:
$ apt-cache search picasa
You can also ask APT to show info about this Picasa package:
$ apt-cache show picasa
Installing Packages with APT
You can now install, using sudo, any available software authored by Google for Ubuntu, using
APT or another package tool. This shows Picasa being installed using APT:
$ sudo apt-get install picasa
Upgrading Packages with APT
Over time, packages change, and new versions add neat new features and fix problems.
You can use APT to upgrade your system to new versions following a two step process.
First, check for updates to the packages your Ubuntu system knows about using the update
option to apt-get:
$ sudo apt-get update
Second, upgrade the packages on your Ubuntu system by using the upgrade option to apt-get.
$ sudo apt-get upgrade
Upgrading a Single Package with APT
Upgrading a single system package on Ubuntu is pretty straightforward using apt-get
install <package>.
Example
$ sudo apt-get install minicom
Removing Packages with APT
You can remove a package from your Ubuntu system by giving apt-get the remove
option. You will be prompted to confirm before actually removing the software:
$ sudo apt-get remove picasa
Cleaning Up Packages with APT
After your initial installation of an Ubuntu release, Ubuntu keeps downloaded packages cached
in /var/cache/apt/ to speed up downloading if you ever need them again.
$ sudo apt-get clean
$ find /var/cache/apt/ -name \*.deb

33
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Chapter 4: Checking and Managing Running Processes


After completing this chapter you will be able to:

Listing Active Processes


Viewing Active Processes with ps
Watching Active Processes with top
Finding and Controlling Processes
Using pgrep to Find Processes
Using fuser to Find Processes
Changing Running Processes

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

How A Process Works


When a system starts up, the kernel initiates a few of its own activities as processes and launches
a program called init. init, in turn, runs a series of shell scripts (located in
/etc) called init scripts, which start all the system services. Many of these services are
implemented as daemon programs, programs that just sit in the background and do their thing
without having any user interface. So even if we are not logged in, the system is at least a little
busy performing routine stuff.

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

Press q to exit when youre done.

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:

sudo apt-get install htop

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

pkill & killall

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:

sudo renice -19 #

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

Chapter 5 : Storage media management


After completing this chapter you will be able to:

Mounting and un mounting storage device


Creating new file system
Testing and repairing file systems
Moving data directly to and from devices
Creating CD-ROM image
Writing CD-ROM IMAGE

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

[root@linuxbox ~]# cd /mnt/cdrom


[root@linuxbox cdrom]# ls

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

Determining Device Names


[me@linuxbox ~]$ ls /dev
The contents of this listing reveal some patterns of device naming

[me@linuxbox ~]$ sudo tail -f /var/log/messages


press Ctrl-c to get the prompt back.

Manipulating Partitions With fdisk


The fdisk program allows us to interact directly with disk-like devices (such as hard
disk drives and flash drives) at a very low level. With this tool we can edit, delete, and
create partitions on the device.
[me@linuxbox ~]$ sudo umount /dev/sdb1
[me@linuxbox ~]$ sudo fdisk /dev/sdb

Creating A New File System With mkfs


we will use mkfs (short for make file system), which can create file systems in a variety of
formats. To create an ext3 file system on the device, we use the -t option to specify the ext3
system type, followed by the name of the device containing the partition we wish to format:
38
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

[me@linuxbox ~]$ sudo mkfs -t ext3 /dev/sdb1


Testing And Repairing File Systems
fsck can also repair corrupt file systems with varying degrees of success, depending on the
amount of damage.
[me@linuxbox ~]$ sudo fsck /dev/sdb1

Backing Up to Removable Media

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

Chapter 6: Working with Data Files


After completing this chapter you will be able to:

Sorting data. Or Redirections


Searching for data.
Compressing data.
Archiving data

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

Searching for data


Often in a large file you have to look for a specific line of data buried somewhere in the middle
of the file. Instead of manually scrolling through the entire file, you can let the grep command
search for you. The command line format for the grep command is:
grep [options] pattern [file]
The grep command searches either the input or the file you specify for lines that contain
characters that match the specified pattern. The output from grep is the lines that contain the
matching pattern.
Here are two simple examples of using the grep command with the file1 file used in the
Sorting data section:
$ grep three file1 three- means 3 lines
$ grep t file1 - mean matching T
two
three
If you just need to see a count of how many lines contain the matching pattern, use the -c
parameter:
$ grep -c t file1
2
$
If you need to specify more than one matching pattern, use the -e parameter to specify each
individual pattern:
40
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

$ 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:

Examining and monitoring network


Transporting files over network
Secure communication with Remote Hosts

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

tracepath & traceroute

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

Press q or Ctrl-C to quit when youre done.

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:

sudo apt-get install ifplugd

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

ifdown & ifup

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.

sudo ifdown eth0


sudo ifup eth0

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.

View detailed statistics for all ports with netstat -s.

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

Downloading Files with wget


Sometimes you need to download a file from a remote server using the command line

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

Transferring files with FTP Commands


Ubuntu comes with the standard FTP client (ftp command), that works the same way
it does on most UNIX and Windows systems. We recommend you use the full-featured,
user-friendly lftp instead.
With these FTP clients, you open a session to the FTP server (as opposed to just grabbing
a file, as you do with wget and curl). Then you navigate the server much as you would a local
file system, getting and putting documents across the network connection.
Here are examples of how to connect to an FTP server with lftp:
$ lftp mirrors.kernel.org Anonymous connection
lftp mirrors.kernel.org:~>
$ lftp francois@example.com Authenticated connection
lftp example.com:~>
$ lftp -u francois example.com Authenticated connection
Password: ******
lftp example.com:~>
$ lftp -u francois,Mypwd example.com Authentication with password
lftp example.com:~>
$ lftp Start lftp with no connection
lftp :~> open mirrors.kernel.org Start connection in lftp session
lftp mirrors.kernel.org:~>

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).

Sharing Remote Directories with Samba


Samba is the open source implementation of the Windows file and print sharing protocol
originally known as Server Message Block (SMB) and now called Common Internet
File System (CIFS). There is an implementation of Samba in Linux, as well as in many other
operating systems. To use samba, install the packages samba and samba-doc.
Viewing and Accessing Samba Shares
To scan your network for SMB hosts, type the following:
$ findsmb
To view a text representation of your network neighborhood (shared directories and printers), use
smbtree:
$ sudo smbtree
Password: ******
47
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

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

Here are a few facts about SSH:


In Ubuntu, run /etc/init.d/ssh start to start the SSH service (sshd daemon). To configure the
service, edit the /etc/ssh/sshd_config file.
To configure the ssh client, edit the /etc/ssh/ssh_config file.

SSH Tunneling is an excellent way to securely use remote graphical tools!


Logging in Remotely with ssh
To securely log in to a remote host, you can use either of two different syntaxes to specify
the user name:
$ ssh -l francois myserver
$ ssh francois@myserver
Accessing SSH on a Different Port

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

Tunneling to an Internet Service


Another example for using SSH tunneling is when your local machine is blocked from
connecting to the Internet
$ ssh -L 12345:google.com:80 myserver
With this example, any connection to the local port 12345 is directed across an SSH tunnel to
myserver, which in turn opens a connection to Google.com port 80.
You can now browse to http://localhost:12345 and use myserver as a relay to the Google.com
web site you can add the N option to prevent the execution of remote commands:
$ ssh -L 12345:google.com:80 N myserver

49
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

Chapter 8: Linux Security


After completing this chapter you will be able to:

Working with Users and Groups


Managing Users the GUI Way
Adding User Accounts
Modifying User Accounts
Deleting User Accounts
Managing Passwords
Adding Groups
Modifying groups
Using file permission symbols
Default file permissions
Changing Security Settings
Changing ownership

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

A text description of the user account (called the comment field)


The location of the HOME directory for the user
The default shell for the user

The /etc/shadow file


The /etc/shadow file provides more control over how the Linux system manages passwords.
Only the root user has access to the /etc/shadow file, making it more secure than the /etc/ passwd
file.
Adding a new user
The primary tool used to add new users to your Linux system is useradd. This command
provides an easy way to create a new user account and set up the users HOME directory
structure all at once. The useradd command uses a combination of system default values and
command line parameters to define a user account. To see the system default values used on your
Linux distribution, enter the useradd command with the -D parameter:
# /usr/sbin/useradd D

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

passwd and chpasswd


A quick way to change just the password for a user is the passwd command:
# passwd test
Changing password for user test.

The -e option is a handy way to force a user to change the password on the next log in

chsh, chfn, and chage


The chsh, chfn, and chage utilities are specialized for specific functions. The chsh command
allows you to quickly change the default login shell for a user. You must use the full pathname
for the shell, and not just the shell name:
# chsh -s /bin/csh test
Changing shell for test.
Shell changed.

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

Creating new groups


The groupadd command allows you to create new groups on your system:
# /usr/sbin/groupadd shared
# tail /etc/group

The groupadd command


doesnt provide an option for adding user accounts to the group. Instead, to add new users,
use the usermod command:
# /usr/sbin/usermod -G shared rich
# /usr/sbin/usermod -G shared test
# tail /etc/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

# /usr/sbin/groupmod -n sharing shared


# tail /etc/group

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

To remove an existing group, use the groupdel command. Here is an example:


$ sudo groupdel myad Remove existing myad group

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

$ last -a Makes it easier to read the remote client hostname


$ sudo lastb List the most recent unsuccessful logins

$ who -u List who is currently logged in (long form)

$ users List who is currently logged in (short form)

Here are some commands for finding out more about individual users on your system:
$ id Your identity (UID, GID and group for current shell)

$ who am i Your identity (user, tty, login date, location)


chris pts/0 Aug 3 2140 (:0.0)
$ finger -s chris User information (short)
Login Name Tty Idle Login Time Office Office Phone
chris Chris Negus tty1 1d Aug 4 13:39 A-111 555-1212
$ finger -l chris User information (long)

Using file permission symbols


the ls command allows us to see the file permissions for files, directories, and devices on the
Linux system:
$ ls l

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

Default file permissions


You may be wondering about where these file permissions come from. The answer, is umask.
The umask command sets the default permissions for any file or directory you create:
$ touch newfile
$ ls -al newfile

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 Security Settings


If youve already created a file or directory, and need to change the security settings on it, there
are a few different utilities available in Linux. This section shows how to change the existing
permissions, the default owner, and the default group settings for a file or directory

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

$ chmod 760 newfile


$ ls -l newfile
-rwxrw---- 1 rich

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

Using these permissions looks like this:


$ chmod o+r newfile
$ ls -l newfile

$ chmod u-x newfile


$ ls -l newfile

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:

Printing with Linux


Preparing files for printing
Sending a print job to a printer
Monitoring and controlling print jobs

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).

Preparing Files for Printing


As command line users, we are mostly interested in printing text, though it is certainly possible
to print other data formats as well.
prConvert Text Files for Printing
pr is often used in pipelines as a filter. In this example, we will produce a directory listing of
/usr/bin and format it into paginated, three-column output using pr:
[me@linuxbox ~]$ ls /usr/bin | pr -3 -w 65 | head
Sending a Print Job to a Printer
lprPrint Files (Berkeley Style)
The lpr program can be used to send files to the printer. It may also be used in pipelines, as it
accepts standard input. For example, to print the results of our multicolumn directory listing
above, we could do this:
[me@linuxbox ~]$ ls /usr/bin | pr -3 | lpr
The report would be sent to the systems default printer. To send the file to a different printer, the
-P option can used like this:
lpr -P printer_name
where printer_name is the name of the desired printer. To see a list of printers known to the
system:
[me@linuxbox ~]$ lpstat a

57
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

lpPrint Files (System V Style)


Like lpr, lp accepts either files or standard input for printing. It differs from lpr in that it supports
a different (and slightly more sophisticated) option set. Table 22-3 lists the common options.

58
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

[me@linuxbox ~]$ ls /usr/bin | pr -4 -w 90 -l 88 | lp -o page-left=36 -o cpi=


12 -o lpi=8
lpstatDisplay Print System Status
The lpstat program is useful for determining the names and availability of printers on the system.
For example, if we had a system with both a physical printer (named printer) and a PDF virtual
printer (named PDF ), we could check their status like this:
[me@linuxbox ~]$ lpstat -a
PDF accepting requests since Mon 05 Dec 2011 03:05:59 PM EST printer accepting requests
since Tue 21 Feb 2012 08:43:22 AM EST Further, we could determine a more detailed
description of the print system configuration this way:
[me@linuxbox ~]$ lpstat -s
system default destination: printer device for PDF: cups-pdf:/ device for printer: ipp://print-
server:631/printers/printer In this example, we see that printer is the systems default printer and
that it is a network printer using Internet Printing Protocol (ipp:// ) attached to a system named
print-server.
The commonly used options are described in Table 22-5.

59
Bosaso University Faculty of Computer Science
Lecturer notes Eng. Roble Mohamed Ali

lpqDisplay Printer Queue Status


To see the status of a printer queue, the lpq program is used. This allows us to view the status of
the queue and the print jobs it contains. Here is an example of an empty queue for a system
default printer named printer :
[me@linuxbox ~]$ lpq
lprm and cancelCancel Print Jobs
CUPS supplies two programs used to terminate print jobs and remove them from the print queue.
One is Berkeley style (lprm), and the other is System V (cancel). They differ slightly in the
options they support but do basically the same thing. Using our print job above as an example,
we could stop the job and remove it this way:
[me@linuxbox ~]$ cancel 603

60
Bosaso University Faculty of Computer Science

Das könnte Ihnen auch gefallen