101.1 – Determine and configure hardware settings – 2
Utilities used to Manage Devices
HAL - Hardware Abstraction Layer
It abstracts your hardware details from you, say and first network care will be eth0. This way Linux will
see any hardware as a standard hardware and you will be able to replace the hardware easily.
Files to know:
Note: All can be displayed with the cat command.
/proc/bus/devices - USB devices on computer system
/proc/cpuinfo - system CPU information
/proc/devices - devices installed on computer system
/proc/dma - Direct Memory Addressing (DMA) channels currently used by the Linux system
/proc/ide/.** - Files with information on the IDE devices on computer system.
/proc/interrupts - IRQ’s being used currently by the Linux system (not used if no driver loaded for
device).
/proc/iomem - I/O ports assigned on computer system
/proc/modules - Kernel modules used on system
/proc/scsi/.** - Files with information on the SCSI devices on computer system.
/proc/version - Kernel version
/proc/partitions - Contains major and minor numbers of each partition as well as number of blocks and
partition name
/dev directory lists devices as file system
contains files representing device access points
devices include, terminal devices (tty), floppy (fd), hard disks (hd, sd), RAM (ram), cdrom (cd)
Examples: /dev/sda designates first hard disk, /dev/sda1 designates first partition on first hard
disk
Hard disks listed in /dev do not necessarily start with sd: they can start with hd, fd, or even some
other letter-combination
/dev/initrd is the initial RAM disk loaded by the bootloader. Newer kernels list RAM disks as
/dev/ram0, /dev/ram1, …
file --special-files /dev/sda shows partitions and startheads and startsectors of the partitions
Coldplug vs Hotplug Device
Hotplug is when you insert hardware into a running computer and Coldplug is when you have to turn
your computer off to install hardware. USB devices are hot pluggable while PCI cards should be cold-
plugged.
PCI Cards
Peripheral Component Interconnect (PCI) - Typically Plug and Play (PnP) devices.
lspci - command that displays info about PCI busses on system & devices connected to the busses.
lspci - k shows the kernel modules associated with each device.
USB Devices
Universal Serial Bus (USB) - protocol and hardware port for moving data to/from devices.
Protocol Standards:
1 - up to 127 devices. up to 120 Mbps data transfer
2 - up to 480 Mbps (4 x Standard 1)
3 - up to 4.8 Gbps (1,000 x Standard 2)
lsusb - Display information about USB devices on computer system.
SCSI Devices
Replaced (though some older systems still have them) IDE drives.
sg_scan - Scans SCSI bus and shows all SCSI devices.
sginfo -l - Shows all SCSI devices
modprobe to add and remove modules from the Linux Kernel
101. 2 – Boot the system – 3
Linux Boot Steps
Note: These are the basic steps. However, the exam expects you to know “the flow” of the boot process.
The steps can be broken up in various ways. Thus “Step 4” is not always as listed below.
1) Power-On Self-Test (POST)
2) Determines the first bootable device found in bootable media list (found in CMOS).
3) Find the Master Boot Record (MBR) in the first sector of the device (called the "boot sector"),
and loads the bootloader software into memory (RAM).
Bootloader
4) Creates a virtual temporary “disk” (ramdisk) in memory (RAM). The “disk” is actually a
filesystem called “initrd image.”
5) Loads the Linux kernel into memory (file name is: /boot/vmlinuz-version.gz, where version is
the current version number of the kernel).
Linux Kernel
Bootloader
Bootloader can be GRUB (1&2) or LILO which are great for disks less than 2TB.
/etc/lilo.conf
/boot/grub/grub.cfg
/boot/grub/menu.lst
GRUB Stages
1 - Stored on MBR. Points simply to stage 1.5 or state 2.
1.5 - Stored after MBR, but before first partition. Contains drivers needed to load Stage 2.
2 - Stored on disk partition. Shows a menu which allows selection of the kernel image to be loaded.
(Menu is from configuration file)
GRUB Configuration Files
/boot/grub/grub.conf (Fedora distribution).
/boot/grub/menu.lst (Debian or SUSE distribution)
How to Install Grub on Boot Device of Your Choice
Issue the command: grub-install device
Example: grub-install /dev/sda
Also can be more specific examples:
grub-install --root-directory=/boot hd0 (1st hard drive)
grub-install --root-directory=/boot hd1 (2nd hard drive)
Kernel Modules
General Kernel Module Notes
Kernel Module Commands
lsmod = shows information about what kernel modules are currently loaded on the system. (Basically
displays the contents of /proc/modules file.)
insmod = inserts (loads) a single module into the kernel. (needs the full module file name which ends in
.mo)
modprobe = loads a single module into the kernel (just needs the module name)
configuration file = /etc/modprobe.conf
options:
rmmod = removes a kernel module
Kernel
Kernel parameters (sometimes called boot parameters) supply the kernel with information about
hardware parameters that it might not determine on its own - say single user mod boot (S)
init
When the kernel finishes loading, it usually starts /sbin/init. This program remains running until the
system is shut down. It is always assigned process ID 1.
first process, process in charge, a big family tree of commands:
pstree
init is being replaced in many distros (say ubuntu with upstart) but still is in exam and has its own
section.
Dmesg
dmesg command will show the full data from kernel ring buffer up to now. But
cat /var/log/dmesg - will show only the data during the boot
/var/log/messages
After the init process comes up, syslog daemon will log messages. It has timestamps and will persist
during restarts.
Different Start-ups
Runlevels
0 - halt
1or S- Single User Mode
2 - Multi-user, no network
3 - Multi-user, network
4 - User-defined
5 - Multi-user, network, GUI
6 – reboot
Set the default runlevel
SysV:
/etc/inittab file, and line is formatted as follows:
id:#:initdefault:
Upstart:
Note: same as above (for some distributions moving to Upstart)
/etc/init/rc-sysinit.conf file, and lines is formatted as follows:
env DEFAULT_RUNLEVEL=#
systemd:
Note: systemd uses the term “target unit” instead of “runlevel”
Default target unit set via a symbolic link command as follows:
ln -sf /lib/systemd/system/runlevel#.target /etc/systemd/system/default.target
Note: the f option on the symbolic link command, forces any current symbolic link to be broken and the
new designated symbolic link to be enforced).
Startup shell script locations
SysV:
Three possible locations (depending upon the distribution):
Upstart:
No startup scripts.
Instead it uses startup configuration files, located in /etc/init.
Configuration files determine what runlevels a service or task start on.
systemd:
No startup scripts.
Instead it uses service configuration unit files in /etc/systemd/system or /lib/systemd/system
(which can be overwritten during software upgrades)
Switch System’s current runlevels
SysV :
Use either the telinit or init command (there are basically “twins”)
init runlevel
example: init 5
Current runlevel:
The runlevel command shows the previous and the current runlevel.
Default runlevel:
Enter the following command to quickly check your default runlevel:
grep :initdefault /etc/inittab
Upstart:
No such thing as runlevels (except for backwards compatibility purposes).
Concerned with system events:
An event is a Linux server occurrence that triggers a needed system state change.
systemd:
Runlevels are not called “runlevels” but instead are called target units.
A target unit is a service (or action) group consisting of a name, type, and configuration file.
A service unit is for managing daemons, while a target unit is simply a group of other units.
Switch a Service’s runlevel(s) to Start On
SysV:
chkconfig --level runlevels_to_start_on service_name on
Example: chkconfig --level 35 cups on
(Will start the cups daemon on runlevels 3 and 5)
Upstart:
Edit the /etc/init/service_name.conf file and change the start on and stop on parameters to the desired
runlevels (Yes…Upstart does not use runlevels, but it still kind-of does. It is in a heavy state of
development (2012-2013). Hopefully, it will settle down soon).
systemd:
Runlevels are not called “runlevels” but instead are called target units.
Edit the /etc/systemd/system/service_name.service file and change the WantedBy parameter to
desired .target (e.g. multi-user.target).
Auditing Services
SysV:
All services:
chkconfig --list
service --status-all
A single service:
chkconfig --list service_name
service service_name status
Example: service cups status
Upstart:
All services: initctl list
A single service: initctl status service_name
systemd:
All services: systemctl list-unit-files --type=service | grep -v disabled
A single service: systemctl status service_name.service
Stop/Start/Restart Services
SysV:
service service_name start
service service_name stop
service service_name restart
Upstart:
initctl start service_name
initctl stop service_name
initctl restart service_name
systemd:
systemctl start service_name.service
systemctl stop service_name.service
systemctl restart service_name.service
Shutdown and reboot from command line
shutdown command:
now (shuts down system immediately)
102.1 - Design hard disk layout - 2
Primary locations you need to know:
/ root - bottom of the directory tree, the root
/var - variable location, log files and dynamic content (such as web sites)
/home - this is the users home directory where their personal files are stored; each user gets their own
home directory /home/bennettnw2 /home/user
/boot - this is where the kernel is stored and supporting files for the kernel; also the boot files to get the
system to boot; often on a diff partition
/opt - third party software vendors use this directory to install their programs; this is also a good
candidate to be on its own partition; enterprise uses this directory extensively
/bin – Essential command binaries
/dev – Device files
/etc – Host-specific system configuration
/media – Mount point for removable media
/mnt – Mount point for mounting a filesystem temporarily
/tmp – Temporary files
Partitions:
In Linux world, devices are defined at /dev/. First SATA disk is /dev/sda , second SATA disk is /dev/sdb ,
… and first SCSI disk (older systems) is /dev/hda.
You have to PARTITION the disks, that is creating smaller parts on a big disk and calling them /dev/sda
(first partition on first SCSI disk) or /dev/hdb3 (3rd partition on second disk).
Swap Space
Swap is temporary storage that acts like RAM. When a percentage of RAM is full, the kernel will move
less used data to swap.
There are two ways to configure swap.
The swap partition is faster than using a swap file. (similar to a page file in windows) The partition is
faster because data is getting written directly to disk rather than data getting written to a file and then
to disk as when you are using a swap file.
The general rule of sizing of the swap space used to be 1.5x to 2.0x the size of your available RAM. With
RAM being much cheaper nowadays, the rule of thumb is to use at least .50x of your available RAM.
Partitions and Mount Points
/dev/sda is the first hard drive (ssusi, sata, etc.) the “a” means it is the first disk the kernel has found.
/dev/sda1 - the 1 means it is the first partition Normally you would have different partitions and those
would be represented by: /dev/sda2, /dev/sda3, etc. Each partition has a specific function within the
system.
Speaking of partitions and specific functions, each partition would need to be mounted to a specific
directory. For example
Commands for mounting
Introduction to LVM
Taking a quick look at LVM (Logical Volume Manager) LVM allows the create of “groups” or disks or
partitions that can be assembled into a single (or multiple) file systems
Example Layout of an LVM Group
On the bottom layer you have your PVs (Physical Volumes).
LVM Main Concepts
In many cases you need to resize your partitions or even add new disks and add them to your mount
points. LVM is designed for this.
LVM helps you create one partition from different disks and add or remove space to them. The main
concepts are:
LVM Commands to set up a layout
102. 2 – Install a Boot Manager – 2
Boot overview
System starts from BIOS and will do a self-test called POST. Then it will hand over the boot process to
the first sector of master boot record ( MBR ).
MBR is on track (Cylinder) 0, side (Head) 0 and Sector 1 of the first disk which defines CHS.
MBR is only 512bytes so we need a smart bootloader to handle larger boot managers and even multiple
systems. Some of these boot loaders are LILO , GRUB and GRUB.
Chain Loading is when a boot loader, loads another boot loader. This is done when a Linux bootloader
needs to start a Windows system.
Installing Grub
grub-install [device] is the command used to install GRUB to the specified device. You have to look for
the boot mount point with which to install GRUB. findmnt /boot will tell you the exact location as a
device; it will tell you exact disk and partition like, /dev/vda1. You can even make it easier and use
/dev/hd0 or ‘(hd0)’ to refer to the first drive of the system.
If you run grub by itself, it will look for the correct partition to install itself. You can even use a grub
“find” command where it will let you know where to find stage1 = /grub/stage1.
This search is relative to root being /boot. So, when the answer you receive is /grub/stage1, it really
means /boot/grub/stage
If your system is up and running fine. Running grub-install is potentially dangerous. This command is
usually run from a liveCD/USB where GRUB is getting installed to a new disk as part of a new system
installation. In short, I should not ever really have to run grub-install.
GRUB shell commands:
grub by itself from the bash shell as root will place you into the GRUB shell help will give you more
information regarding the list of commands you can use in the grub shell find find /grub/stage1 will tell
you (hd0,0) quit will quit the shell for us
Be sure to back up your grub config before tooling around with grub
What are the differences between MBR and GPT?
For GUID to work we needed a replacement for the traditional BIOS. This is where UEFI comes in
(Unified Extensible Firmware Interface).
Booting with GRUB2 on GPT with UEFI
Commands to run to modify our grub2 setup and check current config
IMPORTANT! RHEL based = grub2- and Debian based = grub-
LILO is the (almost) original LInux LOader. It does not depend on you using a specific
filesystem, it can boot Linux kernel images from floppy disks, and it can act as a bootmanager
for other operating systems.
LILO works something like this:
One of the important consequences of the way LILO works is that the location of data on the
disk may not change after /boot/map has been created. This means that if you modify the
configuration in /etc/lilo.conf, install a kernel upgrade, or rename files and directories, you
need to run /sbin/lilo again before things work as expected.
Interacting with the Boot Loader
Learning how to work with the GRUB boot menu in both GRUB and GRUB2. Practice booting the system
using different kernel boot options and learn how to reinstall GRUB to our disk. Also, a walkthrough of
boot using one command at a time.
GRUB Legacy menu
You will have to press any key to get to the menu before the kernel takes over. Once there you will be
able to:
▪ Select which kernel to boot into using your arrow keys
▪ Press the "a" key to add new kernel arguments to the kernel you wish to boot
o you can remove the quiet option
o you can remove the graphical
o you can add 2 to boot into runlevel 2
▪ Press the "c" key to get to the grub command line
o you can read out some of the files in the boot directory
o you can set new options for your configuration file
o you can even reinstall GRUB in the event GRUB breaks
▪ you could use the install command but that is for GRUB Pros
▪ the best way is to use the setup command setup (hd0)
GRUB2 Menu
This will take you directly to the install GRUB2 menu
▪ There is no option for "a"
▪ The "e" key to edit; it looks way different as it gives us all the options rather than just the
command line
▪ These are systemd options rather than sysvinit
o so instead of just appending 2 for runlevel 2; we would need to set up the unit as
systemd.unit and the target as rescue.target
o it would look like systemd.unit=rescue.target
o use <ctrl+x> to boot with the options we just changed
Booting GRUB Manually
▪ ls
▪ ls (hd0,1)/ will show us the boot directory contents
▪ set root=(hd0,1)
▪ linux /boot/vmlinz-4.24.9- 43 - generic root=/dev/vda
▪ intrd /boot/initrd.img-4.24.9- 43 - generic
▪ ram disks and kernel names are very similar
▪ use the boot command to boot with all the options we just set
102. 3 – Manage Shared Libraries - 2
Shared library
A shared library is a group of files that contains functionality that other applications can use. This is good
for developers who only have to focus on their application and can rely on the functionality of shared
libraries to interact with the operating system.
o /lib
o /usr/lib(for 32bit systems) /usr/lib64(for 64bit systems)
▪ some 64bit programs can still use the 32bit libraries
o /usr/local/lib
o /usr/share
o Statically linked library files are compiled into an application when the application is
installed
o Dynamically linked libraries are called when needed by an application
Commands and options when dealing with shared libraries
ldd prints out shared object (library) dependencies
o ldd /bin/cp shows what library files are linked to the cp application
ldconfig configures dynamic linker run-time binding, create a cache based on tlibrary directories
and can show you what is currently cached
o will create a cached listing of the most used libraries based off of a directory I’ve
provided
o this command needs to be run as root/sudo
/etc/ld.so.conf configuration file that points to directories and other configuration files that
hodl reference to library directory locations
o so this is the configuration file that links applications and libraries together?
LD_LIBRARY_PATH legacy environment variable that points to a path where library files can be
read from
o you can use this instead of a configuration file to link libraries and applications
o however, it is best to use a config file as this is old school and outdated but comes in
handy if you are just testing something out
o example of usage `$ export LD_LIBRARY_PATH=/opt/java/jre/lib
o similar to the PATH variable but this is for libraries
102.4 - Use Debian Package Management – 3
Advance Package Tool used as the default for Debian based Linux distros
How does APT work?
Commands and options used for apt
Using Debian Package (dpkg)
The Debian Package Utility
Some common commands and flags for use with dpkg
First you will want to download a .deb file using apt-get download [pkg name]
102.5 - Use RPM and YUM package management – 3
RedHat Package Manager (RPM) and Yellowdog Updater Modified (YUM) are fedora /redhat /rhel /
centos / … tools to manage packages. There are also gui tools for installing and updating. As you saw on
102.4, all package managers can do standard functions like installing, updating and removing packages.
YUM adds extra features likes automatic updates, dependency management and works with
repositories (collection on packages accessed over network or on a CD).
The Yellowdog Updater Modified
o global yum configuration options are set in /etc/yum.conf
o reads repository information from /etc/yum.repos.d
o caches latest repository information in /var/cache/yum
Other RPMs (RedHat Package Managers)
Common yum commands:
The Red Hat Package Manager (rpm)
o the application or utility
o default config files
o how and were to install files
o list of dependencies that the package requires
o located in /var/lib/rpm
o use the rpm --rebuilddb to repair a corrupted rpm database
o yum handles dependencies automatically but rpm does not
Common rpm Commands
NOTE: you can use which to see if a package is installed on your system or not
102. 6 – Linux as a virtualization guest – 1
Virtualization and Containers
What is a Virtual Machine**?**
Virtual Machine Basics:
Virtual Machines in the Cloud
What is a Container?
o able to rapidly deploy web servers when traffic is high
o Docker
o nspawn (from systemd)
o LXD
o OpenShift
What is the difference between the two and why is it important
o was invented to allow the sharing yet segregation of server instances from each other
o protects one operating system from another
o prevention of spare CPU cycles, memory, or disk space go to waste
▪ get the best bang for your buck to make use of all the resource
o emulating virtual hardware through a hyper visor
▪ heavy in terms of system requirements
o use shared operating systems
o more efficient in system resource terms
o more granular management of system resources
103. 1 – Work on the command line – 4
We study:
Terms and utilities:
The administrator’s job with Linux is primarily to operate on the command line. A command line or
console is a separate programmable environment with its own settings, capabilities, and tools. There are
many different shells, this lesson discusses bash, used in most modern operating systems.
You can see the name of the default shell used by the user at the end of each line of the / ** etc /
passwd file. The global command line settings are in the / etc / ** profile file, and the settings for each
user are in the files in his home directory.
However, there are several options for the location of the settings, depending on the family of operating
systems. In general, the settings are searched in the following order: ~ / .bash_profile, ~ / .bash_login,
and ~ / .profile (settings are taken from the first detected file).
The following common commands are used when working in the console (there are more of them, but
in this topic LPI for some reason focuses on these):
The console, as a separate working environment, contains its own variables: environment variables
(global variables used in the OS) and ordinary variables (working within a running console session). The
following commands are available for viewing variables :
To create a variable , use simple syntax variable_name = variable_value. To refer to a variable, the $ sign
is specified, for example:
X ** = 12 ** (set the variable X to 12);
echo $ ** X **** ** (display the value of the variable X _); _
To execute a command in the current directory (you can find it with the pwd command), you need to
specify the full path to the command (_ for example, / home / semaev / _script), if the current directory
is not listed in the values of the PATH environment variable.
The following commands are available for reference:
Separately, mention should be made of the exec command, which allows you to execute a command
outside the current shell, dropping superuser rights.
103. 2 – Process text streams using filters – 2
Objectives
Streams
In UNIX world a lot of data is in TEXT form. Log files, configurations, user inputs, list of files, …
. Filtering this data means taking an input stream of text and performing some conversion on the text
before sending it to an output stream. In this context, a streams is nothing more than “a sequence of
bytes that can be read or written using library functions that hide the details of an underlying device
from the application”.
In simple words, a text stream is an input of text from keyboard, a file, a network device, … and filtering
it is automatically changing it.
As you saw in previous section, modern programming environments and shells (including bash) use
three standard I/O streams:
Piping ( | )
In normal cases, you give input from keyboard and output to the monitor. But in real life of a system
admin, most inputs come from another commands. If you want to give the output of command1 as the
input of command2, you should PIPE them as command1 | command2.
this | looks like a pipe!
jadi@funlife:~/w/lpic/101$ ls - 1 | sort
12
62
amir
jadi
neda
you
jadi@funlife:~/w/lpic/101$ ls - 1 | sort - r
you
neda
jadi
amir
62
12
UNIX philosophy is building small, strong tools and combine them
Redirection ( > )
Another useful way of controlling the streams is >. This help you to redirect your output (mostly to a
file).
jadi@funlife:~/w/lpic/101$ ls - ltrh
total 0
Cat
This command simply outputs its input stream (or the filename you give it). As you saw in previous
section. As most commands, if you do not give an input to it, it will read the data from the keyboard.
jadi@funlife:~/w/lpic/101$ cat > mydata
test
this is the second line
bye
jadi@funlife:~/w/lpic/101$ cat mydata
test
this is the second line
bye
When inputting data, ctrl+d will end the stream.
it is also possible to add files to each other using cat:
jadi@funlife:~/w/lpic/101$ cat mydata directory_data
test
this is the second line
bye
total 0
Od
This command dump s files (shows files in formats other than text). Normal behaviour is OctalDump
(base 8):
jadi@funlife:~/w/lpic/101$ od mydata
0000000<PHONE_NUMBER>63<PHONE_NUMBER>50<PHONE_NUMBER>51<PHONE_NUMBER>50
0000020<PHONE_NUMBER>45<PHONE_NUMBER>56<PHONE_NUMBER>51<PHONE_NUMBER>42
0000040 005145
0000042
Not good… lets use two switches:
od is very useful to find problems in your text files - say finding out if you are using tabs or correct line
endings
Split
Will split files. It is very useful for transferring HUGE files on smaller media (say splitting a 3TB file to 8GB
parts and moving them to another machine with a USB Disk).
jadi@funlife:~/w/lpic/101$ cat mydata
hello
this is second line
but as you can see we are
still writing
and this is getting longer
.
.
and longer
and longer!
jadi@funlife:~/w/lpic/101$ ls
mydata
jadi@funlife:~/w/lpic/101$ split - l 2 mydata
jadi@funlife:~/w/lpic/101$ ls
mydata xaa xab xac xad xae
jadi@funlife:~/w/lpic/101$ cat xab
but as you can see we are
still writing
need to join these files? cat them with cat x* > originalfile.
wc
wc is word count. It counts the characters, lines and bytes in the input stream.
jadi@funlife:~/w/lpic/101$ wc mydata
9 25 121 mydata
It is very normal to count the line numbers with - l switch.
Head & Tail
Shows the head (top) of a file or its tail (bottom). The default lines to show is 10 but you can specify
with - n20 or - 20.
tali -f will continue showing the new lines which are being written at the eng of the file. Very useful.
Expand & Unexpand & tr
Expand will replace the tabs in a stream with spaces (normally 8 but can be defined with -n12 for 12):
jadi@funlife:~/w/lpic/101$ cat howcool
jadi 5
sina 6
rubic 2
you 12
jadi@funlife:~/w/lpic/101$ od - tc howcool
0000000 j a d i \t 5 \n s i n a \t 6 \n r u
0000020 b i c \t 2 \n y o u \t 1 2 \n
0000036
jadi@funlife:~/w/lpic/101$ expand howcool | od - tc
0000000 j a d i 5 \n s i n a
0000020 6 \n r u b i c 2 \n y o
0000040 u 1 2 \n
0000051
Unexpand will do the reverse. The default is converting only the initial blanks but his can be overrided
by using - a.
unexpand needs at least two spaces.
The tr command translates A to 1, B to 2 and C to 3 in a stream you have to tr ‘ABC’ ‘123’. It is a pure
filter so if you need to give it file to work on, you have to use cat:
jadi@funlife:~/w/lpic/101$ cat mydata
hello
this is second line
but as you can see we are
still writing
and this is getting longer
.
.
and longer
and longer!
jadi@funlife:~/w/lpic/101$ cat mydata | tr ‘and’ ‘AND’
hello
this is second liNe
but As you cAN see we Are
still writiNg
AND this is gettiNg loNger
.
.
AND loNger
AND loNger!
Note: all a s are replaced with A.
You should know that if you put - instead of a filename, the data will be replaced from the pipe (or
keyboard stdin).
jadi@funlife:~/w/lpic/101$ wc - l mydata | cat mydata - mydata
hello
this is second line
but as you can see we are
still writing
and this is getting longer
.
.
and longer
and longer!
9 mydata
hello
this is second line
but as you can see we are
still writing
and this is getting longer
.
.
and longer
and longer!
pr
This formats text for classic printers. The default header includes the filename and file creation date and
time, along with a page number and two lines of blank footer.
pr mydata
2015 - 01 - 04 17:58 mydata Page 1
hello
this is second line
but as you can see we are
still writing
and this is getting longer
.
.
and longer
and longer!
It is possible to print in two or more columns and other outdated fun stuff.
When output is created from multiple files or the standard input stream, the current date and time are
used instead of the filename and creation date.
nl
Simply numbers lines.
jadi@funlife:~/w/lpic/101$ nl mydata | head - 3
1 hello
2 this is second line
3 but as you can see we are
cat -n will also number lines.
fmt
Will reformat a text file within margins (say 80 columns width or 60 if you use -w60).
jadi@funlife:~/w/lpic/101$ fmt mydata
hello this is second line but as you can see we are still writing and
this is getting longer… and longer and longer!
sort & uniq
Will sorts its input(s).
jadi@funlife:~/w/lpic/101$ cat uses
you fedora
jadi ubuntu
rubic windows
neda mac
jadi@funlife:~/w/lpic/101$ cat howcool
jadi 5
sina 6
rubic 2
you 12
jadi@funlife:~/w/lpic/101$ sort howcool uses
jadi 5
jadi ubuntu
neda mac
rubic 2
rubic windows
sina 6
you 12
if you want to sort NUMERICALLY (so 9 is lower than 19), use - n - r will reverse the search
and the uniq removes duplicate entries from its input. Normal behaviour is removing only the
duplicated lines but you can change the behaviour for example by giving - f1 to force it to not check fist
field.
jadi@funlife:~/w/lpic/101$ uniq what_i_have.txt
laptop
socks
tshirt
ball
socks
glasses
jadi@funlife:~/w/lpic/101$ sort what_i_have.txt | uniq
ball
glasses
laptop
socks
tshirt
jadi@funlife:~/w/lpic/101$
As you can see, the input HAVE TO BE sorted for uniq to work
uniq has great switches:
jadi@funlife:~/w/lpic/101$ cat what_i_have.txt
laptop
socks
tshirt
ball
socks
glasses
jadi@funlife:~/w/lpic/101$ sort what_i_have.txt | uniq - c #show count of each item
1 ball
1 glasses
1 laptop
2 socks
1 tshirt
jadi@funlife:~/w/lpic/101$ sort what_i_have.txt | uniq - u #show only non-repeated items
ball
glasses
laptop
tshirt
jadi@funlife:~/w/lpic/101$ sort what_i_have.txt | uniq - d #show only repeated items
socks
how many things I have? wc -l what_i_have.txt :)
Cut
cut command will cut a column of one file. Good for separating fields:
Let’s cut the first field of a file.
jadi@funlife:~/w/lpic/101$ cat howcool
jadi 5
sina 6
rubic 2
you 12
jadi@funlife:~/w/lpic/101$ cut - f1 howcool
jadi
sina
rubic
you
normal delimiter is TAB. use -dx to change it to “x” or use | tr ’ ’ ‘\t’ | to convert spaces in your stream to
TABs.
It is also possible to cut fields 1, 2, 3 with - f1- 3 or only characters 4,5,7,8 with - c4,5,7,8.
#paste The paste command pastes lines from two or more files side-by-side! You can not do this in a
normal text editor.
jadi@funlife:~/w/lpic/101$ cat howcool
jadi 5
sina 6
rubic 2
you 12
jadi@funlife:~/w/lpic/101$ cat uses
you fedora
jadi ubuntu
rubic windows
neda mac
jadi@funlife:~/w/lpic/101$ paste howcool uses
jadi 5 you fedora
sina 6 jadi ubuntu
rubic 2 rubic windows
you 12 neda mac
join
Our final field-manipulating command is join, which joins files based on a matching field. The files
should be sorted on the join field.
jadi@funlife:~/w/lpic/101$ cat howcool
jadi 5
sina 6
rubic 2
you 12
jadi@funlife:~/w/lpic/101$ cat uses
you fedora
jadi ubuntu
rubic windows
neda mac
jadi@funlife:~/w/lpic/101$ sort howcool > howcool.sorted
jadi@funlife:~/w/lpic/101$ sort uses > uses.sorted
jadi@funlife:~/w/lpic/101$ join howcool.sorted uses.sorted
jadi 5 ubuntu
rubic 2 windows
you 12 fedora
join does not work on numeric fields unless the fields are all the same length. It default delimiter is any
white space (TAB, space) and it joins on first field. check man join for more info.
Sed
sed is stream editor. It is POWERFUL and can do magic! Just like most of the tools we saw, sed can work
as a filter or take its input from a file. It uses regular expressions and is a great tool for replacing text. If
you need to replace A with B only once in each line in a stream you have to say sed ‘s/A/B/’:
jadi@funlife:~/w/lpic/101$ cat uses
you fedora
jadi ubuntu
rubic windows
neda mac
jadi@funlife:~/w/lpic/101$ sed ‘s/ubuntu/debian/’ uses
you fedora
jadi debian
rubic windows
neda mac
jadi@funlife:~/w/lpic/101$
the pattern for changing EVERY occurrence of A to B in a line is sed ‘s/A/B/g’.
Remember escape characters? They also work here and this will remove every new line from a file and
will replace it with a space:
jadi@funlife:~/w/lpic/101$ cat mydata
hello
this is second line
but as you can see we are
still writing
jadi@funlife:~/w/lpic/101$ sed ‘s/ /\t/g’ mydata > mydata.tab
jadi@funlife:~/w/lpic/101$ cat mydata.tab
hello
this is second line
but as you can see we are
still writing
103. 3 – Perform basic file management – 4
Objectives
ls
Is used to list directories & files. It can use absolute and relative paths
$ ls - l
total 52
you can always mix switches. A famous one is - ltrh (long+human readable sizes+reverse time).
Copying, Moving & Deleting
cp
This will copy files from one place / name to another place / name. If the target is a directory, all sources
will be copied there.
cp source destination
mv
Will move or rename files or directories. It works like cp command. If you are moving a file on the same
file system, the inode wont change.
In general:
But use common sense when answering questions or using cp and mv in real life.
rm
Removes ( Deletes ) files.
General notes
Normally, the cp command will copy a file over an existing copy, if the existing file is writable. On the
other hand, the mv will not move or rename a file if the target exists. You can overcome this using the -
f switch.
Creating and removing directories
The mkdir command creates directories.
$ mkdir dirA dirB
$ ls - ltrh
$ mkdir newDir/insideNew/lastDir
mkdir: cannot create directory ‘newDir/insideNew/lastDir’: No such file or directory
$ mkdir - p newDir/insideNew/lastDir
$ ls newDir/insideNew/ - ltrh
total 4.0K
drwxrwxr-x 2 jadi jadi 4.0K Jan 8 17:13 lastDir
If you need to delete a directory the command is rmdir and you can also use the -p for nested removing:
$ tree
.
├── dirA
├── dirB
├── howcool.sort
└── uses.sort
2 directories, 2 files
$ rmdir dirA dirB
$ mkdir - p newDir/insideNew/lastDir
$ tree
.
├── howcool.sort
├── newDir
│ └── insideNew
│ └── lastDir
└── uses.sort
3 directories, 2 files
$ rmdir - p newDir/insideNew/lastDir
$ tree
.
├── howcool.sort
└── uses.sort
0 directories, 2 files
If you are using rmdir to remove a directory, it MUST BE EMPTY!
Handling multiple files at once
Most of the times we need to work with more than one file. This is Linux and there are ways!
Recursive commands
Recursive means going inside and inside and inside and inside! In many commands -r or -R is dedicated
to recursive commands. Say ls. It uses -R :
$ ls
howcool.sort newDir uses.sort
$ ls - R
.:
howcool.sort newDir uses.sort
./newDir:
insideNew TestFile
./newDir/insideNew:
lastDir
./newDir/insideNew/lastDir:
It is more useful when you are copying or deleting. When using cp or rm, -r (or -R or --recursive) will
copy/delete all files inside the given source.
$ tree mydir
mydir
├── howcool.sort
├── newDir
│ ├── insideNew
│ │ └── lastDir
│ └── TestFile
└── uses.sort
3 directories, 3 files
$ mkdir newCopy
$ cp mydir newCopy
cp: omitting directory ‘mydir’
$ cp - r mydir newCopy
$ tree newCopy/
newCopy/
└── mydir
├── howcool.sort
├── newDir
│ ├── insideNew
│ │ └── lastDir
│ └── TestFile
└── uses.sort
4 directories, 3 files
Same works with rm :
$ rm newCopy
rm: cannot remove ‘newCopy’: Is a directory
$ rm - r newCopy
As you can see we can not rm a file but if using -r (or -R or --recursive) it works because it deletes the dir
and whatever inside it.
rm -rf / is EXTREMELY DANGEROUS: force delete whatever in /
Wildcards and globbing
This is a way to say All files or everything which starts with A or all files with 3 letter names which end
in A or B or C.
There are main cases:
So… this means that you can use these patterns in your commands to point to these files:
command meaning
rm * delete all files
ls A*B all files starting with A ending with B
cp ???.* /tmp Copy all files with 3 characters, then a dot then whatever (even nothing) to /tmp
rmdir [a-z]* remove all directories which start with a letter
Touch
The touch command with no option will update the modification date of a file to the current time (will
create a file if it is not exists).
/touch$ ls - l
total 0
[ian@echidna lpi103-2]$ touch - t<PHONE_NUMBER>10.59 f3
[ian@echidna lpi103-2]$ touch - d 11am f4
[ian@echidna lpi103-2]$ touch - d “last fortnight” f5
[ian@echidna lpi103-2]$ touch - d “yesterday 6am” f6
[ian@echidna lpi103-2]$ touch - d “2 days ago 12:00” f7
[ian@echidna lpi103-2]$ touch - d “tomorrow 02:00” f8
[ian@echidna lpi103-2]$ touch - d “5 Nov” f9
[ian@echidna lpi103-2]$ ls - lrt f*
[ian@echidna lpi103-2]$ date
Fri Aug 14 18:33:48 EDT 2009
[ian@echidna lpi103-2]$ date - r f1
Fri Aug 14 18:25:50 EDT 2009
[ian@echidna lpi103-2]$ touch - r f1 f1a
[ian@echidna lpi103-2]$ ls - l f1*
Finding files
The find command helps us to find files based on MANY criteria. Look at this:
$ find. - iname "[a-j]"*
./howcool.sort
./alldata
./mydir/howcool.sort
./mydir/newDir/insideNew
./howcool
a common switch is - iname which says “name but case is not important (z is same as Z)”. Also - d is
commonly used:
$ find. - iname "my"
./myfiles
./mydata.noenter
./mydata
./mydir
./mydir/hereisMYfile.txt
./touch/myfile
./mydata.tab
$ find. - type f - iname "my"
./myfiles
./mydata.noenter
./mydata
./mydir/hereisMYfile.txt
./touch/myfile
./mydata.tab
These are the most common file types:
you can also search for file sizes:
command meanint
So this will find all files ending in tmp with size between 1M and 100M in /var/ directory:
find /var - iname ’tmp - size +1M - size - 100M
you can find all empty files with find. -size 0b or find. -empty
Acting on files
We can act on files with various switches:
switch meanint
But the best way to run commands on found files is - exec switch. You can point to the file
with ‘{}’ or {} and finish your command with ;.
This will remove all empty files in this directory and its subdirectories:
find. - empty - exec rm ‘{}’ ;
or this will rename all htm files to hfml
find. - name ".htm" - exec mv ‘{}’ ‘{}l’ ;*
At last you have to know the - mtime switch for finding files based on their time.
switch meanint
if you add - daystart switch to - mtime or - atime it means that we want to consider days as calendar
days, starting at midnight.
Identify a file
That is the file command:
$ file mydata.tab
mydata.tab: ASCII text
$ file /bin/bash
/bin/bash: ELF 64 - bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for
GNU/Linux 2.6.32, BuildID[sha1]=cb63ec0718f2022619814c04a5b6cd8a36752a83, stripped
$ file mydata.tab
mydata.tab: ASCII text
$ file /bin/bash
/bin/bash: ELF 64 - bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for
GNU/Linux 2.6.32, BuildID[sha1]=cb63ec0718f2022619814c04a5b6cd8a36752a83, stripped
$ file - i mydir
mydir: inode/directory; charset=binary
Compressing files
Zip
we mostly use gzip and gunzip in linux. It is very easy:
$ ls * - ltrh
Bzip2
is another compressing tool. Works just the same but with another compression algorithm.
$ ls * - ltrh
$ ls * - ltrh
Archiving files
Sometimes we need to create an archive file from many files for easier moving or backing up. This is
done with cpio and tar.
Tar
TapeARchive or tar is the most common archiving tool. In automatically create an archive file from a
directory and all its subdirs.
Common switches are
switch meanint
If you issue absolute paths, tar removes the starting slash (/) for safety reasons when creating an
archive. If you want to override, use -p option.
tar can work with tapes and other storages. Thats why we use - f to tell it that we are working with files.
Cpio
Gets a list of files and creates archive (one file) of it which can be opened later.
$ ls | cpio - o > allfilesls.cpio
3090354 blocks
find. - name "" | cpio - o > myarchivefind.cpio*
to decompress it:
mkdir extract
mv myarchivefind.cpio extract
cd extract
cpio - id < myarchivefind.cpio
dd
The dd command copies data from one location to another. This data comes from files. You can use it
just like copy:
$ cat howcool
jadi 5
sina 6
rubic 2
you 12
$ dd if=howcool of=newcool
0+1 records in
0+1 records out
30 bytes (30 B) copied, 0.0227904 s, 1.3 kB/s
$ cat newcool
jadi 5
sina 6
rubic 2
you 12
$
But it is used in many other cases specially writing directly to block devices such as /dev/sdb or changing
data to upper/lower case.
This will backup my whole hard to a file:
or better:
Another common usage is creating files of specific size:
$ dd if=/dev/zero of=1g.bin bs=1G count=1
103.4 – Use streams, pipes and redirects - 4
Objectives
We’ve already talked about basics of piping and redirects in previous sections. Here you will get a
deeper understanding of these concepts.
Redirecting standard IO
On a linux system most shells use streams for input and output (a list of characters)… These streams can
be from (and toward) various things including keyboard, block devices (hards, usb stick, …), window of a
program, fiels, …
What are these file descriptions? There are used to control the output. If you need to control where
your output goes, you can add n> or n>>.
if the n is not given, the default is standard output
The user who runs the command should have write access to the file.
$ ls
fiona habib mahmoodrm minoo mojtaba sina
$ ls j*
ls: cannot access j*: No such file or directory
$ ls m*
mahmoodrm minoo mojtaba
$ ls j m**
ls: cannot access j*: No such file or directory
mahmoodrm minoo mojtaba
$ ls j m > output 2> errors**
$ cat output
mahmoodrm
minoo
mojtaba
$ cat errors
ls: cannot access j*: No such file or directory
$
Redirecting both stdout and stderr to one location
Sometimes (say during automated tasks) we prefer to send both standard output and standard error to
same place, Use &> and &>> to say both stderr and stdout.
It is also possible to use &1 and &2 and &0 to refer to current place of stdout, stderr & stdin. In this
case ls > file1 2>&1 means redirect output to file1 and output stderr to same place as stdout (file1)
Be careful! ls 2>&1 > file1 means print stderr to current location of stdout (screen) and then change the
stdout to file1
Sending to null
In linux, /dev/null is like a trash-can. You can send anything there and it disappears. So it is normal to
say:
$ ls j m > file1**
ls: cannot access j*: No such file or directory
$ ls j m > file1 2>/dev/null**
$ cat file1
mahmoodrm
minoo
mojtaba
Redirecting input
The < operand redirects the input.
$ cat uses
you fedora
jadi ubuntu
rubic windows
neda mac
narsin arch
$ tr ’ ’ ‘,’ < uses
you,fedora
jadi,ubuntu
rubic,windows
neda,mac
narsin,arch
Here-documents
Many shells, have here-documents (also called here-docs) as a way of input. You use << and
a WORD and then whatever you input is considered stdin till you give only the WORD in one line.
$ tr ’ ’ ‘.’ << END_OF_DATA
this is a line
and then thiswe’ll still type
and,
done!
END_OF_DATA
this.is.a.line
and.then.this
we’ll.still.type
and,
done!
Here-Documents are very useful if you are writing scripts and automated tasks.
Pipes
Piping is sending one commands output to another commands input (Piping the stdout to stdin). You
use | for this task.
As previously seen, many commands use a hyphen - in place of a filename as an argument to
indicate when the input should come from stdin rather than a file.
$ cat what_i_have.txt
laptop
socks
tshirt
ball
socks
glasses
$ cut - f1 - d’ ’ what_i_have.txt | sort | uniq - c | sort - nr
2 socks
1 tshirt
1 laptop
1 glasses
1 ball
If you need to start your pipeline with the contents of a file, start with cat filename | … or use a < stdin
redirect.
Xargs
This command reads input from stdin and uses them as arguments.
$ ls | xargs echo these are files:
these are files: errors f file1 fiona habib mahmoodrm minoo mojtaba output output.txt sina uses
what_i_have.txt
if you do not give any command to the xargs , the echo will be the default command (it will show the
stdin).
Have in mind that xargs breaks input based on blanks and use any part as an argument. You can limit the
number of arguments with –max-args (same as - n) switch and escape blanks or quote them to prevent
them from breaking.
One important switch is - I. This is useful if you need to pass stdin arguments in the middle (or even start)
of your commands. use the form xargs -I SOMETHING echo here is SOMETHING end:
$ cat what_i_have.txt
laptop
socks
tshirt
ball
$ cat what_i_have.txt | xargs - I DATA echo I have DATA and I love it.
I have laptop and I love it.
I have socks and I love it.
I have tshirt and I love it.
I have ball and I love it.
If you use -L, the input will break by line and not by blanks.
Tee
What if you need to see the output on screen and also save it to a file? Ugly way is redirecting to the file
and using tail -f file in another window. Nice way is using tee and giving it one or more filenames for
standard output (if you need to save stderr , first redirect it to stdout ). This will write the output to those
files and also writes them to the screen:
$ ls - 1 | tee allfiles myfiles
allfiles
f
fiona
habib
mahmoodrm
minoo
mojtaba
myfiles
sina
If you want to prevent overwriting files, use the - a switch to append to files if exists.
103.5 – Create, Monitor and Kill Processes - 4
Objectives
Foreground and Background jobs
One of the great points of linux on its beginning days, was the ability to run many programs at the same
time. This is done with sending programs to the background.
Normally if you run a program on the terminal, it blocks your terminal but sending a command to the
background will prevent this:
xeyes &
But what if we started it normally? We can break / cancel it with Ctrl+c or suspend it using Ctrl+z.
$ xeyes
^Z
[1]+ Stopped xeyes
$ jobs
[1]+ Stopped xeyes
$ bg
[1]+ xeyes &
$ jobs
[1]+ Running xeyes &
$ sleep 1000 &
[2] 7395
$ jobs
[1]- Running xeyes &
[2]+ Running sleep 1000 &
$ fg %2
sleep 1000
^Z
[2]+ Stopped sleep 1000
$ jobs
[1]- Running xeyes &
[2]+ Stopped sleep 1000
$ bg sle
[2]+ sleep 1000 &
$ jobs
[1]- Running xeyes &
[2]+ Running sleep 1000 &
Nohup
The nohup command lets you run your commands even after you logged out and writes its output
to nohup.out :
$ nohup ping <IP_ADDRESS>
nohup: ignoring input and appending output to ‘nohup.out’
^C$ cat nohup.out
PING <IP_ADDRESS> (<IP_ADDRESS>) 56(84) bytes of data.
64 bytes from <IP_ADDRESS>: icmp_seq=1 ttl=51 time=225 ms
64 bytes from <IP_ADDRESS>: icmp_seq=3 ttl=51 time=223 ms
— <IP_ADDRESS> ping statistics —
4 packets transmitted, 2 received, 50% packet loss, time 3010ms
rtt min/avg/max/mdev = 223.584/224.767/225.950/1.183 ms
It is common to use 2> to redirect the nohup errors to a file: nohup script.sh > mynohup.out 2>&1 &
Kill
You can control processes by signals. Actually pressing Ctrl+c and Ctrl+z is also sending signals. Another
way for this is using the kill command:
$ jobs
[3] Running xeyes &
[4] Running sleep 1000 &
[5]- Running sleep 2000 &
[6]+ Running sleep 3000 &
$ kill %4
$ jobs
[3] Running xeyes &
[4] Terminated sleep 1000
[5]- Running sleep 2000 &
[6]+ Running sleep 3000 &
$ jobs
[3] Running xeyes &
[5]- Running sleep 2000 &
[6]+ Running sleep 3000 &
If is also possible to use PIDs in from of the kill or send other signals:
signal number signal name meaning
1 SIGHUP Informing the process that its controlling terminal (like an ssh connection) is terminated
15 SIGTERM normal termination request
9 SIGKILL forcefully kills the proccess
So you can do a kill -9 8733 to force process ID 8733 to close.
Now you can understand what nohup means: go not answer to the SIGHUP.
killall
Will send the given signal (or 15) to all the processes with the given name:
$ jobs
[3] Running xeyes &
[5]- Running sleep 2000 &
[6]+ Running sleep 3000 &
$ ps - ef | grep sleep
jadi 7864 7651 0 21:07 pts/1 00:00:00 sleep 2000
jadi 7865 7651 0 21:07 pts/1 00:00:00 sleep 3000
jadi 7977 7651 0 21:14 pts/1 00:00:00 grep sleep
$ killall sleep
[5]- Terminated sleep 2000
[6]+ Terminated sleep 3000
$ jobs
[3]+ Running xeyes &
$ ps - ef | grep sleep
jadi 7980 7651 0 21:14 pts/1 00:00:00 grep sleep
Monitoring Processes
ps
The ps command shows running processes on your computer.
$ sleep 1000 &
[1] 7678
$ sleep 1001 &
[2] 7679
$ xeyes &
[3] 7680
$ ps
PID TTY TIME CMD
7651 pts/1 00:00:00 bash
7678 pts/1 00:00:00 sleep
7679 pts/1 00:00:00 sleep
7680 pts/1 00:00:00 xeyes
7681 pts/1 00:00:00 ps
But using ps aux (= - aux) or ps -ef is also common & shows ALL processes on this system:
$ ps - aux | wc - l
293
Every process has a ProcessID (PID) and a PPID (Parent Process ID).
finding processes
You’ve seen that ps -ef shows processes from all users. We can grep on that and see who is
running gedit and what is its process ID:
$ ps - ef | grep gedit
jadi 6213 4604 9 20:06? 00:04:43 gedit
jadi 7725 7651 0 20:55 pts/1 00:00:00 grep gedit
but there is also a more direct way:
$ ps - C gedit - o user,pid,tty,time,comm
USER PID TT TIME COMMAND
jadi 6213? 00:04:49 gedit
It is also possible to use the --sort switch to sort output based on different fields (+ for ascending & - for
descending).
$ ps - af --sort +comm,-sid
UID PID PPID C STIME TTY TIME CMD
root 5486 5478 0 19:59 pts/12 00:00:00 - su
root 4444 1169 0 19:56 tty4 00:00:00 - bash
jadi 6638 5412 0 20:10 pts/0 00:00:04 node /usr/local/bin/sslocal
jadi 7778 7651 0 20:58 pts/1 00:00:00 ps - af --sort +comm,-sid
jadi 7678 7651 0 20:48 pts/1 00:00:00 sleep 1000
jadi 7679 7651 0 20:48 pts/1 00:00:00 sleep 1001
jadi 7775 7651 0 20:58 pts/1 00:00:00 sleep 1000
jadi 7776 7651 0 20:58 pts/1 00:00:00 sleep 1000
jadi 7777 7651 0 20:58 pts/1 00:00:00 sleep 1000
root 5478 5477 0 19:59 pts/12 00:00:00 su -
root 5477 5008 0 19:59 pts/12 00:00:00 sudo su -
jadi 7680 7651 0 20:48 pts/1 00:00:01 xeyes
Top
Processes are changing and sometimes you need to check them live. top command will help you:
$top
top - 21:00:44 up 1:16, 5 users, load average: 1.51, 1.65, 1.78
Tasks: 293 total, 1 running, 292 sleeping, 0 stopped, 0 zombie
%Cpu(s): 19.0 us, 5.0 sy, 0.0 ni, 70.9 id, 5.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 8060264 total, 5359812 used, 2700452 free, 169240 buffers
KiB Swap: 7811068 total, 0 used, 7811068 free. 2250692 cached Mem
6570 jadi 20 0 1437752<PHONE_NUMBER>2 S 18.4 6.8 12:00.96 firefox
4870 jadi 20 0 1762516<PHONE_NUMBER>4 S 12.2 3.7 7:37.05 compiz
4492 jadi 9 - 11<PHONE_NUMBER>6 8940 S 6.1 0.1 1:06.81 pulseaudio
4532 root 20 0<PHONE_NUMBER>6 60192 S 6.1 1.0 12:16.63 Xorg
4723 jadi 20 0<PHONE_NUMBER> 5512 S 6.1 0.1 9:51.52 ibus-daemon
5648 jadi 20 0 1641556<PHONE_NUMBER>40 S 6.1 2.5 3:20.88 chrome
7082 jadi 20 0 1210748 73136 42528 S 6.1 0.9 0:36.51 Telegram
7806 jadi 20 0 33796 3004 2500 R 6. 1 0.0 0:00.02 top
1 root 20 0 29528 4320 2584 S 0.0 0.1 0:01.71 init
You can see the processes, system load, uptime, CPU status, memory, … and do some stuff:
key during top functionality
h help
q quit
M sort based on memory usage"
c show full commands
k kill after asking pid and signal
Free
The free command will show you info about the system memory. The default is kilobytes but you can
change it with - mfor megabytes, - g for gigabytes or even - b for bytes:
$ free - m
total used free shared buffers cached
Mem: 7871 5231 2640<PHONE_NUMBER>
Uptime
The uptime command shows the time, how long the system is up, how may users are logged in and the
load average of 1, 5 & 15 minutes:
$ uptime
21:18:52 up 1:34, 5 users, load average: 2.38, 2.64, 2.41
103.6 – Modify process execution priorities – 2
Objectives
$ top
top - 08:44:51 up 13:00, 5 users, load average: 0.57, 1.50, 1.50
Tasks: 290 total, 2 running, 288 sleeping, 0 stopped, 0 zombie
%Cpu(s): 38.4 us, 9.4 sy, 0.0 ni, 49.3 id, 2.8 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 8060264 total, 7858348 used, 201916 free, 360144 buffers
KiB Swap: 7811068 total, 0 used, 7811068 free. 2842344 cached Mem
13605 jadi 25 5 1473652<PHONE_NUMBER>8 R 54.5 6.6 3:25.50 firefox
11157 root 20 0<PHONE_NUMBER>52 94484 S 6.1 1.4 3:26.18 Xorg
12265 jadi 20 0 1210484 75848 42264 S 6.1 0.9 0:32.06 Telegram
12671 jadi 20 0 1800508<PHONE_NUMBER>0 S 6.1 3.4 1:27.35 compiz
15035 jadi 20 0<PHONE_NUMBER>0 34228 S 6.1 0.7 0:00.93 /usr/bin/termin
15066 jadi 20 0 33796 3076 2448 R 6.1 0.0 0:00.02 top
1 root 20 0 29528 4320 2584 S 0.0 0.1 0:02.27 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.27 ksoftirqd/0
5 root 0 - 20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H ```
Chkecn th
There is NI column, it shows how nice the process is. The nicer the process, the less CPU it asks. Nice can
be from -20 to 19 (a process with nice = - 20 is ANGRY and asking for a lot of CPU while a process with
nice = 19 is SUPER NICE and lets other processes use most of the CPU).
If you do not use nice command, processes will have nice level of 0. This can be checked
with nice command:
$ nice
0
It is also possible to tell ps command to write the nice parameter of processes:
$ ps - l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 15044 15035 0 80 0 - 7453 wait pts/29 00:00:00 bash
0 S 1000 15052 15044 0 60 - 20 - 3976 hrtime pts/29 00:00:00 sleep
0 R 1000 15080 15044 0 80 0 - 4680 - pts/29 00:00:00 ps
Setting priorities when running commands
If you need to change the niceness level of a program you can running it with nice command and -
n switch (for nice):
$ nice - n - 20 echo “I am running!”
nice: cannot set niceness: Permission denied
I am running!
$ sudo nice - n - 20 echo “I am running!”
I am running!
$ sudo nice - n 19 echo “I am running!”
I am running!
Please note to two points:
If you run a command with nice without any parameters, the nice value will be 10:
$ nice xeyes &
[1] 15217
$ ps - l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 15044 15035 0 80 0 - 7455 wait pts/29 00:00:00 bash
0 S 1000 15217 15044 0 90 10 - 12522 poll_s pts/29 00:00:00 xeyes
0 R 1000 15218 15044 0 80 0 - 4680 - pts/29 00:00:00 ps
Changing priorities
The renice command can change the niceness of running processes:
$ ps - ef | grep firefox
jadi 13605 11226 30 08:28? 00:10:13 /usr/lib/firefox/firefox
jadi 15192 15044 0 09:01 pts/29 00:00:00 grep firefox
$ sudo renice - n - 10 13605
13605 (process ID) old priority 5, new priority - 10
103.7 – Search text files using regular expressions - 3
Objectives
Regex
Regular expression, Regex, regexp is a pattern to describe what we want to match from a text. Here will
discuss the form of regex which is used with the grep (generalised regular expression processor)
command.
There is two kind of regex in GNU grep: Basic an Extended.
Basic blocks
Adding two expressions
If you need to add (concat) two expressions, just write them after each other.
Regex Will match
a ali, mina, hamid, jadi
na nasim, mina, nananana batman, mona
Repeating
Regex Will match Note
ab ab, aaab, aaaaab, aaabthis (^)
ab b, mobser Because there is a b here with zero a before it
a+b ab, aab, aaabenz
won’t match sober or b because there needs to be at
least one a
a?b
ab, a ab , b, batman (zero a then
b), …
Alternation (|)
If you say a|b it will match a or b.
Character Classes
The dot (. ) means any character. So … will match anything with at least two character in it. You can also
create your own classes with [abc] which will match a or b or c and [a-z] which match a to z.
You can also refer to digits with \d and
Ranges
There are easy ways to commonly used classes. Named classes open with [: and close with :]
Range Meaning
[:alnum:] Alphanumeric characters
[:blank:] Space and tab characters
[:digit:] The digits 0 through 9 (equivalent to 0-9)
[:upper:] and
[:lower:]
Upper and lower case letters, respectively.
Range Meaning
^ (negation)
As the first character after [ in a character class negates the sense of the remaining
characters
A common form is .* which matches any character (zero or any length).
Matching specific locations
Samples
grep
The grep command can search inside the files.
$ grep p friends
payam
pedram
There are the most important switches:
switch meaning
$ **grep p ***
friends:payam
friends:pedram
what_I_have.txt:laptop 2
what_I_have.txt:pillow 5
what_I_have.txt:apple 2
$ grep p * - n
friends:12:payam
friends:15:pedram
what_I_have.txt:2:laptop 2
what_I_have.txt:3:pillow 5
what_I_have.txt:4:apple 2
$ grep p * - l
friends
what_I_have.txt
$ grep p * - c
friends:2
what_I_have.txt:3
$
If is very common to combine grep and find: find. -type f -print0 | xargs -0 grep -c a | grep -v ali # find all
files with a in them but not ali `
Extended grep
Extended grep is a GNU extension. It does not need the escaping and much easier. It can be used with -E
option or egrep command which equals to grep -E.
Fixed grep
If you need to search the exact string (and not interpret it as a regex), use grep -F or fgrep so the fgrep
this$ wont go for the end of the line and will find this$that too.
Sed
In previous lessons we saw simple sed usage. Here I have great news for you: sed understands regex! If
is good to use - r switch to tell sed that we are using them.
$ sed - r “s/^(a|b)/STARTS WITH A OR B/” friends
STARTS WITH A OR Bmir
mina
jadi
STARTS WITH A OR Bita
STARTS WITH A OR Bli
hassan
Main switches:
switch meaning
$ sed - rn “/^(a|b)/p” friends
amir
bita
ali
103.8 – Perform basic file editing operations using vi - 3
Objectives
Introduction
vi is a great tool! The best editor ever (some say after Emacs) and it is installed on all linux systems.
Some say it is difficult to use and abnormal and some say it is the most natural editor there can be. Lets
see.
vi can be used with simplest keyboards and over network on ssh terminals
In many systems, vi command is a link / alias to vim and there are many versions of vi. Check with –
version switch:
$ vi --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Oct 20 2014 16:08:47)
Included patches: 1 - 273
Modified by<EMAIL_ADDRESS>Compiled by buildd@
Small version without GUI. Features included (+) or not (-):
+acl - farsi - mouse_sgr - tag_old_static
$ vi file.txt
vi moded
vi has 2 different modes:
Moving the cursor
If you need to move around, use these keys:
key function
h One character to the left (only current line)
j One line down
k One line up
l One character to the right (only current line)
w Next word on the current line
e Next end of word on the current line
b Previous beginning of the word on the current line
Ctrl-f Scroll forward one page
Ctrl-b Scroll backward one page
Jumping
key function
G With no number, will jump to the end & 10G will jump to line 10
H 5H will go to the 5th line from the top of the screen
L 3L will move the cursor to the 3rd line to the last line of the screen
Editing text
These command during the command mode will help you enter, edit, replace, … test:
key function
i Enter the insert mode
a Enter the insert mode after the current position of the cursor
r replace only one character
o open a new line below the cursor and go to the insert mode
O open a new line above the cursor and go to the insert mode
c clear to a location and go to the insert mode the replace till there and then normal insert (cw^ will
overwrite the current word)
d
delete. you can mix with w (dw) to delete a word. Same as cw but dw does not to to the insert
mode
dd Delete the current line
x Delete character at the position of the cursor
p Paste the last deleted text after the cursor
P Paste the last deleted text before the cursor
xp swaps the character at the cursor position with the one on its right
Searching
key function
/ (^) Search forward (/happiness will find the next happiness)
? Search backward
n repeat previous search. You can also use / and? without any parameters)
Search wraps around to the top once the bottom of file is reached
Exiting
It is always funny when you see someone entering to the vi and now knowing how to exit! Learn these
and prevent the laughter:
key function
:q! Quit editing without saving = runaway after any mistake
:w! Write the file (whether modified or not). Attempt to overwrite existing files or read-only or other unwritable files
:w myfile.txt Write to a new name
ZZ Exit and save the file if modified
:e! Reload the file from disk
:! Run a shell command
Entering colon (:) during command mode will move the cursor to the bottom of the screen and vi will
wait for your commands. Press ESC to return back to the normal command mode.
Help
You can always ask for help with :help or :help subject. This way vi will open a help text which you can
use / search just like any other text. Close it with :q command.
104.1 – Create partitions and filesystems – 2
Objective
Blocked devices
Is a technical term for any storage device which can be formatted to fixed sized blocks and blocks should
be able to be accessed individually. That is Hard disks, USB Memories, CDs, …
In long ls format, the first b indicates Block Device:
$ ls - l /dev/loop1 /dev/sd[a-z]
brw-rw---- 1 root disk 7, 1 Jan 8 10:46 /dev/loop1
brw-rw---- 1 root disk 8, 0 Jan 8 10:46 /dev/sda
Some block devices mostly used as one single filesystem (like CDs & Floppies) and some are divided
into Partitions (Hard disks).
fdisk
fdisk is the main command for viewing / changing and creating partitions. - l switch is for show:
root@funlife:~# fdisk - l /dev/sda
Disk /dev/sda: 298.1 GiB,<PHONE_NUMBER>76 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000beca1
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 43094015 43091968 20.6G 83 Linux
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
It is also possible to run fdisk in interactive mode. m will show you the help:
root@funlife:~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.25.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): m
Help:
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
p displays the current partitions :
Command (m for help): p
Disk /dev/sda: 298.1 GiB,<PHONE_NUMBER>76 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000beca1
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 43094015 43091968 20.6G 83 Linux
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
/dev/sda6 107704320 625141759 517437440 246.8G 83 Linux
You may remember the disk layouts from other chapters. fdisk can create them. Lets first delete my first
partition:
Command (m for help): p
Disk /dev/sda: 298.1 GiB,<PHONE_NUMBER>76 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000beca1
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 43094015 43091968 20.6G 83 Linux
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
/dev/sda6 107704320 625141759 517437440 246.8G 83 Linux
Command (m for help): d
Partition number (1-3,5,6, default 6): 1
Partition 1 has been deleted.
I’m brave! Now lets create a smaller one there:
Command (m for help): n
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): p
Partition number (1,4, default 1): 1
First sector (2048-625142447, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-43094015, default 43094015): +15G
Created a new partition 1 of type ‘Linux’ and of size 15 GiB.
Command (m for help): p
Disk /dev/sda: 298.1 GiB,<PHONE_NUMBER>76 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000beca1
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 31459327 31457280 15G 83 Linux
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
/dev/sda6 107704320 625141759 517437440 246.8G 83 Linux
This new partitioned is not formatted but still marked 83 for later use. If I needed to use this partition
as swap I had to set its ID to 82:
Command (m for help): p
Disk /dev/sda: 298.1 GiB,<PHONE_NUMBER>76 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000beca1
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 31459327 31457280 15G 83 Linux
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
/dev/sda6 107704320 625141759 517437440 246.8G 83 Linux
Command (m for help): t
Partition number (1-3,5,6, default 6): 1
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS /.
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext’d (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’.
Command (m for help): p
Disk /dev/sda: 298.1 GiB,<PHONE_NUMBER>76 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000beca1
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 31459327 31457280 15G 82 Linux swap / Solaris
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
/dev/sda6 107704320 625141759 517437440 246.8G 83 Linux
b is code for FAT32 (windows 95).
But all we done was in memory! We need to write it to the partition table. ‘v’ will verify the setup:
Command (m for help): v
Remaining 11639159 unallocated 512 - byte sectors.
It tells me that I have unallocated space! I’m waisting my hard but I’m fine with it. So lets save it
with w command (for write ):
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run
partprobe(8) or kpartx(8).
Formatting the partition
Linux can handle more than 100 kind of partitions but most commons are:
Format Description
ext2
second extended filesystem was developed to address shortcomings in the Minix filesystem used in
early versions of Linux. It has been used extensively on Linux for many years. There is no journaling in
ext2, and it has largely been replaced by ext3 and more recently ext4.
ext3 ext2 + journaling, total storage can be 1EXAByte and each file can be 16TB, ...
ReiserFS
ReiserFS is a B-tree-based filesystem, great for large numbers of small files, journaling, no longer in
active development & does not support SELinux, replaced with Reiser4.
XFS journaling, caches to RAM, great for uninterruptible power supplies
swap Swap space must be formatted for use as swap space, but it is not generally considered a filesystem.
vfat
FAT32, no journaling, good for data exchange with windows, does not understand permissions and
symbolic links
ext4 newer than ext3
You can format your partitions with mkfs command (and mkswap for swap). This is a front end to
commands like mkfs.ext3 for ext3, mkfs.ext4 for ext4 and mkfs.reiserfs for ReiserFS. full list of installed
on your system is here:
root@funlife:~# ls /sbin/mk*
/sbin/mkdosfs /sbin/mkfs /sbin/mkfs.cramfs /sbin/mkfs.ext3 /sbin/mkfs.ext4dev /sbin/mkfs.minix
/sbin/mkfs.ntfs /sbin/mkhomedir_helper /sbin/mkswap
/sbin/mke2fs /sbin/mkfs.bfs /sbin/mkfs.ext2 /sbin/mkfs.ext4 /sbin/mkfs.fat /sbin/mkfs.msdos
/sbin/mkfs.vfat /sbin/mkntfs
The main switch is - type (or - t) to specify the format:
root@funlife:~# mkfs - t ext3 /dev/sda1
mke2fs 1.42.10 (18-May-2014)
/dev/sda1 contains a ext4 file system
last mounted on /mnt on Mon Dec 22 12:04:22 2014
Proceed anyway? (y,n) y
Creating filesystem with 5386496 4k blocks and 1349040 inodes
Filesystem UUID: 0b5aad86-b507-41b9-a0ff-cf899cb92785
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
root@funlife:~#
This will have a same effect: mkfs.ext3 /dev/sda1
If you need to assign a lable to the partition, you have to use the - L lable_name option. Please note that
in renect system, people use UUIDs instead of labels. UUID of a disk can be viewed with:
$ blkid /dev/sda1
/dev/sda1: UUID=“59d8cbdb-0e78- 4605 - 8aaf-cf02fcb85d2e” SEC_TYPE=“ext2” TYPE=“ext3”
Some systems use GUID Partition Table (GPT) instead of older MBR. In this case you have to use
the gdisk tool which has more capabilities than fdisk.
root@funlife:~# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.8
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing ‘q’ if you don’t want to convert your MBR partitions
to GPT format!
Creating different partitions
Partition Format type Sample Command Notes
/dev/sda3 ext4 (^) mkfs -t ext4 -L data/dev/sda3 Named it dafa. Or use the mkfs.ext4command
/dev/sdb2 xfs mkfs -t xfs -i size=512 /dev/sdb2 telling it to have larger inodes (normal is 256)
/dev/sda8 ReiserFS mkfs -t reiserfs /dev/sda8 Or you can use mkreiserfs command.
/dev/sdc FAT32 mkfs -t vfat /dev/sdc Or you can use mkfs.vfat command
/dev/sda2 swap mkswap /dev/sda2 will be used as swap space
104.2 – Maintain the Integrity of Filesystems – 2
Objectives
fsck
If anything bad happens for your filesystem, you will have a corrupted file system. The command to fix
this is fsck. Technically this command is a front end for many commands:
jadi@funlife:~$ ls /sbin/fsck
/sbin/dosfsck /sbin/fsck.ext2 /sbin/fsck.fat /sbin/fsck.vfat
/sbin/e2fsck /sbin/fsck.ext3 /sbin/fsck.minix
/sbin/fsck /sbin/fsck.ext4 /sbin/fsck.msdos
/sbin/fsck.cramfs /sbin/fsck.ext4dev /sbin/fsck.nfs
Some of these are just hardlinks to e2fsck command
A common switch during boot is - A which tells fsck to check all file systems in /etc/fstab ordered
by passno in that file which is 6th field (File systems with passno of 0, wont be checked during the boot.
root@funlife:~# fsck /dev/sdb
fsck from util-linux 2.25.1
e2fsck 1.42.10 (18-May-2014)
/dev/sdb is in use.
e2fsck: Cannot continue, aborting.
root@funlife:~# umount /dev/sdb
umount: /dev/sdb: not mounted
root@funlife:~# umount /dev/sdb1
root@funlife:~# fsck /dev/sdb
fsck from util-linux 2.25.1
e2fsck 1.42.10 (18-May-2014)
ext2fs_open2: Bad magic number in super-block
fsck.ext2: Superblock invalid, trying backup blocks…
fsck.ext2: Bad magic number in super-block while trying to open /dev/sdb
The superblock could not be read or does not describe a valid ext2/ext3/ext4
filesystem. If the device is valid and it really contains an ext2/ext3/ext4
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck - b 8193 <device>
or
e2fsck - b 32768 <device>
You can also check filesystems with UUID (find them with blkid command or with labels):
root@funlife:~# fsck /dev/sdb
fsck from util-linux 2.25.1
e2fsck 1.42.10 (18-May-2014)
ext2fs_open2: Bad magic number in super-block
fsck.ext2: Superblock invalid, trying backup blocks…
fsck.ext2: Bad magic number in super-block while trying to open /dev/sdb
The superblock could not be read or does not describe a valid ext2/ext3/ext4
filesystem. If the device is valid and it really contains an ext2/ext3/ext4
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck - b 8193 <device>
or
e2fsck - b 32768 <device>
root@funlife:~# blkid
/dev/sda1: LABEL=“movies”
/dev/sdb1: UUID=“BA82-BECD” TYPE=“vfat” PARTUUID=“381add66-01”
root@funlife:~# fsck LABEL=movies
fsck from util-linux 2.25.1
root@funlife:~# fsck UUID=“BA82-BECD”
fsck from util-linux 2.25.1
fsck.fat 3.0.26 (2014- 03 - 07)
/dev/sdb1: 14 files, 1972/945094 clusters
You can use - N switch to see what command/test is going to be executed:
root@funlife:~# fsck - N UUID=“BA82-BECD”
fsck from util-linux 2.25.1
[/sbin/fsck.vfat (1) – /dev/sdb1] fsck.vfat /dev/sdb1
If you want to check a XFS filesystem, you have to use xfs_check command
tune2fs
This is a command to tune ext file systems. It can show information and set many options. The - l option
lists the current configs:
jadi@funlife:~$ sudo tune2fs - l /dev/sda2
tune2fs 1.42.10 (18-May-2014)
Filesystem volume name: <none>
Last mounted on: /
Filesystem UUID: 1651a94e-0b4e-47fb-aca0-f77e05714617
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent
flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 1531904
Block count: 6123046
Reserved block count: 306152
Free blocks: 2302702
Free inodes: 1073461
First block: 0
Block size: 4096
Fragment size: 4096
Reserved GDT blocks: 1022
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Mon Dec 1 10:21:42 2014
Last mount time: Sat Jan 31 17:21:51 2015
Last write time: Sat Jan 31 17:21:51 2015
Mount count: 32
Maximum mount count: - 1
Last checked: Mon Dec 1 10:21:42 2014
Check interval: 0 (<none>)
Lifetime writes: 103 GB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
First orphan inode: 786620
Default directory hash: half_md4
Directory Hash Seed: 16c38a41-e709-4e04-b1c2-8a79d71ea7e8
Journal backup: inode blocks
xfs_info
This is same as the tune2fs but for xfs file systems.
xfs_info should be used on mounted file systems
du & df
In many cases you want to find out about the free space of a disk or find how much space a directory is
using. This space can be used by the blocks of files or inodes.
inodes contain the information about files. Information like the owner, when the last time it is used or
edited, its size, if its a directory or not and peoples access rights on if. The inode number is unique within
a particular filesystem and is also called files serial number.
df
The DiskFree command is used to find out about the free and used space of file systems.
jadi@funlife:~$ df - TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 23G 15G 7.7G 65% /
none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup
udev devtmpfs 3.9G 4.0K 3.9G 1% /dev
tmpfs tmpfs 788M 1.4M 786M 1% /run
none tmpfs 5.0M 4.0K 5.0M 1% /run/lock
none tmpfs 3.9G 19M 3.9G 1% /run/shm
none tmpfs 100M 28K 100M 1% /run/user
/dev/mapper/chome ext4 243G 229G 14G 95% /home/jadi
/dev/sdb1 vfat 3.7G 7.8M 3.6G 1% /media/jadi/BA82-BECD
Here the -T
switch make df to show the file system types and -H
make numbers human readable (in
powers of 1000). Please note that -h
is also human readable but in powres of 1024 (e.g. shows 1k for
1000 bytes.
If you need the inode data, use the - i switch:
jadi@funlife:~$ df - i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda2 1531904<PHONE_NUMBER>288 30% /
none 1007533 4 1007529 1% /sys/fs/cgroup
udev 1003703<PHONE_NUMBER> 1% /dev
tmpfs 1007533<PHONE_NUMBER> 1% /run
none 1007533 3 1007530 1% /run/lock
none 1007533<PHONE_NUMBER> 1% /run/shm
none 1007533 33 1007500 1% /run/user
/dev/mapper/chome 16171008<PHONE_NUMBER>1715 2% /home/jadi
/dev/sdb1 0 0 0 - /media/jadi/BA82-BECD
vfat file format has no inodes; there is no owner or access rights on vfat filesystems.
du
The DiskUsage command give information about the used space of directories and files. The common
switches are:
switch usage
--max-depth 2 shows only 2 directories furthur
jadi@funlife:~/w/lpic$ du
16 ./101
701456 ./done
701464 ./Logo/chert
704588 ./Logo
12 ./data
12 ./100
jadi@funlife:~/w/lpic$ du - c
16 ./101
701456 ./done
701464 ./Logo/chert
704588 ./Logo
12 ./data
12 ./100
9432884 total
jadi@funlife:~/w/lpic$ du - hs
9.0G.
Repairing
We used the fcsk for showing file system information but if is designed to fix file systems too. If the
boot time check find a problems, you will be put into a command line to fix the problems.
On non-journaling file systems (ext2) the fsck will show you many questions about each block and you
have to say y if you want it to fix them. On journaling file systems (ext3&4, xfs, …) the fsck has much less
tasks to perform.
For xfs file systems , we have xfs _ check command
An important switch is - n which causes these commands not to fix anything and just show what was
going to be done.
debugfs
This is an interactive tool for debug an ext filesystem. It opens the filesystem in read-only mode unless
we tell it not to (with - w option). It can un-delete files and directories…
root@funlife:~# debugfs /dev/sda2
debugfs 1.42.10 (18-May-2014)
debugfs: cd /etc/ <-- cd
debugfs: pwd <-- show were am I
[pwd] INODE: 524289 PATH: /etc
[root] INODE: 2 PATH: /
debugfs: stat passwd <-- show data on one file
Inode: 527187 Type: regular Mode: 0644 Flags: 0x80000
Generation:<PHONE_NUMBER> Version: 0x00000000:00000001
User: 0 Group: 0 Size: 2145
File ACL: 0 Directory ACL: 0
Links: 1 Blockcount: 8
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0x548d4241:a7b196fc – Sun Dec 14 11:24:41 2014
atime: 0x54cc635b:6acfc148 – Sat Jan 31 08:38:43 2015
mtime: 0x548d4241:a01076f8 – Sun Dec 14 11:24:41 2014
crtime: 0x548d4241:9f1c52f8 – Sun Dec 14 11:24:41 2014
Size of extra inode fields: 28
EXTENTS:
(0):2188172
debugfs: ncheck 527187 <-- node check an inode
Inode Pathname
527187 /etc/passwd
debugfs: q <-- quit
Superblock
Unix systems use superblocks to save filesystem metadata. Most of the times this block is located at the
beginning of the file system and replicated on other locations too. The - n of mke2fs displays superblock
locations
mke2fs 1.41.9 (22-Aug-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
7159808 inodes, 28637862 blocks
1431893 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
874 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Other tools
For the LPIC exam, it is good to know about these commands.
filesystem command usage
ext tune2fs Show or set ext2 and ext3 parameters or even set the journaling options
ext dumpe2fs
Prints the super block and block group descriptor information for an ext2 or ext3
filesystem.
ext debugfs
Is an interactive file system debugger. Use it to examine or change the state of an ext2 or
ext3file system.
reiserfs reiserfstune show and set parameters
reiserfs debugreiserfs
Prints the super block and block group descriptor information for an ext2 or ext3
filesystem.
XFS xfs_info display information
XFS xfs_growfs expand file system
XFS xfs_admin change parameters on XFS file systems
XFS xfs_repair repair the problems
XFS xfs_db checks and debugs the filesystem
104.3 – Control mounting and unmounting of filesystems – 3
Objectives
Configure the mounting of a filesystem. Tasks include manually mounting and unmounting filesystems,
configuring filesystem mounting on bootup, and configuring user-mountable removable filesystems.
Mounting and Unmounting
Basic commands
cat /etc/fstab
mount /dev/sda1 /media
umount /media
Some switches
mount - t ext4 /dev/sda1 /media
mount - o remount,ro /dev/sda1
Get info on UUID and Label and Format
blkid /dev/sda2
Bootup
/etc/fstab
note:
swap
swapon
swapoff
swapon - s
104.5 – Manage file permissions and ownership – 3
Objectives
Users and Groups
A linux system can have many users and many groups. You can login with one user and use su command
to change to another group. Each user belongs to one primary group and can be a member of other
groups too.
There commands like whoami , groups and id to determine who you are.
$ whoami
jadi
$ groups
jadi adm cdrom sudo dip plugdev netdev lpadmin sambashare debian-tor
$ id
uid=1000(jadi) gid=1000(jadi)
groups=1000(jadi),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),102(netdev),108(lpadmin),124(samb
ashare),125(debian-tor)
$ su root -
Password:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
uid=0(root) gid=0(root) groups=0(root)
exit
$ whoami
jadi
id shows both user and group information
There info are stored in /etc/passwd and /etc/group
$ cat /etc/group | grep adm
adm:x:4:syslog,jadi
lpadmin:x:108:jadi
File ownership & permissions
Files also belong to one user and one group.
$ ls - l /sbin/fdisk ~/w/lpic/notes.txt
In many distros, when you create a user, system creates a group with same name and assign that users
files to that group
Another part of the ls -l command shows the permissions on that file. Linux system users a 3 layer
permission: permissions for the owner, for the group member and for others.
Each layer also has 3 different parts:
These are shown at the first column of ls - command as - rw-rw-r–. The character meanings are as follow:
bit meaning
1 What this entry is. Dash (-) is for ordinary files, 'l' is for links & 'd' is for directory
2,3,4 read, write and execute access for the owner
5,6,7 read, write and execute access for the group members
8,9,10 read, write and execute access for other users
Indicated if any other access methos (such as SELinux) applies to this file - not part of the 101
exam
As you can see in our example, characters 2 to 10 show the accesses. A - there means “no access on this
part” and read, write and execute are shown by r, w & x.
In the following line:
$ ls - l /sbin/fdisk
Let’s look at another example:
$ ls - l /home/
total 12
drwxr-xr-x 160 jadi jadi 12288 Feb 7 11:44 jadi
The first character is a d so this is a directory! The owner (jadi) has read, write and execute access but
other members of the group jadi and others only have read and execute access on this directory
(execute means that they can see the files inside it).
Chanhging permissions
It is possible to change the permissions on files & directories using the chmod command. There are to
ways to tell this command what you want to do:
When using octal codes, you have to to create an octal string to tell chmod what you want to do. This
way, 0 means no access, means execute, 2 means write and 4 means read. So, if you want to give
read+execute, you have to give 4+1 which is 5. This table shows every possible combination:
Symbolic Octal
rwx 7
rw- 6
r-x 5
r-- 4
--x 1
If you want to give rwx to owner, rx to group and only x to others, you have to use 751 :
$ ls - ltrh myfile
$ ls - ltrh myfile
One very common switch on chmod is - R for recursive chmoding on files. This will give read permission
of all files inside /tmp/ to any user:
Access modes
So, you have access only to your files. But how you should change your password? or use programs
which needs access to system files? You should be able to access /etc/passwd or /etc/shadow to change
your password but you should not be able to access other people files!
Normally when you run a program, it runs with your access levels but linux has two special bits on each
file; suid (set user id) and guid (set group id). If these are set on a file, that file be will be executed with
the access of the owner of the file and not the user who is running it.
$ ls - ltrh /usr/bin/passwd
Did you note the s in the place of executable bit for the user and for the group? That means when any
user runs this program, it will be run be the access of the owner of the file (which is root) instead of that
users id.
It is possible to set / unser the suid and sgid using chmod and +s or - s instead of x.
The last special option is chmod is the sticky bit which lets only the owner of the file to delete it, even if
other users have write (delete) access on that directory. This is good for places like /tmp.
Sticky bit is identified by t and will be shown on the last bit of a directory:
$ ls - dl /tmp
drwxrwxrwt 13 root root 77824 Feb 8 21:27 /tmp
As you can see the sticky bit is set and although all users have write access in this directory, they wont
be able to delete each others files.
Lets review how you can set these access modes:
access mode octal symbolic
suid 4000 u+s
guid 2000 g+s
sticky 1000 t
guid on a directory will force any new file in that directory to have the guid of the directory itself.
umask
But what will be the access of the new files? What happens when you touch a new file? This is set
with umask. This command tells the system what permissions should not be given to new files:
$ umask
0002
Which removes write (2) permissions from files.
If we need to change umask , it can be done with the same command:
$ umask
0002
$ touch newfile
$ ls - ltrh newfile
Changing owner and groups
If you need to change the ownership or group belonging of a file or directory, use the chown command:
$ ls - ltrh newfile
A common switch is - R to do the chown recursively and the general style is chown newuser:newgroup
file.
There is also a command specially for changing the group:
$ sudo chgrp postgres newfile
$ ls - ltrh newfile
If a user is member of different groups, she can change her default group using the newggrp command:
$ touch newfile
$ ls - ltrh newfile
104.6 – Create and change hard and symbolic links – 2
Objectives
On a storage device, a file or directory is contained in a collection of blocks. Information about a file is
contained in an inode, which records information such as the owner, when the file was last accessed,
how large it is, whether it is a directory or not, and who can read from or write to it.
$ ls -i $ ls -R
A link is simply an additional directory entry for a file or directory
$ ls -il $ vi link2file.txt #will edit both $ mv myfile.txt
A link is simply an additional directory entry for a file or directory, allowing two or more names for the
same thing.
The exact mechanism for storing the second name may depend on both the file system and the length
of the name. Symbolic links are also called symlinks.
Soft links , or symlinks , merely point to another file or directory by name rather than by inode. Soft links
can cross file system boundaries.
$ cp myfile.txt newfile.txt $ vi myfile.txt $ cat myfile.txt $ cat softlink.txt $ cat hardlink.txt $ cat
newfile.txt $ ls -il
You can create hard links only for files and not for directories. The exception is the special directory
entries in a directory for the directory itself and for its parent (. and …)
$ ls -ltrhi /etc/grub2.cfg
You will get an error if you attempt to create hard links that cross file systems or that are for directories.
$ ln mydir link2dir # error! $ ln -s mydir link2dir
If you are using relative names, you will usually want the current working directory to be the directory
where you are creating the link; otherwise, the link you create will be relative to another point in the file
system.
$ ln -s myfile.txt mydir/ #broken link
$ cd mydir $ ln -s …/myfile.txt.
we can find symbolic links with ls -i or even find:
$ find. -type l
and they are useful!
$ which java # linking to specific versions $ ls -ltrhi /etc/grub2.cfg # easier admin tasks $ ls -l /usr/lib64/
104.7 – Find system files and place files in the correct location – 2
Objectives
Filesystem Hierarchy Standard (FHS) is a document describing the Linux / Unix file hierarchy. It is very
useful to know these because it lets you easily find what you are looking for:
directory usage
bin Essential command binaries
boot Static files of the boot loader
dev Device files
etc Host-specific system configuration
lib Essential shared libraries and kernel modules
media Mount point for removable media
mnt Mount point for mounting a filesystem temporarily
opt Add-on application software packages
sbin Essential system binaries
directory usage
srv Data for services provided by this system
tmp Temporary files
usr Secondary hierarchy
var Variable data
home User home directories (optional)
lib Alternate format essential shared libraries (optional)
root Home directory for the root user (optional)
The /usr filesystem is the second major section of the filesystem, containing shareable , read - only data.
It can be shared between systems, although present practice does not often do this.
The /var filesystem contains variable data files , including spool directories and files, administrative and
logging data, and transient and temporary files. Some portions of /var are not shareable between
different systems, but others, such as /var/mail, /var/cache/man, /var/cache/fonts, and
/var/spool/news, may be shared.
Path
A general linux install has a lot of files; 741341 files in my case. So how does it find out where to look
when you type a command? This is done by a variable called PATH :
$ echo $PATH
/home/jadi/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
;/home/jadi/bin/
And for root user:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
As you can see, this is the list of directories separated with a colon. Obviously you can change your path
with export PATH=$PATH:/usr/new/dir or put this in .bashrc to make it permanent.
which, type and whereis
The which command shows the first appearance of the command given in path:
$ which mkfd
$ which mkfs
/sbin/mkfs
use the - a switch to show all appearance in the path and not only the first one.
But what happens if you which for?
$ which for
$ type for
for is a shell keyword
As you can see, which did not found anything for for and we used type.
$ type type
type is a shell builtin
$ type for
for is a shell keyword
$ type mkfs
mkfs is /sbin/mkfs
$ type mkfd
bash: type: mkfd: not found
The type command is more general that which and also understand and shows the bash keywords.
Another useful command in this category is whereis. Unlike which, whereis shows man pages and source
codes of programs alongside their binary location.
$ whereis mkfs
mkfs: /sbin/mkfs.bfs /sbin/mkfs.ext3 /sbin/mkfs.ext4 /sbin/mkfs.vfat /sbin/mkfs.cramfs
/sbin/mkfs.minix /sbin/mkfs.ext2 /sbin/mkfs.msdos /sbin/mkfs.fat /sbin/mkfs.ntfs /sbin/mkfs.ext4dev
/sbin/mkfs /usr/share/man/man8/mkfs.8.gz
$ whereis ping
ping: /bin/ping /usr/share/man/man8/ping.8.gz
$ whereis chert
chert:
$
Find
We have already seen this command in detail but lets see a couple of new switches.
$ find /tmp/ - maxdepth 1 - user jadi | head
$ find /tmp/ - maxdepth 1 - user jadi | head
/tmp/asheghloo.png
/tmp/tmpAN6Drb
/tmp/wrapper- 24115 - 2 - out
/tmp/sni-qt_goldendict_20048-sRlmvN
/tmp/asheghloo.gif
/tmp/zim-jadi
/tmp/3la.txt
/tmp/unity_support_test.0
/tmp/batman.jpg
Or even find the files not belonging to any user / group with - nouser and - nogroup.
Like other tests , you can add a! just before any phrase to negate it. So this will find files not
belonging to jadi: find.! -user jadi
Locate & Updatedb
Find and know that it is slow, It searches the file system on each run but lets see the fastest command:
$ locate happy
/home/jadi/.Spark/xtra/emoticons/Default.adiumemoticonset/happy.png
/home/jadi/.Spark/xtra/emoticons/sparkEmoticonSet/happy.png
/home/jadi/Downloads/jadi-net_radio-geek_040_antihappy.mp3
/usr/share/emoticons/kde4/unhappy.png
/usr/share/pixmaps/fvwm/mini.happy.xpm
/usr/share/pixmaps/pidgin/emotes/default/happy.png
/usr/share/pixmaps/pidgin/emotes/small/happy.png
/usr/src/linux-headers-3.13.0- 40 - generic/include/config/happymeal.h
/usr/src/linux-headers-3.16.0- 25 - generic/include/config/happymeal.h
/usr/src/linux-headers-3.16.0- 28 - generic/include/config/happymeal.h
/usr/src/linux-headers-3.16.0- 29 - generic/include/config/happymeal.h
And it is fast:
$ time locate kernel | wc - l
11235
real 0m0.341s
user 0m0.322s
sys 0m0.015s
This is fast because its data comes from a database created with updatedb command which is usually
run on a daily basis with a cron job. Its configuration file is /etc/updatedb.conf or /etc/sysconfig/locate:
$ cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS=“yes”
PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs"
PRUNEFS=“NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs
devfs mfs shfs sysfs cifs lustre tmpfs usbfs udf fuse.glusterfs fuse.sshfs curlftpfs ecryptfs fusesmb
devtmpfs”
Please note that you can update the db by running updatedb as root and get some info about it by -
S switch of locatecommand:
$ locate - S
Database /var/lib/mlocate/mlocate.db:
73,602 directories
711,894 files
46,160,154 bytes in file names
18,912,999 bytes used to store database
# And... the LPIC1 exam 101 is DONE! Congrats.
[http://j.mp/jadilpic1](http://j.mp/jadilpic1)
# LPIC-1 Exam 10 2
**105.1 - Customize and use the shell environment** **_Weight - 4_**
**Objectives**
- Set environment variables (e.g. PATH) at login or when spawning a new shell
- Write Bash functions for frequently used sequences of commands
- Maintain skeleton directories for new user accounts Set command search path with the proper
directory
**Terms**
### •.
- source
- /etc/bash.bashrc
- /etc/profile
- env
- export
- set
- unset
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
- ~/.bashrc
- ~/.bash_logout
- function
- alias
- lists
**Environment variables**
We should set them when we login or change shell. Oh! And what happen when we are not logged in?
;)
**login vs non-login shell**
Sometimes you _login_ into the shell, say after a ssh but sometimes you just _open_ a shell; say in GUI.
**login shell**
This happens when you give your user and pass to enter a shell. Many steps are involved to setup
your variables and settings. These are the steps:
**1 -** /etc/profile is run
**2 -** A line in /etc/profile runs whatever is in /etc/profile.d/*
Now the global profile is loaded and system will go for user specific profiles:
**3 -** /home/USERNAME/.bash_profile
**4 -** /home/USERNAME/.bash_login
**5 -** /home/USERNAME/.profile
Note that only one of the 3, 4 & 5 will be run. The system will go for .bash_profile and IF IT IS NOT
THERE will try for .bash_login and IF IT IS NOT THERE will try to run .profile. If any of these exists, the
system wont look any furthur. So if you have only 4 & 5, only the 4 will be run.
At the end, the system loads:
**6 -** /home/USERNAME/.bashrc
which is users information (like aliases).
Note: /etc/profile also loads /etc/bashrc or /etc/bash.bashrc
**Interactive (non-login) shell**
If you run a shell in an **interactive mode** (non-login) shell say from a GUI, only two things will be run:
1.^ /etc/bash.bashrc (or in some systems /etc/bashrc)^
2. /home/USERNAME/.bashrc
**Adding global configs for login shell**
You can add your global new config files int /etc/profile.d/ (with .sh at the end). It is cleaner and
better than editing the /etc/profile because an update can overwrite your changes if you do so.
**Adding global configs for interactive/non-login shell**
You can use /etc/bash.bashrc file (some systems /etc/bashrc). This is good for _aliases_ and other
global configs.
**user specific configs**
Most of the time PATH and env vars go into the in ~/.bash_profile and aliases go into the
~/.bashrc. Have a look at them!
**Aliases**
Most of the time they are defined in ~/.bashrc and look like this:
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
It is kind of a shortcut.
**/etc/skel**
This directory contains files which will be used as a **starting template** for each new user.
**.bash_logout**
Runs when you logout from a login shell. In many distros it only clears the screen so the next person
will not be able to watch what you were doing before you logout.
**. (and source)**
Yes, only a dot! This is a shortcut for the bash source command. You can find it in files like
/etc/profile. It runs the executable in front of it as part of the current environment.
Note: If you just execute a file (without source or dot) bash creates a child, runs the
executable there and then closes it.
**Functions**
Like "real" programming languages, Bash has functions, though in a somewhat limited
implementation. A function is a subroutine, a code block that implements a set of operations, a
"black box" that performs a specified task. Wherever there is repetitive code, when a task repeats
with only slight variations in procedure, then consider using a function.
funnyls () {
ls -ltrh
echo “This is a funny ls”
### }
Set
Set allows you to change the values of shell options and set the positional
parameters, or to display the names and values of shell variables.
Using set we can configure how bash works. These are some samples:
switch result
- b
Cause the status of terminated background jobs to be reported immediately, rather than before
printing the next primary prompt.
- e return in case a pipline, command, ... return non-zero
- n
Read commands but do not execute them; this may be used to check a script for syntax errors.
This option is ignored by interactive shells.
- t Exit after reading and executing one command.
- C Prevent output redirection using ‘>’, ‘>&’, and ‘<>’ from overwriting existing files.
**Export**
Set an environment variable. Mark each name to be passed to child processes in the
environment.
$ export name=jadi
$ echo $name
jadi
Note: when exporting, the variable will exists in child processes (commands you run from the
current shell). If you only say name=jadi and run a new command in your shell, the name **wont
be** jadi in **that** shell.
$ name=jadi
$ echo $name
jadi
$ bash
$ echo $name
$ exit
exit
$ echo $name
jadi
$ export name=jadi
$ bash
$ echo $name
jadi
**Unset**
This command _has nothing to do_ with set command! This can unset the defined variables or
functions.
$ **name=jadi**
$ **echo $name**
jadi
$ **unset name**
$ **echo $name**
You can also unset functions.
**env**
can set, remove or display variables or even run a command in a modified environment.
Syntax
env [OPTION]... [NAME=VALUE]... [COMMAND [ARGS]...]
Options
- u NAME
--unset=NAME
Remove variable NAME from the environment, if it was in the
environment.
### -
- i
--ignore-environment
Start with an empty environment, ignoring the inherited
environment.
**lists**
Bash even has arrays! We will see them later when scripting but you can do things like:
$ **list=(salam donya man injam)**
$ **echo ${list[1]}**
donya
$ **list=("salam donya" "man injam")**
$ **echo ${list[1]}**
man injam
**105.2 - Customize or write simple scripts - 4**
**Objectives**
- Use standard sh syntax (loops, tests)
- Use command substitution
- Test return values for success or failure or other information provided by a command
- Perform conditional mailing to the superuser
- Correctly select the script interpreter through the shebang (#!) line
- Manage the location, ownership, execution and suid-rights of scripts
**Terms and Utilities**
- for
- while
- test
- if
- read
- seq
- exec
**Shell Scripts**
Are a way of automating tasks.
**Shebang**
If a line starts with **#!** it is called **shebang** and tells the shell what **_interpreter_** to use for running
this script.
Note: Normally a # at the beginning of a script is for showing _comments_. Do not confuse it
with _Shebang_ (#!)
In many cases we run shells with #!/bin/bash or #!/bin/sh
We can use the command we already know in our shell scripts. A sample is:
#!/bin/bash
echo
echo "We are learning! Wowww..."
echo
**Variables**
Already seen in some parts. You can define variables like this **VARNAME=VALUE**. A sample:
#!/bin/bash
NAME=Jadi
echo
echo "$NAME is learning! Wowww..."
echo
Note: you can also do NAME="Jadi"
**Command substitution**
Sometimes you need to have a variable with the result of something to a variable. In this case
you can use $() construct:
FILES=$(ls - 1)
**Executing scripts**
If the file is executable, we can run them using the ./script_name.sh if we are in the same
directory, or give the complete path or include their directory ine $PATH variable. As you can see
they are just normal programs.
Another way is giving our scripts name as a parameter to the bash or sh commands.
Note: you know that for making a file executable we can do chmod 755 filename or chmod +x
fiename.
**Conditions**
Up to now, we were just running commands one by one. That is not very _programmatic_. If we are
going to have some _logic_ in our programs, we need _conditions_ and _loops_. First we will cover
conditions, using the if command. Its usage is like this:
if [condition]
then
do something
do another thing
else
do new things
even funnier things
fi
Note: else part is optional, if, then, fi is enough.
Conditions can be TRUE or FALSE. A very simple conditions is if [ "Linux" = "Linux" ]. Silly? I know
but we will change it soon but for now, learn the syntax! Specially the _spaces_ and _=_ for checking if
two strings are equal.
#!/bin/bash
kernel=$(uname - s)
if [ $kernel = "Linux" ]
then
echo YES. You are using a Linux
else
echo "Not a linux :("
fi
Note spaces and using double quotes (") on second echo because it has (character which will be
interpreted by bash if we do not enclose the string in a double quote.
The actual checking of the condition is done by test command which is writer as [ some test].
There are the other options:
conditions what is means
“a” = “b” if two strings are equal (here it will return False)
“a” != “b” string a is not equal to string b
4 - lt 40 if 4 is lower than 40 (True)
5 - gt 15 if 5 is greater than 15 (False)
5 - ge 5 if 5 is greater or equal 5
5 - le 3 if 5 is lower or equal to 3
9 - ne 2 9 is not equal with 2 (True)
- f FILENAME if file FILENAME exists
- s FILENAME if file exists and its size is more than 0
- x FILENAME file exists and is executable
**Read**
Using read we can read the user input. Look at this:
1 #!/bin/sh
2
3 echo "what is your name?"
4 read NAME
5
6 echo "Hello $NAME"
7
8 if [ $NAME = "Jadi" ]
9 then
10 echo "Oh I know you!"
11 else
12 echo "I wish I knew you"
13 fi
14
15 echo "Bye"
**For loop**
Generally, loops are used to run a specific set of commands more than once. The syntax is like
this:
for VAR in SOME_LIST;
do
some stuff with $VAR
some other stuff
done
Note the in, ;, do and done.
On each loop, the VAR will be equal to one of the SOME_LIST elements. SOME_LIST can be
numbers, name of files, words, ...
for NUM in 1 2 3 4 5 6;
do
echo $NUM
done
But what if you needed to go 1 to 42? We have the seq command which can be used like seq 1
42 or a shorthand like {1..42}.
Good part is we can use non-numbers too!
for FILE in $(ls);
do
echo $FILE
wc $FILE
done
**While loop**
This is another kind of loop but loops _while_ a conditions is TRUE. This is the syntax:
while [condition]
do
do something
do anohter thing
done
Note: If your condition will remains true all the time, the while loop will run _forever_. This is called
an _infinite loop_
This is sample:
VAR=52
while [ $VAR - gt 42 ]
do
echo VAR is $VAR and it is still greater than 42
let VAR=VAR- 1
done
we will have an infinite loop if we use let VAR=VAR+1. Ctrl+C will help us to _break_ the loop.
Note the let usage! If you just just say VAR=1 and then VAR=$VAR+1, then VAR will be equal
to 1+1 as an string!.
**Mailing the root user**
For sending mail, you need to install **mailutils**. Then the mail command will send emails. You can
send the mail to the root user by issuing this command:
jadi@funlife:~$ mail root
Cc:
Subject: Hi there root
hello there. This is my mail
And root will get this email. She can read it using mail command.
If you need to send emails in a script, just do:
$ echo "Body!" | mail - s "Subject" root
This can be easily embeded as poart of your scripts!
**105.3 - SQL data management** **_- 2_**
**Objectives**
- Use of basic SQL commands
- Perform basic data manipulation
**Terms and Utilities**
- Insert
- Update
- Select
- Delete
- From
- where
- group by
- order by
- join
**Databases**
This module is about SQL language and MySQL is one of the many SQL databases. For this lesson, a
database consists of some **table** s and each table has some **row** s and **filed** s. Lets have a look. In this
lesson we are not going to _create_ or _design_ databases. You only need to have a general
understanding of databases (SQL databases) and know some command to use (read query or
update or add to them). The database I'm going to use in this lesson is called lpic and has two
tables contact and info.
**mysql command line**
You only need to know that mysql is a command line program to interactively connect to a mysql-
server. I use it like this:
$ **mysql -u root -p**
Which means I'm going to use user root and will provide a password. It was also possible to say:
$ **mysql -u root -p mypass lpic**
To provide the pass on command line (not a good idea for security reasons!) and tell mysql
program to connect to lpic database when it starts.
**Using a database**
When you connect to a database, you have to use the use command to select which database you
are going to issue commands on. Normally a database server (say mysql) can have 100s of
different databases in it, each for one user or program.
jadi@funlife:~$ **mysql -u root -p**
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.6.25-0ubuntu<IP_ADDRESS> (Ubuntu)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> **SHOW DATABASES;**
### +--------------------+
| Database |
### +--------------------+
| information_schema |
| bad |
| good |
| lpic |
| mysql |
| performance_schema |
| ugly |
### +--------------------+
7 rows in set (0.00 sec)
mysql> **USE LPIC;**
Reading table information for completion of table and column names You
can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
### +---------------- +
| Tables_in_lpic |
+---------------- +
| info |
| phonebook |
+---------------- +
2 rows in set (0.00 sec)
As you can see mysql is friendly and shows lovely tables! I've told her to use lpic and then show
tables and now I know that I have two tables: info & phonebook.
Note: it is common to type MYSQL commands in CAPITAL LETTERS and names and values and ..
in lower case.
### SELECT
**SELECT** is obvious! It selects from a table. When we are not sure what field we are looking for, we
can select * to get all fields.
mysql> **SELECT * FROM phonebook;**
### +-------- +------------------ +---------------- +
| name | email | phone |
+-------- +------------------ +---------------- +
| jadi |<EMAIL_ADDRESS>| +9890something |
| nasrin |<EMAIL_ADDRESS>| +9898989898 |
| sina |<EMAIL_ADDRESS>| +687randomnum |
haale | | 0935secret | +--------+------------------+----
------------+ 4 rows in set (0.00 sec)
### |
Or ask for a specific field:
mysql> **SELECT name FROM phonebook;**
### +--------+
| name |
### +--------+
| jadi |
| nasrin |
| sina |
| haale |
### +--------+
3 rows in set (0.00 sec)
### WHERE
You can **add** **_conditions_** to your SQL queries using **WHERE**. Let’s have a look at the other table
we have:
mysql> **SELECT * FROM info;**
### +--------+--------+--------+-------+
| name | height | weight | mood |
+--------
±—
---- ±------- ±------ +
| jadi | 180 | 74 | happy |
| sina | 175 | 81 | happy |
| nasrin | 174 | 68 | happy |
| mina | 171 | 59 | sad |
+--------
±—
---- ±------- ±------ +
4 rows in set (0.00 sec)
What if we only wanted to see our _happy_ friends?
mysql> **SELECT * FROM info WHERE mood = 'happy';**
### +--------+--------+--------+-------+
| name | height | weight | mood |
+--------
±—
---- ±------- ±------ +
| jadi | 180 | 74 | happy |
| sina | 175 | 81 | happy |
| nasrin | 174 | 68 | happy |
+--------
±—
---- ±------- ±------ +
3 rows in set (0.00 sec)
mysql> **SELECT * FROM info WHERE mood = 'happy' AND weight >= 80;**
### +--------
- +-------- +------- +
| name | height | weight | mood |
### +--------
- +-------- +------- +
| sina | 175 | 81 | happy |
### +--------
- +-------- +------- +
3 rows in set (0.00 sec)
Or if I only needed the **name** :
mysql> **SELECT name FROM info WHERE mood = 'happy' AND weight >= 80;**
### +------+
| name |
### +------+
| sina |
### +------+
### ORDER BY
This is used if you want to **sort** the data based on one field. Here I'm checking my phone book
based on people’s names:
mysql> **SELECT * FROM phonebook ORDER BY name;**
### +-------- +------------------ +---------------- +
| name | email | phone |
+-------- +------------------ +---------------- +
| haale | | 0935secret |
| jadi |<EMAIL_ADDRESS>| +9890something |
| nasrin |<EMAIL_ADDRESS>| +9898989898 | | sina |
<EMAIL_ADDRESS>| +687randomnum | +--------+--------
----------+----------------+ 4 rows in set (0.00 sec)
This order can be done on any field, including numbers:
mysql> **SELECT * FROM info ORDER BY height;**
### +--------+--------+--------+-------+
| name | height | weight | mood |
+--------
±—
---- ±------- ±------ +
| mina | 171 | 59 | sad |
| nasrin | 174 | 68 | happy |
| sina | 175 | 81 | happy |
| jadi | 180 | 74 | happy |
+--------
±—
---- ±------- ±------ +
4 rows in set (0.00 sec)
### GROUP BY
This will **_group_** the output. Unfortunately, this is not very clear.
mysql> **SELECT * FROM info GROUP BY mood;** +---
---+--------+--------+-------+
| name | height | weight | mood |
### + ------+-------- +-------- +------- +
| jadi | 180 | 74 | happy |
| mina | 171 | 59 | sad |
+ ------+-------- +-------- +------- +
2 rows in set (0.00 sec)
We've seen that we have only two moods in our table: sad & happy. When SELECTing all fields
(that is *) from this table GROUP BY mood, SQL will check all the moods, shows us only ONE from
each. This can be used like the **uniq** command you learned from LPIC101:
mysql> **SELECT mood FROM info GROUP BY mood;**
### +------- +
| mood |
+------- +
| happy |
| sad |
+------- +
2 rows in set (0.00 sec)
Which gives you all available moods in the table. In real life this is not very useful and most of the
times it is combined with count. Have a look:
mysql> **SELECT count(mood), mood FROM info GROUP BY mood;**
### +-------------+-------+
| count(mood) | mood |
### + ------------- +------- +
| 3 | happy |
| 1 | sad |
+ ------------- +------- +
2 rows in set (0.00 sec)
Which counts home many rows have that specific mood. So I have 3 happy friends and one sad
friend.
Note: count is not part of LPIC 105.3
### INSERT
Another clear command. It **adds a new row** to a table. Say I want to add some data to
phonebook:
mysql> **INSERT INTO phonebook (name, phone, email) VALUES ('ghasem',
'+982112345678', '');**
Query OK, 1 row affected (0.01 sec)
mysql> **SELECT * FROM phonebook;**
### +-------- +------------------ +---------------- +
| name | email | phone |
+-------- +------------------ +---------------- +
| jadi |<EMAIL_ADDRESS>| +9890something |
| nasrin |<EMAIL_ADDRESS>| +9898989898 |
| sina |<EMAIL_ADDRESS>| +687randomnum |
| haale | | 0935secret |
| ghasem | | +982112345678 |
### +--------+------------------+----------------+
5 rows in set (0.00 sec)
### DELETE
This will DELETE from a table. But be careful of what you delete... WHERE is your fiend here:
mysql> **DELETE FROM phonebook WHERE name = 'ghasem';**
mysql> **SELECT * FROM phonebook;**
### +-------- +------------------ +---------------- +
| name | email | phone |
+-------- +------------------ +---------------- +
| jadi |<EMAIL_ADDRESS>| +9890something |
| nasrin |<EMAIL_ADDRESS>| +9898989898 |
| sina |<EMAIL_ADDRESS>| +687randomnum |
| haale | | 0935secret |
+-------- +------------------ +---------------- +
4 rows in set (0.00 sec)
### UPDATE
It updates (changes) row and again WHERE is your friend:
mysql> **SELECT * FROM phonebook;**
### +-------- +------------------ +---------------- +
| name | email | phone |
+-------- +------------------ +---------------- +
| jadi |<EMAIL_ADDRESS>| +9890something |
| nasrin |<EMAIL_ADDRESS>| +9898989898 | | sina |
<EMAIL_ADDRESS>| +687randomnum | | haale | |
0935secret | +--------+------------------+----------------+ 4
rows in set (0.00 sec)
mysql> **UPDATE phonebook SET<EMAIL_ADDRESS>WHERE name = 'haale';** Query
OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> **SELECT * FROM phonebook;**
### +-------- +------------------
– +
| name | email | phone |
### +-------- +------------------
– +
| jadi |<EMAIL_ADDRESS>| +9890something |
| nasrin |<EMAIL_ADDRESS>| +9898989898 |
| sina |<EMAIL_ADDRESS>| +687randomnum |
| haale |<EMAIL_ADDRESS>| 0935secret |
### +-------- +------------------
– +
4 rows in set (0.00 sec)
### JOIN
Join will **join** / **mix two tables**.
mysql> **SELECT * FROM phonebook JOIN info;**
### +-------- +------------------ +---------------- +-------- +--------
±----
— ±------ +
| name | email | phone | name | height | weight | mood |
### +-------- +------------------ +---------------- +-------- +--------
±----
— ±------ +
| jadi |<EMAIL_ADDRESS>| +9890something | jadi | 180 | 74 | happy |
| nasrin |<EMAIL_ADDRESS>| +9898989898 | jadi | 180 | 74 | happy |
| sina |<EMAIL_ADDRESS>| +687randomnum | jadi | 180 | 74 | happy |
| haale |<EMAIL_ADDRESS>| 0935secret | jadi | 180 | 74 | happy |
| jadi |<EMAIL_ADDRESS>| +9890something | sina | 175 | 81 | happy |
| nasrin |<EMAIL_ADDRESS>| +9898989898 | sina | 175 | 81 | happy |
| sina |<EMAIL_ADDRESS>| +687randomnum | sina | 175 | 81 | happy |
| haale |<EMAIL_ADDRESS>| 0935secret | sina | 175 | 81 | happy |
| jadi |<EMAIL_ADDRESS>| +9890something | nasrin | 174 | 68 | happy |
| nasrin |<EMAIL_ADDRESS>| +9898989898 | nasrin | 174 | 68 | happy |
| sina |<EMAIL_ADDRESS>| +687randomnum | nasrin | 174 | 68 | happy |
| haale |<EMAIL_ADDRESS>| 0935secret | nasrin | 174 | 68 | happy |
| jadi |<EMAIL_ADDRESS>| +9890something | mina | 171 | 59 | sad |
| nasrin |<EMAIL_ADDRESS>| +9898989898 | mina | 171 | 59 | sad |
| sina |<EMAIL_ADDRESS>| +687randomnum | mina | 171 | 59 | sad |
| haale |<EMAIL_ADDRESS>| 0935secret | mina | 171 | 59 | sad |
### +-------- +------------------ +---------------- +-------- +--------
±----
— ±------ +
16 rows in set (0.00 sec)
Every single row from first table (phonebook) is copied in front of the second table (info). Not very
useful _yet_. It becomes useful when you give a _common field_ or tell it to JOIN tables based on a
criteria; using WHERE. Here is the magic:
mysql> **SELECT * FROM phonebook JOIN info ON phonebook.name = info.name;**
### +-------- +------------------ +---------------- + --------+-------- +-------- +------- +
| name | email | phone | name | height | weight | mood |
+-------- +------------------ +---------------- + --------+-------- +-------- +------- +
| jadi |<EMAIL_ADDRESS>| +9890something | jadi | 180 | 74 | happy |
| sina |<EMAIL_ADDRESS>| +687randomnum | sina | 175 | 81 | happy |
| nasrin |<EMAIL_ADDRESS>| +9898989898 | nasrin | 174 | 68 | happy |
### +-------- +------------------
– ±------- ±------- ±------- ±------ +
3 rows in set (0.00 sec)
Great! Now I have my friends list, their moods and their phone numbers! Say I'm bored and
I need to phone a cool friend:
mysql> **SELECT phonebook.name, phone, mood FROM phonebook JOIN info ON
phonebook.name = info.name WHERE mood = 'happy';**
### +-------- +---------------- +------- +
| name | phone | mood |
+-------- +---------------- +------- +
| jadi | +9890something | happy |
| sina | +687randomnum | happy |
| nasrin | +9898989898 | happy |
+-------- +---------------- +------- +
3 rows in set (0.00 sec)
Note: both tables have a field called name so I needed to use phonebook.name to tell SQL which
name I want to show.
mysql> **SELECT phonebook.name, phone, mood FROM phonebook JOIN info ON
phonebook.name = info.name AND height < 175;**
### +-------- +------------- + -------+
| name | phone | mood |
+-------- +------------- + -------+
| nasrin | +9898989898 | happy |
+-------- +------------- + -------+
1 row in set (0.00 sec)
mysql> **SELECT * FROM info WHERE mood = 'sad';**
### +------+--------+--------+------+
| name | height | weight | mood |
### + ------+-------- +-------- +------ +
| mina | 171 | 59 | sad |
+ ------+-------- +-------- +------ +
1 row in set (0.01 sec)
mysql> **UPDATE info SET mood = 'happy' WHERE name = 'mina';**
Query OK, 0 rows affected (0.02 sec) Rows matched: 1 Changed: 0
Warnings: 0
mysql> **SELECT * FROM info WHERE mood = 'sad';**
Empty set (0.00 sec)
mysql> **SELECT * FROM info;**
### +--------+--------+--------+-------+
| name | height | weight | mood |
+--------
±—
---- ±------- ±------ +
| jadi | 180 | 74 | happy |
| sina | 175 | 81 | happy |
| nasrin | 174 | 68 | happy |
| mina | 171 | 59 | happy |
+--------
±—
---- ±------- ±------ +
**Quit**
mysql> **quit**
Bye
jadi@funlife:~$
**106.1 - Install and configure X11** **_- 2_**
**Objectives**
- Verify that the video card and monitor are supported by an X server
- Awareness of the X font server
- Basic understanding and knowledge of the X Window configuration file
**Terms and Utilities:**
- /etc/X11/xorg.conf
- xhost
- DISPLAY
- xwininfo
- xdpyinfo
- X
**History**
This lesson is useless in modern life! Very strange but practically nothing in this lesson is used in
real life because **xorg.conf** , **xhost** , ... is not used in any modern linux system anymore. Maybe
they are here so you won’t be shocked if you see an older linux.
### X
The X Window System is a network transparent window system which runs on a wide range of
computing and graphics machines; including practically ALL Linux systems with graphical interfaces.
It is also called X11 because of its version, X window system, X server.
**/etc/X11/xorg.conf**
This is file X used to use for its configuration. In most cases this is automatically generated and
works. Newer systems do not have this file so let’s have a look at a xorg.conf I found on the
Internet.
Section "Files"
FontPath “/usr/share/X11/fonts/misc”
FontPath “/usr/share/X11/fonts/100dpi/:unscaled”
FontPath “/usr/share/X11/fonts/75dpi/:unscaled”
FontPath “/usr/share/X11/fonts/Type1”
FontPath “/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType”
EndSection
This part is about **Fonts**. When X-Server is running it needs these files. FontPaths tell X11 where
fonts are. It also can refer to an IP running a font-server which is not common these days. Font
servers used to be responsible of rendering fonts to be shown on clients.
Section "Module"
Load “bitmap”
Load “ddc”
Load “dri”
Load “extmod”
Load “freetype”
Load “glx”
Load “int10”
Load “type1”
Load “vbe”
Load “dbe”
EndSection
These are modules. We are asking X server to load so called modules.
Next we have to define our **InputDevices** :
Section "InputDevice"
Identifier “Generic Keyboard”
Driver “kbd”
Option “CoreKeyboard”
Option “XkbRules” “xorg”
Option “XkbModel” “pc105”
Option “XkbLayout” “us”
EndSection
Section "InputDevice"
Identifier “Configured Mouse”
Driver “mouse”
Option “CorePointer”
Option “Device” “/dev/input/mice”
Option “Protocol” “ImPS/2”
Option “Emulate3Buttons” “true”
Option “ZAxisMapping” “4 5”
EndSection
Section "InputDevice"
Identifier “Synaptics Touchpad”
Driver “synaptics”
Option “SendCoreEvents” “true”
Option “Device” “/dev/psaux”
Option “Protocol” “auto-dev”
Option “RightEdge” “5000”
EndSection
As you can see each device has an **Identifier** , **Driver** and **some options**. We just defined a
mouse, a keyboard and a touchpad and gave them some names. Section "Device"
Identifier “ATI Technologies, Inc. Radeon Mobility 7500 (M7 LW)”
Driver “radeon”
BusID “PCI:1:0:0”
Option “DynamicClocks” “on”
Option “CRT2HSync” “30-80”
Option “CRT2VRefresh” “59-75”
Option “MetaModes” "1024x768 800x600 640x480
1024x768+1280x1024"
EndSection
A graphic card is defined above. Again it has its identifies (name), its drivers and some
options (like support resolutions, refresh rates, ...). This device needs a screen and a
monitor:
Note: The vesa points to a low resolution, always working driver. It is used for
troubleshooting.
Section "Monitor"
Identifier “Generic Monitor”
Option “DPMS”
EndSection
Section "Screen"
Identifier “Screen0”
Device “Screen0 ATI Technologies, Inc. Radeon Mobility 7500 (M7 LW)”
Monitor “Generic Monitor”
DefaultDepth 24
SubSection “Display”
Depth 1
Modes “1024x768”
EndSubSection
SubSection “Display”
Depth 4
Modes “1024x768”
EndSubSection
SubSection “Display”
Depth 8
Modes “1024x768”
EndSubSection
SubSection “Display”
Depth 15
Modes “1024x768”
EndSubSection
SubSection “Display”
Depth 16
Modes “1024x768”
EndSubSection
SubSection “Display”
Depth 24
Modes “1024x768”
EndSubSection
EndSection
Note how the screen uses the defined monitor (using its identifier "Generic Monitor") and defined
graphic card. Also note the different colour modes (say 24bit 1024x768).
At the end we have to glue all of the above in one place as **ServerLayout** :
Section "ServerLayout"
Identifier “DefaultLayout”
Screen “Default Screen”
InputDevice “Generic Keyboard”
InputDevice “Configured Mouse”
InputDevice “Synaptics Touchpad”
EndSection
We have a layout with a screen and 3 input devices :)
Note: Do not panic. It is enough for you to understand the Section and EndSection and a general
understanding of the xorg.conf
**xwininfo**
The **xwininfo** command is a window information utility for X. Run it and it waits for you to click on
any window and gives you some information about that _window_ like its size, position, color
depth, ...
$ **xwininfo**
xwininfo: Please select the window about which you
would like information by clicking the mouse in
that window.
xwininfo: Window id: 0x5400004 "jadi@funlife: ~/w/lpic/lpic1book"
Absolute upper-left X: 629
Absolute upper-left Y: 245
Relative upper-left X: 10
Relative upper-left Y: 36
Width: 655
Height: 426
Depth: 32
Visual: 0x71
Visual Class: TrueColor
Border width: 0
Class: InputOutput
Colormap: 0x5400003 (not installed)
Bit Gravity State: NorthWestGravity
Window Gravity State: NorthWestGravity
Backing Store State: NotUseful
Save Under State: no
Map State: IsViewable
Override Redirect State: no
Corners: +629+245 -82+245 - 82 - 97 +629- 97
- geometry 80x24- 72 - 87
**xdpyinfo**
This give you information about the running X session. Things like screens, color depth,
version, name, ...
name of display: :0
version number: 11.0
vendor string: The X.Org Foundation
vendor release number: 11701000
X.Org version: 1.17.1
maximum request size: 16777212 bytes
motion buffer size: 256
bitmap unit, bit order, padding: 32, LSBFirst, 32
image byte order: LSBFirst
number of supported pixmap formats: 7
supported pixmap formats:
depth 1, bits_per_pixel 1, scanline_pad 32
depth 4, bits_per_pixel 8, scanline_pad 32
depth 8, bits_per_pixel 8, scanline_pad 32
depth 15, bits_per_pixel 16, scanline_pad 32
depth 16, bits_per_pixel 16, scanline_pad 32
depth 24, bits_per_pixel 32, scanline_pad 32
depth 32, bits_per_pixel 32, scanline_pad 32
keycode range: minimum 8, maximum 255
focus: window 0x5400005, revert to Parent
number of extensions: 28
### BIG-REQUESTS
Composite
### DAMAGE
### ...
### ...
**xhost**
This command used to **control the access** to the X server. If you are on a X server and run **xhost** it
tells you the access status.
$ **xhost**
access control enabled, only authorized clients can connect
SI:localuser:jadi
As you can see only authorized clients can connect. To open it for all:
jadi@funlife:~$ **xhost +**
access control disabled, clients can connect from any host
And for closing it again:
jadi@funlife:~$ **xhost -**
access control enabled, only authorized clients can connect
Or open it for only one specific IP:
jadi@funlife:~$ **xhost +<IP_ADDRESS>**
<IP_ADDRESS> being added to access control list
jadi@funlife:~$ **xhost**
access control enabled, only authorized clients can connect
INET:<IP_ADDRESS> (no nameserver response within 5 seconds)
SI:localuser:jadi
### DISPLAY
This variable tell graphical program where to show their graphical output (where to draw their
inputs). In normal cases this is set on my own machine:
$ **echo $DISPLAY**
### :0
But if another X is listening to all IPs (after xhost +) or listening to my machine (after xhost
<IP_ADDRESS>) I can change the DISPLAY environment and connect my graphical output to that
machine. In this case if I run a graphical program, its output (windows) will be shown on another
machine:
$ **export DISPLAY=<IP_ADDRESS>:0**
$ xeyes # the eyes will be shown on <IP_ADDRESS> machine
Note: This wont work if you test it on a modern machine. Most X11s do not listen on any port
these days.
**106.2 - Setup a display manager** **_- 1_**
**Objectives**