mazzakolinux.commazzakolinux.com/wp-content/uploads/2017/02/Linux-Ac… · Web viewwccan be used...
Transcript of mazzakolinux.commazzakolinux.com/wp-content/uploads/2017/02/Linux-Ac… · Web viewwccan be used...
-Linux Academy Notes-
User Guide:red = commandPurple = command options figure =figures with respective file name, located inside Figure folder
Lecture: Setting up Linux Academy server:
ssh [email protected] pwuser@hostname
change pw: passwdCurrent pw: 123456New Pw:
Change root pwSuper user command: su –Takes to root promptRoot pw: 123456Change root pw:root@hostnameexit
user@hostnameLS command: lsVncpasswd command: vncpasswdPassword:Verify:
To Reboot: go to root: su –root@hostnamereboot server: reboot
Lecture: Free Software Foundation (FSF) and Open Source Initiative (OSI)
GPL: General public license is license used by Linux Kernel.FSF: Free Software Foundation, driving force behind Gnu’s Not Unix (GNU) project.
Freedom 0: Free to use software, SW for any purpose.Freedom 1: Free to examine the source code and modify.Freedom 2: Free to redistribute.Freedom 3: Free to redistribute modified SW.
2 versions of GPL used today: GPLv2 and GPLv3
Lesser GPL. LGPL. Library GPL are blocks of codes that can be used by other program eg: dialogue box etc.
OSI: Is a process for SW development through peer review and transparency.
cat concatenate: link things together in a series, to display text file on screen
Lecture: Desktop Skills Hands-On
KDE: K desktop environment for Linux, default for Mandriva and SUSE (Prn: Suza).
GNOME (prn: Nom): Popular desktop environment default for Linux distributions like Red Hat, CentOS
LXDE: Lightweight X11 Desktop Environment, for using less resources, used for old computers
UNITY: Canonical, publisher of Ubuntu distributions, created this
XFCE: modeled as commercial desktop
Terminal: Provides text-mode user interface
Lecture: Industry uses of Linux, Cloud Computing and Virtualization
Industry uses of Linux OS: Web Servers, Mobile app servers (some games servers), SQL DB servers (e.g. MySQL)
Linux is a contender Cloud computing. Eg. Gmail, Evernote, Dropbox Clouds is backed by Linux based back-end OS.
Virtualization is the creation of virtual OS through a virtualization SW that is known as hypervisor. This allows us to virtualize an OS in another computer/OS known as host. The virtualized OS that live upon this host are known as guest OS or Virtual Machines.
Virtualization SW examples: VirtualBox, VMWare, KVM/QEMU, XEN allows us to virtual in machines like Windows Laptop.
Lecture: Basic Shell
Shell: DOS is command line Shell Linux is a command line Shell, but we can choose Shells from:
Sh – Bourne shellBash – Bourne again shell (default for most Linux distributions)Csh – C shell (similar to C language)Tsch – tsch shellZsh – Z shellKsh – Korn shell
Echo $SHELL – to show text in a commandTo change shell- zsh
Sudo yum install (to install zsh shell)
cat /etc/passwd – shows which shell we are default.
Control+alt+F2-F6 (go in and out of other shells/new sessions) and, Alt-F1 come back to original session
Lecture: Command Line Syntax – ls
(L) ls- list (gives listing of files and folders).ls –a shows all hidden filesls –l shows long string of details of files with datesls –p shows folders file name with ‘/’ at the end of file name
ls -R lists the directory and folders and what files live in files1, 2
Make directory mkdir dirtest1Change directory cd dirtest1/Lecture: Command Line Syntax- $PATH, Case Sensitivity
Every variable starts with ‘$’ sign.
echo $PATH shows pathcp copy
Lecture: Command Line Syntax- Basic Commands
halt shuts down Linux OSreboot (Root) shuts down Linux and restarts back up init o (Root) Shuts down OS, init 6 (shuts and back up, reboot)
shutdown (must be root)
shutdown –H Halt the machineshutdown –P power off machine shutdown –R Reboot machine shutdown –h just power off, like halt commandshutdown –c Cancel power off
exit close terminal session from (public IP)su - substitute user or super user from user to root, or vice versa
env shows all environment variable in system
top shows list of application & processes running in the system with process idq quitclear clear screen
which halt shows full path of where application liveswhich reboot which su
whoami shows which user you are
netstat shows status of network and current counteractions route views routing tableifconfig show net configuration in network card and modify settings (root)ip addr shows all the ip address
Lecture: Command Line Syntax- uname
uname shows what OS we usinguname –s display linux Kernel nameuname –n displays system host nameuname –r displays Kernel release numberuname –v version number for Kerneluname –m hardware architecture uname –p shows processor typeuname –i hardward platformuname -o shows OS uname –a shows all info at once
Lecture: Command Line Syntax- Command History, Command Completion
Every time we hit command, its saved in the .bash_history file
ls –a shows hidden filecat .bash_history
cat ^ shows last entry
export HISTFILESIZE=99999echo $HISTFILESIZE
Type: tar –zxvf + te (can’t remember the whole file name) + Tab multiple types), it will auto complete
Then you can start to type the rest of the file name from the list and it will auto complete typing the full name P.S: Tar is used to zip/unzip files
Lecture: Command Line Syntax –cd and pwd
cd change directory, will bring to home folder/directory i.e. to user@drbindash1
pwd Print working directory (user/root)
If you want to move to a specific folder:cd dirtest1 takes us to test1 folderLecture: Shell Configuration Files
Login shell, unlike terminal, looks black like DOS
cat /etc/bashrc contains shells functions and aliases cat /etc/profile (etc pronunced et-c)
ls –a all hidden files
alt f2-f6 to move into Login Shell
Lecture: Variables- Environment/System Variables and user defined variable
Variable is a place holder for another value, that are stored in RAM that is reserved to store whatever value you want to put in it. It’s a container within a memory. There is user defined and system defined variable.
An Environment is a set of variables that are used in configuring system computing environments by the system itself.
eg: we used Tilda key (~) after cd command, it will take us to home. How does cd knows this?
Some commonly used environment ENV variables of Linux systems:
BASH and SHELL variable /bin/bashCPU i686 specs of your systemDISPLAY location of X windowsEUID user id of current userHISTFILE stores no. of command saved in our filesHISTSIZE how many commands history saved eg. 9999HOME path to current home user directoryHOST and HOSTNAMELOGNAME username of current userMAIL path to mailbox fileOLDPWD stores path to prior current directoryOSTYPE type of OS currently being run, usually Linux
PATH PWD path to current working directoryPSI stores characters used to do shell promptUSER & USERNAME store name of current user env shows all variablesset shows all variable in alphabetical order
cd /var/opt/
$PATH=$PATH:/var/opt/ Add new path
vim to edit file
Lecture: Variable- User Defined
Recreate customized user defined variable:AWESOME=”stephen”
echo $AWESOME (Shows Stephen)
export awesome (other Shells get this).
Cannot contain hyphens or underscore. Always use all CAPS. Variable names cannot contain space.
Lecture: Globbing
Wild card character to find names from the system. Global Command = Globbing. Built into bash shell now.
ls *.txt find any file name with .txt at the end.
If we know beginning of a wild card name but not the end, then:
ls test* shows file with name ‘test’ and ‘*’ meaning anything that follows that.
ls ?.txt ‘?’ means 1 character.
If we know 9 characters, then:
ls ?????????.txt shows files with 9 characters.
If I know my file name starts with Monday and ends with file. Then:
ls Monday-file?.txt
To find specific range of letters then,
ls [F]*.txt Shows file with name begins with F and is a .txt file.
ls [f].txt shows file with starting lower case f.ls –al shows all the files.
ls f[igh][lfz]e*txt 1st letter certainly is f but 2nd could either be i or g or h and the 3 rd with l/f/z and certainly ends with e. We use ‘*’ because we don’t know the number, it is used for any character/set of character/ or nothing.
Lecture: Quoting
Double Quote to substitute values or command. eg: echo “Username is $USER”This will print what the current Username: Username is drbindash
Single quote: preserves the literal meaning of each character of a given string and will turn off the (special) meanings of all character.
eg: echo ‘Username is $USER’. This will print/echo the literal Username of $USER. Username is $USER
Backslash: Takes away or removes the (special) meaning from a single character and can be used as an escape character. If we did not have the \ character before $5.00 here it would try and interpret the following command as variable.
$ echo “The cheeseburger is going to cost you $5.00” Will be printed: The cheeseburger is going to cost you .00
But if we used the \ we can negate our special character $echo “The cheeseburger is going to cost you \$5.00”. Printed: The cheeseburger is going to cost you $5.00.
Lecture: Formatting Commands
\ backslash can be terminator too. We can use \ and go to the next line with prompting the command.
Lecture: Working with Options
Linux command parameters can be divided into 2 types>
Parameter with a dash (-) are called options and with no leading dash are called arguments.
ls –al are two options (a= all, l =long listing), we can use this as ls –a –l
Formatting commands conceptually looks like this:
command – what to do?options – How to do it?arguments - What to do with it?
Using the Command Line to Get Help:
Lecture: Man
The Linux MAN (Manual) pages or Manual Page Systems. Meant to be used only for quick remainder, not as tutorial. It’s for only someone who knows the basics.
Man Pages are organized into 9 sections. We use section 1 the most.
Section 1. Executable programs and shell commands.2 System calls provided by kernel3 library calls4 device files usually stored in /dev5 file formats6 games
7 Miscellaneous (macro packages, conventions, and so on)8 system admin commands (programs run mostly or exclusively by root)9 Kernel routines
man cp learn about copy files and directoryman cal learn about calendar man rm removeman vim man passwdman 5 passwd Pulls up section 5 and specific file formats about passwd
What if we don’t the man page or what command?
whatis passwd shows 3 diff possible man (manual) pages with keyword passwd
apropos man Find everything about manual (man)
Easier way by using K flag:
man –k man Anything about manualman –k passw Anything about passwordman –k network Anything about networkman –k mv Anything about move
MAN pages are organized like to:
Name, Synopsis (flags), Description, Options (or flag sections), Files, See Also, Bugs, History, Author
mv test.sh logs/ Move test.sh file to logs filemv Monday-file1.txt newfilename.txt Moves Monday-file1 and renames it simultaneously too
Using less key commands to navigate MAN pages:
H or h displays helpPage down, spacebar, ctrl V, Ctrl F moves down 1 screenPage Up, Esx V, Ctrl B moves up one screenn or / repeats the previous searchQ or ZZ quits/Options Searches ‘Options’ in Man page, Works like Ctrl-F
Man less Shows all about less
Lecture: Info
Info Page: Similar to man but the goal is to support functions which man cannot. E.g.: Hyperlinks.
info info to learn about info system itself
Navigate info:
? Displays help infoN nextP backU upArrow Up downL displays last pageEnter moves to new node once you have selected itT displays top pageQ exits from system
Lecture: locate, find, whereis, and using/usr/share/doc/
Other ways to get help:
README files, readme.txt
The downloaded README for unzipped source files are usually found in:
/usr/doc/packagename/usr/share/doc/packagename/usr/share/doc/packages/packagename
Some programs have configuration files located in:
/etc
If we are using RPM-based Linux distribution we can use the RPM tool to locate documentation for a package.
Eg: rpm-ql packagename | grep doc
rpm –ql passwd | grep doc (|) is called piping out
To find readme file for the Yum application manager:rpm –ql yum | grep README
Most packages use best practice naming convention.
Other ways to find:
find /usr/share/doc –name “*passwd*”locate passwdwhereis passwd
Utilities to read different file formats:
file extensions: Programs to read them with
.1- .9 man, info, less
.gz or .bz2 gunzip, bunzip2, less
.txt less and any text editor vi, vim
.html .htm any web browser
.odt LibreOffice, OpenOffice.org, any word processor app
.pdf xpdf, Adobe reader
.tif, .png.,.jpg Gimp
Using Directories and Listing Files:
Lecture: The Linux File System
File system is where data is saved. You can save a file somewhere and find it later.
Linux file system used a hierarchy (FSH- File System Hierarchy) standard to organize and store data. At the top of the tree there is root directory. Under it are sub directories.
ls
bin dev home lib64 mnt proc run srv tmp usrboot etc lib media opt toot sbin sys users var
bin directory: contains files to run the Linux shell. Next is boot directory, contains boot file to boot file system. Dev (Device) has various file hard drive system we can install. Block devices: pen drive.Next is etc (prn: 8c). Services running in the system.
Eg: /etc/aliases contains a table used to redirect all to local users/etc/exports configured file systems to be exported to remove NFS clients/etc/hosts a list of hostname to IP address mappings /etc/passwd our Linux systems user accounts file/etc/shadow contains encrypted passwords for our user accounts/etc.X11 where X windows configuration files are/etc/fstab, /etc/groups, .etc/grub.conf and so on.
Home folder or directory contains sub directory. We have user called user and drbindash.
Lib (library) folder contains code library used for programs. Media directory is used by open source or fedora for USB.Mnt (mount) used to mount external drives.opt (option) files for installing some programs manually.proc (process) has numbered folders for various processes.root directory is the root user’s home directory. run sbin is similar to bin folder. Has system manager files. srv (services) sys info about system hardware.tmp info about temporary filesusers usrvar Linux variable data including log files.
Linux disk files systems contain file system format. Disk file system is for reliability of file system in a system. Linux have wide variety of disk file system like: ext2 (oldest and fastest file system, max 2TB file, 255 characters long, system itself 4TB), ext3 (updated ext2, allows journaling, records transactions in journals, in event of power outage it can replay system when power is back on unlike ext2 file system), Reiser file system (uses journaling too but different internal structure, 16TB itself, 8TB biggest file), ext4 (1XB volume, 1TB file).
Lecture: Files, Directories
pwd power print working directory shows which directory we are at ls shows various directory and suppose we want to go to media directorycd media/ change directory to mediapwd shows we are in media directorycd just cd will take us to home and it shows tilda (~) i.e. Homecd .. takes us up one directorycd user/
If we want to move two directories up:
cd ../ ..
We can use ls to list contents:
lsls/var/log Shows contents in log folderls/home/user shows contents in userls –l Long listing of the folders.ls –R displays content of current directory and sub directories
Lecture: Hidden Files and Hidden Directories
Linux OS need to hide so that we don’t accidently edit or mess it up. Any file beginning with period (.) is hidden. eg: .local
If we want to create a hidden file. touch .testfile.txt (file with period in front).
To create new folder (directory): mkdir .testfolder (with period in front).
Lecture: Absolute and Relative Paths
pwd prints working directory, which is /home/user which is the relative path i.e. path we are currently in. Absolute path: We provide the long and complete path. Eg: ls /home/user/testdir2/ or use relative path: ls testdir2/
Creating, Moving and Deleting Files
Lecture: Files and Directories
root@dr touch file.txt created a file.txt
ls –al see details of file
touch file.txt will update the time stamp of file.txt
touch –d “February 1 2015” file.txt File.txt will be dated February 1st
cp file.txt file1.txt will copy file.txt
cp file.txt newdir1/ will copy file.txt to newdir1 folder
ls
copy file.txt newdir1/newfilename.txt will have a brand new file newfilename.txt at newdir1
-f f flag force-p flag preserving original file ownership on file-i flag interactive flag, copy (cp) will ask before overwriting a file that already exists-R recursive will copy all directories recursively-a archive, same as –R, preserves ownership of the older files as well-s symbolic links-u update, copy file only if original file is new than old file
root@dr mv file1.txt file2.txt moves file1 and simultaneously renames it
mv file2.txt testdir2/ moves file2 to new directory testdir2
Let’s check testdir2 and check contents: cd testdir2/
To move and rename file while taking to new location:
mv file.txt testdir2/newname.txtLet’s see folder testdir2: cd testdir2/
ls
To delete file:
rm newname.txt removes or deletes the filey type yes for confirmationcheck: ls
root@ dr: Let’s create few files:
touch file1 file2 file3 file4ls shows filesrm file1 file2 file3 Are you sure? y y yls
mkdir directory3 creates new directory ls
mkdir –m sets mode flag
mkdir firstdir/secdir
Message: cannot create directory ‘firstdir/secdir’: No such file or directory
mkdir –p firstdir/sec dirls
cd .. move back a folderls
rmdir dirtest1/
Message: Directory empty
rm –rf dirtest1/ remove file and folder without prompting us each file and folder, so we use f for forcels
ls –al check long directory listing
touch –d “January 27 2013” newdir1/
ls –al to check contents of directory, the date is updated
Lecture: Case Sensitivity
Linux commands file names and directory name are all case sensitive.
user@dr: ls
./script.sh run script.sh file
cd /var/log change directory into var/log foldercdpwdcd /VAR/log doesn’t work because its Caps
cd /etc Lets go to etc folder, and se X11 folder
cd x11 can’t because x is not Caps
Lecture: Simple Globbing and Quoting
user@dr: For uncertain files names, we use Globbing, using wild card characters.
ls *.txt should return any file that has any file name that ends with extension .txt
Let’s say we need to find something but forget what’s it’s called. Then we use ‘?’
ls ?.txt can’t show any file because we don’t have single character file
touch a.txt create a file a.txt
ls ?.txt shows a.txt
for 3 digit files:
ls *??? * = 0 or more characters. This shows 2 files, that has 3-digit extension
ls [a-z}*.txt Any files that start with alphabet a to z
rm [a-z]*.txt removes each file that begin with letter a-z and is a .txt file
echo $PATH prints path environment for our user
echo “Path is $PATH” will print out the path
echo ‘I want to print $PATH’ will literally print ‘I want to print PATH’
There are 3 different quotes we can use:
Double quote “ ---“ Protects everything enclosed. Eg:
echo “$SHELL” will print what our shell is
echo “Today is $(date)” will show Dec.3 …
Single quote turns OFF special meaning: Eg: echo ‘Today is $(date)’ will print ‘Today is $(date)’
The backslash will change special meaning of a character within a text, such as quotation marks.
eg: echo “Path is \$PATH” will print Path is $PATH echo “Path is $PATH” will print: the actual path. ie: Path is /usr/local/bin:…
Let’s create an environment variable: Eg:
FILE=”/etc/resolve.conf”
echo “File is \”$FILE\” What we did is put a special character and wrapping will print:
File is ‘/etc/resolve.conf”
Now,
FILE=”/etc/resolve.conf”
echo “File is \$FILE” will print File is $FILE
Furthermore, we use –e option of echo command to enable interpretation of backslash escapes.
Eg: user@dr:
$ is used as variable, but we need to use money dollar sign, then:
echo “Chinese takeout was $30.00” will print Chinese takeout was 0.00
Now, echo “Chinese takeout was \$30.00” will print Chinese takeout was $30.00
echo “The CIFS network path is \\Servername\ShareName” will print:
The CIFS network path is \Servername\ShareName
Eg: echo “Linux Academy is the best place on the earth to learn
we can just:
echo “Linux Academy \is the best \place on the \earth to learn \
Backslash acts as ENTER and we can just go on and on.
The power of the Command Line.
Lecture: Files, Directories
Archive is zip file format in Windows. In Linux it is Tar files. Tar stands for “Tape Archive”. Tar doesn’t compress file like zip would, but just archives files.
root@dr:
lsls folder1/
If we want to archive folder1 then:
tar –cf c means create file.tartar –xf extracts file file.tar
tar –cf folder1tar.tar folder1 ls
Since we achieved, we don’t need folder1tar. So we remove it by:
rm –rf folder1
To untar or extract tar file:
tar –xf folder1tr.tarls
we can see the folder back again.
lsls folder1
Now, rm folder1tar.tar removes folder1tar.tar filey
cd folder1/ let’s move into folder1 and see files inside it
tar –cf files.tar file1.txt file3.txt ls
tar –xf files.tar ls
rm file1.txt file3.txtyyls
we only have file2 now and have deleted file1 and file3 and achieved it
Now,
tar –xf files.tarlsThe file1 and file3 are back.
Lecture: Archives, Compression
We have 3 ways in Linux file system to compress files.
Gzip compressed file format, use utility gzip, to unzip we use gunzipBzip2 have better algorithms, for large files, bizip2 to compress and bunzip utility to unzipZip compress files using zip and unzip to unzip
Example:
root@dr:
lszip command not found, is not default in centos or Red-Hat enterprise server
yum install –y zip
clear
ls
cd folder1
zip file3zip.zip file3.txt will zip file3.txt file and rename it to file3.zip.zip too
ls
rm file3.txt will remove the original file3.txt
To unzip zip file, we need the unzip utility on centos:
yum install –y unzip
unzip file3zip.zip will unarchive the archive and extract file3.txt
zip –r folderzip1. folder1 will go into folder 1 and recursively zip all files inside
We can now remove folder1, since its backed up.
rm –rf folder1
unzip folderzip1.zip will create the original folder and extract all the files.
rm folderzip1.zip removes folderzip.1
root@dr: ls
Shows files. Let’s create a file and give name:
tar –cf folder1.tar folder1lsls –al shows file size of folder1.tar, which we will compress:
gzip folder1.tar will compress the folder1.tar filels –al to check file size and extension
To un-compress:
gunzip folder1.tar.gzls
tar –xvf folder1.tar (v = verbose will show actual progress, x = extract, f= file)
rm folder1.tar remove
Now using bizip2 utility, we compress folder1.tar:
We need to install it first:
yum install –y bizip2
bizip2 folder1.tar will compress folder1.tar file
ls
bunzip2 foler1.tar.bz2 will unzip the file to original folder1.tar
tar WITH compression:
tar –zcf (gzip) file.tar.gz or file.tgztar –jcf (bzip2) file.tar.bz2 or file.tb2
tar –zcf folder1.tgz Will compress folder1 directory
to untar and uncompress: tar –zxvf folder1.tgz
Searching and Extracting Data from Files:
Lecture: Commands
cat displays the contents of test file eg. cat file1.txtless reads a filehead/tail locates the files on systemfind locates files on system eg: find . (period . = current directory)grep search a string in our file eg: grep –in dhcp var/log/messages
(-i) flag lets grep search without case sensitivity(-in) flag shows which lines the content are in
sort organize alphabetically or remove it eg: sort file.txt or sort –r file.txt r= reverse order, sort –n number.txt n= numerically 1-20
cut manipulates data by columns, cut –c2-4 file2.txt, will return column 2-4, or cut –c2.4.6 file2.txt
wc can be used to do word count and statistics on the file eg: wc sort.txt,wc – w sort.txt (will shows total words in sort file), wc –c sort.txt (shows characters).
Lecture: Command Line Pipes
Standard Bash File Descriptors:
stdin this means we have standard inputstdout this means we have standard outputstderr this descriptor stands for standard error
root@ dr:
cat abc.txt | grep bbc will display files with bbc
Lecture: I/O redirection
Input output
The > symbol is redirection symbol
su go to root
tail /var/log/messages shows last 10 log files
tail /var/log/messages > logtemp.text instead of printing in screen, the output will be saved at logtemp.text
cat filethatdoesntexitst.txt The standard error message is displayed, NO such file or directory
cat filethatdoesntexists.txt >2 myerrors the error will be saved to myerror file, >2 = standard error
Error messages can be overwritten in old files, so we can write the standardout in the same file:
ls
vi runnungprocesses create file called runningprocesses
ps will display processes
ps >> runningprocesses the error will go and add to runningprocesses
cat runningprocesses see the content in runningprocesses file
command 1> stdout_file 2> stderr_file redirect one file for error and another file for output
eg: mount 1> mountfile 2> mounterror
ls
cat mounterror shows mounterror file
root@dr:
tail < /var/log/messages sends the text string /var/log/messages to the tail command as an input
cat words have couple of files inside
sort < words this will pass files as input into the sort command
Lecture: Regular Expressions (regex- prn: rej-ex)
Many linux command are used for text processing. Powerful tool to describe files are regex.
Figure: Regular expressions
grep bb abc.txt will find anything that has bb in abc.txt file
grep cc$ abc.txt lines that end in cc
grep ab abc.text
grep ^ab abc.text shows anything in abc.txt file that starts with ab
grep .cc abc.txt anything that follows by cc, eg; acc, 1cc, ucc etc
grep ^.b abc.txt beginning of anything that is 1 character and followed by b
grep ^…$ abc.txt search beginning of line that can have 3 single character and at the end of line. eg anything that starts and ends with 3 characters
ls file* list command of files that have file name file1, file4, filex etc
grep ^3server hostnames line items that has 3 servers in it in the host
grep [ser] hostnames anything that is s or e or r
grep [^ab] hostnames anything that is NOT a or b
Turning Commands into a script
Lecture: Basic text editing
root@dr: nano
GNU nano 2.3.1 text editor software, like notwpad on windows
Vi is the professional text editor.
nano newfile creates newfile in nano
Inside GNU:
start creating text
Ctrl k delete whole entire line
Ctrl X save file
Ctrl U paste file
root@dr: vi
Inside Vi text editor:
We are in command mode in Vi once we start. We need to goto INSERT mode. Hit the i key.
In command line mode you can’t edit file. Only in insert mode you can. We can get to insert mode in: i, INSERT, S, O or A key.
In INSERT mode you can edit, change, type or delete text from file. You can’t perform file operation. You can goto command key by Esc key.
Hit the i key- this takes us to INSERT mode, which allows us to type text editing.
: takes to command line mode.Simply,
:w newfilename will let us save the file.
Also,
exit also writes the file and exits from Vi all together.
:wq this will write the current file and exits from Vi, taking to command line.
:q closes Vi w/o saving the file.
:e! brought back all the changes made.
word at cursor and type dw will delete the word and space.
de will delete word only after cursor not space.
dd delete the entire line.
entering p command will paste the word we deleted last.
/command to search for a term. eg: /here will search here.
/this takes us to This.
Lecture: Basic Shell Scripting
Figure: Arguments, Shell Scripting, Syntax, Script commands, if/then, if/then/else, Looping structures, for loop, until loop, seq loop
root@dr:
ls
rm file1.txt && echo “File was Deleted” || “File was Not Deleted” will remove the file1.txt and give us the command “File was Deleted” , the double pipe || means OR . If we run the same command again, it will display “File was Not Deleted” because there was no such file.
if/then/else
fi condition means we are ending ‘if’ statement
Looping structures come in 3 styles: while loop, until loop and for loop.
Until loop: until certain condition is true, the command runs.
root@dr:
vi firstscript.sh we create a script called firstscript.sh
At top of file we need to crate shebang (check spelling)
#!/bin/bash
It is good practice to say what we are doing with command by typing:
#say hello world to the shell prompt and tell us what user it is
Let’s start:
echo Hello World –This is the $USER (environment variable) this is our first script!
:w write file
Before we can execute script, we need to give it executable permission:
chmod +x firstscript.sh ./firstscript.sh
This will print: Hello World- This is the user root this is our first script!
vi findlist.sh create findlist.sh file
To create shell scripting using some arguments, first we go to Vi editor and create list called:
vi findlist.sh
First we create shebang (check spelling):
#!/bin/bashBest practice Comment: # List all the contents in a directory and write the output to a file called dir_list.txt.
ls >> (upend to a file) dir_list.txt This will list out content of current file directory to file called dir_list.txt
Let’s save file: :w
Let’s go to prompt: ls and we can see new file findlist.sh
Before we write, we need to make it executable by:
chmod +x findlist.sh change mod command and add x = executable bit to our findlist.sh
ls Now we list it out we see its green like another .sh file, meaning color coding is executable file.
Let’s kick it off: ./findlist.sh
ls We can see file and dir_list.sh
We cat it out: cat dir_list.txt
ls >> dir_list.txt list file in our directory and upend to file called dir_list.txt
We need to be able to tell our script what directory to list and print out that directory file, or accept user input within the script.
Open VI editor:
#!/bin/bash#List all the contents in a directory and write the output to a file
Go to insert mode and create first argument:
location=$1
ls $location >> dir_list.txtecho “Script is complete and has indexed the $location”
:w change changes
Remove directory file we last created:
ls rm dir_list.txt
Tell our script what location we want to run on:
./findlist.sh /tmp
cat dir_list.txt
cat findlist.sh
Let’s make a few more change to our script:
root@dr: vi
#!/bin/bash means we are using born again shell to run this#List the contents in a directory and write the output to a file comment of what script is doing
location=$1 first argumentfilename=$2 2nd argument
ls $location > $filename telling what folder to run on and what file name we are givingecho “Script is complete and has indexed the $location echo “##########”echo “Displaying contents of our $filename”echo “##########” echo $filename
Let’s back out: :q
Let’s kick it off: root@dr:
./findlist.sh /var/log logfile.log call name of script: findlist.sh, the location where we ant to log script: /var/log folder and give it name: logfile.log
vi findlist.sh
Let’s create if statement:
if [ -z “$location” ] if variable z is empty then echo the messagethen
echo “Please provide location argument”exit 0
fi
if [ -z “Sfilename” ]then
echo “Please provide a filename”exit 0
fi
Let’s save changes: :w
and run: ./findlist.sh
…………..The Linux Operating System:
Lecture: Windows, Mac, and Linux Differences
Windows: Uses proprietary apps which work in Windows and might not on Linux. Active directory. Database needs when you use Microsoft SQL.
Apple OSX: Own hardware and software allows tight integration within its own ecosystem. Security- extremely difficult to lock down and manage. Free upgrade. Best video and photo editing.
Linux: Desktop is free and personal and can customize to fit our needs. Most apps use web based apps nowadays, so don’t need to run apps on desktop.
GUI and CLI: All these OS have GUI (Graphical user interface) and CLS (Command line interface). Linux can use CLI only w/o using GUI. You can install Linux in command line only.
Lecture: Distribution Life Cycle Management
Design – Develop – Deploy- Manage – Retire
Lecture: Kernel
Unix like OS. Developed and created by Linus Torvalds. Kernel is the core of any Linux installation and is responsible for managing every other piece of software on a running Linux computer. To maintain order on a chaotic Linus system of processes, the Kernel imposes order by using hierarchy. When the system boots, typically one process called the init process starts up the /sbin/init that in turn manages child processes.
Lecture: Processes
Every process has an associated process id (PID).Every parent process has a parent id (PPID).We can identify these PIDS and PPIDS with tools like ps.
user@dr:
ps –u drbindash –forest
ps u U drbindash Shows cup, memory percentage etc.
ps ax | grep vim shows process id
Load average 0 = system that has no programs demanding CPU time. A system average of 1 is a system with one program running a CPU intensive task.
Lecture: syslog, klog, dmesg (d message)
Most apps in Linux that we run in background (known as demons) write operation titbits to log files. Linux stores most of its logs in var log directory.
lscd var/lscd logls
tail messages shows last 10 lines of messages log filestail boot.log shows last 10 lines
root@dr: dmesg
dmesg | less
dmesg | grep console
Lecture: /lib, /usr/lib, /etc, /var/log
Figure: Data locations
root@dr: cd /libls –alcd /usr/libls -la
cd var/log stores standalone logs or demon for apps folders etcls –al
Lecture: Internet, Network, Routers
Everyone has dedicated network connection: Wi-Fi, home dsl router, broadband etc. IP is Internet Protocol. Internet is based on protocol based on TCP IP protocol- Transmission Control Protocol. Protocol is a set of rule. IP Protocol works with the UDP- User Datagram Protocol.
OSI reference model was designed in 1983. It has 7 layers. Information flows down through the system and flows up the receiving side.
Figure: OSI Reference Model
reference model has 7 layer. 1st is the physical layer which transmits electrical signal between hosts. Is the actual network card. From modem to switch.
2nd layer is data link, which defines the rules and procedures to accessing 1 st layer. It decides how to receive data and verify data from 1st layer.
3rd layer is network layer. Enables routing of data. Specifies how to determine the next network point. IP protocol operates at this layer. As lower ICMP.
4th is transport layer at the receiving end. Receives info from upper layer on OSI model and divides data into small transmittable chunks known as packets.
Layer 5 is session layer. For maintaining connection.
Layer 6 is presentation layer.
Layer 7 is application layer. PC1 sends fragments data as packets (which are transferred on network Media) to PC2 that receiver re-sequences and defragments data.
Figure: pc1 pc2
The entire picture of IP protocol and the transmission control protocol is TCP/IP.
TCP ensures data exchanged between two network hosts is exchanged reliably. Typically, upper-layer applications that use TCP are: Web servers, e-mail servers and FTP servers.
UDP- User Data Protocol. Is connectionless protocol for applications that can tolerate less reliability can use UDP. e.g.: Streaming audio and VoIP- Voiceover IP. But we cannot lose email so, email needs TCP.
ICMP- Internet Control Message Protocol, is for testing and verifying network communication between hosts.
IP Ports: TCP and UDP both provide ports for upper layer protocols. Port numbers range from 0 up to 65536 for each individual IP address.
Figure: Port numbers regulated by Internet Corporation for Assigned Signs and Numbers- ICAN.
Registered ports: 1024-49151.
Dynamic PORTS or private ports: 49152-65535. Used by network services for temporary connection.
IP address consists of 4 numbers, separated by periods. e.g.: 192.168.0.1
MAC address is hard coded in a hardware and are unique. Eg TV, laptop have their own unique MAC address.
The decimal number in IP address cannot be greater than 255 because the IP number is actually converted to binary number- 0 and 1. Each number has to be an eight-bit binary number called an octet.
e.g.: 192.168.1.1 in binary format: 11000000.10101000.00000001.00000001
Simple ways to convert decimal to binary.
11000000 = 128 +64 = 192
10101000= 128 +32+ 8 = 16800000001= 1 = 1
Figure: Conversion tool
IPV4 is a 32 bit IP scheme.IPV6 is a 128 bit IP scheme: e.g.: FE80:0000:0000:0000:0202:B3FF:FE1E:8329
NAT Network Address Translation.
Subnet Mask: Network vs node with our IP address.
(Network 192.168.1).(1 node)
Figure: Subnet Mask
Lecture: Domain Name Service and the Default Gateway aka DNS aka Network Router.
DNS servers translate domain names (website) into IP addresses.e.g.: www.linuxacademy.com - 54.165.61.14 and 54.164.230.15
root@dr: dig www.linuxacademy.com
Install package called: bind-utils with yum to install dig
Lecture: Network Configuration
Figure: Network Tools
/etc/resolv.conf (FILE) is where DNS server information is stored.
root@dr:
vi /etc/resolv.conf
nameserver followed by IP address is used to resolve name to IP addresses
ifconfig shows current network configuration
ping www.google.com We get icmo, and shows IP address we are responding to.
ping 172.31.29.142
route add –net (add network) 10.10.40.0 (give a netmask) 255.255.255.0 (give gateway) gw 10.10.30.1
remove network or routes by: route del –net 10.40.40.0 and so on like above
Set default route by: route add default 10.10.30.1
Troubleshooting network: root@dr:
netstat –a lists all listening and non-listening sockets netstat –i display network stats of network interfacesnetstat –l listening the listening socketsnetstat –s displays summary for each protocolnetstat –r shows routing table, which is same thing as typing route
traceroute DIST_HOSTNAMe or IP traces a route a packet takes through router to destination. Eg: traceroute www.google.com
Security and File Permissions | Basic Security and Identifying User Types
Lecture: Root and Standard Users
user@dr: echo
ls /home shows all users
dr@user:
pwd print working directorycd /home/user cannot because it’s different usertouch testfile create testfilesu –user switch to user
Root user can see all the files from all the users. Root user has its own home directory too.
root@dr: Install utility called finger
yum install –y finger at centosapt-get install finger at Ubuntu
@root: finger root find out info on our root user
finger user find out full name, login name, home directory, default shell, how long they have been associated with the Linux file system
id drindash shows what’s userid for dr, group id, which group its associated with
root@dr:
/etc/shadow where Linux user has password/etc/group system group/etc/passwd local authentication and has the system users
drbindash:x:1002:1003::/home/drbindash:/bin/bash are respectively: username, password vault, userid, group id, full name, home directory and default shell
root@dr: cat /etc/shadow
drbindash: Xaswe2(&(&&(($%()&^%^&*(daas : 16844:2121232:2: are Userid: encrypted name
cat /etc/passwd will display username, encrypted password, last modified field, max days before pw must be changed, days warning for pending password expiration, no of days to wait before password has been expired to deleted the account NEVER EDIT THESE FILE MANUALLY AND USE PWCK UTILITY!
root@dr: pwck shows users
pwconv if there is no username shown that exists, this utility will run through and add missing users
user@dr: su superuser or substitute user to root
$ changes to # means we are a super root user now
clear takes to user
user@dr: su – drbindashecho $PATH shows path for drbindash
clear
user@dr: sudo yum upgrade for upgrade in Centos server
yum upgrade just by itself, will ask for root privilege, if we don’t type sudo
root@dr: w displays info about the user, userl ogin time, idle time, jcpu, pcpu, commandline
jcpu is time for all process by tty, pcpu is time used by current process etc
root@dr:
who -b shows last boot time of our Linux machine
who –m host name and user associated with it
who –r current run level for user
who –q number of users logged in to machine
who –a all the above info at once
last user see what user performed last command, IP addresses
Lecture: System Users
cat /etc/passwd all users in Linux system reside here. Called system users.
Creating Users and Groups
Lecture: User IDs
User account have its own unique user id or UID. User account has textual username eg: drbindash, steven, bob, frank etc. Linux kernel doesn’t know anything about the textual username and instead use user id. UID can be same for two different users with different name. But in technical environment it is not recommended.
root@dr: id shows the current user and its uid and gid, user and group id
id drbindash shows userid of drbindash and group id and primary group
id –u user (drbindash) shows userid only
id –g user shows group id only
id –G user shows other group we are member of
id –Gn user shows what user we’re currently on
groups user shows which group the user is in
last shows who, where and when logged into the system and if they are stilled logged in
* root has userid and group id as Zero 0
Lecture: Creating Users and groups
root@dr# useradd khattam adds new user as khattam using default parameters
vi /etc/default/useradd this file shows default group, home directory, shell, expire and inactive time
ls /home/ shows defaults for khattam
useradd –D shows defaults when we add new user
vi /etc/login.defs this file shows default values and specifies default set for new account
ls –al /etc/skel shows hidden files and folder for users
touch /etc/skel/everyonegetsme created new file called everygetsme in etc/skel folder
man useradd see manual for useradd, and various flags, eg e flag, f flag etc and the functions
root@dr:
useradd user2 creates user called user2
useradd –c “Stephen Smith” –m –p “linuxacademy123” –s “/bin/bdash” ssmith made user “Stephen Smith” with username ssmith and password linuxacademy123
cat /etc/passwd
useradd user3 created new user without password
passwd –S user3 we see that the password is locked because there is no password set
clear
passwd user3 to created new password for user3
passwd –S user3 -S flag is for settings for user3
man passwd manual for password and various flags
managing existing user using usermod utility:
man usermod manual for usemod, c for modify user full name, -f inactive mp of days before password expiration, -g set user default group, -G additional group the user can be made member of, -L lock, -m move home, -l login, -p sets user password, -u change or specify userid, -U unlock users account that was previously locked.
eg: Jenifer Aniston married and became Jenifer Smith: We need to change her last name:
There is already J Aniston username. now,
root@dr: usermod –l jsmith (new userid) –c “Jenifer Smith (full name)” janiston (which user the changes are to?) Clean version: usermod –l smith –c “Jenifer Smith” janiston Deleting users@root : userdel jsmith deletes the jsmith
cat /etc/passwd in this file where users are stored shows that jsmith is already deleted
ls /home but janiston is still there, so:
userdel –r jsmith will delete user home folder too
Lecture: Group Commands
Let’s say we have 20 users and 5 have special permission. We don’t want to individually assign permissions, rather do it as a group, and just make one change. Group are defined in /etc/group files.
root@dr: vi /etc/group
Group:Password:GID:users
vi /etc/gshadow much like /etc/shadow file but for group, this file is where group password/s are stored
groupadd accounting create group for accounting department
vi /etc/groupfile
man groupadd manual for adding group
groupmod –g 1010 accounting change group id for accounting group
man groupmod manual for group modifying
groupmod OPTIONS groupname utility for adding group to users
man groupmod manual for modifying group
Group ID is 1006 for accounting, change to 1008: groupmod –g 1010 accounting
cat /etc/group shows the changegroupdel accounting deletes the group
cat /etc/group shows that the group is deletedManaging File Permissions and Ownership
Lecture: File/Directory Permissions and Owners
Permission: User, Group, Other
d= directory, - = file, r= read, w= write, x = execute
7 = 421, full permissions, read + write + execute
In octal notations:
R =4W =2X =1Total =7
Octal notation adding:
-rw - r- - r - - 420 4 4 6 4 4 = 644
ls –l
eg: drwxrwx-r will be drw xrw xr-x respectively for USER, group and other i.e. everybody else in the Linux system
Setting permissions using the octal notation:
chmod 555 filename 5= r read (4) and x execute (1)orchmod ugo+w (this adds write permissions to the user, group, and other).
u = userg = groupo = other
To change permissions of a file, you 1st either need to already be the owner of a file or you need to be the root account.
To change the ownership of a file, we can use the chown command i.e. change owner command:
- chown username:groupname filename- chown Stephen:accounting filename.txt
Removing permissions:
chmod o-r filename.txt we removed the read permissions from others o=other, r=read.
chmod g-w filename.txt we removed the read permissions from groups g=group, w=write.
chmod u-r filename.txt we removed the read permissions from users u=user, r=read.
Adding permissions:
chmod o+r filename.txt we added the read permissions from others.
chmod g+w filename.txt we added the read permissions from groups.
chmod u+r filename.txt we added the read permissions from users.
e.g:
chmod 644 filename.txt
R =4W =2X =1
6= 4+2 = read and write permissions4= Read, no write, no execute permissions
eg: drwxrwxr-x user user means it’s for directory=d and first set of rwx is for user, 2nd rwx is for group and the r-x is for other and has read and execute permission but no write permission.
-rw-r- - r- - it starts with dash ( –) means it’s a file, not a directory. 1 st set of 3 is for user ie read and write permission, group has read only and other/everyone else on system has read permission.
clear
Let’s create a file: user@dr:
touch testfile3.txtls –la long listing for permission
default permissions: -rw-rw-r- - To change permission: chmod +x filename adding execution
To make read, write, execute for group and others:
chmod ugo+rwr testfile3.txt ls –la
Let’s remove execute permission:
chmod ugo-x testfile3.txtls –la
To change permissions using octal notation:
Formula: RWE = 421
read (4) write (2) and execute(1) = 644
To add read write for user and read only for group and other:
chmod 644 testfile3.txt ls –la
Now, chmod 555 testfile3.txt read and execute on user, group and other.
chmod 444 testfile3.txt read for all 3 bits i.e. on all user, group and other.
To change ownership of file:
root@dr:
ls –l see firstscript.sh
To change owner for the file firstscript.sh to user stephen and group stephen:
chown stephen.stephen firstscript.sh
To add write command to group:
chmod g+w firstscript.sh u
Revision:
r=read, w=write, x=executeread=4, write=2, execute=1
max= 777 permission
Special Directories and Files:
Lecture: Symbolic Links
Symbolic link is pointer or shortcut (as in iphone keyboard) to a filename.
user@dr: ln –s location s= symbolic flag
vi file1.txt
this really is file1.txt
Now, let’s create symbolic link: ln –s file1.txt myfilelink.txt
ls –l shows myfilelink.txt pointing to file.txt
vi myfilelink.txt will takes us to file1.txt
cat myfile1.txt
Let’s say we have network card is at a long file name:
vi /etc/sysconfig/network-scripts/ifcfg-eth0 which is a long file name
We can just change it: root@dr: pwd see where we are:
ls –s /etc/sysconfig/network-scripts/ifcfg-eth0 NIC1ls –l will show changed file namecat /etc/sysconfig/network-scripts/ifcfg-eth0 and cat NIC1 will show the exact same thing Lecture: System files, Special files, and Sticky Bits
Special directories and files:
/var contains files that change often such as mail, logs etc. Under /var folder is:/var/tmp contains files that do NOT get deleted on reboot/tmp contains temporary files that do get deleted on reboot
/tmp folders can be written by any users on Linux file systems and delete it too. The problem is if I write, another can delete it. It can create havoc. The problem with temporary files is that it has permissions 777 eg: rwx rwx rwx . But, when we add sticky bit to a folder, it makes it so that only users that create their own folders and files even if it has 777 permissions.
cd /sticky ls –l
The sticky directory (because it has a ‘d’ in front) and has drwxrwxrwx permission.
user@dr: touch file1.txt create a filevi file1.txt edit the filerm file1.txt y
ls –l /sticky/ shows 777 permissions for sticky folder
To add sticky bits: chmod o+t /sticky t=sticky flagls –l shows new permission: drwxrwxrwxt with ‘t’ at the end, meaning only the creator can remove the file now even if it has 777 permissions.
Let’s add 4th octet while adding permission now, since we know sticky bit:
chmod 0777 /sticky removes the sticky on the /sticky folderchmod 777 filename will default no sticky bit, 0 is invisible, and we only use to add:
chmod 1777 will add sticky bit permissions
chmod o-t /sticky will remove the sticky, o=other, t= sticky to add it back: chmod 1777 /sticky or chmod o+t /sticky