Friday, 4 July 2014


RHCSA 7 Exam Notes #1: Understand and use essential tools


Word version available here:
https://drive.google.com/file/d/0B9WPh0iDN4KwejAxaVJZS0hTWVk/edit?usp=sharing
Access a shell prompt and issue commands with correct syntax.
Basic Linux Commands:

ls -lh    (human readable format)
ls -a (show hidden files)           

First Character File Type explanation: d=directory,l=symbolic link,p=uni-directional named pipe file,s=bi-directional socket file

pwd                             (Show present working directory)
cd /usr/bin                 (go to directory /usr/bin off root)
cd /                              (go to root directory of filesystem)
cd ~                             (go to users home folder)
cd -                              (switch back to last dir)
cd..                              (move up 1 folder lvl)

who                             (who is logged in)
w                                 (a variation of above)
id                                 (id and context of current user)
groups                         (what groups does current user belong to)
uname -a                     (system info)
hostname                    (fqdn of server)

last                              (user logins, logout and system boot)
last reboot                   (same as above but only shows system boots)
lastb                            (failed logins)

clear                            (clears terminal window)
whereis cat                 (provides path to binary and man pages)

history 17                    (show last 17 commands)
!ch                               (reexecute last command starting with ch)
!82                               (reexecute command number 82 from previous history command)

Use input-output redirection (>, >>, |, 2>, etc.)
Ll > ll.out                     (generates text file we can sort in the next command)
Sort ll.out > sort.out    (creates sorted version sort.out, use >> to append to existing file)

Normally errors and output are piped to terminal window. You can manipulate this as follows:
 
Standard Input=0, Standard Output =1, Standard Error=2
mailx student < /tmp/myball              (mail the user student the contents of file myball)
find / -name cat -print 2> /dev/null  (/dev/null is a black hole)
ls /etc /tmp 1> outputfile 2>&1          (pipes directory listing of two directories to output file with any associated errors)


Use grep and regular expressions to analyze text
grep -i student /etc/passwd /etc/group         (searches for pattern student in passwd & group files and display filename. -i ignores case)
grep -i olives ~/Desktop/*      (searches all files in users home/Desktop folder for text olives)
grep -i ^student /etc/passwd  (searches file passwd for lines beginning with student)
rep nologin$ /etc/passwd     (searches file passwd for lines ending with nologin)
ll /etc | egrep ‘drwx|xin’        (searches ll directory listing output for permission matches)

diff -i -c myball1 myball2       (compares both files and uses 3 sections in output to show differences, can be used on directories also, -i ignores case, -c makes output readable)

While we’re at it what about finding files themselves:
find / -iname ‘myball*’           (case insensitive search starting in / for file starting with myball, will find /root/myball1 & /root/myball2)
find /etc -size +1G                   (find files in /etc over 1Gigabyte in size)
find /usr -user student            (find files owned by user Student)
find /tmp -mtime +90             (find files modified more than 90 days ago; also -90,90,-atime)
find /var -perm 777                (find files with permissions set to 777)


Access remote systems using ssh and VNC
I wish they were all this easy! SSH is a doddle, VNC is almost so if you ignore the painful official documentation instructions!

ssh root@server1.example.com         (connect to remote server1 as user root)

yum -y install tigervnc-server             (“yum -y install vnc” to get the client)
Next, use GUI Menu: Applications,Sundry,Firewall to configure a permanent exclusion for vnc-server
vncserver                                            (Starts VNC Server and prompts for password)

VNC uses ports 5900 for service and 5901+ for each connection. To test, open The Windows Client and put in the IP Address followed by port number “192.168.192.129:5901” for example and enter the password used earlier.
Click Applications / Internet / TigerVNC Viewer to test with the Linux Client


Log in and switch users in multiuser targets
su -                  (switch to root and execute root’s initialization files including PATH variables)
su - lisa            (switch to user Lisa, not space between - and lisa’s username)

To permit users to use sudo run command “visudo” and copy root line as follows:
 
root                 ALL=(ALL)        ALL
student            ALL=(ALL)        NOPASSWD:  ALL
test with sudo firewall-config as an example, you won’t be prompted for user passwd with the option used above for student

Archive, compress, unpack, and uncompress files using tar, star, gzip, and bzip2

tar cvf /tmp/test.tar /var /etc/ntp.conf         (c - creates tarball, v - verbose, f - specify name, creates test.tar in /tmp with contents of /var and the file ntp.conf)

Note: Error “Removing leading ‘/’ from member names" when adding individual files can be worked  around by running the command from the directory where the files you want to archive are.

 
tar rvf /tmp/test.tar /etc/yum.conf                (r - appends to existing uncompressed tarball)
tar tvf /tmp/test.tar                                        (t - shows contents of test.tar tarball, add z or j if compressed)
tar xvf /tmp/test.tar –C /tmp                         (x – extracts contents of test.tar, use -C extract to specified directory)
tar cvfz /tmp/test.tar.gz /etc/yum.conf         (Create tarball and compress with gzip)
tar xvfz /tmp/test.tar.gz                                 (Extract gzipped tarball)
tar cvfj /tmp/test.tar.bz2 /etc/ntp.conf          (Create tarball and compress with bzip2)
tar xvfj /tmp/test.tar.bz2                               (Extract bzipped tarball)
bzip2 is slower but compresses better than gzip. Note: you can’t append to compressed archives.

star is not installed by default but is SELinux aware, sudo yum –y install star-1*
star –h                                                 (to show help or use “man star”)
Command works the same as per TAR but accepts extended options
star cvf /tmp/myball.tar /home         (tvf and xvf options as per tar) gzip /root/myball1 /root/myball2      (gzips both files & appends .gz extension)
gunzip /root/myball1.gz                     (unzips file)
bzip2 /root/myball1 /root/myball2    (create 2 new bzipped2 files with .bz2 extension)
bunzip2 /root/myball1.bz2                 (unzips file)


Create and edit text files
touch myball2   or   vi myball2           (creates new file myball2)
vi has 3 modes, command mode (default) for copy, paste, replace and search etc; input mode for inserting text into file, last line mode for commands starting in :

input mode:
i or I                 (inset test at current position or beginning of current line)
escape to exit input mode

command mode:
dd                    (deletes the current line)
D                      (deletes from cursor position to end of current line)
u                      (undoes last command or U undoes all changes done on current line)
?apples            (searches for work apples, n continues search for next instance)
yy & p              (yy copies current cursor line into buffer, p pastes it below the current line)

last line mode:
:%s:/oldword/newword/gi      (replaces all occurrences of oldword with newword, case insensitive)
:r myball4        (reads file myball4 and inserts it below the current line)
:wq!                 (write all changes and quit)
:wq! myball3   (saves file as myball3)


Create, delete, copy, and move files and directories
mkdir –p /usr/mike/wibbly/wobbly    (creates all directories in one go)
cp –i myball1 myball2            (copies file but -i states to confirm any overwrite)
cp -r folder1 folder2               (copies folder1 & all subdirectories to folder2)
mv -i  myball1 test                  (moves myball1 into subfolder test)
mv myball1 myball2               (renames file)
mv newdir test                        (moves newdir folder into test folder)
mv newdir2 newdir3               (renames folder)
rm deleteme                           (deletes file deleteme)
rm -r test                                 (deletes non empty directory)

Create hard and soft links
Inodes – the glue which points to an individual file or joins two files at the hip
Soft Link – equivalent to windows shortcut
ln –s bigball linktobigball        (creates a pointer file linktobigball which links to file bigball)
ll -i                                           (shows link between files in earlier step, 1st column show inode number which should be different)
Hard Link – associates two files to same inode number
ln bigball myotherbigball       (file have identical permissions, owndership, time stamp and contents. Change one, it changes the other)
ll -i                                           (shows both files now have same inode number in 1st column)

List, set, and change standard ugo/rwx permissions
What’s ugo you may ask? User, Group & Owner permissions. They are assigned combinations of rwx, otherwise called Read, Write and Execute permissions.

-rw-rw-r--. 1 michael michael 39 Jul  4 12:30 myball34
[File type]-[Owner perms]-[Owners Group Member perms]-[Public perms]  # links  File Owner  Owners Group  File Size  Modified Timestamp  Filename

chmod [u/g/o/a(all)] [+/-/=] [r/w/x] myball34
chmod u+w myball34             (gives write permissions to owner)
chmod a=rwx myball34          (gives all permissions to all 3 categories)
decimal method: 0=None, 4=Read, 5=Execute, 6=Read&Write, 7=Read&Write&Execute
chmod 744 myball34              (gives all permissions to Owner and Read to other two)
chmod 770 myball34              (gives all permissions to Owner and Owner Group, None to Public)
chmod o+t /tmp                      (sets sticky bit for public on /tmp to protect against deletion)

umask command displays 0002 and umask –S shows default permissions in rwx format
To calculate take 666 for files -002 gives 664, so that’s R&W for Owner and Owner Group and Read for Public. Use 777 for folders -002 which gives R&W&X for Owner and Owner Group and R&X for Public. To set new defaults such as 640 File & 750 Directory use umask 027 (666-X=640, 777-X=750). Not permanent.

To change the owner of a file use ll –n to show UID and GID and the following command:
chown student2 mybigball
chgrp student2 mybigball
chown student2:student2 mybigball               (does both in one go!)
chown –R student2:student2 /usr/student2   (does folder /usr/student2 and all subfolders in one go)

Locate, read, and use system documentation including man, info, and files in /usr/share/doc
man star                                  (Brings up manual for Star command)
man -k system-config             (Find command using all man pages that match system-config)
man –f host.conf                     (Finds info on file/command)
star –h
cat –help
passwd -?
info star
/usr/share/doc contains folders for all installed packages with various files in each one