Linux MIT
Linux MIT
Linux MIT
Nội dung
The Linux Foundation hosts Linux, the world's largest and most pervasive open
source software project in history. It is also home to Linux creator Linus Torvalds
and lead maintainer Greg Kroah-Hartman. The success of Linux has catalyzed
growth in the open source community, demonstrating the commercial efficacy of
open source and inspiring countless new projects across all industries and levels
of the technology stack.
As a result, the Linux Foundation today hosts far more than Linux; it is the
umbrella for many critical open source projects that power corporations today,
spanning virtually all industry sectors. Some of the technologies we focus on
include big data and analytics, networking, embedded systems and IoT, web
tools, cloud computing, edge computing, automotive, security, blockchain, and
many more.
1.1.2 The Linux Foundation Training
Although we believe in individual initiative and creativity, learning Linux and
open source technologies need not be intimidating. You can leverage the power
of collaboration to jump start your learning. Our classes build the critical skills
that individuals and organizations need to get the most out of Linux and open
source technologies and continue with self-directed learning.
Classroom
Online
On-Site
Events-based.
1.1.3 Training That Builds Skills
The Linux Foundation training is for the community and is designed by members
of the community. To get more information about specific courses offered
by The Linux Foundation visit our catalogue:
These events are the best place to gain visibility within the open source
community quickly and advance open source development work by forming
connections with the people evaluating and creating the next generation of
technology. They provide a forum to share and gain knowledge, help
organizations identify software trends early to inform future technology
investments, connect employers with talent, and showcase technologies and
services to influential open source professionals, media, and analysts around the
globe.
The Linux Foundation hosts an increasing number of events each year, including:
To learn more about The Linux Foundation events and to register, click here.
On the next page, you will learn some more details about the many available
Linux distributions and the families they can be considered to belong to. Because
there are literally hundreds of distributions, we have not covered them all in this
course. Instead, we have decided to focus on the three major distribution families,
and we have chosen distributions from within each family to use for illustrations,
examples, and exercises. This is not meant to suggest that we endorse these
specific distributions; they were simply chosen because they are fairly widely
used and each is broadly representative of its respective family.
For now, what you need to know is that this course focuses on the three major
Linux distribution families that currently exist. However, as long as there are
talented contributors, the families of distributions and the distributions within
these families will continue to change and grow. People see a need, and develop
special configurations and utilities to respond to that need. Sometimes that effort
creates a whole new distribution of Linux. Sometimes, that effort will leverage
an existing distribution to expand the members of an existing family.
For a rather long list of available distributions, see The LWN.net Linux
Distribution List.
1.2.2 The Linux Kernel Distribution Families and Individual Distributions
Red Hat Enterprise Linux (RHEL) heads the family that
includes CentOS, CentOS Stream, Fedora and Oracle Linux.
Fedora has a close relationship with RHEL and contains significantly more
software than Red Hat's enterprise version. One reason for this is that a diverse
community is involved in building Fedora, with many contributors who do not
work for Red Hat. Furthermore, it is used as a testing platform for future RHEL
releases.
1.2.3 The Red Hat Family
We will use CentOS Stream and CentOS more often for activities,
demonstrations, and labs because there is no cost to the end user, and there is a
longer release cycle than for Fedora (which releases a new version every six
months or so).
The basic version of CentOS is also virtually identical to RHEL, the most popular
Linux distribution in enterprise environments. However, CentOS 8 has no
scheduled updates after 2021. The replacement is CentOS 8 Stream. The
difference between the two versions is CentOS Stream gets
updates before RHEL, while CentOS gets them after. For most purposes this
matters very little and not at all for this course. While there are alternatives to
CentOS Stream that look more like the older CentOS, for this course we find
CentOS 8 Stream to works just fine. From time to time you will see examples and
screenshots from CentOS 7 that have not been changed as they would be invariant
in newer distribution versions.
Some of the key facts about the Red Hat distribution family are:
Ubuntu aims at providing a good compromise between long term stability and
ease of use. Since Ubuntu gets most of its packages from Debian’s stable branch,
it also has access to a very large software repository. For those reasons, we will
use Ubuntu LTS (Long Term Support) as the reference to Debian
family distributions for this course.
The Debian family is upstream for Ubuntu, and Ubuntu is upstream for Linux
Mint and others.
Kernel version 5.8 is used in Ubuntu 20.04 LTS.
It uses the DPKG-based APT package manager (using apt, apt-get, apt-cache,
etc., which we cover in detail later) to install, update, and remove packages in the
system.
Ubuntu has been widely used for cloud deployments.
While Ubuntu is built on top of Debian and is GNOME-based under the hood, it
differs visually from the interface on standard Debian, as well as other
distributions.
Chương 2. Giới thiệu HĐH Linux
Mục tiêu
In this section, we follow the surprising history of how Linux evolved from a
project of one Finnish college student, into a massive effort with an enormous
impact on today's world.
In 1992, Linux was re-licensed using the General Public License (GPL)
by GNU (a project of the Free Software Foundation or FSF, which promotes
freely available software), which made it possible to build a worldwide
community of developers. By combining the kernel with other system
components from the GNU project, numerous other developers created complete
systems called Linux distributions in the mid-90’s.
Linux borrows heavily from the well-established UNIX operating system. It was
written to be a free and open source system to be used in place of UNIX, which
at the time was designed for computers much more powerful than PCs and was
quite expensive. Files are stored in a hierarchical filesystem, with the top node of
the system being the root or simply "/". Whenever possible, Linux makes its
components available via files or objects that look like files. Processes, devices,
and network sockets are all represented by file-like objects, and can often be
worked with using the same utilities used for regular files. Linux is a fully
multitasking (i.e. multiple threads of execution are performed simultaneously),
multiuser operating system, with built-in networking and service processes
known as daemons in the UNIX world.
Internet Relay Chat (IRC) software (such as WeeChat, HexChat, Pidgin and
XChat)
Online communities and discussion boards including Linux User Groups (both
local and online)
Many collaborative projects hosted on services such as GitHub and GitLab
Newsgroups and mailing lists, including the Linux Kernel Mailing List
Community events, e.g. Hackathons, Install Fests, Open Source Summits and
Embedded Linux Conferences.
A portal to one of the most powerful online user communities can be found
at linux.com. This site is hosted by The Linux Foundation and serves over one
million unique visitors every month. It has active sections on:
News
Community discussion threads
Free tutorials and user tips.
We will refer several times in this course to relevant articles or tutorials on this
site.
So, what is a Linux distribution and how does it relate to the Linux kernel?
The Linux kernel is the core of the operating system. A full Linux distribution
consists of the kernel plus a number of other software tools for file-related
operations, user management, and software package management. Each of these
tools provides a part of the complete system. Each tool is often its own separate
project, with its own developers working to perfect that piece of the system.
While the most recent Linux kernel (and earlier versions) can always be found
in The Linux Kernel Archives, Linux distributions may be based on different
kernel versions. For example, the very popular RHEL 8 distribution is based on
the 4.18 kernel, which is not new, but is extremely stable. Other distributions may
move more quickly in adopting the latest kernel releases. It is important to note
that the kernel is not an all or nothing proposition, for example, RHEL/CentOS
have incorporated many of the more recent kernel improvements into their older
versions, as have Ubuntu, openSUSE, SLES, etc.
Distribution Roles
2.5.1 Services Associated with Distributions
The vast variety of Linux distributions are designed to cater to many different
audiences and organizations, according to their specific needs and tastes.
However, large organizations, such as companies and governmental institutions
and other entities, tend to choose the major commercially-supported distributions
from Red Hat, SUSE, and Canonical (Ubuntu).
CentOS and CentOS Stream are popular free (as in no cost) alternatives to Red
Hat Enterprise Linux (RHEL) and are often used by organizations that are
comfortable operating without paid technical support. Ubuntu and Fedora are
widely used by developers and are also popular in the educational realm.
Scientific Linux is favored by the scientific research community for its
compatibility with scientific and mathematical software packages.
Both CentOS variants are binary-compatible with RHEL; i.e. in most cases,
binary software packages will install properly across the distributions.
Note that CentOS is planned to disappear at the end of 2021 in favor of CentOS
Stream. However, there are at least two new RHEL-derived substitutes: Alma
Linux and Rocky Linux which are establishing a foothold.
Many commercial distributors, including Red Hat, Ubuntu, SUSE, and Oracle,
provide long term fee-based support for their distributions, as well as hardware
and software certification. All major distributors provide update services for
keeping your system primed with the latest security and bug fixes, and
performance enhancements, as well as provide online support resources.
Mục tiêu
The BIOS software is stored on a ROM chip on the motherboard. After this, the
remainder of the boot process is controlled by the operating system (OS).
A number of boot loaders exist for Linux; the most common ones
are GRUB (for GRand Unified Boot loader), ISOLINUX (for booting from
removable media), and DAS U-Boot (for booting on embedded
devices/appliances). Most Linux boot loaders can present a user interface for
choosing alternative options for booting Linux, and even other operating systems
that might be installed. When booting Linux, the boot loader is responsible for
loading the kernel image and the initial RAM disk or filesystem (which contains
some critical files and device drivers needed to start the system) into memory.
Master Boot Record
For systems using the BIOS/MBR method, the boot loader resides at the first
sector of the hard disk, also known as
the Master Boot Record (MBR). The
size of the MBR is just 512 bytes. In this
stage, the boot loader examines
the partition table and finds a bootable
partition. Once it finds a bootable
partition, it then searches for the second
stage boot loader, for example GRUB,
and loads it into RAM (Random Access
Memory). For systems using the
EFI/UEFI method, UEFI firmware
reads its Boot Manager data to
determine which UEFI application is to
be launched and from where (i.e. from
which disk and partition the EFI
partition can be found). The firmware
then launches the UEFI application, for
example GRUB, as defined in the boot
entry in the firmware's boot manager.
This procedure is more complicated, but
more versatile than the older MBR
methods.
The second stage boot loader resides under /boot. A splash screen is displayed,
which allows us to choose which operating system (OS) to boot. After choosing
the OS, the boot loader loads the kernel of the selected operating system into
RAM and passes control to it. Kernels are almost always compressed, so its first
job is to uncompress itself. After this, it will check and analyze the system
hardware and initialize any hardware device drivers built into the kernel.
The mount program instructs the operating system that a filesystem is ready for
use, and associates it with a particular point in the overall hierarchy of the
filesystem (the mount point). If this is successful, the initramfs is cleared from
RAM and the init program on the root filesystem (/sbin/init) is executed.
init handles the mounting and pivoting over to the final real root filesystem. If
special hardware drivers are needed before the mass storage can be accessed, they
must be in the initramfs image.
Text-Mode Logins
Usually, the default command shell is bash (the GNU Bourne Again Shell), but
there are a number of other advanced command shells available. The shell prints
a text prompt, indicating it is ready to accept commands; after the user types the
command and presses Enter, the command is executed, and another prompt is
displayed after the command is done.
Besides starting the system, init is responsible for keeping the system running
and for shutting it down cleanly. One of its responsibilities is to act when
necessary as a manager for all non-kernel processes; it cleans up after them upon
completion, and restarts user login services as needed when users log in and out,
and does the same for other background system services.
Traditionally, this process startup was done using conventions that date back to
the 1980s and the System V variety of UNIX. This serial process had the system
passing through a sequence of runlevels containing collections of scripts that
start and stop services. Each runlevel supported a different mode of running the
system. Within each runlevel, individual services could be set to run, or to be shut
down if running.
However, all major distributions have moved away from this sequential runlevel
method of system initialization, although they usually emulate many System V
utilities for compatibility purposes. Next, we discuss the new methods, of
which systemd has become dominant.
Furthermore, shutdown and reboot was seen as a relatively rare event; exactly
how long it took was not considered important. This is no longer true, especially
with mobile devices and embedded Linux systems. Some modern methods, such
as the use of containers, can require almost instantaneous startup times. Thus,
systems now require methods with faster and enhanced capabilities. Finally, the
older methods required rather complicated startup scripts, which were difficult to
keep universal across distribution versions, kernel versions, architectures, and
types of systems. The two main alternatives developed were:
3.3.1 Upstart
While the migration to systemd was rather controversial, it has been adopted by
all major distributions, and so we will not discuss the older System V method
or Upstart, which has become a dead end. Regardless of how one feels about the
controversies or the technical methods of systemd, almost universal adoption has
made learning how to work on Linux systems simpler, as there are fewer
differences among distributions. We enumerate systemd features next.
One systemd command (systemctl) is used for most basic tasks. While we have
not yet talked about working at the command line, here is a brief listing of its use:
Starting, stopping, restarting a service (using httpd, the Apache web server, as an
example) on a currently running system:
$ sudo systemctl start|stop|restart httpd.service
Enabling or disabling a system service from starting up at system boot:
$ sudo systemctl enable|disable httpd.service
In most cases, the .service can be omitted. There are many technical differences
with older methods that lie beyond the scope of our discussion.
Conventional disk filesystems: ext3, ext4, XFS, Btrfs, JFS, NTFS, vfat, exfat,
etc.
Flash storage filesystems: ubifs, jffs2, yaffs, etc.
Database filesystems
Special purpose filesystems: procfs, sysfs, tmpfs, squashfs, debugfs, fuse, etc.
This section will describe the standard filesystem layout shared by most Linux
distributions.
3.4.1 Phân vùng và hệ thống tập tin Partitions and Filesystems
A partition is a physically contiguous section of a disk, or what appears to be so
in some advanced setups.
Windows Linux
Linux uses the ‘/’ character to separate paths (unlike Windows, which uses ‘\’),
and does not have drive letters. Multiple drives and/or partitions are mounted as
directories in the single filesystem. Removable media such as USB drives and
CDs and DVDs will show up as mounted
at /run/media/yourusername/disklabel for recent Linux systems, or
under /media for older distributions. For example, if your username is student a
USB pen drive labeled FEDORA might end up being found
at /run/media/student/FEDORA, and a file README.txt on that disc would
be at /run/media/student/FEDORA/README.txt.
Nearly all installers provide a reasonable default layout, with either all space
dedicated to normal files on one big partition and a smaller swap partition, or with
separate partitions for some space-sensitive areas like /home and /var. You may
need to override the defaults and do something different if you have special needs,
or if you want to use more than one disk.
Most installers also provide options for adding categories of software. Common
applications (such as the Firefox web browser and LibreOffice office suite),
developer tools (like the vi and emacs text editors, which we will explore later in
this course), and other popular services, (such as the Apache web server tools or
MySQL database) are usually included. In addition, for any system with a
graphical desktop, a chosen desktop (such as GNOME or KDE) is installed by
default.
All installers set up some initial security features on the new system. One basic
step consists of setting the password for the superuser (root) and setting up an
initial user. In some cases (such as Ubuntu), only an initial user is set up; direct
root login is not configured and root access requires logging in first as a normal
user and then using sudo, as we will describe later. Some distributions will also
install more advanced security frameworks, such as SELinux or AppArmor. For
example, all Red Hat-based systems including Fedora and CentOS always
use SELinux by default, and Ubuntu comes with AppArmor up and running.
Linux Installation Software Choices
Each distribution provides its own documentation and tools for creating and
managing these files.
3.5.5 Linux Installation: The Process
The actual installation process is pretty similar for all distributions.
After booting from the installation media, the installer starts and asks questions
about how the system should be set up. These questions are skipped if an
automatic installation file is provided. Then, the installation is performed.
Most installers have the option of downloading and installing updates as part of
the installation process; this requires Internet access. Otherwise, the system uses
its normal update mechanism to retrieve those updates after the installation is
done.
The demonstrations show how to install Linux directly on your machine, erasing
everything that was there. While the demonstrations will not alter your
computer, following these procedures in real life will erase all current data.
The Linux Foundation has a document: "Preparing Your Computer for Linux
Training" (see below) that describes alternate methods of installing Linux
without over-writing existing data. You may want to consult it, if you need to
preserve the information on your hard disk.
These alternate methods are:
1. Re-partitioning your hard disk to free up enough room to permit dual boot (side-
by-side) installation of Linux, along with your present operating system.
2. Using a host machine hypervisor program (such as VMWare's products or Oracle
Virtual Box) to install a client Linux Virtual Machine.
3. Booting off of and using a Live CD or USB stick and not writing to the hard disk
at all.
The first method is sometimes complicated and should be done when your
confidence is high and you understand the steps involved. The second and third
methods are quite safe and make it difficult to damage your system.
Mục tiêu
By the end of this chapter, you should be able to:
We will learn how to manage sessions using the GUI for the three Linux
distribution families that we cover the most in this course: Red Hat (CentOS,
Fedora), SUSE (openSUSE), and Debian (Ubuntu, Mint). Since we are using
the GNOME-based variant of openSUSE rather than the KDE-based one, all are
actually quite similar. If you are using KDE (or other Linux desktops such as
XFCE), your experience will vary somewhat from what is shown, but not in any
intrinsically difficult way, as user interfaces have converged to certain well-
known behaviors on modern operating systems. In subsequent sections of this
course we will concentrate in great detail on the command line interface, which
is pretty much the same on all distributions.
Ubuntu, CentOS, and openSUSE Desktops
A service called the Display Manager keeps track of the displays being provided
and loads the X server (so-called, because it provides graphical services to
applications, sometimes called X clients). The display manager also handles
graphical logins and starts the appropriate desktop environment after a user logs
in.
X is rather old software; it dates back to the mid 1980s and, as such, has certain
deficiencies on modern systems (for example, with security), as it has been
stretched rather far from its original purposes. A newer system, known
as Wayland, is gradually superseding it and is the default display system for
Fedora, RHEL 8, and other recent distributions. For the most part, it looks just
like X to the user, although under the hood it is quite different.
Display Manager
Although these can be mixed, generally a set of utilities, session manager, and
window manager are used together as a unit, and together provide a seamless
desktop environment.
If the display manager is not started by default in the default runlevel, you can
start the graphical desktop different way, after logging on to a text-mode console,
by running startx from the command line. Or, you can start the display manager
(gdm, lightdm, kdm, xdm, etc.) manually from the command line. This differs
from running startx as the display managers will project a sign in screen. We
discuss them next.
Desktop Environment
4.3 GUI Startup
When you install a desktop environment, the X display manager starts at the end
of the boot process. It is responsible for starting the graphics system, logging in
the user, and starting the user’s desktop environment. You can often select from
a choice of desktop environments when logging in to the system.
The default display manager for GNOME is called gdm. Other popular display
managers include lightdm (used on Ubuntu before version 18.04 LTS)
and kdm (associated with KDE).
Another common desktop environment very important in the history of Linux and
also widely used is KDE, which has often been used in conjunction with SUSE
and openSUSE. Other alternatives for a desktop environment include Unity
(present on older Ubuntu, but still based on GNOME), XFCE and LXDE. As
previously mentioned, most desktop environments follow a similar structure to
GNOME, and we will restrict ourselves mostly to it to keep things less complex.
For the most part, this is not a problem. Graphical interfaces are designed to be
easy to navigate and figure out, and they really do not vary very much, not only
from one distribution to another, but even between operating systems. So, the
only way you can get more adept at working efficiently on your desktop is to
simply explore, play, and modify. The same points will apply to the next chapter,
on graphical system configuration.
Linux is so customizable that very few people who use it stay with the default
look and feel of the desktop. You may as well get started now in making your
desktop reflect your likes and personality.
Start by changing the desktop background to something that better suits yours
tastes; perhaps one of the provided backgrounds, a solid color of your choice, or
a personal picture that you can transfer onto your Linux environment.
Next, select a theme from the available themes for your distribution that, again,
suits your tastes and personality. Have fun and explore with this exercise.
NOTE: This does not suspend the computer; all your applications and
processes continue to run while the screen is locked.
Using the graphical interface: Clicking in the upper-right corner of the desktop,
and then clicking on the lock icon.
Using the keyboard shortcut SUPER-L: (The SUPER key is also known as
the Windows key).
The keyboard shortcut for locking the screen can be modified by altering
keyboard settings, the exact prescription varying by distribution, but not hard to
ascertain.
To re-enter the desktop session you just need to provide your password again.
The screenshot below shows how to lock the screen for Ubuntu. The details vary
little in modern distributions.
Initiating the shutdown process from the graphical desktop is rather trivial on all
current Linux distributions, with very little variation. We will discuss later how
to do this from the command line, using the shutdown command.
In all cases, you click on either a settings (gear) or a power icon and follow the
prompts.
1. Click either the Power or the Gear icon in the upper-right corner of the screen.
2. Click on Power Off, Restart, or Cancel. If you do nothing, the system will
shutdown in 60 seconds.
Shutdown, reboot, and logout operations will ask for confirmation before going
ahead. This is because many applications will not save their data properly when
terminated this way.
Always save your documents and data before restarting, shutting down, or
logging out.
4.4.2.5 Suspending
All modern computers support Suspend (or Sleep) Mode when you want to stop
using your computer for a while. Suspend Mode saves the current system state
and allows you to resume your session more quickly while remaining on, but uses
very little power in the sleeping state. It works by keeping your system’s
applications, desktop, and so on, in system RAM, but turning off all of the other
hardware. This shortens the time for a full system start-up as well as conserves
battery power. One should note that modern Linux distributions actually boot so
fast that the amount of time saved is often minor.
Suspending the System
To suspend the system, the procedure starts the same as that for shutdown or
locking the screen.
NOTE: To wake your system and resume your session, move the mouse or
press any button on the keyboard. The system will wake up with the screen locked,
just as if you had manually locked it; type in your password to resume.
On the following pages you will learn how to perform basic operations in Linux
using the graphical interface.
Locating Applications
To set default applications, enter the Settings menu (on all recent Linux
distributions) and then click on either Default Applications or Details > Default
Applications. The exact list will vary from what is shown here in the Ubuntu
screenshot according to what is actually installed and available on your system
Default Applications
To start the file manager you will have to click on its icon (a file cabinet) which
is easily found, usually under Favorites or Accessories. It will have the
name Files.
This will open a window with your Home directory displayed. The left panel of
the File Manager window holds a list of commonly used directories, such
as Desktop, Documents, Downloads and Pictures.
You can click the Magnifying Glass icon on the top-right to search for files or
directories (folders).
File Manager
Every user with an account on the system will have a home directory, usually
created under /home, and usually named according to the user, such
as /home/student.
By default, files the user saves will be placed in a directory tree starting there.
Account creation, whether during system installation or at a later time, when a
new user is added, also induces default directories to be created under the user's
home directory, such as Documents, Desktop, and Downloads.
In the screenshot shown for Ubuntu, we have chosen the list format and are also
showing hidden files (those starting with a period). See if you can do the same on
your distribution.
Home Directories
Other Locations
You can switch between the Icons and List formats, either by clicking the familiar
icons in the top bar, or you can press CTRL-1 or CTRL-2 respectively.
In addition, you can also arrange the files and directories by name, size, type, or
modification date for further sorting. To do so, click View and select Arrange
Items.
The file browser provides multiple ways to customize your window view to
facilitate easy drag and drop file operations. You can also alter the size of the
icons by selecting Zoom In and Zoom Out under the View menu.
To open the File Manager from the command line, on most systems simply
type nautilus.
The shortcut key to get to the search text box is CTRL-F. You can exit the search
text box view by clicking the Search button or CTRL-F again.
Another quick way to access a specific directory is to press CTRL-L, which will
give you a Location text box to type in a path to a directory.
1. Based on Location or File Type, select additional criteria from the dropdown.
2. To regenerate the search, click the Reload button.
3. To add multiple search criteria, click the + button and select Additional Search
Criteria.
For example, if you want to find a PDF file containing the word Linux in your
home directory, navigate to your home directory and search for the word
“Linux”. You should see that the default search criterion limits the search to
your home directory already. To finish the job, click the + button to add another
search criterion, select File Type for the type of criterion, and select PDF under
the File Type dropdown.
Searching for Files
You will learn much more about using text editors in a later chapter.
1. Select all the files and directories that you want to delete.
2. Press CTRL-Delete on your keyboard, or right-click the file.
3. Select Move to Trash.
Note that you may have a Delete Permanently option which bypasses the trash
folder, and that this option may be visible all the time or only in list (rather than)
icon mode.
As a precaution, you should never delete your Home directory, as doing so will
most likely erase all your GNOME configuration files and possibly prevent you
from logging in. Many personal system and program configurations are stored
under your home directory.
You will have to open the file browser window and navigate to
the /var/log directory. This will vary somewhat from distribution to distribution.
On most recent distributions you will click on Other Locations -> Computer.
Navigate to the /var/log directory. Set the view option to List rather than Icon,
and then click on the date.
1. Create a new text file on the desktop named lab.txt, using the graphical file
manager.
2. Delete the file by sending it to Trash.
3. Verify the file is now in ~/.local/share/Trash, or a subdirectory thereof. NOTE:
You will have to get your file browser to show hidden files and directories, those
that start with a .).
4. Recover the file and make sure it is in its original location.
1. Open the file manager and navigate to your home directory. Once your user
configuration is set up for it this is as simple as right clicking in the
directory, and selecting Create New->Text File and giving it a name.
(You may see something other than Text File.)
With the GNOME file manager, there is no such option in the default
setup. You must create a file called new in your Templates directory first.
The easiest way to do this is to type Alt-F2 and then in the little window
that opens up
student:/tmp> touch ~/Templates/new
Once you have done that, you can either right click in the directory, or
somewhere on the right side of the title bar to open up a dialog where there
will be an option to create a new file of some type.
Note that the GNOME File Manager does not include a Delete or Permanenly
Delete option by default. To enable this you have to go into preferences and turn
it on. You can enter the preferences menu by starting the File Manager and on the
top task bar on the desktop, click on its icon and get into the preference menus.
Another non-obvious step :( .
Chương 5. Cấu hình hệ thống từ Giao diện đồ họa
Mục tiêu
By the end of this chapter, you should be able to:
Apply system, display, and date and time settings using the System Settings panel.
Track the network settings and manage connections using Network Manager in
Linux.
Install and update software in Linux from a graphical interface.
NOTE: We will revisit all these tasks later, when we discuss how to accomplish
them from the command line interface.
For the GNOME Desktop Manager, one clicks on the upper right-hand corner
and then selects the tools image (screwdriver crossed with a wrench or a gear).
Depending on your distribution, you may find other ways to get into the settings
configuration as well. You will also find variation in the menu layout between
Linux distributions and versions, so you may have to hunt for the settings you
need to examine or modify.
One can also click on the Users icon (which may be under Details) to set values
for system users, such as their login picture, password, etc.
Important things you can do with this tool include selecting a theme,
configuring extensions which you can get from your distribution or download
from the Internet, control fonts, modify the keyboard layout, and set which
programs start when you login.
The most recent GNOME versions have removed a lot of the functionality
of gnome-tweaks; extensions now have to be configured using a new app
called gnome-extensions-app. The reasoning for this is obscure.
The screenshot here is from a Red Hat system with quite a few extensions
installed, but not all being used.
The X server, which actually provides the GUI, uses /etc/X11/xorg.conf as its
configuration file if it exists; In modern Linux distributions, this file is usually
present only in unusual circumstances, such as when certain less common graphic
drivers are in use. Changing this configuration file directly is usually for more
advanced users.
If you click on the time displayed on the top panel, you can adjust the format with
which the date and time is shown; on some distributions, you can also alter the
values.
The more detailed date and time settings can be selected from the Date &
Time window in the System Settings Menu.
NOTE: You can also ascertain your current resolution by typing at the command
line:
Click either the gear or tool icon in the top right corner, depending on your
distribution, and select System Settings.
Click on either Displays or Devices -> Displays. The resolution drop down
should be evident and inutitive to use.
After noting the time change, change the time zone back to your local time zone.
On a GNOME desktop, click on System Settings -> Date & Time or System
Settings -> Details -> Date & Time, and Note the current Location settings.
You will either have to click on Unlock to give the root password, or will be
asked for it when you try to change the time zone, depending on
your GNOME version.
Use the Region and City drop-downs to switch the region to Europe and the city
to London, or type London into the Location widget and pick the proper location
from the list that appears. (You may also just be able to click at the right place on
a map of the world.)
Look at the clock, and note that the displayed time has changed. Now, set the time
zone back in the same way.
On KDE desktops, the details are somewhat different but it is not rocket science
to figure out how to do this, and it should take you less time to find the
configuration methods than it does to describe them.
Network Configuration
In this section, you will learn how to manage network connections, including
wired and wireless connections, and mobile broadband and VPN connections.
For static configurations that do not use DHCP, manual setup can also be done
easily through Network Manager. You can also change the
Ethernet Media Access Control (MAC) address if your hardware supports it. The
MAC address is a unique hexadecimal number of your network card.
Wireless networks are usually not connected by default. You can view the list of
available wireless networks and see which one (if any) you are currently
connected to by using Network Manager. You can then add, edit, or remove
known wireless networks, and also specify which ones you want connected by
default when present.
Click on the upper-right corner of the top panel, which brings up a settings and/or
network window. While the exact appearance will depend on Linux distribution
and version, it will always be possible to click on a Wi-Fi submenu, as long as
the hardware is present. Here is an example from a RHEL 8 system:
Configuring Wireless Connections
Select the wireless network you wish to connect to. If it is a secure network, the
first time it will request that you enter the appropriate password. By default, the
password will be saved for subsequent connections.
Selecting a Network
If you click on Wi-Fi Settings, you will bring up the third screenshot. If you click
on the Gear icon for any connection, you can configure it in more detail.
Older and other Linux distributions may look quite a bit different in detail, but
the steps and choices are essentially identical, as they are all running Network
Manager with perhaps somewhat different clothing.
5.6.3 Mobile Broadband and VPN Connections
You can set up a mobile broadband connection with Network Manager, which
will launch a wizard to set up the connection details for each connection.
Once the configuration is done, the network is configured automatically each time
the broadband network is attached.
You might get support for VPN as a separate package from your distributor. You
need to install this package if your preferred VPN is not supported.
If you have wireless hardware, see what wireless networks are available, if any.
Click the link below to view a solution to the Lab exercise.
Click the Network Manager icon in the upper right corner of your desktop.
(Appearance can vary by distribution.) This should bring up the wired and
wireless connection state, and which wireless networks are available.
5.7 Installing and Updating Software
Each package in a Linux distribution provides one piece of the system, such as
the Linux kernel, the C compiler, utilities for manipulating text or configuring
the network, or for your favorite web browsers and email clients.
Packages often depend on each other. For example, because your email client can
communicate using SSL/TLS, it will depend on a package which provides the
ability to encrypt and decrypt SSL and TLS communication, and will not install
unless that package is also installed at the same time.
All systems have a lower-level utility which handles the details of unpacking a
package and putting the pieces in the right places. Most of the time, you will be
working with a higher-level utility which knows how to download packages from
the Internet and can manage dependencies and groups for you.
In this section, you will learn how to install and update software in Linux using
the Debian packaging system (used by systems such as Ubuntu as well) and
RPM packaging systems (which is used by both Red Hat and SUSE family
systems). These are the main ones in use although there are others which work
well for other distributions which are less used.
1. Click Activities
2. In the Search box, type YaST
3. Click the YaST icon
4. Click Software Management
You can also find YaST by clicking on Applications > Other-YaST, which is a
strange place to put it.
openSUSE's Software Management
Mục tiêu:
xterm
konsole (default on KDE)
terminator
$ ls -a
On any but some of the most recent GNOME-based distributions, you can always
open a terminal by right-clicking anywhere on the desktop background and
selecting Open in Terminal. If this does not work you will once again need to
install and activate the appropriate gnome-shell-extension package.
You can also hit Alt-F2 and type in either gnome-terminal or konsole,
whichever is appropriate.
The screenshot shows elementary uses of these programs. Note the use of the pipe
symbol (|) used to have one program take as input the output of another.
For the most part, we will only use these utilities in screenshots displaying various
activities, before we discuss them in detail.
Command
Options
Arguments
The command is the name of the program you are executing. It may be followed
by one or more options (or switches) that modify what the command may do.
Options usually start with one or two dashes, for example, -p or --print, in order
to differentiate them from arguments, which represent what the
command operates on.
On your own systems, you may need to set up and enable sudo to work correctly.
To do this, you need to follow some steps that we will not explain in much detail
now, but you will learn about later in this course. When running on Ubuntu and
some other recent distributions, sudo is already always set up for you during
installation. On other Linux distributions, you will likely need to set up sudo to
work properly for you after the initial installation.
Next, you will learn the steps to set up and run sudo on your system.
That should be it. For the rest of this course, if you use sudo you should be
properly set up. When using sudo, by default you will be prompted to give a
password (your own user password) at least the first time you do it within a
specified time interval. It is possible (though very insecure) to configure sudo to
not require a password or change the time window in which the password does
not have to be repeated with every sudo command.
Linux production servers are usually installed without the GUI, and even if it is
installed, usually do not launch it during system startup. Removing the graphical
interface from a production server can be very helpful in maintaining a lean
system, which can be easier to support and keep secure.
One virtual terminal (usually number one or seven) is reserved for the graphical
environment, and text logins are enabled on the unused VTs. Ubuntu uses VT 7,
but CentOS/RHEL and openSUSE use VT 1 for the graphical display.
An example of a situation where using VTs is helpful is when you run into
problems with the graphical desktop. In this situation, you can switch to one of
the text VTs and troubleshoot.
To switch between VTs, press CTRL-ALT-function key for the VT. For
example, press CTRL-ALT-F6 for VT 6. Actually, you only have to press
the ALT-F6 key combination if you are in a VT and want to switch to another
VT.
Your method will depend on your distribution and your greeter program (gdm,
lightdm, kdm) and whether you have a systemd, SysVinit, or Upstart system.
Basic Operations
Once your session is started (either by logging into a text terminal or via a
graphical terminal program), you can also connect and log into remote systems
by using Secure SHell (SSH). For example, by typing ssh student@remote-
server.com, SSH would connect securely to the remote machine (remote-
server.com) and give student a command line terminal window, using either a
password (as with regular logins) or cryptographic key to sign in without
providing a password to verify the identity.
When administering a multi-user system, you have the option of notifying all
users prior to shutdown, as in:
One way to locate programs is to employ the which utility. For example, to find
out exactly where the diff program resides on the filesystem:
If which does not find the program, whereis is a good alternative because it looks
for packages in a broader range of system directories:
as well as locating source and man files packaged with the program.
Command Result
http://linuxfoundation.s3-website-us-east-
1.amazonaws.com/TIY/usingcd/index.html
Absolute pathname: An absolute pathname begins with the root directory and
follows the tree, branch by branch, until it reaches the desired directory or file.
Absolute paths always start with /.
Relative pathname: A relative pathname starts from the present working
directory. Relative paths never start with /.
Multiple slashes (/) between directories and files are allowed, but all but one slash
between elements in the pathname is ignored by the system. ////usr//bin is valid,
but seen as /usr/bin by the system.
Most of the time, it is most convenient to use relative paths, which require less
typing. Usually, you take advantage of the shortcuts provided by: . (present
directory), .. (parent directory) and ~ (your home directory).
For example, suppose you are currently working in your home directory and wish
to move to the /usr/bin directory. The following two ways will bring you to the
same directory from your home directory:
Absolute pathname method: $ cd /usr/bin
Relative pathname method: $ cd ../../usr/bin
Command Usage
cd / Changes your current directory to the root (/) directory (or path you
supply)
ls –a List all files, including hidden files and directories (those whose name
start with . )
Suppose that file1 already exists. A hard link, called file2, is created with the
command:
$ ln file1 file2
Note that two files now appear to exist. However, a closer inspection of the file
listing shows that this is not quite true.
The -i option to ls prints out in the first column the inode number, which is a
unique quantity for each file object. This field is the same for both of these files;
what is really going on here is that it is only one, file but it has more than one
name associated with it, as is indicated by the 2 that appears in the ls output.
Thus, there was already another object linked to file1 before the command was
executed.
Hard links are very useful and they save space, but you have to be careful with
their use, sometimes in subtle ways. For one thing, if you remove
either file1 or file2 in the example, the inode object (and the remaining file name)
will remain, which might be undesirable, as it may lead to subtle errors later if
you recreate a file of that name.
If you edit one of the files, exactly what happens depends on your editor; most
editors, including vi and gedit, will retain the link by default, but it is possible
that modifying one of the names may break the link and result in the creation of
two objects.
Hard Links
7.8.5.4 Soft (Symbolic) Links
Soft (or Symbolic) links are created with the -s option, as in:
$ ln -s file1 file3
Notice file3 no longer appears to be a regular file, and it clearly points to file1 and
has a different inode number.
Symbolic links take no extra space on the filesystem (unless their names are very
long). They are extremely convenient, as they can easily be modified to point to
different places. An easy way to create a shortcut from your home directory to
long pathnames is to create a symbolic link.
Unlike hard links, soft links can point to objects even on different filesystems,
partitions, and/or disks and other media, which may or may not be currently
available or even exist. In the case where the link does not point to a currently
available or existing object, you obtain a dangling link.
student:/tmp> which ip
/usr/sbin/ip
student:/tmp> whereis ip
ip: /usr/sbin/ip /usr/share/man/man7/ip.7.gz /usr/share/man/man8/ip.8.gz
Command Usage
cat Used for viewing files that are not very long; it does not provide any
scroll-back.
tac Used to look at a file backwards, starting with the last line.
less Used to view larger files because it is a paging program. It pauses at each
screen full of text, provides scroll-back capabilities, and lets you search
and navigate within the file.
NOTE: Use / to search for a pattern in the forward direction and ? for a
pattern in the backward direction. An older program named more is still
used, but has fewer capabilities: "less is more".
tail Used to print the last 10 lines of a file by default. You can change the
number of lines by doing -n 15 or just -15 if you wanted to look at the
last 15 lines instead of the default.
head The opposite of tail; by default, it prints the first 10 lines of a file.
7.8.6.1 touch
touch is often used to set or update the access, change, and modify times of files.
By default, it resets a file's timestamp to match the current time.
$ touch <filename>
This is normally done to create an empty file as a placeholder for a later purpose.
touch provides several useful options. For example, the -t option allows you to
set the date and timestamp of the file to a specific value, as in:
This sets the myfile file's timestamp to 4 p.m., December 9th (12 09 1600).
touch
mkdir sampdir : It creates a sample directory named sampdir under the current
directory.
mkdir /usr/sampdir : It creates a sample directory called sampdir under /usr.
Removing a directory is done with rmdir. The directory must be empty or the
command will fail. To remove a directory and all of its contents you have to
do rm -rf.
mkdir
If you are not certain about removing files that match a pattern you supply, it is
always good to run rm interactively (rm –i) to prompt before every removal.
Command Usage
mv Rename a file
rm Remove a file
While typing rm –rf is a fast and easy way to remove a whole filesystem tree
recursively, it is extremely dangerous and should be used with the utmost care,
especially when used by root (recall that recursive means drilling down through
all sub-directories, all the way down a tree).
Command Usage
mv Rename a directory
student@c8 $
This could prove useful if you are working in multiple roles and want to be always
reminded of who you are and what machine you are on. The prompt above could
be implemented by setting the PS1 variable to: \u@\h \$.
For example:
$ echo $PS1 \$
$ PS1="\u@\h \$ "
student@c8 $ echo $PS1
\u@\h \$
student@c8 $
By convention, most systems are set up so that the root user has a pound sign (#)
as their prompt.
Please take a look at the following Try-It-Yourself exercise: Working With Files
and Directories Using the Command Prompt.
Lab 7.3: Creating, Moving and Removing Files
Create an empty file named exercise.txt and move this file to the /tmp directory,
using a relative pathname from your home directory. Then, delete this file using
an absolute pathname.
Usually, stdin is your keyboard, and stdout and stderr are printed on your
terminal. stderr is often redirected to an error logging file, while stdin is
supplied by directing input to come from a file or from the output of a previous
command through a pipe. stdout is also often redirected into a file.
Since stderr is where error messages are written, usually nothing will go there.
In Linux, all open files are represented internally by what are called file
descriptors. Simply put, these are represented by numbers starting at zero. stdin is
file descriptor 0, stdout is file descriptor 1, and stderr is file descriptor 2.
Typically, if other files are opened in addition to these three, which are opened
by default, they will start at file descriptor 3 and increase from there.
On the next page and in the chapters ahead, you will see examples which alter
where a running command gets its input, where it writes its output, or where it
prints diagnostic (error) messages.
7.9.1 I/O Redirection
Through the command shell, we can redirect the three standard file streams so
that we can get input from either a file or another command, instead of from our
keyboard, and we can write output and errors to files or use them to provide input
for subsequent commands.
If you want to send the output to a file, use the greater-than sign (>) as in:
Because stderr is not the same as stdout, error messages will still be seen on the
terminal windows in the above example.
If you want to redirect stderr to a separate file, you use stderr’s file descriptor
number (2), the greater-than sign (>), followed by the name of the file you want
to hold everything the running command writes to stderr:
7.9.2 Pipes
The UNIX/Linux philosophy is to have many simple and short programs (or
commands) cooperate together to produce quite complex results, rather than have
one complex program with many possible options and modes of operation. In
order to accomplish this, extensive use of pipes is made. You can pipe the output
of one command or program into another as its input.
In order to do this, we use the vertical-bar, pipe symbol (|), between commands
as in:
The above represents what we often call a pipeline, and allows Linux to combine
the actions of several commands into one. This is extraordinarily efficient
because command2 and command3 do not have to wait for the previous
pipeline commands to complete before they can begin hacking at the data in their
input streams; on multiple CPU or core systems, the available computing power
is much better utilized and things get done quicker.
Pipeline
7.9.4 locate
The locate utility program performs a search taking advantage of a previously
constructed database of files and directories on your system, matching all entries
that contain a specified character string. This can sometimes result in a very long
list.
To get a shorter (and possibly more relevant) list, we can use the grep program
as a filter. grep will print only the lines that contain one or more specified strings,
as in:
which will list all the files and directories with both zip and bin in their name.
We will cover grep in much more detail later. Notice the use of | to pipe the two
commands together.
Wildcard Result
[set] Matches any character in the set of characters, for example [adf] will
match any occurrence of a, d, or f
To search for files using the ? wildcard, replace each unknown character with ?.
For example, if you know only the first two letters are 'ba' of a three-letter
filename with an extension of .out, type ls ba?.out.
To search for files using the * wildcard, replace the unknown string with *. For
example, if you remember only that the extension was .out, type ls *.out.
Find
Searching for files and directories named gcc: $ find /usr -name gcc
Searching only for directories named gcc: $ find /usr -type d -name gcc
Searching only for regular files named gcc: $ find /usr -type f -name gcc
Another good use of find is being able to run commands on the files that match
your search criteria. The -exec option is used for this purpose.
The {} (squiggly brackets) is a placeholder that will be filled with all the file
names that result from the find expression, and the preceding command will be
run on each one individually.
Please note that you have to end the command with either ‘;’ (including the
single-quotes) or "\;". Both forms are fine.
One can also use the -ok option, which behaves the same as -exec, except
that find will prompt you for permission before executing the command. This
makes it a good way to test your results before blindly executing any potentially
dangerous commands.
Here, -ctime is when the inode metadata (i.e. file ownership, permissions, etc.)
last changed; it is often, but not necessarily, when the file was first created. You
can also search for accessed/last read (-atime) or modified/last written (-mtime)
times. The number is the number of days and can be expressed as either a number
(n) that means exactly that value, +n, which means greater than that number, or -
n, which means less than that number. There are similar options for times in
minutes (as in -cmin, -amin, and -mmin).
Note the size here is in 512-byte blocks, by default; you can also specify bytes
(c), kilobytes (k), megabytes (M), gigabytes (G), etc. As with the time numbers
above, file sizes can also be exact numbers (n), +n or -n. For details, consult the
man page for find.
For example, to find files greater than 10 MB in size and running a command on
those files: $ find / -size +10M -exec command {} ’;’
Note you will get a lot of noise about trying to look at files and directories normal
users are not allowed to examine. If you preface the find command
with sudo these will not occur.)
In this section, you will learn how to install, remove, or search for packages from
the command line using these two package management systems.
Most of the time users need to work only with the high-level tool, which will take
care of calling the low-level tool as needed. Dependency resolution is a
particularly important feature of the high-level tool, as it handles the details of
finding and installing each dependency for you. Be careful, however, as installing
a single package could result in many dozens or even hundreds of dependent
packages being installed.
Package Managers: Two Levels
dnf is the open source command-line package-management utility for the RPM-
compatible Linux systems that belongs to the Red Hat family. dnf has both
command line and graphical user interfaces. Fedora and RHEL 8 replaced the
older yum utility with dnf, thereby eliminating a lot of historical baggage, as
well as introducing many nice new capabilities. dnf is pretty much backwards-
compatible with yum for day-to-day commands.
Working with Different Package Management Systems
zypper is the package management system for the SUSE/openSUSE family and
is also based on RPM. zypper also allows you to manage repositories from the
command line. zypper is fairly straightforward to use and
resembles dnf/yum quite closely.
To learn the basic packaging commands, take a look at these basic packaging
commands:
What package is file part of? rpm -qf file dpkg --search file
Lab 7.5: Installing and Removing Software Packages
Using the upper-level package management system appropriate for your Linux
distribution, do the following:
NOTE: If dump is already installed (you will be told so when you try to install),
then do things in opposite order, i.e. remove and then install.
or
student:/tmp> dnf install dump
student:/tmp> dnf remove dump
or
student:/tmp> yum install dump
student:/tmp> yum remove dump
or
student:/tmp> zypper install dump
student:/tmp> zypper remove dump
Chapter 9. Process Management
Mục tiêu:
Processes can be of different types according to the task being performed. Here
are some different process types, along with their descriptions and examples:
Daemons Server processes that run continuously. Many are httpd, sshd,
launched during system startup and then wait for a libvirtd
user or system request indicating that their service is
required.
Threads Lightweight processes. These are tasks that run under firefox,
the umbrella of a main process, sharing memory and gnome-
other resources, but are scheduled and run by the terminal-
system on an individual basis. An individual thread server
can end without terminating the whole process and a
process can create new threads at any time. Many
non-trivial programs are multi-threaded.
Kernel Kernel tasks that users neither start nor terminate and kthreadd,
Threads have little control over. These may perform actions migration,
like moving a thread from one CPU to another, or ksoftirqd
making sure input/output operations to disk are
completed.
Process Scheduling and States
A critical kernel function called the scheduler constantly shifts processes on and
off the CPU, sharing time according to relative priority, how much time is needed
and how much has already been granted to a task.
There are some other less frequent process states, especially when a process is
terminating. Sometimes, a child process completes, but its parent process has not
asked about its state. Amusingly, such a process is said to be in a zombie state; it
is not really alive, but still shows up in the system's list of processes.
ID Type Description
Parent Process Process (Parent) that started this process. If the parent dies, the
ID (PPID) PPID will refer to an adoptive parent; on recent kernels, this is
kthreadd which has PPID=2.
Thread ID Thread ID number. This is the same as the PID for single-
(TID) threaded processes. For a multi-threaded process, each thread
shares the same PID, but has a unique TID.
9.1.5 Terminating a Process
At some point, one of your applications may stop working properly. How do you
eliminate it?
To terminate a process, you can type kill -SIGKILL <pid> or kill -9 <pid>.
Note, however, you can only kill your own processes; those belonging to another
user are off limits, unless you are root.
9.1.6 User and Group IDs
Many users can access a system simultaneously, and each user can run multiple
processes. The operating system identifies the user who starts the process by the
Real User ID (RUID) assigned to the user.
The user who determines the access rights for the users is identified by the
Effective UID (EUID). The EUID may or may not be the same as the RUID.
Users can be categorized into various groups. Each group is identified by the Real
Group ID (RGID). The access rights of the group are determined by the Effective
Group ID (EGID). Each user can be a member of one or more groups.
Most of the time we ignore these details and just talk about the User ID (UID)
and Group ID (GID).
The priority for a process can be set by specifying a nice value, or niceness, for
the process. The lower the nice value, the higher the priority. Low values are
assigned to important processes, while high values are assigned to processes that
can wait longer. A process with a high nice value simply allows other processes
to be executed first. In Linux, a nice value of -20 represents the highest priority
and +19 represents the lowest. While this may sound backwards, this convention
(the nicer the process, the lower the priority) goes back to the earliest days of
UNIX.
nice Output
You can also assign a so-called real-time priority to time-sensitive tasks, such
as controlling machines through a computer or collecting incoming data. This is
just a very high priority and is not to be confused with what is called hard real-
time which is conceptually different, and has more to do with making sure a job
gets completed within a very well-defined time window.
Nice Values
The load average can be viewed by running w, top or uptime. We will explain
the numbers on the next page.
Load Averages
0.45: For the last minute the system has been 45% utilized on average.
0.17: For the last 5 minutes utilization has been 17%.
0.12: For the last 15 minutes utilization has been 12%.
If we saw a value of 1.00 in the second position, that would imply that the single-
CPU system was 100% utilized, on average, over the past 5 minutes; this is good
if we want to fully use a system. A value over 1.00 for a single-CPU system
implies that the system was over-utilized: there were more processes needing
CPU than CPU was available.
If we had more than one CPU, say a quad-CPU system, we would divide the load
average numbers by the number of CPUs. In this case, for example, seeing a 1
minute load average of 4.00 implies that the system as a whole was 100% (4.00/4)
utilized during the last minute.
Short-term increases are usually not a problem. A high peak you see is likely a
burst of activity, not a new level. For example, at start up, many processes start
and then activity settles down. If a high peak is seen in the 5 and 15 minute load
averages, it may be cause for concern.
Interpreting Load Averages
In such cases, you can run the job in the background and free the shell for other
tasks. The background job will be executed at lower priority, which, in turn, will
allow smooth execution of the interactive tasks, and you can type other
commands in the terminal window while the background job is running. By
default, all jobs are executed in the foreground. You can put a job in the
background by suffixing & to the command, for example: updatedb &.
jobs -l provides the same information as jobs, and adds the PID of the
background jobs.
The background jobs are connected to the terminal window, so, if you log off,
the jobs utility will not show the ones started from that window.
Managing Jobs
While you can still type in the terminal window, the shell will not pay
attention to what you input.
5. With jobs -l, see what processes have been launched from this terminal
window:
6. $ jobs -l
7. [1] 17705 Running evince *pdf &
8. [2]- 18248 Running emacs /tmp/hello.tex &
9. [3]+ 19827 Stopped gedit somefile
10. Now put the most recent job (gedit somefile) in background:
11. $ bg
12. [3]+ gedit somefile &
Note you once again input to the terminal window. has no effect
16. To clean up, suspend the process again and then use kill to terminate it:
17. ^Z
18. [3]+ Stopped gedit somefile
19. $ jobs -l
20. [1] 17705 Running evince *pdf &
21. [2]- 18248 Running emacs /tmp/hello.tex &
22. [3]+ 19827 Stopped gedit somefile
23. $ kill -9 19827
24. $ jobs -l
25. [1] 17705 Running evince *pdf &
26. [2]- 18248 Running emacs /tmp/hello.tex &
27. [3]+ 19827 Killed gedit somefile
28. $ jobs -l
29. [1]- 17705 Running evince *pdf &
[2]- 18248 Running emacs /tmp/hello.tex &
9.3 Listing Process: ps and top
9.3.1 The ps Command (System V Style)
ps provides information about currently running processes keyed by PID. If you
want a repetitive update of this status, you can use top or other commonly
installed variants (such as htop or atop) from the command line, or invoke your
distribution's graphical system monitor application.
ps has many options for specifying exactly which tasks to examine, what
information to display about them, and precisely what output format should be
used.
Without options, ps will display all processes running under the current shell.
You can use the -u option to display information of processes for a specified
username. The command ps -ef displays all the processes in the system in full
detail. The command ps -eLf goes one step further and displays one line of
information for every thread (remember, a process can contain multiple threads).
The screenshot shows a sample output of ps with the aux and axo qualifiers.
The ps Command (BSD Style)
9.3.4 top
While a static view of what the system is doing is useful, monitoring the system
performance live over time is also valuable. One option would be to run ps at
regular intervals, say, every few seconds. A better alternative is to use top to get
constant real-time updates (every two seconds by default), until you exit by
typing q.top clearly highlights which processes are consuming the most CPU
cycles and memory (using appropriate commands from within top).
top
The load average determines how busy the system is. A load average of 1.00 per
CPU indicates a fully subscribed, but not overloaded, system. If the load average
goes above this value, it indicates that processes are competing for CPU time. If
the load average is very high, it might indicate that the system is having a
problem, such as a runaway process (a process in a non-responding state).
First Line of the top Output
The percentage of user jobs running at a lower priority (niceness - ni) is then
listed. Idle mode (id) should be low if the load average is high, and vice
versa. The percentage of jobs waiting (wa) for I/O is listed. Interrupts include the
percentage of hardware (hi) vs. software interrupts (si). Steal time (st) is
generally used with virtual machines, which has some of its idle CPU time taken
for other uses.
Both categories display total memory, used memory, and free space.
You need to monitor memory usage very carefully to ensure good system
performance. Once the physical memory is exhausted, the system starts using
swap space (temporary storage space on the hard drive) as an extended memory
pool, and since accessing disk is much slower than accessing memory, this will
negatively affect system performance.
If the system starts using swap often, you can add more swap space. However,
adding more physical memory should also be considered.
The table lists what happens when pressing various keys when running top:
Command Output
9.5 cron
cron is a time-based scheduling utility program. It can launch routine background
jobs at specific times and/or days on an on-going basis. cron is driven by a
configuration file called /etc/crontab (cron table), which contains the various
shell commands that need to be run at the properly scheduled times. There are
both system-wide crontab files and individual user-based ones. Each line of
a crontab file represents a job, and is composed of a so-
called CRON expression, followed by a shell command to execute.
Typing crontab -e will open the crontab editor to edit existing jobs or to create
new jobs. Each line of the crontab file will contain 6 fields:
MIN Minutes 0 to 59
Examples:
9.6 sleep
Sometimes, a command or job must be delayed or suspended. Suppose, for
example, an application has read and processed the contents of a data file and
then needs to save a report on a backup system. If the backup system is currently
busy or not available, the application can be made to sleep (wait) until it can
complete its work. Such a delay might be to mount the backup device and prepare
it for writing.
sleep suspends execution for at least the specified period of time, which can be
given as the number of seconds (the default), minutes, hours, or days. After that
time has passed (or an interrupting signal has been received), execution will
resume.
sleep and at are quite different; sleep delays execution for a specific period,
while at starts execution at a later time.
sleep
Note that the command will run in the directory from which you schedule it
with at.
Do this:
$ chmod +x testat.sh
$ at now + 1 minute -f testat.sh
$ atq
17 Wed Apr 22 08:55:00 2015 a student
$ cat /tmp/datestamp
Wed Apr 22 08:55:00 CDT 2015
What happens if you take the /tmp/datestamp out of the command? (Hint:
type mail if not prompted to do so!)
4. Interactively it is basically the same procedure. Just queue up the job with:
5. $ at now + 1 minute
6. at> date > /tmp/datestamp
7. CTRL-D
$ atq
Lab 9.4: Scheduling a Periodic Task with cron
Set up a cron job to do some simple task every day at 10 a.m.
Set up a cron job to do some simple task every day at 10 AM. Create a file
named mycrontab with the following content:
0 10 * * * /tmp/myjob.sh
Note you if don't really want this running every day, printing out messages like:
Hello I am running /tmp/myjob.sh at Wed Apr 22 10:03:48 CDT 2015
If the machine is not up at 10 AM on a given day, anacron will run the job at a
suitable time.
Chương 10. File Operations
Mục tiêu:
On many systems (including Linux), the filesystem is structured like a tree. The
tree is usually portrayed as inverted, and starts at what is most often called
the root directory, which marks the beginning of the hierarchical filesystem and
is also sometimes referred to as the trunk, or simply denoted by /. The root
directory is not the same as the root user. The hierarchical filesystem also
contains other elements in the path (directory names), which are separated by
forward slashes (/), as in /usr/bin/emacs, where the last element is the actual file
name.
In this section, you will learn about some basic concepts, including the filesystem
hierarchy, as well as about disk partitions.
Filesystems
ext3
ext4
squashfs
btrfs.
It is often the case that more than one filesystem type is used on a machine, based
on considerations such as the size of files, how often they are modified, what kind
of hardware they sit on and what kind of access speed is needed, etc. The most
advanced filesystem types in common use are the journaling varieties: ext4, xfs,
btrfs, and jfs. These have many state-of-the-art features and high performance,
and are very hard to corrupt accidentally.
10.1.3 Linux Partitions
Each filesystem on a Linux system occupies a disk partition. Partitions help to
organize the contents of disks according to the kind and use of the data contained.
For example, important programs required to run the system are often kept on a
separate partition (known as root or /) than the one that contains files owned by
regular users of that system (/home). In addition, temporary files created and
destroyed during the normal operation of Linux may be located on dedicated
partitions. One advantage of this kind of isolation by type and variability is that
when all available space on a particular partition is exhausted, the system may
still operate normally.
The pictures shows the use of the gparted utility, which displays the partition
layout on a system which has three operating systems on it: RHEL 8, CentOS 7,
Ubuntu and Windows.
will attach the filesystem contained in the disk partition associated with
the /dev/sda5 device node, into the filesystem tree at the /home mount point.
There are other ways to specify the partition other than the device node, such as
using the disk label or UUID.
Note the command is umount, not unmount! Only a root user (logged in as root,
or using sudo) has the privilege to run these commands, unless the system has
been otherwise configured.
If you want it to be automatically available every time the system starts up, you
need to edit /etc/fstab accordingly (the name is short for filesystem table).
Looking at this file will show you the configuration of all pre-configured
filesystems. man fstab will display how this file is used and how to configure it.
Executing mount without any arguments will show all presently mounted
filesystems.
On the server machine, NFS uses daemons (built-in networking and service
processes in Linux) and other system servers are started at the command line by
typing:
The text file /etc/exports contains the directories and permissions that a host is
willing to share with other systems over NFS. A very simple entry in this file may
look like the following:
/projects *.example.com(rw)
This entry allows the directory /projects to be mounted using NFS with read and
write (rw) permissions and shared with other hosts in the example.com domain.
As we will detail in the next chapter, every file in Linux has three possible
permissions: read (r), write (w) and execute (x).
After modifying the /etc/exports file, you can type exportfs -av to notify Linux
about the directories you are allowing to be remotely mounted using NFS. You
can also restart NFS with sudo systemctl restart nfs, but this is heavier, as it
halts NFS for a short while before starting it up again. To make sure the NFS
service starts whenever the system is booted, issue sudo systemctl enable nfs.
You can also mount the remote filesystem without a reboot or as a one-time
mount by directly using the mount command:
Remember, if /etc/fstab is not modified, this remote mount will not be present
the next time the system is restarted. Furthermore, you may want to use
the nofail option in fstab in case the NFS server is not live at boot.
NFS on the Client
Now type:
student:/tmp> mount
Each user has a home directory, usually placed under /home. The /root ("slash-
root") directory on modern Linux systems is no more than the home directory of
the root user (or superuser, or system administrator account).
Sometimes, you may group users based on their department or function. You can
then create subdirectories under the /home directory for each of these groups. For
example, a school may organize /home with something like the following:
/home/faculty/
/home/staff/
/home/students/
Home Directories
Likewise, the /sbin directory is intended for essential binaries related to system
administration, such as fsckand ip. To view a list of these programs, type:
$ ls /bin /sbin
Commands that are not essential (theoretically) for the system to boot or operate
in single-user mode are placed in the /usr/bin and /usr/sbin directories.
Historically, this was done so /usr could be mounted as a separate filesystem that
could be mounted at a later stage of system startup or even over a network.
However, nowadays most find this distinction is obsolete. In fact, many
distributions have been discovered to be unable to boot with this separation, as
this modality had not been used or tested for a long time.
Thus, on some of the newest Linux distributions /usr/bin and /bin are actually
just symbolically linked together, as are /usr/sbin and /sbin.
10.2.5 The /proc Filesystem
Certain filesystems, like the one mounted at /proc, are called pseudo-
filesystems because they have no permanent presence anywhere on the disk.
The /proc filesystem contains virtual files (files that exist only in memory) that
permit viewing constantly changing kernel data. /proc contains files and
directories that mimic kernel structures and configuration information. It does not
contain real files, but runtime system information, e.g. system memory, devices
mounted, hardware configuration, etc. Some important entries in /proc are:
/proc/cpuinfo
/proc/interrupts
/proc/meminfo
/proc/mounts
/proc/partitions
/proc/version
/proc/<Process-ID-#>
/proc/sys
The first example shows there is a directory for every process running on the
system, which contains vital information about it. The second example shows a
virtual directory that contains a lot of information about the entire system, in
particular its hardware and configuration. The /proc filesystem is very useful
because the information it reports is gathered only as needed and never needs
storage on the disk.
The /proc Filesystem
Contains entries which are created by the udev system, which creates and
manages device nodes on Linux, creating them dynamically when devices are
found. The /dev directory contains items such as:
The /var directory may be put on its own filesystem so that growth of the files
can be accommodated and any exploding file sizes do not fatally affect the
system. Network services directories such as /var/ftp (the FTP service)
and /var/www (the HTTP web service) are also found under /var.
Note that /etc is for system-wide configuration files and only the superuser can
modify files there. User-specific configuration files are always found under their
home directory.
The /var Directory
1. vmlinuz
The compressed Linux kernel, required for booting.
2. initramfs
The initial ram filesystem, required for booting, sometimes called initrd, not
initramfs.
3. config
The kernel configuration file, only used for debugging and bookkeeping.
4. System.map
Kernel symbol table, only used for debugging.
The screenshot shows an example listing of the /boot directory, taken from a
RHEL system that has multiple installed kernels, including both distribution-
supplied and custom-compiled ones. Names will vary and things will tend to look
somewhat different on a different distribution.
Most of these are what is known as dynamically loaded libraries (also known as
shared libraries or Shared Objects (SO)). On some Linux distributions there exists
a /lib64 directory containing 64-bit libraries, while /lib contains 32-bit versions.
i.e., just like for /bin and /sbin, the directories just point to those under /usr.
Kernel modules (kernel code, often device drivers, that can be loaded and
unloaded without re-starting the system) are located in /lib/modules/<kernel-
version-number>.
/lib/modules Contents
While historically this was done under the /media directory, modern Linux
distributions place these mount points under the /run directory. For example,
a USB pen drive with a label myusbdrive for a user name student would be
mounted at /run/media/student/myusbdrive.
The /mnt directory has been used since the early days of UNIX for temporarily
mounting filesystems. These can be those on removable media, but more often
might be network filesystems, which are not normally mounted. Or these can be
temporary partitions, or so-called loopback filesystems, which are files which
pretend to be partitions.
Directory Usage
Name
diff is used to compare files and directories. This often-used utility program has
many useful options (see: man diff) including:
In this section, you will learn additional methods for comparing files and how to
apply patches to files.
Distributing just the patch is more concise and efficient than distributing the
entire file. For example, if only one line needs to change in a file that contains
1000 lines, the patch file will be just a few lines long.
10.5.3 Using patch
To apply a patch, you can just do either of the two methods below:
The first usage is more common, as it is often used to apply changes to an entire
directory tree, rather than just one file, as in the second example. To understand
the use of the -p1 option and many others, see the man page for patch.
The real nature of a file can be ascertained by using the file utility. For the file
names given as arguments, it examines the contents and certain characteristics to
determine whether the files are plain text, shared libraries, executable programs,
scripts, or something else.
It would be a good idea to read the man pages for both patch and diff to learn
more about advanced options and techniques, that will help one to work more
effectively with patch. In particular, the form of patches has a lot to do with
whether they can be accepted in their submitted form.
Note you could have also done either of these two commands:
Both can be used to synchronize entire directory trees. However, rsync is more
efficient, because it checks if the file being copied already exists. If the file exists
and there is no change in size or modification time, rsync will avoid an
unnecessary copy and save time. Furthermore, because rsync copies only the
parts of files that have actually changed, it can be very fast.
cp can only copy files to and from destinations on the local machine (unless you
are copying to or from a filesystem mounted using NFS), but rsync can also be
used to copy files from one machine to another. Locations are designated in
the target:path form, where target can be in the form of someone@host.
The someone@ part is optional and used if the remote user is different from the
local user.
rsync is very efficient when recursively copying one directory tree to another,
because only the differences are transmitted over the network. One often
synchronizes the destination directory tree with the origin, using the -r option to
recursively walk down the directory tree copying all files and directories below
the one listed as the source.
Note that rsync can be very destructive! Accidental misuse can do a lot of harm
to data and programs, by inadvertently copying changes to where they are not
wanted. Take care to specify the correct options and paths. It is highly
recommended that you first test your rsync command using the -dry-run option
to ensure that it provides the results that you want.
To use rsync at the command prompt, type rsync sourcefile destinationfile,
where either file can be on the local machine or on a networked machine; The
contents of sourcefile will be copied to destinationfile.
Command Usage
These techniques vary in the efficiency of the compression (how much space is
saved) and in how long they take to compress; generally, the more efficient
techniques take longer. Decompression time does not vary as much across
different methods.
In addition, the tar utility is often used to group files in an archive and then
compress the whole archive at once.
Command Usage
gzip -r Compresses all files in the projectX directory, along with all
projectX files in all of the directories under projectX.
gunzip foo De-compresses foo found in the file foo.gz. Under the hood,
the gunzip command is actually the same as gzip –d.
10.6.5 Compressing Data Using bzip2
bzip2 has a syntax that is similar to gzip but it uses a different compression
algorithm and produces significantly smaller files, at the price of taking a longer
time to do its work. Thus, it is more likely to be used to compress larger files.
Command Usage
bzip2 * Compresses all of the files in the current directory and replaces each
file with a file renamed with a .bz2 extension.
bunzip2 Decompresses all of the files with an extension of .bz2 in the current
*.bz2 directory. Under the hood, bunzip2 is the same as calling bzip2 -d.
NOTE: bzip2 has lately become deprecated due to lack of maintenance and the
superior compression ratios of xz which is actively maintained.
Command Usage
The zip program is not often used to compress files in Linux, but is often required
to examine and decompress archives from other operating systems. It is only used
in Linux when you get a zipped file from a Windows user. It is a legacy program.
Command Usage
zip backup * Compresses all files in the current directory and places
them in the backup.zip.
zip -r backup.zip ~ Archives your login directory (~) and all files and
directories under it in backup.zip.
unzip backup.zip Extracts all files in backup.zip and places them in the
current directory.
10.6.6 Archiving and Compressing Data Using tar
Historically, tar stood for "tape archive" and was used to archive files to a
magnetic tape. It allows you to create or extract files from an archive file, often
called a tarball. At the same time, you can optionally compress while creating
the archive, and decompress while extracting its contents.
tar zcvf mydir.tar.gz mydir Create the archive and compress with gzip.
tar jcvf mydir.tar.bz2 Create the archive and compress with bz2.
mydir
tar Jcvf mydir.tar.xz mydir Create the archive and compress with xz.
but this is slower and wastes space by creating an unneeded intermediary .tar file.
This shows that as compression factors go up, CPU time does as well (i.e.
producing smaller archives takes longer).
WARNING!
Typing:
dd if=/dev/sda of=/dev/sdb
to make a copy of one disk onto another, will delete everything that previously
existed on the second disk.
An exact copy of the first disk device is created on the second disk device.
Exactly what the name dd stands for is an often-argued item. The words data
definition is the most popular theory and has roots in early IBM history. Often,
people joke that it means disk destroyer and other variants such as delete data!
Furthermore, while your hardware may be deemed fairly reliable, all devices do
fail in some fashion eventually (even if it is just an unexpected power failure).
Often, this happens at the worst possible time. Periodically backing up files is a
good habit to get into.
First, using tar, back up all files and subdirectories under your home directory.
Place the resulting tarball file in the /tmp directory, giving it the
name backup.tar.
Second, accomplish the same task with gzip compression using the -z option
to tar, creating /tmp/backup.tar.gz.
or equivalently
student:/tmp> tar -cvf /tmp/backup.tar /home/student
Note you can have omitted the - in the options with no change. In the following
we will not bother using the -v option for verbose. To create archives with all
three compression utilities:
student:/tmp> tar zcf /tmp/backup.tar.gz ~
student:/tmp> tar jcf /tmp/backup.tar.bz2 ~
student:/tmp> tar Jcf /tmp/backup.tar.xz ~
Comparing the sizes (first using the -h option to ls to make it human-readable):
student@ubuntu:~student:/tmp> ls -lh /tmp/backup*
-rw-rw-r-- 1 student student 8.3M Apr 17 10:14 /tmp/backup2.tar.gz
-rw-rw-r-- 1 student student 12M Apr 17 10:13 /tmp/backup.tar
-rw-rw-r-- 1 student student 8.4M Apr 17 10:15 /tmp/backup.tar.bz2
-rw-rw-r-- 1 student student 8.3M Apr 17 10:14 /tmp/backup.tar.gz
-rw-rw-r-- 1 student student 8.2M Apr 17 10:15 /tmp/backup.tar.xz
Note in this case there is not much difference in the different archiving methods,
but this particular directory was a bad choice because it already contained a lot of
compressed files. A somewhat better example involving more text files:
student:/tmp> tar cf /tmp/doc.tar /usr/share/doc
student:/tmp> tar zcf /tmp/doc.tar.gz /usr/share/doc
student:/tmp> tar jcf /tmp/doc.tar.bz2 /usr/share/doc
student:/tmp> tar Jcf /tmp/doc.tar.xz /usr/share/doc
which shows xz did best, followed by bz2 and then gz. You may have noticed,
however, the inverse relationship between the size reduction of the compression
and how long it took!
Chapter 14. Network Operations
Mục tiêu:
14.2 IP Addresses
Devices attached to a network must have at least one unique network address
identifier known as the IP (Internet Protocol) address. The address is essential
for routing packets of information through the network.
IP Addresses
IPv4 uses 32-bits for addresses; there are only 4.3 billion unique addresses
available. Furthermore, many addresses are allotted and reserved, but not actually
used. IPv4 is considered inadequate for meeting future needs because the number
of devices available on the global network has increased enormously in recent
years.
IPv6 uses 128-bits for addresses; this allows for 3.4 X 10 38 unique addresses. If
you have a larger network of computers and want to add more, you may want to
move to IPv6, because it provides more unique addresses. However, it can be
complex to migrate to IPv6; the two protocols do not always inter-operate well.
Thus, moving equipment and addresses to IPv6 requires significant effort and has
not been quite as fast as was originally intended. We will discuss IPv4 more
than IPv6 as you are more likely to deal with it.
One reason IPv4 has not disappeared is there are ways to effectively make many
more addresses available by methods such
as NAT (Network Address Translation). NAT enables sharing one IP address
among many locally connected computers, each of which has a unique address
only seen on the local network. While this is used in organizational settings, it
also used in simple home networks. For example, if you have a router hooked up
to your Internet Provider (such as a cable system) it gives you one externally
visible address, but issues each device in your home an individual local address.
Example:
IP address → 172 . 16 . 31 . 46
Each Class A network can have up to 16.7 million unique hosts on its network.
The range of host address is from 1.0.0.0 to 127.255.255.255.
Each Class B network can support a maximum of 65,536 unique hosts on its
network. The range of host address is from 128.0.0.0 to 191.255.255.255.
Each Class C network can support up to 256 (8-bits) unique hosts. The range of
host address is from 192.0.0.0 to 223.255.255.255.
IP Address Allocation
You can assign IP addresses to computers over a network either manually or
dynamically. Manual assignment adds static (never changing) addresses to the
network. Dynamically assigned addresses can change every time you reboot or
even more often; the Dynamic Host Configuration Protocol (DHCP) is used to
assign IP addresses.
Given an IP address, you can obtain its corresponding hostname. Accessing the
machine over the network becomes easier when you can type the hostname
instead of the IP address.
You can view your system’s hostname simply by typing hostname with no
argument.
NOTE: If you give an argument, the system will try to change its hostname to
match it, however, only root users can do that.
The special hostname localhost is associated with the IP address 127.0.0.1 and
describes the machine you are currently on (which normally has additional
network-related IP addresses).
Screenshot Showing Server IP Address of The Linux Foundation Website
Please take a look at the following Try-It-Yourself exercise: Using Domain Name
System (DNS) and Name Resolution Tools.
For Debian family configurations, the basic network configuration files could be
found under /etc/network/, while for Red Hat and SUSE family systems one
needed to inspect /etc/sysconfig/network.
Network Manager
Recent Ubuntu distributions include netplan, which is turned on by default, and
supplants Network Manager. Since no other distribution has shown interest, and
since it can easily be disabled if it bothers you, we will ignore it.
Network Interfaces
ip is a very powerful program that can do many things. Older (and more specific)
utilities such as ifconfig and route are often used to accomplish similar tasks. A
look at the relevant man pages can tell you much more about these utilities.
ip Utility
14.9 ping
ping is used to check whether or not a machine attached to the network can
receive and send data; i.e. it confirms that the remote host is online and is
responding.
To check the status of the remote host, at the command prompt, type ping
<hostname>.
ping is frequently used for network testing and management; however, its usage
can increase network load unacceptably. Hence, you can abort the execution
of ping by typing CTRL-C, or by using the -c option, which limits the number
of packets that ping will send before it quits. When execution stops, a summary
is displayed.
ping
14. 10 route
A network requires the connection of many nodes. Data moves from source to
destination by passing through a series of routers and potentially across multiple
networks. Servers maintain routing tables containing the addresses of each node
in the network. The IP routing protocols enable routers to build up a forwarding
table that correlates final destinations with the next hop addresses.
route
One can use the route utility or the newer ip route command to view or change
the IP routing table to add, delete, or modify specific (static) routes to specific
hosts or networks. The table explains some commands that can be used to manage
IP routing:
Task Command
traceroute
traceroute is used to inspect the route which the data packet takes to reach the
destination host, which makes it quite useful for troubleshooting network delays
and errors. By using traceroute, you can isolate connectivity issues between
hops, which helps resolve them faster.
To print the route taken by the packet to reach the network host, at the command
prompt, type traceroute <address>.
traceroute
Networking Description
Tools
Transfering Files
When you are connected to a network, you may need to transfer files from one
machine to another. File Transfer Protocol (FTP) is a well-known and popular
method for transferring files between computers using the Internet. This method
is built on a client-server model. FTP can be used within a browser or with stand-
alone client programs.
FTP is one of the oldest methods of network data transfer, dating back to the early
1970s. As such, it is considered inadequate for modern needs, as well as being
intrinsically insecure. However, it is still in use and when security is not a concern
(such as with so-called anonymous FTP) it can make sense. However, many
websites, such as kernel.org, have abandoned its use.
FTP Clients
FTP clients enable you to transfer files with remote computers using the FTP
protocol. These clients can be either graphical or command line tools. Filezilla,
for example, allows use of the drag-and-drop approach to transfer files between
hosts. All web browsers support FTP, all you have to do is give a URL
like ftp://ftp.kernel.org where the usual http:// becomes ftp://.
ftp
sftp
ncftp
yafc (Yet Another FTP Client).
FTP has fallen into disfavor on modern systems, as it is intrinsically insecure,
since passwords are user credentials that can be transmitted without encryption
and are thus prone to interception. Thus, it was removed in favor of
using rsync and web browser https access for example. As an alternative, sftp is
a very secure mode of connection, which uses the Secure Shell (ssh) protocol,
which we will discuss shortly. sftp encrypts its data and thus sensitive
information is transmitted more securely. However, it does not work with so-
called anonymous FTP (guest user credentials).
FTP Clients
Secure Shell (SSH) is a cryptographic network protocol used for secure data
communication. It is also used for remote services and other secure services
between two devices on the network and is very useful for administering systems
which are not easily available to physically work on, but to which you have
remote access.
If you want to run as another user, you can do either ssh -l someone
some_system or ssh someone@some_system. To run a command on a remote
system via SSH, at the command prompt, you can type ssh some_system
my_command.
We can also move files securely using Secure Copy (scp) between two networked
hosts. scp uses the SSH protocol for transferring data.
To copy a local file to a remote system, at the command prompt, type scp
<localfile> <user@remotesystem>:/home/user/ and press Enter.
You will receive a prompt for the remote password. You can also
configure scp so that it does not prompt for a password for each transfer.
The solution file contains a step-by-step procedure for exercising many of the
tools we have studied. Please repeat the steps, substituting your actual network
interface names, alternative network addresses and web sites, etc.
Click the link below to view a solution to the Lab exercise.
Suppose you need to perform an Internet search, but your web browser can not
find google.com, saying the host is unknown. Let's proceed step by step to fix
this.
student:/tmp> /sbin/ifconfig
eno167777 Link encap:Ethernet HWaddr 00:0C:29:BB:92:C2
inet addr:192.168.1.14 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:febb:92c2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500
Metric:1
RX packets:3244 errors:0 dropped:0 overruns:0 frame:0
TX packets:2006 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4343606 (4.1 Mb) TX bytes:169082 (165.1 Kb)
Does the IP address look valid? Depending on where you are using this
from, it is most likely a Class C IP address; in the above this
is 192.168.1.14
If it does not show a device with an IP address, you may need to start or
restart the network and/or NetworkManager. Exactly how you do this
depends on your system. For most distributions one of these commands
will accomplish this:
If your device was up but had no IP address, the above should have helped
fix it, but you can try to get a fresh address with:
student:/tmp> hostname
openSUSE
It is rare you would have a problem here, as there is probably always at
least a default hostname, such as localhost.
3. When you type in a name of a site such as google.com, that name needs to
be connected to a known IP address. This is usually done employing
the DNS sever (Domain Name System)
Note:
It is likely that something is wrong with your DNS set-up. (Note on some
systems you will never see the unknown host message, but you will get a
suspicious result like:
where the 127.0.x.x address is a loop feeding back to the host machine you
are on. You can eliminate this as being a valid address by doing:
host 8.8.8.8
8.8.8.8.in-addr.arpa domain name pointer google-public-dns-
a.google.com.
student@linux:~> host google.com 8.8.8.8
Using domain server:
Name: 8.8.8.8
Address: 8.8.8.8#53
Aliases:
Note that there is another file, /etc/hosts, where you can associate names
with IP addresses, which is used before the DNS server is consulted. This
is most useful for specifying nodes on your local network.
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; MBZ: 1c20 , udp: 1280
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 244 IN A 173.194.46.67
google.com. 244 IN A 173.194.46.65
google.com. 244 IN A 173.194.46.71
google.com. 244 IN A 173.194.46.73
google.com. 244 IN A 173.194.46.69
google.com. 244 IN A 173.194.46.68
google.com. 244 IN A 173.194.46.64
google.com. 244 IN A 173.194.46.72
google.com. 244 IN A 173.194.46.70
google.com. 244 IN A 173.194.46.66
google.com. 244 IN A 173.194.46.78
4. Suppose host or dig fail to connect the name to an IP address. There are
many reasons DNS can fail, some of which are:
o The DNS server is down. In this case try pinging it to see if it is alive
(you should have the IP address in /etc/resolv.conf.
o The server can be up and running, but DNS may not be currently
available on the machine.
o Your route to the DNS server may not be correct.
How can we test the route? Tracing the route to one of the public name
server we mentioned before:
Again, this should likely work for you, but what if you only got the first
line in the traceroute output?
If this happened, most likely your default route is wrong. Try:
Most likely this is set to your network interface and the IP address of your
router, DSL, or Cable Modem. Let's say that it is blank or simply points to
your own machine. Here's your problem! At this point, you would need to
add a proper default route and run some of the same tests we just did.
Have a good grasp of best practices and tools for making Linux systems as secure
as possible.
Understand the powers and dangers of using the root (superuser) account.
Use the sudo command to perform privileged operations while restricting
enhanced powers as much as feasible.
Explain the importance of process isolation and hardware access.
Work with passwords, including how to set and change them.
Describe how to secure the boot process and hardware resources.
User Accounts
The Linux kernel allows properly authenticated users to access files and
applications. While each user is identified by a unique integer (the user id or
UID), a separate database associates a username with each UID. Upon account
creation, new user information is added to the user database and the user's home
directory must be created and populated with some essential files. Command line
programs such as useradd and userdel as well as GUI tools are used for creating
and removing accounts.
For each user, the following seven fields are maintained in the /etc/passwd file:
User ID Every user must have a user id UID 0 is reserved for root user
(UID) (UID) UID's ranging from 1-99 are
reserved for other predefined
accounts
UID's ranging from 100-999 are
reserved for system accounts and
groups
Normal users have UID's of 1000
or greater
User Info This field is optional and allows For example: Rufus T. Firefly
insertion of extra information
about the user such as their name
Types of Accounts
By default, Linux distinguishes between several account types in order to isolate
processes and workloads. Linux has four types of accounts:
root
System
Normal
Network
Keep in mind that practices you use on multi-user business systems are more strict
than practices you can use on personal desktop systems that only affect the casual
user. This is especially true with security. We hope to show you practices
applicable to enterprise servers that you can use on all systems, but understand
that you may choose to relax these rules on your own personal system.
last Utility
Understanding the root Account
SUID (Set owner User ID upon execution - similar to the Windows "run as"
feature) is a special kind of file permission given to a file. Use of SUID provides
temporary permissions to a user to run a program with the permissions of the
file owner (which may be root) instead of the permissions held by the user.
The table provides examples of operations which do not require root privileges:
Operations on files that the user has Accessing files that you have access to
proper permissions to access or sharing data over the network
Running SUID-root applications Executing programs such as passwd
In Linux you can use either su or sudo to temporarily grant root access to a
normal user. However, these methods are actually quite different. Listed below
are the differences between the two commands:
su sudo
When elevating privilege, you need to When elevating privilege, you need to
enter the root password. Giving the root enter the user’s password and not the
password to a normal user should never, root password.
ever be done.
Once a user elevates to the root account Offers more features and is considered
using su, the user can do anything that more secure and more configurable.
the root user can do for as long as the Exactly what the user is allowed to do
user wants, without being asked again can be precisely controlled and limited.
for a password. By default the user will either always
have to keep giving their password to do
further operations with sudo, or can
avoid doing so for a configurable time
interval.
The command has limited logging The command has detailed logging
features. features.
sudo Features
sudo has the ability to keep track of unsuccessful attempts at gaining root access.
Users' authorization for using sudo is based on configuration information stored
in the /etc/sudoers file and in the /etc/sudoers.d directory.
sudo Features
Whenever sudo is invoked, a trigger will look at /etc/sudoers and the files
in /etc/sudoers.d to determine if the user has the right to use sudo and what the
scope of their privilege is. Unknown user requests and requests to do operations
not allowed to the user even with sudo are reported. The basic structure of entries
in these files is:
You should edit any of these configuration files by using visudo, which ensures
that only one person is editing the file at a time, has the proper permissions, and
refuses to write out the file and exit if there are syntax errors in the changes made.
The editing can be accomplished by doing a command such as the following ones:
# visudo /etc/sudoers
# visudo -f /etc/sudoers.d/student
Command Logging
Running a command such as sudo whoami results in a log file entry such as:
Command Logging
Process Isolation
Linux is considered to be more secure than many other operating systems because
processes are naturally isolated from each other. One process normally cannot
access the resources of another process, even when that process is running with
the same user privileges. Linux thus makes it difficult (though certainly not
impossible) for viruses and security exploits to access and attack random
resources on a system.
More recent additional security mechanisms that limit risks even further include:
Control Groups (cgroups): Allows system administrators to group processes and
associate finite resources to each cgroup.
Containers: Makes it possible to run multiple isolated Linux systems (containers)
on a single system by relying on cgroups.
Virtualization: Hardware is emulated in such a way that not only processes can
be isolated, but entire systems are run simultaneously as isolated and insulated
guests (virtual machines) on one physical host.
Process Isolation
Hard disks, for example, are represented as /dev/sd*. While a root user can read
and write to the disk in a raw fashion, for example, by doing something like:
Keeping Current
When security problems in either the Linux kernel or applications and libraries
are discovered, Linux distributions have a good record of reacting quickly and
pushing out fixes to all systems by updating their software repositories and
sending notifications to update immediately. The same thing is true with bug fixes
and performance improvements that are not security related.
Timely System Update
However, it is well known that many systems do not get updated frequently
enough and problems which have already been cured are allowed to remain on
computers for a long time; this is particularly true with proprietary operating
systems where users are either uninformed or distrustful of the vendor's patching
policy as sometimes updates can cause new problems and break existing
operations. Many of the most successful attack vectors come from exploiting
security holes for which fixes are already known but not universally deployed.
1. Create a new user, using useradd, and give the user an initial password
with passwd.
2. Configure this user to be able to use sudo.
3. Login as or switch to this new user and make sure you can execute a command
that requires root privilege.
$ ls /root
3. With root privilege, (use sudo visudo) add this line to /etc/sudoers:
sudo su newuser
or
ssh newuser@localhost
sudo ls /root
Originally, encrypted passwords were stored in the /etc/passwd file, which was
readable by everyone. This made it rather easy for passwords to be cracked.
How Passwords Are Stored
Password Algorithm
The SHA-512 algorithm is widely used for security applications and protocols.
These security applications and protocols include TLS, SSL, PHP, SSH, S/MIME
and IPSec. SHA-512 is one of the most tested hashing algorithms.
For example, if you wish to experiment with SHA-512 encoding, the word "test"
can be encoded using the program sha512sum to produce the SHA-512 form (see
graphic):
IT professionals follow several good practices for securing the data and the
password of every user.
Password aging is a method to ensure that users get prompts that remind them to
create a new password after a specific period. This can ensure that passwords, if
cracked, will only be usable for a limited amount of time. This feature is
implemented using chage, which configures the password expiry information for
a user.
Another method is to force users to set strong passwords
using Pluggable Authentication Modules (PAM). PAM can be configured to
automatically verify that a password created or modified using the passwd utility
is sufficiently strong. PAM configuration is implemented using a library
called pam_cracklib.so, which can also be replaced by pam_passwdqc.so to
take advantage of more options.
One can also install password cracking programs, such as John The Ripper, to
secure the password file and detect weak password entries. It is recommended
that written authorization be obtained before installing such tools on any system
that you do not own.
Using chage
With the newly created user from the previous exercise, look at the password
aging for the user.
Modify the expiration date for the user, setting it to be something that has passed,
and check to see what has changed.
When you are finished and wish to delete the newly created account, use userdel,
as in:
You can secure the boot process with a secure password to prevent someone from
bypassing the user authentication step. This can work in conjunction with
password protection for the BIOS. Note that while using a bootloader password
alone will stop a user from editing the bootloader configuration during the boot
process, it will not prevent a user from booting from an alternative boot media
such as optical disks or pen drives. Thus, it should be used with a BIOS password
for full protection.
For the older GRUB 1 boot method, it was relatively easy to set a password
for grub. However, for the GRUB 2 version, things became more complicated.
However, you have more flexibility, and can take advantage of more advanced
features, such as user-specific passwords (which can be their normal login ones).
Furthermore, you never edit grub.cfg directly; instead, you can modify the
configuration files in /etc/grub.d and /etc/defaults/grub, and then run update-
grub, or grub2-mkconfig and save the new configuration file.
Hardware Vulnerability
Your IT security policy should start with requirements on how to properly secure
physical access to servers and workstations. Physical access to a system makes it
possible for attackers to easily leverage several attack vectors, in a way that makes
all operating system level recommendations irrelevant.
For single user computers and those in a home environment some of the above
features (like preventing booting from removable media) can be excessive, and
you can avoid implementing
them. However, if sensitive
information is on your system
that requires careful protection,
either it shouldn't be there or it
should be better protected by
following the above guidelines.
Software Vulnerability
Like all software, hackers occasionally
find weaknesses in the Linux ecosystem.
The strength of the Linux (and open source
community in general) is the speed with
which such vulnerabilities are exposed and
remediated. While specific discussion of
vulnerabilities is beyond the scope of this
course, we have seeded a couple relevant
topics to the Discussion Board (under the
"Local Security" topic) to encourage
further discussion.