Linux Essentials
Mods 2 - 9
Linux Essentials
Mods 2 - 9
CH 1
Linux is Open Source
Linux Has Distributions
Linux Embraces the CLI
The typical computer user today is most familiar with a graphical user interface (GUI)
- In a GNU, applications present themselves in windows that can be resized and moved around
- There are menus and tools to help users navigate
The second type of interface is the CLI, a text-based interface to the computer
- The CLI relies primarily on keyboard input
Operating System
Users today have a choice between three major OSs:
- Microsoft Windows
- Apple macOS
- Linux
Only MS Windows is based on proprietary code that is not Unix or Linux based
Decision Points
- MS has annual license fees
- Apple does not charge annual fees but only works on Apple hardware
- There are multiple Linux providers wo offer enterprise support and although the software is free, support is not
Microsoft Windows
Apple macOS
Linux
Linux Decision Points
Linux Distros
.deb
file formatLinux is a Kernel
Linux is a combination of software called GNU/Linux, which defines the OS
- GNU is the free software that provides open-source equivalents of many common UNIX commands
- The Linux part of this combination is the Linux kernel, which is the core of the OS
The story of Linux begins with UNIX, an OS developed at AT&T Bell Labs in the 1970s
The Desktop
The CLI
- Brose to the terminal application from the application menu
- A virtual terminal can be run at the same time as a GUI but may require the user to log in via the V-terminal before they can execute commands
Major Applications
- Server Applications: The purpose of this software is to server info to other computers, called clients
- Desktop Applications: Web browsers, text editors, music players, or other applications with
- Tools: a loose category of software that exists to make it easier to manage computer systems
Server Applications
One of the early uses of Linux was for web servers.
- A web server hosts content for web pages, which are viewed by a web browser using the HyperText Transfer Protocol (HTTP) or its encrypted flavor, HTTPS
There is a growing demand for Private cloud sever software that can be deployed and administered internally
- the ownCloud project provides software to store, sync and share data from private cloud servers.
- the Nextcloud project also provides private cloud software
Desktop Application
- The Mozilla Foundation came out with Thunderbird, a full featured desktop email client that connects to a POP or IMAP server
- Other notable email clients are Evolution and KMail which are the GNOME and KDE project's email clients.
- Blender, GIMP (GNU Image Manipulation Program), and Audacity which handle 3D movie creation, 2D image manipulation, and audio editing respectively
- LibreOffice is a fork of the OpenOffice application suite
- the Mozilla Firefox and Google Chrome browsers are open-source web browsers that are fast, feature rich and have excellent support for web developers
Shells:
- Users interact with a Linux system through a shell, which accepts commands to execute
- Linux offers a variety of shells to choose from such as; the Bourne shell, C shell, the Bourne Again (Bash) shell, the tcsh, the Korn shell (Ksh), and the zsh
Text editors:
- most Linux systems provide a choice of text editors which are commonly used at the console to edit configuration files.
- The two main editors are Vi (Vim for modern) and Emacs
- Pico and Nano are available on most systems and provide very basi, yet user friendly text editing
Debian Package Management:
.deb
extentiondpkg
, apt-get
, aptitude
, Synaptic, and Software CenterRPM (Red Hat Package Manager) Package Management:
.rpm
file for each software packagerpm
commandComputer programming languages provide a way for a programmer to enter instructions in a more human readable format, and for those instructions to eventually become translated into something the computer understands.
- an interpreted language translates the written code into computer code as the program runs
- A compiled language is translated all at once
- Python has excellent statistical processing abilities and is a favorite in academia
- As you browse the web, a web server can send back the cookie, which is a small piece of text, along with the web pag
- Browsers typically offer cookie-related settings, users can opt to have the browser tell the site not to track
Protecting Yourself
- Use a good, unique password everywhere you go
- Use a password manager like KeePassX to generate passwords
- Limit the info you give to sites to only what is needed
- Make a point of checking for system updates periodically
- a firewall is a device that filters network traffic, and Linux has on built-in
- If you are using Ubuntu, then the Gufw is a graphical interface to Ubuntu's Uncomplicated Firewall (UFW)
Privacy Tools
- Administrators deploy encryption with authentication keys almost every system that communicates with the outside world
- The HyperText Transfer Protocol Secure (HTTPS) standard used on web servers to ensure that data transmitted between users and online resources cannot be intercepted as it travels on the open internet
- Virtual private networks (VPN) work by creating an encrypted channel of communication between two systems, so that transmitted between the is scrambled by an algorithm only the systems know
- The Tor project and tools like its Tor Browser relay internet requests through a network of servers that prevent websites and others from learning the identity of the person making the request
Password Issues
There are many levels of access and various means of passwords management of a Linux system
- When usrs are created, they are given different login permissions depending on what groups they are assigned to
- Services that run on systems such as databases can also have login permissions with their own passwords and privileges
Increasingly users and administrators are turning to password manager programs to store login credentials in encrypted form
The Four Primary Cloud Dev Models
- A Cloud infrastructure deployed by a provider to offer cloud services to the general public and organizations over the internet
- A cloud infrastructure that is set up for the sole use of a particular organization
- a cloud infrastructure that is set up for the sole use by a group of organizations with common goals or requirements
- a cloud composed of two or more individual clouds, each of which can be private, community, or public clouds
Linux in the Cloud
- Flexibility: Cloud computing provides the capability to provision IT resources quickly and at any time.
- Accessibility: Applications and data reside centrally and are accessed anywhere over a network from any device
- Community Cloud: Consumers can scale IT resources to meet workload demand, thereby eliminating overhead from underutilized resources
- Hybrid Cloud: A significant portion of Linux server operating in the cloud are created and managed by automated management programs rather than human operators
- This process frees up admins to monitor computing operations rather than manually configuring and updating systems
- Security: Linux can help offset security issues because it is one of the most secure and reliable operating systems available
- Linux is open source, meaning its source code can be inspected for vulnerabilities and compatibility issues
Virtualization
- this optimizes usage of physical resources and dramatically reduces costs over the previous one machine, one OS model
Containers and Bare Metal Deployment
"Two groups can be considered the most influential forces in the world of open source: The Free Software Foundation and the Open-Source Initiative"
The Free Software Foundation
- Also GPLv2, GPLv3, LGPLv2, and LGPLv3
Open-Source Licensing
- Ownership - Who owns the intellectual property
- Money Transfer - Does it cost anything? How do you pay?
- Licensing - What do you get? What can you do with the software? How many computers? Can you share it?
Creative Commons
- You can use CC BY content for any use but must credit the copyright holder
- Copyleft version of the Attribution license
- You may redistribute the content under the same conditions as CC-BY but may not change it
- Like CC-BY, but may not use it for commercial purposes
- Requires that your changes be shared under the same license
- Allows sharing the content to be used for non-commercial purposes, but people may not change the content
- Public Domain
Open Source Philosophy
Open-Source Business Models
If the software is free, how can a company monetize it?
The Open-Source Initiative
free
from restrictions (Libre) and free
from cost (Free)Command Line Interface
The Shell
The Bash shell also has many popular features, a few of which are listed below:
- Command Line History
- Inline editing
- Scripting
- The ability to place cmds in a file and then interpret (effectively use Bash to execute the contents of) the file, resulting in all of the cmds being executed
- Aliases
- The ability to create short nicknames for longer commands
- Variables
- Used to Store info for the Bash shell and for the usr
When a terminal application is run, and a shell appears, displaying an important part of the interface - the prompt
sysadmin@localhost:~$
- Username (
sysadmin
)- System name (
localhost
)- Current Directory (
~
)
- The
~
symbol refers to the usrs home dir
Command Structure and Syntax
Arguments
ls
cmd is given the name of a dir as an argument, it lists the contents of that dir
sysadmin@localhost:~$
ls /etc/pp
ip-down.d ip-up.d
sysadmin@localhost:~$
ls /etc/ppp /etc/ssh
Options
-l
option of the ls
cmd results in a long listing, providing additional info about the files that are listedl
for long or r
for reverse
sysadmin@localhost:~$
ls -lr
- Single-letter options are preceded by a single dash - character, like the
-h
option- Full-word options are preceded by two dash -- characters like the full-word from of the
-h
option, the--human-readable option
Commands
options
and arguments
- Options are used to modify the core behavior of a command
- Arguments are used to provide additional info (such as filename or a usrname)
command [option] [arguments]
Command History
To view the entire history list of a terminal, use the history
command:
sysadmin@localhost:~$
history
- date
- ls
- cal 5 2030
- history
If the desired command is in the list that the history
cmd generates, it can be executed by typing an exclamation point ! character and then the number next to the command (i.e. !3
)
If the history command is passed a number as an argument, it outputs that number of previous commands from the history list
sysadmin@localhost:~$
history 3
- date
- ls /home
- history 3
to execute the most recent command type !! and hit Enter
!command
and hit EnterVariables
local
and environment
Local Variables
Local
or shell
, variables exist only in the current shell. When the user closes a terminal window or shell, all of the variables are lost
variable=value
the following example creates a local variable named variable1
and assigns it a value of something
sysadmin@localhost:~$
variable1=ssomething
to display the value of the variable, use a dollar sign $
charcter followed by the variable name as an argument to the echo
cmd:
- echo $variable1
Something
Environment Variables
Environment variables
, also called global variables, are available system-widePATH
, HOME
, and HISTSIZE
variables the cmd in the example below displays the value of the HISTSIZE
variable:
sysadmin@localhost:~$
echo$HISTSIZE
(output)
The env
cmd outputs a list of the environment variables.
the export
cmd is used to turn a local variable into an environment variable
sysadmin@localhost:~$
export variable1sysadmin@localhost:~$
env | grep variable1
variable1=Something
Exported variables can be removed using the unset
command
Path Variables
PATH
variablePATH
variable lists all the places that the system can look for programs to executethe following command displays the path of the current shell:
sysadmin@localhost:~$
echo $PATH
/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/sbin:/bin:/usr/
If the command is not found in any directory listed in the PATH
variable, then the shell returns a cmd not found
error
Command Types
type
command can be used to determine information about command types
- Interal commands
- External commands
- Aliases
- Functions
External Commands
the which
cmd searches for the location of a command by searching the PATH
variable
sysadmin@localhost:~$
which ls
/bin/lssysadmin@localhost:~$
which cal
/usr/bin/cal
External commands can be executed by typing the complete path to the command
sysadmin@localhost:~$
/bin/ls
Desktop Docs Downloads Music (etc)
type
cmd displays the location of the cmd:-
sysadmin@localhost:~$
type calcal is /usr/bin/cal
-a
option to the type
cmd
sysadmin@localhost:~$
type -a echo
Aliases
ls -l
is commonly aliased to l or llalias
cmd:
sysadmin@localhost:~$
alias
type
command can ID aliases to other commands
sysadmin@localhost:~$
type ll
ll is aliased to 'ls - alF'
Internal Commands
- i.e. the
cd
cmd is a part of the bash shell
type
command IDs the cd command as an internal cmd
sysadmin@localhost:~$
type cd
cd is a shell builtin
Functions
Functions can also be built using existing commands to:
- Create new commands
- Override commands built-in to the shell or commands stored in files
Aliases and Functions are normally loaded from the initialization files when the shell first starts
Control Statements
- Semicolon -
;
- Double ampersand -
&&
- Double Pipe -
||
sysadmin@localhost:~$
cal 1 2015; cal 2 2015; cal 3 2015
&&
acts as a logical "and" if the first cmd is successful, then the second command (to the right of the &&) will also run:
sysadmin@localhost:~$
ls /etc/xml && echo success
catalog catalog.old xml-core.xml xml-core.xml.old
success
sysadmin@localhost:~$
ls /etc/junk || echo failed
ls: cannot access /etc/junk: No such file or directory
failed
Quoting
Backslash Character
\
$1
and $PATH
are not variables:
sysadmin@localhost:~$
echo "The service costs $1 and the path is $PATH"
$PATH
treated as a variable and $1
not?
- in this case. Use the backslash
\
char in front of the dollar sign$
char to prevent the shell from interpreting it
sysadmin@localhost:~$
echo The service costs \$1 and the path is $PATH
Single Quotes
sysadmin@localhost:~$
echo The car cost $100
The car cost 00sysadmin@localhost:~$
echo 'The car cost $100'
That car cost $100
Backquotes
Note the output of the echo Today is date
command line:
sysadmin@localhost:~$
echo Today is date
Today is date
To execute the date
cmd so the output of that cmd is sent to the echo cmd, put date cmd inside of two backquotes
sysadmin@localhost:~$
echo Today isdate
Today is Mon Nov 21 08:58:53 CST 2022
Double Quotes
- Glob char, aka Wild Cards, are symbols that have special meaning to the shell (i.e
*
,?
)
sysadmin@localhost:~$
echo "the glob character *, ? and [ ]"
the glob characters are *, ? and [ ]
PATH
variables)Finding Commands and Documentation
whereis
cmd.gzip
, resulting in a filename that ends in .gz
Info Documentation
Man Pages
man pages
(aka manual page)Additional Help
Man Page Sections
- NAME: provides the name of the command and very brief description
ls - list directory contents- SYNOPSIS: Provides examples of how the command is executed
ls [option]... [file]...- DESCRIPTION: Provides a more detailed description of the command
- OPTIONS: Lists the options for the command as well as a description of how they are used
- FILES: Provides are more detailed desc of the cmd
- AUTHOR: Provides the name of the person who created the man page and (sometimes) how to contact the person
- REPORTING BUGS: Provides details on how to report problems with the command
- COPYRIGHT: Lists the options for the command as well as description of how they are used
- SEE ALSO: Provides you with an idea of where you can find additional info. This often includes other commands that are related to this command
Searching Man Pages
Pattern not found (press Return)
.n
. To return to a previous match of the term, press N
Viewing Man Pages
man
command
- man
command
ls
cmd:
sysadmin@localhost:~$
man ls
man
cmd uses a pager
to display documents, Usually, this pager is the less
command, but on some distributions, it may be the more
command. Both are very similar in how they performMan Page Categorized by Sections
To organize all of these man pages, they are categorized by sections.
- General Commands
- System Calls
- Library Calls
- Special Files
- File Formats and Conventions
- Games
- Miscellaneous
- Sys Admin Commands
- Kernel Routines
The man
cmd searches each of these sections in order until it finds the first match
cal
cmd belongs to the first section of man pages:
sysadmin@localhost:~$
man -f passwd
passwd (5) - the password file
passwd (1) - change user password
passwd (lssl) - compute password hashes
sysadmin@localhost:~$
man 5 passwd
Find Any File or Directory
locate
cmdThis cmd searches a database of all files and directories that were on the system when the database was created
- However, any files created that day will not be searchable with the
locate
cmd because the database is updated nightly- It is possible to update the
locate
database manually by running theupdatedb
cmd as root.
The output can be quite large so it may be helpful to use the following options:
- The
-c
option to thelocate
command will list how many files match:
sysadmin@localhost:~$
locate -c passwd- The
-b
option only includes listings that have the search term in thebasename
of the filename. to limit the output even further, place a \ character in front of the search term:
sysadmin@localhost:~$
locate -b "\passwd"
Info Documentation
To display the info documentation for a command, use the info
cmd
sysadmin@localhost:~$
info ls
You can navigate the document using the arrow keys
Navigating Info Documentation
Viewing Info Documentation
Man pages are excellent sources of info, but they do tend to have a few disadvantages:
- Each man page is a separate document, not related to any other man page
- Man pages may can be difficult to read
The info
cmd also provides documentation on OS cmds and features
- Info documentation as a logical organizational structure, making reading documentation easier.
- Info is broken down into categories that work much like a table of contents in a book
Consider man pages to be more of a reference resource and info documents to be more of a learning guide
Using the Help Option
--help
option to the commandAdditional System Documentation
Paths
/home/sysadmin
is a path to the home directoryListing Files in a Directory
ls
(list) command is one of the most powerful tools for navigating the filesystem
- ls [option]... [file]...
ls
command is used to display the contents of a directory and can provide detailed info about the filesls
cmd list the files in the current dirls
command can also be used to list the contents of any directory in the filesystem, provided the path to the dir as an argumentDirectory Structure
To view the contents of the root Dir, us the ls
cmd with the / char as the argument
sysadmin@localhost:~$
ls /
Notice that there are many directories with descriptive names including /boot, which contains files to boot the computer
Current Directory
pwd
cmd can be used to determine where the user is currently located within the filesystempwd
command prints the working directory, which is the current location of the user within the filesystem
- pwd [OPTIONS]
sysadmin@localhost:~$
pwd
/home/sysadmin
Changing Directories
To navigate the filesystem, use the cd
cmd:
sysadmin@localhost:~$
cd Documentssysadmin@localhost:~/Documents$
After changing directories, the new location can also be confirmed in the new prompt, again shown in blue
cd
cmd will take the user to their home dir ~
no such file or directory
Home Directory
sysadmin
would have a home dir called /home/sysadmin
Paths - Shortcuts: The ..
Characters
Art
dir back to the School
dir:
sysadmin@localhost:~/Documents/School/Art$
cd ..sysadmin@localhost:~/Documents/School$
sysadmin@localhost:~/Documents/School$
cd ../../Downloads
Relative Paths
sysadmin@localhost:~/Documents/$
cd School/Artsysadmin@localhost:~/Documents/School/Art$
Paths - Shortcuts: The .
Characters
.
char always represents the current directoryAbsolute Paths
/home/sysadmin
is an absolute path; it tells the system to:
- Begin at the root / directory > move into the
home
directory > then into thesysadmin
dir
/home/sysadmin
is used as an argument to the cd command, it moves the user into the home directory for the sysadmin
user
sysadmin@localhost:~/Documents$
cd /home/sysadminsysadmin@localhost:~$
Human Readable Sizes
Listing Directories
ls -d
is used, it refers to the current directory, and not the contents within it.
represents the current directoryls -l
command lists the contents of the dirls -d
command in a meaningful way requires the addition of the -l
option Long Display Listing
-l
option to the ls
command to view this info
- each output line describes the metadata about a single file
Recursive Listing
-R
option to the ls
command/etc/ppp
dir were listed first and the files in the /etc/ppp/peers
dir were listed afterListing Hidden Files
ls
command omits hidden files by default-a
option to the ls
cmd
- i.e., the
.bashrc
file in the home directory customizes features of the shell
Sort a Listing
-S
option (capital letter s
)-S
option works by itself, it is most useful when used with the -l
option so the file size are visible-h
option to display human-readable file sizes-t
option sorts files based on the time they were modified-r
option. it can be used alone or combined with either the -S
or -t
optionsls -l
command:
- File Type:
-rw-r--r-- 1 root 17869 Mar 14 17:48 alternative.log
drwxr-x--- 2 root adm 4096 Mar 14 17:48 apache2
- The first character of each line indicates the type of file
d - directory - a file used to store other files-
- regular file - Includes readable files, images files, binary files, and compressed files
l - symbolic link - points to another file
s - socket - allows for communication between processes
p - pipe- allows for communication between processes
b - block file - used to communicate with hardware
c - character file - used to comminate with hardware
Permissions:
*drwxr-xr-x* 1 root root 0 Apr 11 21:58 upstart
- The next nine characters demonstrate the permissions of the file
- Permissions indicate how certain users can access a file
Hard Link Count:
-rw-r----- *1* syslog adm 23621 Aug 23 15:17 auth.log
- This number indicates how many hard links point to this file
- Every file is owned by a user account
- this is important because the owner has the rights to set perms on a file
- Indicates which group owns this file, this is important because any member of this group has a set of perms on the file
- Size of files in bytes
- Indicates when a file's contents were last modified
- The name of the file or dir
Globbing
- Instead of manipulating a single file at a time, you can easily execute commands that will affect many files
Copy Files and Directories
Moving Files
mv
commandmv [source] [destination]
Permission denied
" error messageCreating Files and Directories
Removing Files and Directories
Brackets [ ]
/etc/[gu]*
will print any file that begins with either a g
or u
char and contains zero or more additional char-
characterecho /etc/[a-d]*
Exclamation Point !
echo /etc/[!a-t] will display any file that does not begin with an
athru
t`The Question Mark ?
/etc
directory that begin with the letter t and have exactly 7 characters after the t characterecho /etc/*.???
commandListing With Globs
ls
command sees a directory as an argument, it will display the contents of the directory, not just the directory nae-d
option, which tells ls
cmd to display the name of the dirs, instead of their contentsThe Asterisk *
/etc
dir that begin with the letter t:sysadmin@localhost:~$ echo /etc/t*
/etc/terminfo /etc/timezone
t*
matches any file in the /etc directory that begins with the character t
followed by zero or more of any characterCopying Files
cp
cmd is used to copy files. It requires a source and a destinationcp [source] [destination]
/etc/hosts
file to your home dircp /etc/hosts ~
Verbose Mode
-v
option will cause the cp
command to produce output if successful-v
option stands for verbose-v
option used with the cp
command:cp -v /etc/hosts ~
/etc/hosts' -> '/home/sysadmin/hosts
Avoid Overwriting Data
cp
command can be destructive to existing data if the destination file already exists-i
(interactive) option, the cp
will prompt before overwriting a file y
or n
-i
option requires you to answer y
or n
for every copy which could be tedious if there are a lot of filesn
to each prompt, use the -n
option. It essentially stands for "no rewrites"Copying Directories
cp
command to copy directories will result in an error message-r
(recursive) option to the cp
command will have it copy both files and directories
- Be careful with this option, entire dirs will be copied.
Additional mv Options
cp
cmd, the mv
cmd provides the following options
-i
-- interactive: Ask if a file is to be overwritten-n
-- No Clobber: Do not overwrite a destination files' contents-v
--Verbose: Show the resulting move
-r
option as the mv
command will by default move directoriesRenaming Files
mv
command is not just used to move a file, but also to rename a filemv newexample.txt myexample.txt
Creating Files
touch
cmd as demonstarted belowMaking Directories
mkdir
commandDeleting Files
rm
commandrm
could cause problems when deleting multiple files by using glob characters-i
option when deleting multiple filesDeleting Directories
rm
cmd can be used to delete directories. However, the default usage (no options) of the rm
cmd will fail to delete a dir-r
option to the rm
cmd-i
option with the rm
commandCompressing Files
- Lossless: No info is removed from the file
- Lossy: Info might be removed from the file
Archiving Files
tar
, which is a short form of TApe aRchive
- Create: Make a new archive out of a series of files
- Extract: Pull one or more files out of an archive
- List: Show the contents of the archive without extracting
Zip Files
zip
is to add files to an archive and compress it
zip [options] [zipfile [file...]]
i.e.zip alpha_files.zip alpha*
tar
does), so you must use the -r
option to indicate recursion is to be used-l
list option of the unzip
cmd lists files in .zip archivestar
, you can pass filenames on the command linegzip
.
- The og size of the file called
longfile.txt
is 66540 bytes- The file is compressed by invoking the
gzip
command with the name of the files as the arguments- After that command completes, the original file is gone, and a compressed version with a file extension of
.gz
is left in its place- The file size is now 341 bytes
gzip
command will provide this info, by using the -l
optiongunzip
cmd or the gzip -d
cmdgunzip
does it work, the longfile.txt
file is restored to its og size and file nameCreate Mode
tar -c [-f Archive] [options] [files...]
tar
command requires two named options
-c
-- Create an archive-f Archive
-- Use archive file. the argumentarchive
will be the name of the resulting archive file
gzip
on the archive or by having tar
do it with the -z
optionbzip2
compression can be used instead of gzip
by substituting the -j
option for the -z
option and using .tar.bz2
, .tbz
, or .tbz2
as the file extensionList Mode
tar -t [-f archive] [options]
tar
archive, compressed or not, you can see what's in it by using the -t
optiontar -tjf folders.tbz
- this examples uses
-t
-- List the files in the archive-j
-- Decompress with the bzip2 cmd-f
archive -- operate on the given archive
Extract Mode
tar -x [-f archive] [options]
-x
option once it's copied into a different directorytar -xjf folders.tbz
click to edit