basics and Linux Essentials Book (Reference Notes by Farzand Ali)

Basics of Linux (17 hours)

1.Linux was created in 1991 (The History of Linux began in 1991 with the commencement of a personal project by a Finnish student, Linus Torvalds, to create a new free operating system kernel.)

2.Distributions of Linux (//CentOS/Fedora/Red Hat/Susi/Google Android is a version of Linux/ DSL(Damn Small Linux))

3.Linux and Unix are different Oses

4.open source (support licensing / source code)(all open source software are not free, but mostly are) (you are allowed to see the source code)(depending on the license you can modify the code)

5.Shell (GUI(Graphical )/LUI(Line User Interface)(CLI/Command Line Interface)) (is the screen to interact with the OS)

6.Root user (Root of the operating system is the highest level of the OS)(highest level the users can get into) (Root user is the highest level user, more like Admin user in windows) (home directory is the highest level directory for a particular user)

7.Capitalization is huge thing in Linux: (case sensitive folders and files) (Usernames are case sensitive too)

8.Desktop and Server versions of distributions (mostly the server versions of Linux are CLI based)

9.Linux is used for servers (e.g: Proxy server as , Apache Web server etc.)

Basics of Linux / Linux Essentials 1 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::::GUI and CLI:::: Linux – both CLI and GUI are independent Windows – powershell and GUI Mac – Unix underpinning and still GUI

::Beta vs. Stable:: Beta – Beta is testing, not ready for production. Stable – Stable means it's been tested, and is ready for production.

::::Distributions:::: Debian SUSE Red Hat Cent OS Ubuntu (based on Debian) Raspberry Pi

::Desktop Manager:: KDE – SUSE GNOME/Unity – Fedora/Ubuntu Other – Xbuntu/Debian (Any distro can use any desktop manager. These are just default (except unity is Ubuntu's thing, although it is open source) (you can manage the workspace whichever way you like)

:::::Linux File System Basics:::::: (forward slash for the root file system) (there are other directories too depending on the distribution) (everything in Linux is a file) (everything is mounted under the /) (under home directory you have all users) (under mnt directory you have all mounted drives) (/etc - has system wide application configuration files) (/home/ali - has user specific application configuration files) (/media - folder for cdroms, etc.) (/lib and /usr/lib - folders for linked library files used by binaries in /bin and /usr/bin)

Basics of Linux / Linux Essentials 2 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(/var/log - folder for log files/for different daemons runnig on the systems)

::Linux Directory Structure::

Basics of Linux / Linux Essentials 3 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::::Linux in embedded systems:::: Android phones (Android is a by google) Roku (NowTV box) Kindle Chromebooks Chromecast Mars Rover Factory automation (Vmware ESX is also Linux) Raspberry Pi (model b+)

::OpenSource:: A software whose source code is available for modification by anyone. e.g. Red Hat(paid/source code provided/with support) e.g. CentOS (free/source code/no support)

Basics of Linux / Linux Essentials 4 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::::Major OpenSource Applications:::: OpenOffice LibreOffice Firefox Thunderbird (email client by firefox) Chromium (opensource of chromeOS) Apache (web server) Samba (Interact with windows file shares) NFS MySQL PostFix apt-get yum Webmin Python SSH Clients Dolphin Browser

::Shell:: The program providing the traditional text interface to the linux system. “command interpreter” -commands, options and arguments -globbing -qouting -variables (and the PATH variable)

::::Installing Linux::::: You can install Ubuntu using a Virtual Box

::Server installation::

1.server versions are bare-bones (LUI - Line user interface)(CLI) (less attack surface) (runs for years and years without crashing)

2.download Ubuntu (64bit server version)

3.intall->yes->hostname->partition disks(use entire disk)->yes->username->password->

Basics of Linux / Linux Essentials 5 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

4.DNS/Mail/Print/SAMBA/LAMP(linux apache My php) web server->grub boot loader(for dual boot OSes)->

(no GUI/terminal screen just like DOS prompt)(called bash prompt) (to see the task manager in Linux) fali@ubuntu:~$ top

::Desktop Version:: download the desktop version 32bit version live CD (entire OS is on the CD and you can boot from the CD; and run the OS e.g backtrack for penetration testing)

::Ubuntu Software Centre:: For installing and un-installing software (Categories of tools and programs to install) (Command line programs like apt-get and yum are used to install programs)

::Understanding OpenSource Software and Licensing:: -FSF( Foundation) -No proprietary -everything free -GPL(General Public License) (everything based on GPL needs to be GPL too) (e.g. Red Hat is GPL so CentOS based on it is also free) -LGPL(Lesser GPL) -FreeBSD -Gnu GPL

-OSI(Open Source Initiation) -releasing source code according to their licensing -GPL -LGPL -FreeBSD -BSD -MIT

::Freedom and Business:: -Sell Service and support -Open source option with commercial add-ons (e.g. clearOS, AD connector, smoothwall etc.) -One product open source, other for profit -Hardware vendor might make OS drivers

Basics of Linux / Linux Essentials 6 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::Terminal Emulator:: CLI for the system

::Console:: Sitting right at the computer

:::Basic Linux tasks:::

::clear:: (just to clear the screen) fali@ubuntu:~$clear

::sudo:: root user (just like administrator in windows) you can not login as a root user in ubuntu straight away. sudo (super-user do) (just like run as administrator in windows)

::man Pages:: (manual pages)(just like ? command in MS-DOS) (better to use google instead) fali@ubuntu:~$man ping fali@ubuntu:~$man ls fali@ubuntu:~$man echo fali@ubuntu:~$man man (type the letter 'q' to exit)

(searches all man pages for a specific keyword then you can open a specific man page) fali@ubuntu:~$man -k echo

::info:: (more verbose info than man pages) fali@ubuntu:~$info ls fali@ubuntu:~$info ping

(documentation for the programs) fali@ubuntu:~$cd /usr/share/doc

Basics of Linux / Linux Essentials 7 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::tasksel:: (task select) (list of servers to install) fali@ubuntu:~$sudo tasksel password: (e.g. LAMP server)

::apt-get:: (to install individual packages or applications) (installing from repositories) fali@ubuntu:~$sudo apt-get install apache2 (to uninstall the package) fali@ubuntu:~$sudo apt-get remove apache2 (to update the software/checks for the update) fali@ubuntu:~$sudo apt-get upgrade

::Services:: (you have to restart the services sometimes) (usually in windows if a service crashes the whole server crashes, but is not the case with linux) (specific server component e.g. web server stopped) fali@ubuntu:~$sudo /etc/init.d/apache2 start fali@ubuntu:~$sudo /etc/init.d/apache2 stop fali@ubuntu:~$sudo /etc/init.d/apache2 restart

::top:: (similar to task manager in windows) (memory/cpu and processes i.e pid (process id)) fali@ubuntu:~$top k 1578 ('k' is used to kill the process being inside 'top', where 1578 is the pid)(press enter twice then) q (press 'q' to quit or exit) s (press 's' to change the default 3 secs to any number of secs)(how often it refreshes) i (press 'i' to hide all the processes that are idle) (press again for normal view) f (press 'f' to see the fields to display for top command)(to sort by a specific field)

Basics of Linux / Linux Essentials 8 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::basic navigation:: (to move back to the root directory) fali@ubuntu:~$cd / fali@ubuntu:~$ls (moves to the directory 'etc' under your current directory) fali@ubuntu:~$cd etc fali@ubuntu:/etc~$cd / fali@ubuntu:~$ (/root/folder1/folder2/abc)(case-sensitive)('/' means it will move to root and then sub-directories) (directories below root) fali@ubuntu:~$cd /etc/folder1/abc

-absolute path (cd /home/spowers) -relative path (cd spowers) -”.” and “..” -~ (.. means directory above) (. means current directory) (~ current home directory)

:::::::::::::::ls:::::::::::::::::::: (to list the files in a directory) fali@ubuntu:~$ls

(to see more detailed permissions, name, ownerships and dates for all the files) fali@ubuntu:~$ls -l

(shows all files in a block list) fali@ubuntu:~$ls -m

(shows files for all the folders under the current directory recursively) fali@ubuntu:~$ls -R

(all the files/folders starting with file word) fali@ubuntu:~$ls file*

(for recursive detailed and even hidden files) fali@ubuntu:~$ls -Rla

Basics of Linux / Linux Essentials 9 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(all the hidden files too) fali@ubuntu:~$ls -a

(to hide a file so it doesn't show by ls command) fali@ubuntu:~$mv file1.txt .file1.txt fali@ubuntu:~$ls fali@ubuntu:~$ls -a

(seeing the help file for ls) fali@ubuntu:~$ls –-help

(seeing the long list of files/folders under a specific path) fali@ubuntu:~$ls -l /home/spowers/documents

(to look for specific files under the same directory) fali@ubuntu:~$ls *.txt fali@ubuntu:~$ls file1.* fali@ubuntu:~$ls file?.txt

::search files and directories:: (to search for files and directories) fali@ubuntu:~$sudo find . (find is recursive)( . Means current directory) fali@ubuntu:~$sudo find / fali@ubuntu:~$sudo find . -type f (f means files) (as opposed to 'find .' it only shows the file) fali@ubuntu:~$sudo find . -type d (d means directories) fali@ubuntu:~$sudo find -iname file123 ('-iname' makes it case-insensitive) ( to search every file that starts with php) ('*' wildcard means anything) fali@ubuntu:~$sudo find -iname *php.ini* (to search all .conf file extensions) fali@ubuntu:~$sudo find -iname *.conf fali@ubuntu:~$sudo find -iname “file*” fali@ubuntu:~$sudo find . -type f | grep abc

Basics of Linux / Linux Essentials 10 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(finds all the files then looks for only abc)

::grep:: fali@ubuntu:~$grep abc abc.txt (find abc in a particular file) fali@ubuntu:~$grep abc * (find abc in all the files) fali@ubuntu:~$grep bb$ abc.txt (grep bb only where it is present at the end of the line) fali@ubuntu:~$grep ^ab abc.txt (grep 'ab' only where it is present at the start of the line) fali@ubuntu:~$grep a.b abc.txt (. means only single character) fali@ubuntu:~$grep ^a.b abc.txt fali@ubuntu:~$grep ^...$ abc.txt fali@ubuntu:~$grep thing$ file3.txt

::sort:: (sorts the file alphabetically) fali@ubuntu:~$sort file3.txt fali@ubuntu:~$sort -r file3.txt (sorts in reverse) fali@ubuntu:~$sort -R file3.txt (R is for randomize)

::cut:: fali@ubuntu:~$cut -c2-4 file1.txt (columns 2 to 4 range) fali@ubuntu:~$cut -c2,4 file1 (AND operation) fali@ubuntu:~$cut -c2- file1.txt (goes till end as no end value) fali@ubuntu:~$cut -d” “ -f2 file2.txt (here the delimiter is space, but can be anything e.g. a colon : in a password file) fali@ubuntu:~$cut -d” “ -f2-4 file2.txt fali@ubuntu:~$cut -d” “ -f2- file2.txt fali@ubuntu:~$cut -d” “ -f3 file2.txt | cut -c2

Basics of Linux / Linux Essentials 11 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::wc:: fali@ubuntu:~$wc file3.txt (4 18 84 file3.txt)(4 lines, 18 words and 84 characters) fali@ubuntu:~$wc -w file1.txt (word count only) fali@ubuntu:~$wc -c file1.txt (character count only) fali@ubuntu:~$wc -c file1.txt | cut -d” “ -f1 (will show only 18)

Basics of Linux / Linux Essentials 12 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::making, copying, moving, renaming, deleting:: (to create a directory) fali@ubuntu:~$sudo mkdir folder1 fali@ubuntu:~$sudo mkdir /etc/network/folder1

(to delete files and folders) (-R to recursively remove everything in a folder) fali@ubuntu:~$sudo rm * (to delete everything) fali@ubuntu:~$sudo rmdir folder2 fali@ubuntu:~$sudo rm folder1 -R fali@ubuntu:~$sudo rm folder1 -Rf fali@ubuntu:~$sudo rmdir folder1 -R fali@ubuntu:~$sudo rm file1 fali@ubuntu:~$sudo rm /etc/network/folder1

(move or rename files and folder) (there is no command to rename the file or folder, but you can use move command) fali@ubuntu:~$sudo mv file1 file2 -R fali@ubuntu:~$sudo mv /file1 /etc/file2

(to rename and move in the current folder) fali@ubuntu:~$sudo mv test.bak test2.bak

(to move file to home directory and rename) fali@ubuntu:~$sudo mv test2.bak ~/text3.txt

(to copy a file or backup a file) fali@ubuntu:~$sudo cp folder1 folder1.bak -R fali@ubuntu:~$sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

(to copy a folder or backup a folder) fali@ubuntu:~$sudo cp -R folder1 folder2 fali@ubuntu:~$sudo cp -R My\ Hand “Your Hand”

(to get the backed up folder) (recursive is not used in this) fali@ubuntu:~$sudo mv folder1.bak folder1

Basics of Linux / Linux Essentials 13 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::Mounting Drives:: (external HDD plugged-in and want to mount) (make a directory under /mnt folder) fali@ubuntu:~$sudo mkdir /mnt/flashdrive (to list the physical HDDs connected to the computer) fali@ubuntu:~$sudo fdisk -l (e.g. /dev/sdb1) (to mount drive) fali@ubuntu:~$sudo mount /dev/sdb1 /mnt/flashdrive (to list HDD files and folders) fali@ubuntu:/mnt/drive~$ls -l

(to unmount HDD) fali@ubuntu:~$sudo umount /dev/sdb1

(to mount cdrom) fali@ubuntu:~$sudo mount /dev/cdrom /mnt/cdrom1

:::Vim for editing files in Linux::: (there are no file associations like windows.txt or extensions) ::starting vim:: (use fali@ubuntu:~$sudo vi file)(for ubuntu as vim not supported anymore) (open or create a file with vim) fali@ubuntu:~$sudo vim file1 (if you want to edit the configuration file, you will have to change the ownership of the file to you) fali@ubuntu:~$sudo chown ali file1.conf fali@ubuntu:~$sudo chown root file1.conf

(open or create a file with vim) fali@ubuntu:~$sudo vim /etc/passwd

(editing and navigating) (you will have to enter the INSERT mode to edit the file) (letter 'a') (to move out of the INSERT mode just 'esc') (to search/find down the file :/*max* ) (to search/find up the file :/?*max* ) (letter 'n' to the next search) fali@ubuntu:~$cd /etc/php5/apache

Basics of Linux / Linux Essentials 14 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali) fali@ubuntu:/etc/php5/apache~$sudo vim php.ini :/*upload* (if you keep pressing n it keeps moving to next search instance) (press letter 'a' for INSERT mode to edit) (press 'esc' to move off INSERT mode)

(saving and exiting) (to simply open vim) fali@ubuntu:~$sudo vim (to open a file when you are inside vim) :e /etc/filename

:q (to quit) :q! (to force quit) :wq (to save and quit) :w php.ini2 (to save as)

:::::::::::::Users in Linux:::::::::::::: ::3 types of users:: standard – with admin rights/without admin rights root - system – just like service accounts in windows running different processes and programs

::id:: (info about a user) fali@ubuntu:~$id spongebob uid=1001 gid=1001 groups=1001 (if it is an admin user, it will belong to 'sudo' group along side some other groups)

::w:: (what logged in users are doing) fali@ubuntu:~$w

::who:: (who is logged in)(also shows terminal sessions for each user) fali@ubuntu:~$who

Basics of Linux / Linux Essentials 15 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::whoami:: (just to see who am i logged in as) fali@ubuntu:~$whoami

//etc/passwd – file that contains information about all the user but the hashed and encrypted passwords are saved in another file.(shows all the users on the system even the service accounts)(usually system user accounts are low in numbers)(also shows the directories of the service accounts) fali@ubuntu:~$cat /etc/passwd

//etc/group – file that contains information about all the groups, including the service account groups.(shows groups and the users under it) fali@ubuntu:~$cat /etc/group

//etc/shadow – file that contains the actual hashed and encrypted passwords as opposed to the /etc/passwd file. fali@ubuntu:~$sudo cat /etc/shadow

::sudo:: (substitute user doing)(to run commands with root priveleges) fali@ubuntu:~$sudo ls /root (e.g. spongebob will still not be allowed even after sudo as it does not belong to the group 'sudo' group)

(to add a user) fali@ubuntu:~$sudo adduser ali fali@ubuntu:~$sudo useradd ali

(to delete user) fali@ubuntu:~$sudo userdel ali

(to delete user and the user home directory) fali@ubuntu:~$sudo userdel -r ali (-r is for removing home directory and mail spool)

(to manually delete the user home directory if it is left behind) fali@ubuntu:~$sudo rm -r /home/ali

Basics of Linux / Linux Essentials 16 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(all user informatin is stored in a text file /etc/passwd)(users passwords information file) (usernames and home directories) (to see all the information for users) (different services also have usernames) (you can rename the username in this file) fali@ubuntu:~$sudo vi /etc/passwd (or fali@ubuntu:~$sudo vim /etc/passwd)

(to change the user password) fali@ubuntu:~$sudo passwd ali

(to clear the screen) fali@ubuntu:~$clear

::last:: (shows when users were last logged in) fali@ubuntu:~$last fali@ubuntu:~$ssh patrick@localhost fali@ubuntu:~$last

::::::::::::Groups in Linux:::::::::::: (put users into groups and give permissions to groups) fali@ubuntu:~$sudo groupadd marketing (add a group) fali@ubuntu:~$sudo groupdel marketing (delete a group) fali@ubuntu:~$sudo adduser ali marketing (add a user in a group) fali@ubuntu:~$sudo deluser ali marketing (to remove a user from a group) fali@ubuntu:~$sudo gpasswd -a ali managers fali@ubuntu:~$sudo gpasswd -d ali managers

(to see all the information about the groups)(groups configuration file) fali@ubuntu:~$sudo vi /etc/group (fali@ubuntu:~$sudo vim /etc/group)

(to see all the groups you are a member of) fali@ubuntu:~$groups (to see all the groups) fali@ubuntu:~$cat /etc/group

Basics of Linux / Linux Essentials 17 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(to add a user to a secondary group) fali@ubuntu:~$sudo usermod -a -G grouptest1 patrick

(to add a user to sudo group) fali@ubuntu:~$sudo usermod -a -G sudo usertest1

:::::::::::permissions:::::::::::::::::

three digit number e.g. 214 first digit= permissions of the owner of the file (e.g. read, write and execute) second digit= permissions of the group owner third digit= everybody else (not even an owner) (mostly given read and execute for example to execute the scripts e.g 5) 4=read 2=write 1=execute 0=nothing

(to change the permissions) fali@ubuntu:~$sudo chmod 755 folder1 -R fali@ubuntu:~$sudo chmod 775 file1

Basics of Linux / Linux Essentials 18 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(chmod stands for change mode) (-R switch is for recursive, for a folder) (three digit number depends on the security policy)

(to see the permissions for files and folders in the current directory) fali@ubuntu:~$ls -l drwxrwxrwx 2 root root test1 (rwxrwxrwx means 777 permission) fali@ubuntu:~$sudo chmod 755 folder1 -R 7=rwx (4+2+1) 5=rx- (4+1) fali@ubuntu:~$ls -l drwxr-xr-x 2 ali group1 test1

(execute for e.g. .txt file means that there are scripts in it) (execute for folder means you can change a working directory to be the working directory) (in windows the file or folder can be accessed by several groups, but in Linux only by one user, one group and everyone) (but can use access control list to allow multiple groups to access file or folder) fali@ubuntu:~$sudo chmod u+w file1 fali@ubuntu:~$sudo chmod g+r file1 fali@ubuntu:~$sudo chmod o+x file1 fali@ubuntu:~$sudo chmod -R u+w folder1 fali@ubuntu:~$sudo chmod u-w file1 fali@ubuntu:~$sudo chmod g-r file1 fali@ubuntu:~$sudo chmod o-x file1 fali@ubuntu:~$sudo chmod u+rwx,g+rw,o+x file1 fali@ubuntu:~$sudo chmod u-rwx,g-rw,o-x file1 fali@ubuntu:~$sudo chmod ugo-w file1

Basics of Linux / Linux Essentials 19 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

:::::::Ownerships of files and folders:::::::: fali@ubuntu:~$sudo chown -R user1 folder1 (to change the user ownership of a folder and sub-folders) fali@ubuntu:~$sudo chown user1 file1 (to change the user ownership of a file) fali@ubuntu:~$sudo chgrp -R group1 folder1 (to change the group ownership of the folder and sub-folders) fali@ubuntu:~$sudo chgrp group1 file1 (to change the group ownership of the file) fali@ubuntu:~$ls -l drwxrwxrwx 2 root root 4096 test1 (first root is the user-owner and the second root is the group-owner) fali@ubuntu:~$sudo chgrp -R ali folder1 fali@ubuntu:~$ls -l drwxrwxrwx 2 ali root 4096 test1 fali@ubuntu:~$sudo chgrp -R testgroup1 folder1 fali@ubuntu:~$ls -l drwxrwxrwx 2 ali testgroup 4096 test1

(changing ownership in one line)(-R is for recursive in case of a folder) fali@ubuntu:~$sudo chown ali:marketing file1 fali@ubuntu:~$sudo chown ali.marketing file1 fali@ubuntu:~$sudo chown -R ali:marketing folder1

:::Simple Linux Commands::: applications->system tools->terminal (login as root user) fali@ubuntu:~$sudo su (to assign password to root) fali@ubuntu:~$sudo passwd root (to see present working directory) fali@ubuntu:~$pwd (to move to the root directory) fali@ubuntu:~$cd /

Basics of Linux / Linux Essentials 20 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(to see the manual for a particular command/to see switches on the commands) fali@ubuntu:~$man pwd (to list the items in the current folder) fali@ubuntu:~$ls (to change the directory/press the tab to complete the word) fali@ubuntu:~$cd Desktop (to take you back to the home directory/tilda symbol) (when you are in your home directory it shows tilda symbol) fali@ubuntu:~$cd ~ (to clear the screen command) fali@ubuntu:~$clear (to move back up one folder) fali@ubuntu:~$cd.. (touch command to create files) fali@ubuntu:~$cd Desktop fali@ubuntu:~/Desktop$touch ali.txt fali@ubuntu:~/Desktop$touch folder1/ fali@ubuntu:~/Desktop$touch newfile2.ext fali@ubuntu:~/Desktop$touch My Hand (creates two files) fali@ubuntu:~/Desktop$touch “My Hand” (if you use touch again on an already created file it only modifies or updates the date/time)(files and folder names are case-sensitive) (long format to list file and folders)(for more detail) fali@ubuntu:~$ls -l (to make directory) fali@ubuntu:~$mkdir folder1 (to move the file into a folder) fali@ubuntu:~$mv ali.txt folder1/ (to copy a file) fali@ubuntu:~$cp ali.txt ali1.text (to remove a file) fali@ubuntu:~$rm ali.txt (to remove a directory/without -R it comes with an error if the folder is not empty) fali@ubuntu:~$rmdir -R folder1 (to change user permissions/change modify command) fali@ubuntu:~$chmod u+x ali.txt fali@ubuntu:~$ls -l

Basics of Linux / Linux Essentials 21 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(to change group permissions/change modify command) fali@ubuntu:~$chmod g+x ali.txt fali@ubuntu:~$ls -l (to remove everybody's read permissions/ a for all) fali@ubuntu:~$chmod a-r ali.txt (to give execute permissions to other/ o for other) fali@ubuntu:~$chmod o-x ali.txt

(3 digit permissions command) rwx rwx rwx rwx r-x -w- 111 101 010 7 5 2 fali@ubuntu:~$chmod 752 ali.txt

(complicate permissions command) fali@ubuntu:~$chmod g+r,o+x ali.txt

(to change ownership) (ali user owner and group1 owner) fali@ubuntu:~$sudo chown ali:group1 ali.txt fali@ubuntu:~$ls -l

(to take you up a folder) fali@ubuntu:~$cd..

(to see all the files in the home directory) fali@ubuntu:~$ls -a . (work in the same folder) .. (move up one folder)

(to see the network interface config) fali@ubuntu:~$ifconfig fali@ubuntu:~$ifconfig eth0 (for ethernet 0 interface) fali@ubuntu:~$ifconfig lo (for loopback interface)

(to shutdown an interface) fali@ubuntu:~$sudo ifconfig eth0 down (to no-shutdown an interface) fali@ubuntu:~$sudo ifconfig eth0 up

Basics of Linux / Linux Essentials 22 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(to see where the current DNS server info is stored) fali@ubuntu:~$cat /etc/resolv.conf nameserver 127.0.0.1 search localdomain

(ping tool/utility) fali@ubuntu:~$ping google.com fali@ubuntu:~$ping 192.168.1.1 (traceroute not installed by default in ubuntu)

(to find a file or a folder under the root) fali@ubuntu:~$sudo -find / -name folder123

(command similar to nslookup/to lookup IP addresses for DNS names) fali@ubuntu:~$dig google.com

(to see the routing table of the machine) fali@ubuntu:~$route

(concatenate command) (> this erases the previous content if the file already exists) fali@ubuntu:~$cd Desktop fali@ubuntu:~Desktop$ls /etc > text1.txt

(concatenate command)(>> to append at the end of the file rather than erasing the previous contents) fali@ubuntu:~$cd Desktop fali@ubuntu:~Desktop$ls /usr/ > text1.txt

(to edit a file using gedit) fali@ubuntu:~$sudo gedit ali.txt

(to edit a file using vim) fali@ubuntu:~$sudo vim ali.txt

(to edit a file using vi)(fedora and ubuntu doesn't seem to have vim editor) fali@ubuntu:~$sudo vi ali.txt

Basics of Linux / Linux Essentials 23 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

:::Linux Commands for Beginners::: (linux file system)(folder structure) (colours for files and folders) (folders in blue and files in white) fali@ubuntu:~$ls -l

(permissions string: if it starts with a letter 'd' that means it is a directory) fali@ubuntu:~$ls -l drwxrwxrwx 1 root root ------ali.txt fali@ubuntu:~$ls /home/ali (without a / it means you are referring to a folder in a current working directory)

(~ tilda means home directory) fali@ubuntu:~$cd ~/ali

(cd.. to move to the previous folder/ up one level) fali@ubuntu:~$cd..

(print working directory command) fali@ubuntu:~$pwd

::nano text editor:: fali@ubuntu:~$nano fali@ubuntu:~$nano /home/ali/ali.txt

::to switch to the root account:: fali@ubuntu:~$sudo -s (to exit out of root account) fali@ubuntu:~$exit

::the cat command:: (cat stands for concatenate) (to create empty files) fali@ubuntu:~$nano animals fali@ubuntu:~$nano people

(to simply see the contents of the file) fali@ubuntu:~$cat animals fali@ubuntu:~$cat people

Basics of Linux / Linux Essentials 24 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali) fali@ubuntu:~$cat animals people (didn't change the contents of the file but showed it on the screen together)

(to create a newfile by using cat for two files and thus combining the contents) fali@ubuntu:~$cat animals people > newfile

(the contents of animals file added at the end of the newfile) fali@ubuntu:~$cat animals >> newfile

(if single > is used then it overwrites the whole file) fali@ubuntu:~$cat animals > newfile

(pipe in multiple commands filter) fali@ubuntu:~$cat abc.txt | grep aa fali@ubuntu:~$cat abc.txt | grep aa | grep b

::to useradd processes:: (to kill the system monitor program) fali@ubuntu:~$pidof gnome-system-monitor 3414 fali@ubuntu:~$kill 3414

(if the program is not responding)(to force close a program) (it does not free up the resources the program was using) fali@ubuntu:~$kill -KILL 3414

(if you don't know the name of the process) (list of all the processes under your user account) (by default only shows the processes you are running as a user) fali@ubuntu:~$ps -ux

(to see the list of processes running for all the users) fali@ubuntu:~$ps -aux

Basics of Linux / Linux Essentials 25 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

(to see the processes running for a particular user) (then you can kill a specific process for a user) fali@ubuntu:~$ps -U ali

(shows all the iterations or instances of a program/process) fali@ubuntu:~$pc -C gnome-system-monitor

::creating users:: (-m to create a home directory straight away) (-s for default shell the user will log into)(bash shell) (-g for group as users by default)(the main group) (-G for a user to be a member of different groups too) fali@ubuntu:~$sudo useradd ali -m -s /bin/bash -g users -G marketing fali@ubuntu:~$sudo passwd ali (allows you to set the password for the new user)

::echo command:: fali@ubuntu:~$echo "Hello YouTube" Hello YouTube (useful in scripting when asking something from the user)

::variables:: fali@ubuntu:~$HELLOMESSAGE="Hello Youtube" (variable now contains the string Hello Youtube) fali@ubuntu:~$echo $HELLOMESSAGE fali@ubuntu:~$echo $PATH (environmental variable) fali@ubuntu:~$a=”Hello There” fali@ubuntu:~$a=”Good Buddy” fali@ubuntu:~$echo $a $b fali@ubuntu:~$echo “$a$b” fali@ubuntu:~$echo $a; echo $b (these variables are stored only in the current instance of the terminal)

::alias command:: (to create your own commands) fali@ubuntu:~$alias mycommand='echo Hello World' fali@ubuntu:~$mycommand

Basics of Linux / Linux Essentials 26 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

Hello World fali@ubuntu:~$alias n='nano myfile' fali@ubuntu:~$n

(to see all the aliases you have already) fali@ubuntu:~$alias fali@ubuntu:~$alias ls1='ls --color -l'

:: .bashrc file:: (used to store aliases and variables each time you login rather than creating them again) (you have to be in the home directory) fali@ubuntu:~$pwd fali@ubuntu:~$cd ~ fali@ubuntu:~$nano .bashrc echo Hello Ali alias ls1='ls --color -l' (whenever you open a new terminal it reads the .bashrc file first) (now even if you logout and log in again aliases and variables will be already stored) fali@ubuntu:~$ls

::passwd:: (to change your own password) fali@ubuntu:~$passwd

(to move to a root user)(root user account can change the password for any user) fali@ubuntu:~$sudo s fali@ubuntu:~$passwd ali

(next time the user logs in the password will be expired and he will have to change it) fali@ubuntu:~$passwd -e ali

Basics of Linux / Linux Essentials 27 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::which command:: (to see where the executable of the command is located) (different distributions have specific locations and we need to see before we write a script) fali@ubuntu:~$which ls /bin/ls fali@ubuntu:~$which firefox /usr/bin/firefox fali@ubuntu:~$which cat /bin/cat

::whatis command:: (it shows the description of the command) fali@ubuntu:~$whatis firefox fali@ubuntu:~$whatis ls fali@ubuntu:~$whatis ping

::whereis command:: (to see where the binaries are for a specific command (Also shows where is it's man page)) fali@ubuntu:~$whereis man fali@ubuntu:~$whereis ping

::viewing resources::

:df command: (to view used and unused resources on linux) (df means disk free) fali@ubuntu:~$df fali@ubuntu:~$df -h

:du command: (du means disk usage) fali@ubuntu:~$du (for human readable in gigabytes) fali@ubuntu:~$du -h (for summary) fali@ubuntu:~$du -sh

(to see how much space etc folder is using)

Basics of Linux / Linux Essentials 28 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali) fali@ubuntu:~$sudo -sh /home/ali

:free command: (how much RAM is being used) fali@ubuntu:~$free (shows all in megabytes) fali@ubuntu:~$free -m (shows in humand readble/Mbytes) fali@ubuntu:~$free -tom

For more information: http://www.linuxatemyram.com/ http://www.thegeekscope.com/check-linux-memory-usage-using-free-command/

::the watch command:: (repeats the same command over and over again till you press ctrl break) fali@ubuntu:~$watch free -m (keep executing the command after every 2 secs) (to change it to 0.5 sec)(to keep an eye on the ram usage) fali@ubuntu:~$watch -n 0.5 free -m

:::files and folders with spaces::: fali@ubuntu:~$nano test file (if you try to do this, the file name will be test) (and will take it as two different files) fali@ubuntu:~$nano test\ file fali@ubuntu:~$ls (this output can be using sometimes so its better to do ls -l) fali@ubuntu:~$ls -l

(second way of doing this) fali@ubuntu:~$nano "test file" fali@ubuntu:~$mkdir "test dir" fali@ubuntu:~$rm -r "test dir"

::the file command:: (to know what type of file it is) (auto complete the file folders name by pressing tab)

Basics of Linux / Linux Essentials 29 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali) fali@ubuntu:~$ls -l fali@ubuntu:~$file ali.mp3

::less:: (to read a file) (to read to a document especially compressed) fali@ubuntu:~$less abc.txt fali@ubuntu:~$less text.gz (unzips it on the fly/gz is a compressed file in Linux) (press 'q' to get back to the command line)

::head and tail commands:: (by deafult head and tail shows 10 lines) fali@ubuntu:~$sudo -s (be careful not to delete the log files) fali@ubuntu:~$cd /var/log fali@ubuntu:~$ls

(shows the first ten lines of the file) fali@ubuntu:~/var/log$head dmeg (shows the last ten lines of the file) fali@ubuntu:~/var/log$tail dmeg

(to view a specific number of lines) fali@ubuntu:~/var/log$head -n2 dmeg fali@ubuntu:~/var/log$tail -n2 dmeg

(to see the first ten lines of two files) fali@ubuntu:~/var/log$head dmesg kern.log

(to see the last ten lines in real time) (-f is for follow)(to tshoot plug and play hardware) fali@ubuntu:~/var/log$tail -f dmesg

::to find files and folders:: (/home/ali means where to search from) (asterisk wildcard at the start and at the end means anything at the end or start) fali@ubuntu:~$find /home/ali -name *CCNA*

(you should have permissions to the folder you are searching under)

Basics of Linux / Linux Essentials 30 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali) fali@ubuntu:~$sudo find / -name smb.conf

(find files mofidied 24 hours ago) (-1 means less than 24 hours ago) fali@ubuntu:~$find /home/ali -mtime -1 (less than 3 days ago) fali@ubuntu:~$find /home/ali -mtime -3

::standard input, output and error:: (this is standard output) fali@ubuntu:~$ls /home/ali fali@ubuntu:~$cat ali.txt

(anything that is considered as an error is standard error) (this is standard error) fali@ubuntu:~$ls /root permission denied

(redirected standard output to a file) fali@ubuntu:~$ls /home/ali > testfile

(output to standard output) fali@ubuntu:~$cat testfile standard input=0 standard output=1 standard error=2

(specific telling that standard output need to be redirected) fali@ubuntu:~$ls /home/ali 1> testfile

(specific telling that standard error need to be redirected) (so the output will be redirected to the standard output) (file will be empty as no error) fali@ubuntu:~$ls /home/ali 2> testfile fali@ubuntu:~$ls /root 2> testfile fali@ubuntu:~$cat testfile permission denied

Basics of Linux / Linux Essentials 31 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::installing software/applications on ubuntu and debian based Linux systems::

::apt-get command:: (update freshens up the database system) (all packages change with time)(update downloads the latest packages from repositories) fali@ubuntu:~$sudo apt-get update

(to install a game) (apt-get is smart enough to know what other dependencies to install for the package) (so press yes) fali@ubuntu:~$sudo apt-get install frozen-bubble

(to remove the program) fali@ubuntu:~$sudo apt-get remove frozen-bubble

(to remove the configuration files installed with the program/package) fali@ubuntu:~$sudo apt-get remove --purge frozen-bubble

(to remove packages that are no longer needed) (be careful as it could sometimes remove important packages) fali@ubuntu:~$sudo apt-get autoremove

(all packages are saved in case you want them and are cached) (it is better to keep it on cache as it could be required sometimes) fali@ubuntu:~$sudo apt-get clean

:::::Archiving Files::::: (archiving is simply adding files together without compression) -tar (tape archive)(archiving) -zip/unzip (archiving/compression) -gzip/gunzip (compression) -bzip2/bunzip2 (compression)(more compressed)

Basics of Linux / Linux Essentials 32 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::tar:: fali@ubuntu:~$tar -cf files.tar file2.jpg file3.jpg fali@ubuntu:~$tar -c -f files.tar file2.jpg file3.jpg fali@ubuntu:~$tar -xf files.tar fali@ubuntu:~$tar -xvf files.tar (c for create) (f for file) (x for extract) (v for verbose) (tar does recursive automatically)

:: tar with compression:: fali@ubuntu:~$tar -cvfz files.tgz file2.jpg file3.jpg (files.tar.gz or file.tgz) fali@ubuntu:~$tar -zxvf files.tgz fali@ubuntu:~$tar -cvfj bzipfile.tbz folder1 (bzipfile.tar.bz2 or bzipfile.tbz) fali@ubuntu:~$tar -xcvf bzipfile.tbz

::zip:: fali@ubuntu:~$zip -r file.zip folder1 (archives and compresses) fali@ubuntu:~$unzip file.zip

::gzip:: fali@ubuntu:~$gzip files.tar fali@ubuntu:~$gunzip files.tar ::bzip2:: fali@ubuntu:~$bzip2 files.tar fali@ubuntu:~$bunzip2 files.tar (gzip and bzip2 are common compression algorithms in linux)

::::Logs:::: syslog – program that managers storage of logs for daemons on the system. klog – logs kernel specific messages. Dmesg – used to view kernel messages. fali@ubuntu:~$cd /var/log

Basics of Linux / Linux Essentials 33 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali) fali@ubuntu:~$ls syslog mail.log dmesg fali@ubuntu:~$tail syslog fali@ubuntu:~$dmesg (entire kernel log)

::scripting:: ::Editors:: vi – hard to user, but everywhere pico - nano – esaily available on ubuntu ::Interpreter:: /bin/sh /bin/bash (every shell script/program is written for bash) ::Variables:: $ ANYNAME $1 – first argument $2 – first argument $? – first argument ::scripting commands:: echo – prints on the screen for – loop if – conditional statement

(you can have if statements and loops in scripting) (usually done to make linux administration easy) (these are called bash scripts) (you have to use hash bang at the start of the script file to tell its a bash script) fali@ubuntu:~$nano myscript #!/bin/bash ls HELLOMESSAGE="Hello this is a script with commands" echo $HELLOMESSAGE (save and exit)

(now to give execute permission throughtout) fali@ubuntu:~$chmod +x myscript (now execute the file) fali@ubuntu:~$./myscript

Basics of Linux / Linux Essentials 34 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::Scripts Examples::

Basics of Linux / Linux Essentials 35 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

Basics of Linux / Linux Essentials 36 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali)

::::Symbolic links:::: ls -s file1 location_for_link (this makes a pointer to the file) fali@ubuntu:~$ls -s rubies.txt diamonds.txt (diamonds file points to rubies files)(so if we make any changes in the diamond file, they will all be made

Basics of Linux / Linux Essentials 37 Linux basics and Linux Essentials Book (Reference Notes by Farzand Ali) automatically in rubies file too) fali@ubuntu:~$ls -l (shows symabolic links too)

::::Sticky Bit:::: /tmp – short term storage/gets erased (all users can write, so they can also delete each other's files and folders) /var/tmp – same, but doesn't get erased /var/ - files that change often/mail, logs etc

(to set sticky bit, so only the user owner can delete it) chmod o+t folder1 chmod 0777 folder1 user1@ubuntu:~$mkdir temp user1@ubuntu:~$chmod 777 temp user1@ubuntu:~$ls -l drwxrwxrwx user1@ubuntu:~$cd temp user1@ubuntu:~/temp$vi testfile1.txt user1@ubuntu:~/temp$ls -l -rw-r—r-- user2@ubuntu:~/temp$rm testfile1.txt (it will delete the file) user1@ubuntu:~$chmod o+t temp user1@ubuntu:~$ls -l drwxrwxrwt (t shows that the stikcy bit is on) user1@ubuntu:~$cd temp user1@ubuntu:~/temp$vi newfile1.txt user1@ubuntu:~/temp$ls -l -rw-r—r-- user2@ubuntu:~/temp$rm newfile1.txt (it will not delete the file) (to turn off the sticky bit) user1@ubuntu:~$chmod 0777 temp (to turn on the sticky bit) user1@ubuntu:~$chmod 1777 temp (shows that the temp folder under the root has stikcy bit on) user1@ubuntu:~$ls -l / drwxrwxrwt tmp

Basics of Linux / Linux Essentials 38