Linux presentation
A BASIC INTRODUCTION
What is Linux ?
Linux is a free and open source operating system.
At it’s core, the Linux operating system is derived from the Unix OS.
Unix was created in the 1960s by Dennis Ritchie and Ken
Thompson, both of them also invented the C programming
language.
Linux was initially named GNU and was developed by Richard
Stallman
Linux was the name of the kernel created in 1991 by Linux Torvalds,
a student at the University of Helsinki.
People started calling the GNU OS, Linux – because of the name of
the kernel
What distributions of Linux do
exist?
Linux OS has multiple distributions (called distros) that are derived from it’s initial deployment.
Most of the are FREE and offer full functionality:
Examples:
Debian
Ubuntu
CentOS
OpenSUSE
Mint
Gentoo
Slackware.
What distributions of Linux do
exist (II)?
Some examples of non-free (enterprise) Linux distros are:
Red Hat Enterprise Linux
SUSE Linux Enterprise Server
Oracle Linux
Scientific Linux
Turbo Linux
Linux Mandriva
For more info about various types of Linux:
http://distrowatch.com/
Why should I care about Linux ?
In September 2008 Steve Ballmer (Microsoft CEO) claimed 60% of servers run Linux
and 40% run Windows Server. According to IDC's report covering Q2 2013, Linux
was up to 23.2% of worldwide server revenue.
Linux is used as:
Server (HTTP, FTP, DNS, file server, etc)
Desktop (it’s a free alternative to Microsoft’s Windows XP, Vista, 7, 8 family)
Supercomputer operating system:
According to Wikipedia & top500.org, over 95% of Supercomputers use Linux
as their host OS.
You can also find Linux distros in:
Routers, firewalls, switches
Smartphones (see Android) Graph showing the market share of
Gaming consoles (Sony PlayStation, Valve SteamBox) Linux in the SuperComputer area
Simplified architecture of Linux
(I)
Kernel:
The kernel is the heart of the operating system.
It interacts with hardware and most of the tasks like memory management, task scheduling and file
management.
Shell:
The shell is the utility that processes your requests.
When you type in a command at your terminal, the shell interprets the command and calls the program
that you want.
The shell uses standard syntax for all commands.
C Shell, Bourne Shell and Korn Shell are most famous shells which are available with most of the Unix
variants.
Simplified architecture of Linux
(II)
Commands and Utilities:
There are various commands and utilities which you would use in your day to day activities.
cp, mv, cat and grep are few examples of commands and utilities.
There are over 250 standard commands plus numerous others provided through 3rd party software.
All the commands come along with various optional options.
Files and Directories:
All data in Linux is organized into files. All files are organized into directories.
These directories are organized into a tree-like structure called the filesystem.
Simplified architecture of Linux
(III)
The diagram:
Remote access to a linux server
(I)
Usually is done via SSH
The SSH server can be installed like this:
sudo apt-get install openssh-server // in Ubuntu
yum install openssh-server // In RedHat, CentOS
Start the SSH server:
sudo service ssh restart // in Ubuntu
service sshd start //in Redhat, CentOS
Download a terminal emulator client:
putty or Ericom Interconnect
Remote access to a linux server
(II)
Run putty, enter the hostname/IP, the port (default is 22) and hit “Open”.
Remote access to a linux server
(III)
Enter the user/password and you are connected to the Linux BASH environment
BASH – the Linux shell
BASH is a programming/scripting language
BASH shell is the Linux equivalent of the Windows cmd
BASH is a command processor that typically runs in a text window, where the user types
commands that cause actions
BASH runs scripts (python, perl, etc)
It has been ported to Windows (via Cygwin)
BASH – the golden rule
When you do not know what a command does:
man – stands for manual
man ls
man cd
man grep
etc,
Who am I ? Who’s logged in ?
whoami – shows the user you are currently logged in with
users – displays (all) the users currently logged in
System information
So you are logged into this black Linux shell, but you have no info about the type of Linux distro
or the architecture…
uname – prints the name, version and other details about the current machine and the
operating system running on it.
lsb_release -a - prints Distribution information.
Copying, renaming and deleting
files
Make a copy of a file using the cp command.
cp source_file destination_file
Renaming a file with the mv command:
mv old_file new_file
Delete one or multiple files with rm:
rm filename1
rm filename1 filename2 filename3 //multiple files
rm –r –f /home/cristian/* // deletes all files in /home/Cristian without confirmation
rm *.txt ./ //deletes all .txt files in the current directory
Change directory, list files
cd - The cd command is used to change the current directory (i.e., the directory in which the
user is currently working) in Linux.
cd /home/cristian
cd ~ // “~” stands for the user’s home directory
ls - list directory contents
ls –lh /home/
ls ./
Read, create, modify and display
files (I)
cat - (short for concatenate) command is one of the most frequently used commands on Linux
It can be used for:
Display text file on screen
Read text file
Create a new text file
Modifying file
Read, create, modify and display
files (II)
Read text file
cat file_name
cat /path/to/file
Create a new text file
cat > newfile.txt // can be done with the touch command
Read, create, modify and display
files (III)
Modifying file:
To append (add data to existing) data to a file called foo.txt, enter:
Extra:
List the foo.txt file and display line numbers
Very useful when you encounter script errors
Filter output with grep
grep - searches the named input FILEs for the lines that match the specified pattern
grep is the equivalent of findstr.exe in Windows
Example:
I want to list the /var/log/messages file for the “error” patern
grep error /var/log/messages
Or with pretty colors
grep -i error /var/log/messages
Control the output with more &
less
more - is a filter for paging through text one screenful at a time
less - is a program similar to more (1), but which allows backward movement in the file as
well as forward movement.
The syntax:
more /my/log/file
less /my/log/file
Server load (I)
Hard drive usage:
df - displays the amount of disk space available on the file system
du - estimates and displays the disk space used by files and directories
Server load (II)
Processor, memory, general server load
top - provides a dynamic real-time view of a running system. It can display system summary information, as well as a list of
processes or threads currently being managed by the kernel
Server load (II)
Processor, memory, general server load
htop – similar to top, but with more details and fancier colors
Linux directory hierarchy
In Windows we call them folders, in Linux the term used is directory/directories.
Linux directory hierarchy (II)
The equivalent of the “C:\” partition in Windows is referred in Linux as “/” – also called “root directory”,
or “slash”.
The Linux filesystem has the root directory at the top of the directory tree.
The following list of directories are subdirectories of the root directory. This directory is denoted by
the / (pronounced "slash") symbol.
/bin:
Contains executable programs such as ls (“dir” in Windows) and cp (“copy” in Windows). These programs are designed to make the
system usable.
/etc
Contains configuration files which are local to the machine. Programs store configuration files in this directory and these files are
referenced when programs are run.
/home
Contains user account directories. Each user created by the system administrator will have a subdirectory under /home with the
name of the account. This is the default behaviour of Linux systems. E.g. User account for Anna is created, her home directory will
be located in /home/anna.
Linux directory hierarchy (III)
/mnt
Used for mounting temporary filesystems. When mounting a CD-ROM for instance, the standard mount point location is
/mnt/cdrom.
/opt
Used for storing random data that has no other logical destination.
/proc
Provides information about running processes and the kernel. A directory is provided for each running process. Useful system
information such as the amount of Random Access Memory (RAM) available on the system as well as Central Processing Unit (CPU)
speed in Megahertz (MHz) can be found within the /proc directory.
/root
This is the home directory for the super user (root). This directory is not viewable from user accounts. The /root directory usually
contains system administration files.
/sbin
Similar to /bin, this directory contains executable programs needed to boot the system, however the programs within /sbin are
executed by the root user.
/tmp
This directory is used for temporary storage space. Files within this directory are often cleaned out either at boot time or by a
regular job process.
Linux directory hierarchy (IV)
/usr
Used to store applications. When installing an application on a Debian GNU/Linux machine, the typical path to install would be
/usr/local. You will notice the directory structure within /usr appears similar to the root directory structure.
/var
This directory contains files of variable file storage. Files in /var are dynamic and are constantly being written to or changed. This
the directory where websites are usually stored in.
Users and groups
Similar to Windows:
Linux has limited access users and, by default, one administrator (called “root”)
root is the user name or account that by default has access to all commands and files on Linux.
It is also referred to as the root account, root user and the superuser.
You can grant root like access to limited users using sudo (see “Run as Administrator in Windows”)
Users and groups (II)
With sudo, as a limited permissions user, you can be granted, temporarily, administrator/root access to execute
commands usually restricted to only the root user.
sudo is used in Linux Debian derivatives distros (Ubuntu, SteamOS from Valve, Kali Linux, etc) – but not limited to only
Debian
sudo cand be installed on any Linux system
Not every user can use sudo. That user must be present in the /etc/sudoers file
In the BASH environment/the linux shell, the root user can be recognized by
the pound sign (#). Limited users can be recognized by the “$” sign after their name.
When not sure about the user you are currently logged in, issue the whoami command
Users and groups (III)
All users have:
user IDs (uid), group IDs (gid).
The uid and gid are always decimal numbers and start from 1000 or 10000
The root superuser usually has uid and gid 0 (zero)
A specific user can be member of multiple groups.
The id command show all the information you need to know about a user
Try issuing the id root command and see what happens
Users and groups (IV)
How do I add a new user via the linux shell?
useradd Cristian –p test123
The command above created a new user called ericom with the password test123
How do I assign a user to another group?
usermod –G root cristian
I added the user Cristian to the root group.
Users and groups (V)
Create a new group:
Delete a group:
Users and groups (VI)
Change the password of a user with the passwd command:
Login as root if you are changing a password for an account different than yours
If you are logged in with a limited user account, use the su command or sudo su to login as root
Permissions system in Linux (I)
Each file and directory has three user based permission groups:
owner - The Owner permissions apply only the owner of the file or directory, they will not impact the
actions of other users.
group - The Group permissions apply only to the group that has been assigned to the file or directory,
they will not effect the actions of other users.
all users - The All Users permissions apply to all other users on the system, this is the permission group
that you want to watch the most.
Permissions system in Linux (II)
Permission Types
Each file or directory has three basic permission types:
The read permission grants the ability to read a file. When set for a directory, this permission grants the ability to
read the names of files in the directory, but not to find out any further information about them such as contents, file
type, size, ownership, permissions.
The write permission grants the ability to modify a file. When set for a directory, this permission grants the ability to
modify entries in the directory. This includes creating files, deleting files, and renaming files.
The execute permission grants the ability to execute a file. This permission must be set for executable programs,
including shell scripts, in order to allow the operating system to run them. When set for a directory, this permission
grants the ability to access file contents and meta-information if its name is known, but not list files inside the
directory, unless read is set also
Permissions system in Linux (III)
View the permissions:
ls is the utility you need
Is the equivalent of dir in Windows
Standard usage is ls –lh (list, show permissions and display them in human readable format)
Any file or folder that starts with a dot character (for example, /home/user/.config), commonly called a dot file or dotfile, is
hidden.
Permissions system in Linux (IV)
Reading the file and directory permissions
-rw-r--r-- 1 root ericom 0 Jun 12 16:02 file.txt
The first character (-) indicates the file type and is not related to permissions. The remaining nine characters are in three sets, each
representing a class of permissions as three characters:
The first set represents the user class.
The second set represents the group class.
The third set represents the others class.
Each of the three characters represent the read, write, and execute permissions:
• r if reading is permitted, - if it is not.
• w if writing is permitted, - if it is not.
• x if execution is permitted, - if it is not.
In our example, -rw-r--r-- root ericom means:
rw- r-- r--
The owner (root) can read and The users in the ericom group can Everyone else can read the file
write the file read the file
Permissions system in Linux (V)
Another example:
-rwxr-x--- 1 root ericom 144K Jun 12 11:02 script.sh
rwx r-x ---
Owner (root in this case) The users in the ericom Everyone else cannot read,
can read, write and group can read and write or execute the files.
execute the file execute the file
Permissions system in Linux (VI)
The alternative to the symbolic (rwx) permission system:
Meet the octal notation:
Symbolic Notation Octal Notation English
---------- 0000 no permissions
---x--x--x 0111 execute
--w--w--w- 0222 write
--wx-wx-wx 0333 write & execute
-r--r--r-- 0444 read
-r-xr-xr-x 0555 read & execute
-rw-rw-rw- 0666 read & write
-rwxrwxrwx 0777 read, write, & execute
Permissions system in Linux (VII)
Modify the permissions with chmod
When you:
grant permission you use the plus sign “+”
take permission away you will use the minus sign “-”
Example 1:
Grant permission for read, write and execute to the file owner
chmod u+rwx file.txt //in octal: chmod 700 file.txt
Example 2:
Take away all privileges from user eircom for file.txt
chmod u-rwx file.txt
Permissions system in Linux
(VIII)
Example 3:
Grant permission for read, write and execute for user, group and everyone else
chmod ugo+rwx file.txt // in octal: chmod 777 file.txt
Example 2:
Take away all privileges from user, group and everyone else
chmod ugo-rwx file.txt // in octal: chmod 000 file.txt
Example 3:
Grant recursive permission in a specific directory
chmod -R ugo+rwx /path/to/my/directory // in octal: chmod –R 777 /path/to/my/directory
How do I install software using a
package manager?
In Debian & Ubuntu like systems:
apt-get install apache2
// installs the Apache httpd server
In Redhat and CentOS like systems:
yum install httpd
//installs Apache httpd server. See the difference in names!
RPMs and DEB files
RPM Package Manager (RPM) (originally Red Hat Package Manager) is a package management
system. The name RPM variously refers to the .rpm file format, files in this format, software
packaged in such files, and the package manager itself.
deb is the extension of the Debian software package format and the most often used name for
such binary packages.
How do I install software without
a package manager (I)?
In Debian & Ubuntu like systems:
wget http://www.eu.apache.org/dist//directory/apacheds/dist/2.0.0-M20/apacheds-2.0.0-M20-amd64.deb
//download the file DEB file
chmod +x apacheds-2.0.0-M20-amd64.deb // make the file executable
dpkg -i apacheds-2.0.0-M20-amd64.deb // install the Apache DEB package
/etc/init.d/apache2 start ///start Apache
How do I install software without
a package manager (II)?
In Redhat and CentOS like systems:
wget ftp://rpmfind.net/linux/centos/5.11/os/i386/CentOS/httpd-2.2.3-91.el5.centos.i386.rpm
//download the RPM file
chmod+x httpd-2.2.3-91.el5.centos.i386.rpm // make the file executable
rpm -i httpd-2.2.3-91.el5.centos.i386.rpm // install the httpd RPM file
service httpd start // start the Apache server
How do I install software by
compiling from the source (I)?
Software can be installed from the code source without being a developer
You need root access or you can use sudo
You will need a C compiler (called GCC in Linux)
Access to a BASH console is mandatory
How do I install software by
compiling from the source (II)?
Example. Install pidgin from source code in Ubuntu.
sudo apt-get install build-essential // this will install the compiler and other required libraries
Now you’ll need your desired application’s source code. These packages are usually in compressed files with the .tar.gz
or .tar.bz2 file extensions.
wget http://downloads.sourceforge.net/project/pidgin/Pidgin/2.10.11/pidgin-2.10.11.tar.bz2
tar -xjvf pidgin-2.10.11.tar.bz2 // extract the content of the archive
cd pidgin-2.10.11 // navigate to the new created directory
./configure // configure the new install
make // compile the program
make install // install the software on your system
Known Linux server applications
HTTP server:
Apache (httpd), nginx
SQL:
Mysql (mysqld), SQLite, postgresql
FTP servers:
Proftpd, Pure-FTPd, vsFTPd, Filezilla
DNS servers (Bind),
Firewall (iptables, ipchains),
SMTP servers (postfix, qmail, sendmail),
POP3 / IMAP servers (Dovecot, Courier)
Remote access server (OpenSSH)
Known Linux applications (I)
Text editors
vi
Vi is a powerful text editor included with most Linux systems, even embedded ones. Sometimes you'll have to edit a text file on a
system that doesn't include a friendlier text editor, so knowing Vi is essential.
Known Linux applications (II)
Text editors
nano
nano is a small and friendly text editor. Besides basic text editing, nano offers many extra features like an interactive search and
replace, go to line and column number.
Known Linux applications (III)
Text editors
emacs
Emacs is one of the oldest and most versatile text editors available for Linux and UNIX-based systems. It's been around for a long time
and is well known for its powerful and rich editing features.
How to run Linux scripts
You have the blockip.sh script that is located /home/eircom
First check if the script can be executed by the user you are currently logged in with:
ls -lh /home/ericom/blockip.sh
If you cannot execute it, do a:
chmod u+rx /home/cristi/blockip.sh // or chmod 500 /home/cristi/blockip.sh
Run the script:
/home/ericom/blockip.sh // or if you are already in the /home/eircom, run it with ./blockip.sh
If your connection drops your scipt might crash
Make the script run after you exit the shell or the connection is interrupted:
nohup /home/cristi/blockip.sh & // hit enter twice
Creating and Extracting archives
(I)
Most seen file extensions are .tar.gz and .tar.bz2 which is a tar archive further compressed using gzip or
bzip algorithms respectively.
Create archives
tar -cvf mynewarchive.tar /var/www
(will create mynewarchive.tar with the content of /var/www)
Extract a tar.gz archive:
tar -xvzf tarfile.tar.gz
Extract tar.bz2/bzip archives
tar -xvjf archivefile.tar.bz2
Extract files to a specific directory or path
tar -xvzf abc.tar.gz -C /opt/folder/
Creating and Extracting archives
(II)
Extract a single file
tar -xz -f archive.tar.gz "./new/file.txt"
Extract multiple files
tar -xv -f abc.tar.gz "./new/cde.txt" "./new/abc.txt“
Extract multiple files using wildcards
tar -xv -f abc.tar.gz --wildcards "*.txt“
Automatically perform tasks –
cron (I)
cron is the system process which will automatically perform tasks for you according to a set schedule.
The schedule is called the crontab, which is also the name of the program used to edit that schedule.
The crontab is a list of commands that you want to run on a regular schedule, and also the name of the
command used to manage that list.
Automatically perform tasks –
cron (II)
How to use crontab
In BASH issue the following commands:
crontab –e // edit the cron for the user you are currently logged in with
crontab –l // list the current crontab file
The crontab file is usually edited with the vi text editor (see http://www.shortcutworld.com/en/linux/vi.html for the shortcuts)
Automatically perform tasks –
cron (III)
How to use crontab (youtube)
Automatically perform tasks –
backup with tar & cron (I)
Backup your files with tar:
tar -cf backup.tar /var/www/vhosts/
tar -cvz -f archive-$(date +%Y%m%d).tar.gz /var/www/vhosts/
nohup tar -cf backup.tar /var/www/vhosts/ & // this will keep the backup running if you disconnect from the BASH session
Use crontab to schedule automatic backup:
Add this line to crontab to backup your files every day at 4:00 AM
0 4 * * * tar -cvz -f archive-$(date +%Y%m%d).tar.gz /var/www/vhosts/
Automatically perform tasks –
backup with tar & cron (II)
Use crontab to schedule automatic backup:
Add this line to crontab to backup your files every day at 4:00 AM
0 4 * * * /bin/tar -cvz -f archive-$(date +%Y%m%d).tar.gz /var/www/vhosts/
The steps:
1. crontab -e
2. Go to the end of the file
3. Press the “i” key (for insert)
4. Paste the backup command here (push the scroll button on the mouse or shift+insert)
5. Press the ESC key
6. Type :wq //to save and close crontab:
The iptables firewall (I)
What is iptables ?
Iptables is a rule based firewall system and is normally pre-installed on a Linux operating system
which is controlling the incoming and outgoing packets. By-default the iptables is running
without any rules, we can create, add, edit rules to it.
service iptables start|stop|restart|status // check the status of the iptables service in Redhat/CentOS
sudo iptables -L -n -v // check the status of the iptables service in Debian, Ubuntu
The iptables firewall (II)
Iptables -L // list the current rules of the iptables firewall
iptables –flush // delete all the rules temporarily.
(Me blocking (not) most of China’s IPs)
The logs (I)
The default log folder in Linux is /var/log
How do I view log files on Linux?
Go to /var/log directory using the following cd command:
# cd /var/log
To list files use the following ls command:
# ls or ls -lh
The logs (II)
Common logs and their location in Linux:
/var/log/messages : General message and system related stuff
/var/log/auth.log : Authenication logs
/var/log/kern.log : Kernel logs
/var/log/cron.log : Crond logs (cron job)
/var/log/maillog : Mail server logs
/var/log/qmail/ : Qmail log directory (more files inside this directory)
/var/log/httpd/ : Apache access and error logs directory
/var/log/lighttpd/ : Lighttpd access and error logs directory
/var/log/boot.log : System boot log
/var/log/mysqld.log : MySQL database server log file
/var/log/secure or /var/log/auth.log : Authentication log
/var/log/utmp or /var/log/wtmp : Login records file
/var/log/yum.log : Yum command log file
The logs (III)
Display a specific log file:
# less /var/log/messages
# more -f /var/log/messages
# cat /var/log/messages
# tail -f /var/log/messages
# grep -i error /var/log/messages
grep with pretty colors:
The logs (IV) – empty large (log)
files
To empty large files you need to issue one of the following commands:
> /path/to/large/logfile
echo “ ” > /path/to/large/logfile
In the screen shot above I am emptying my 1.7 MB /var/log/messages log file
Networking in Linux (I)
The is no “Local area connection”
Naming convention is:
◦ eth0
◦ eth1, etc
Subinterfaces/virtual network cards are noted with “.”
◦ eth0.1, eth0.2,
◦ eth 1.1, eth1.2, etc
Networking config files are in /etc/sysconfig/network-scripts/
Networking in Linux (II)
Modify DNS servers:
/etc/resolv.conf - is the file you need
List it’s contents with
cat /etc/resolv.conf
Add or delete existent DNS servers, just edit /etc/resolv.conf with a text editor (vi, nano, etc)
Networking in Linux (III)
ifconfig - ifconfig stands for "interface configuration". It is used to view and change the
configuration of the network interfaces on your system. See ipconfig in Windows.
Networking in Linux (IV)
netstat – a useful tool for checking your network configuration and activity
Networking in Linux (V)
lsof - a command meaning "list open files", which is used in many Unix-like systems to report a
list of all open files and the processes that opened them.
Networking in Linux (VI)
route - view and manipulate the TCP/IP routing table in both Unix-like and Microsoft Windows
operating systems.
Or ip route list
Networking in Linux (VII)
Add a default route:
ip route add default via 192.168.1.254
Delete route from table:
ip route delete 192.168.1.0/24 dev eth0
Networking in Linux (VIII)
ping – utility used to test the reachability of a host on an Internet Protocol (IP) network and to
measure the round-trip time for messages sent from the originating host to a destination
computer and back.
In Windows you need to ping –t to ping forever.
In Linux this is the default behaviour. Ctrl+C or Ctrl+Z to stop any Linux command from running
continuous.
Networking in Linux (IX)
ping can be blocked by any firewall software. Is there an alternative to ping ?
Yes.
Introducing hping. - hping is a free packet generator and analyser for the TCP/IP protocol.
Networking in Linux (IX)
DNS tools:
host - host is a simple utility for performing DNS lookups. It is normally used to convert names to
IP addresses and vice versa.
dig - is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays
the answers that are returned from the name server(s) that were queried.
Examples:
host ericom.com 8.8.8.8
dig @8.8.8.8 eircom.com in A
Networking in Linux (X)
nmap - (Network Mapper) is a security scanner used to discover hosts and services on a
computer network, thus creating a "map" of the network.
What can be done with nmap ?
Host discovery – Identifying hosts on a network. For example, listing the hosts that respond to TCP
and/or ICMP requests or have a particular port open.
Port scanning – Enumerating the open ports on target hosts.
Version detection – Interrogating network services on remote devices to determine application name
and version number.
OS detection – Determining the operating system and hardware characteristics of network devices.
Networking in Linux (XI)
nmap example. Probing for open ports
Networking in Linux (XII)
nmap example. OS detection and open ports
Networking in Linux (XII)
FUN FACT. nmap is so cool that it starred in The Matrix movie.
http://nixware.net