Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Computer System Administration

(ECC 4209)

Lecture 2
(Introduction to Linux)

sjh@upm.edu.my
Contents
1. What makes Linux different?
2. Basic survival skills
3. Getting help
What makes Linux different
from other operating systems?
• Linux is free, easier than other OSs to install exactly
where and when needed for any use you can imagine.
• Linux makes it possible to do various really useful and
creative things.
• Linux was built with the same technology and comes with
most of the same tools as the deeply mature UNIX OS.
• Linux distributions also provide sophisticated software
package management systems that reliably install and
maintain any applications available through online
repositories.
What makes Linux different from
other operating systems? (cont)
• But beyond free, Linux is open source, which means
anyone can take the code base and reshape it into
anything they want
• A distribution (sometimes shortened to distro) is a
customized stack of software that’s packaged along with
the Linux kernel and distributed with tools for installing a
working version of Linux of user computers
Linux Distributions
(www.distrowatch.org)
Purpose Distro
Security/anti-hacking Kali Linux
Parrot
Consumer desktop Mint
Elementary OS
Lightweight Puppy Linux
(old hardware; diagnostics) LXLE
Internet of Things administration Snappy Ubuntu Core

Enterprise server room Rock Linux (community version of RedHat)


OpenSUSE (community version of SUSE)
Cloud computing Amazon Linux (AWS AMI)
Ubuntu Server (AWS AMI)
All-purpose (except lightweight) Ubuntu
Functional Declarative NixOS (Nix package manager)
(State-of-the-Art) GNU Guix (Guix package manager)
Embedded System OpenWRT (router gateway)
Yocto project (meta distro)
Basic Linux Survival Skills
• UNIX Filesystem Hierarchy Standard or FHS (including
pseudo file systems)
• Navigation (ls, pwd, and cd), file management tools (cat,
less, touch, mkdir, rmdir, rm, cp, and mv)
• Some tricks (like tab completion and file globbing), sudo,
and where to turn for help (man, info, and journalctl)
Linux Installation
• Many choice of installing Linux:
– Install hard disk (can be dual-partitioned with Windows/macOS)
– Install WSL2 (Windows Sub-system Linux on hypervisor
alongside Windows)
– Install on VM (preferred method)
• You can also use ready made VM
– https://www.osboxes.org
• Live Linux (not recommended – not installed)
– Live CD
– USB
Linux File System
• File system is a data table (index) that creates apparent
connections between individual files and groups of files
with identifiable locations on a disk (see Figure on
Directory Tree)
• A file system uses such an index to provide the
appearance of an organized set of directories and files
within a single disk division known as a “partition”
• Most commonly used Linux file system is ext4 but there
others for example ZFS and Btrfs (these include volume
manager)
• But Linux can also work with storage drives that were
formatted using file systems from other platforms like
FAT32 and NTFS (e.g WSL)
Linux Directory Tree

Directory tree
representation
of data files

Data stored on disk


Linux File System

(continue)
All the files in a disk partition are kept in directories
beneath the root directory (folder), which is represented by
the / (forward slash) character.
• The way these directories are arranged is largely governed
by the UNIX Filesystem Hierarchy Standard (FHS), similar
to other UNIX and macOS (Figure)
• Top-level directories (directories located directly beneath
the main root or / ), include
– /etc, which contains configuration files that define the way
individual programs andservices function
– /var, which contains variable files belonging to the system or app
– /home directory where individual users are given directories for
their private files
• Do not confuse between main root, / , and /root
directory (for root or admin user home directory)
Common top-level directories as defined by the UNIX FHS

Root

Program configuration files

Frequently changing content


(eg. log files)

User account files

System binary files

User binary files

Shared libraries

Third-party binaries
Linux Navigation Tools
• ls
– List directory contents
– l flag (l stands for long) lists not only the object names,
but their file permissions, owner, group, file size, and
time stamp.
– Adding a directory designation like /var displays the
contents of that directory
• ls -l /var
• ls -lh /var (display file size in human readable format)
– Note that short form equivalent for command i.e same
output (see online manual)
• ls -h
• ls --human-readable
– Can display directory and sub-directory
• ls -R
Linux Navigation Tools
(continue)
• pwd
– Print Working Directory (i.e path to current directory)
– Show current directory
• cd
– Change directory
– Change into other directory via absolute or relative path
– Relative path
• cd (take you back to the home user’s directory)
• cd .
• cd ..
– Absolute path
• cd /
• cd /home/yourname (same as cd)
Linux Navigation Tools
(continue)
• cat
– Output file contents into terminal
• cat /etc/fstab
• cat /etc/group
– Short from concatenate
• cat can join multiple strings or files into a single text stream
• less
– Output file contents into terminal, with scrolling feature
– Output file contents into terminal, with scrolling feature
– can scroll up and down through the file with the arrow,
PgUp, PgDn, and spacebar keys
– When done, press the q key to exit
– less /etc/services
Bash shell

Shell
(Interprets commands)

Operating System Software


(Processes commands using kernel resources)

Kernel

Hardware
Linux File Management Tools
• need to know how to create, destroy, move, and
copy files
• touch updates its time stamp without making any
• changes
– $touch myfile
– $cat myfile
– $nano myfile
– $vim myfile
• Note that nano is modeless editor and vim is a modal editor
– Vi/Vim is one of the popular Unix/Linux editor in addition to Emacs
– enter Insert Mode by pressing the i key and then typing your text
– save your file by pressing Esc, then type :w, and then exit by:q
– type :wq for save and exit at the same time
Creating and Deleting Directories
• Every object within file system is represented by a
unique collection of metadata called an inode
– file system is built from the metadata associated with all
the many inodes
– object used by UNIX systems to identify the disk
location and attributes of files within a file system
– Usually one inode for each file or directory.
– $stat myfile (display more information including
inode information)
• Output includes data describing the file’s name,
attributes, time stamps and inode ID number
• When move, copy, or delete a file or directory, all is
really editing its inode attributes, not its ID
Creating and Deleting Directories
(continue)
• Create a new directory that you can use
– $ mkdir myplace
• Move to your new directory and create a file there by
cd myplace and touch newfile
• Move back up to parent directory by cd .. and
delete the new directory by rmdir myplace
– rmdir: failed to remove 'myplace':
Directory not empty
– This is a built-in check to prevent you from accidentally
deleting directories
– To get around this
• --ignore-fail-on-non-empty argument to the rmdir
command
• rm -r myplace (add the -r flag, meaning recursive)
Copying And Moving Files
• Create a few more files and a new directory:
– $ touch file1 file2 file3
– $ mkdir newdir
• You can make an identical copy of an object
using cp. This example creates a copy of
– file1 within the directory newdir:
– $ cp file1 newdir
• cp command recognize newdir as a directory
rather than a file.
– If no directory newdir in the current location, cp would
instead make a new copy of file1 named newdir
Copying And Moving Files
(continue)
• mv command will permanently move an object
from one place to another.
– $ mv file2 newdir
– original file would no longer be available
• Copy, move, or delete directories using the same
commands as for files, adding the -r flag where necessary
• Potential to move more than just the directory you see and
any existing layers of unseen nested levels will also be
dragged
– always check your output or results of command
File Globbing
• Globbing (derived from the word global)
– applying wildcard characters to the filenames
addressed by commands
• To move all the contents of the current directory
to some other location:
– $ mv * /some/other/directory/
• To move all files whose names begin with the
letters file, but leaves everything else untouched:
– $ mv file* /some/other/directory/
• To move files with names file1, file2, file3 …, file15 and
wanted to move only those between file1 and file9, you’d
use the question mark (?) instead of the asterisk:
– $ mv file? /some/other/directory/
Deleting Files
• Files and objects can be deleted using rm.
– Note that operations are irreversible
– $ rm file1 (delete file1 from the directory)
• File globbing can be applied to rm the same way
as to cp or mv, and with the same efficiency
– $ rm file* (deletes all files in the current directory
whose names begin with the letters file)
• Adding the -r argument to a delete operation will make
the action recursive and delete the contents of any
subdirectories in the specified path:
– $ rm -r *
– very dangerous combination, especially when working
with root authority
Keyboard Tricks
• Cutting And Pasting
– Ctrl-c (copy) and Ctrl-v (paste) key combinations won’t work
but Shift-Ctrl-c and Shift-Ctrl-v will.
– Cut and paste by right-clicking your mouse and selecting the
appropriate operation from the menu

• Tab Completion
– Press the Tab key tells Bash to display its best guess
on the command line
– $ touch my-very-long-filename_66-b.txt
– $ rm my-<tab>
Pseudo file systems
• A normal file is a collection of data that can be
accessed over even after a system reboot.
• Contents of a Linux pseudo (or virtual) file, do not
exist in the normal sense
– /sys and /proc directories
– pseudo file’s contents are dynamically generated by
the OS itself to represent specific values
• Use cat to display of bytes on the disk,
designated by the system as sda:
– $ cat /sys/block/sda/size
– 1937389568
– common source of information and data
all system processes rely including GUI
– Examples top, htop
Pseudo file systems
(continue)
• For example howto know your drive designation?
• Linux organizes attached storage as block
devices
– Check contents of /sys/block directory
– $ cd /sys/block
– $ ls
– loop0 loop1 loop2 sda sr0
Sudo
• For practical reasons, using an OS account that
enjoys full administration powers is risky
• But fully restricting to non-administration account
makes it impossible to get anything done
• Linux solve this problem by providing selected
accounts with admin authority using sudo
• Once you confirm your identity by providing your
password (not root password), your command will
be treated as though it was issued by the root user
– $ cat /etc/shadow
– cat: /etc/shadow: Permission denied
– $ sudo cat /etc/shadow
– [sudo] password for ubuntu:
• Please note that non-root user shell prompt is
Getting Help
(man files)
• man files (or manual files) are installed together
with any software being installed on Linux
– $man man
– $man ls
• man files contents include:
– NAME (of the installed software)
– SYNOPSIS (detailed syntax overview)
– DESCRIPTION (details explanation includes a list of
command-line arguments and flags)
– EXAMPLES (typical usages examples)
– SEE ALSO (related command and functions)
• man files is written in the vi/vim format for reading,
navigation, etc
Getting Help
(man files - continue)
Section Descriptions

1 General commands
2 System calls

Library functions, covering in particular the C


3
standard library

Special files (usually devices, those found in /dev)


4
and drivers

5 File formats and conventions


6 Games and screensavers
7 Miscellaneous

8 System administration commands and daemons


Getting Help
(info)
• Info is the alternative and more interactive help
• Content is arranged alphabetically by topic with
like Basics and Compression (see Figure)
• You can use the up and down arrow keys to scroll
between lines; and, press Enter into topic’s page.
– for example for File Permissions is under Basics
– menu section indicates that the lines that follow are
links to more pages another level down
– u key will take you back up one level
– pressing q will exit
• Install info if not already installed
– sudo apt install info
Getting Help
(info - continue)
Getting Help
(Internet)
• Most of problems are rather common and most of
the sysadmins have solved them
• Many of the solutions were the result of reaching
out for help in an online community forum lik
– link:serverfault.com
– link:linuxquestions.org/questions
• Typing “my server crashed” in the search field not
be very useful, ths need more details
– What kind of server is it: an Apache web server?
– Did any error messages appear in your browser?
– Did the crash generate any log entries?
Getting Help
(Internet - Error info from system logs)
• Getting error information from system logs
– # journalctl | grep filename.php
– pipes the output of journalctl to the grep filter
– pipe symbol “|” is the Shift-\ combination
• Use grep in sequence to narrow your results further
– contain the word error
• # journalctl | grep filename.php | grep error
– Do not contain the word error
• # journalctl | grep filename.php | grep -v error
Getting Help
(Internet - Searching the Internet)
• Suppose output from journalctl includes
– [Fri Oct 12 02:19:44 2017] [error] [client
54.208.59.72]
– ➥ Client sent malformed Host header
– No point searching the internet for the date stamp or IP
address
– Search for “Client sent malformed Host header”
• Another way to minimize false positives is to ignore
pages containing a particular string
– writing scripts -movie
– excludes Hollywood results
Summary
• Just about any Linux command-line operation will
make use of some or all of five basic tools: ls, pwd,
cd, cat and less
• Linux uses pseudo file systems to expose data on
the hardware environment to processes and users
• Authorized users can invoke sudo to gain
administration permissions for individual commands
• There’s a great deal of documentation and other
help available through the man system, info, and
online search
Key Terminologies
• File system is made up of data files indexed in a way that allows the
perception of a directory-based organization
• Process is an active instance of a running software program
• Disk partition is the logical division of physical storage device that can
be made to work exactly like a standalone device
• Partitions are common organizational tools for all modern OS
• Bash is a command-line UI for executing system actions
• Plain text that is usable for administration purposes is text made up of
a limited set of characters and without extraneous formatting code
• File globbing involves using wildcard characters to refer to multiple
files with a single command
• Tab completion employs the Tab key to suggest possible completions
of a partially typed command
• Pseudo file systems are directories containing files with dynamic data
automatically generated at or after system boot
Command-line review
• ls -lh /var/log lists the contents and full,
human-friendly details of the /var/log/ directory
• cd, by itself, returns you to your home directory.
• cp file1 newdir copies a file called file1 to the
directory named newdir
• mv file? /some/other/directory/ moves all files
containing the letters file and one more character to
the target location
• rm -r * deletes all files and directories beneath the
current location. Use rm command with great care.
• man sudo opens the man documentation file on using
sudo with commands
References
• Linux in Action, David Clinton:
– https://www.manning.com/books/linux-in-action
• Learning Modern Linux, Michael Hausenblas:
– https://www.oreilly.com/library/view/learning-moder
n-linux/9781098108939/
• Linux Administration Best Practices, Scott Alan
Miller:
– https://www.packtpub.com/product/linux-administra
tion-best-practices/9781800568792

You might also like