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