RHCSA Course
RHCSA Course
PERMISSIONS
chgrp <name of the group> <filename/Dir name> #change the group
of a file
chown <username> <filename> #change the ownership of a file to a
new user
chwon <username>:<groupname> <filename> #changes the user
and group of the file
chmod u+w <file_name or file/dir path> #set permission to a file for
the owner
chmod g+w <file_name or file/dir path> #set for group (chmod
g+rwx)
chmod o+r <file_name or file/dir path> #set for others
chmod u-w, g-r, o-x to remove permissions
find [dir/file path] [parameter, eg –name, -size] #search for file
find /home/darl –iname books #will find files named books with non-
case sensitive
find cmin -5 #file metadata changed in the last 5 mins
find mmin -5 #files modified in the last 5minutes
find mmin +5 #files modified in the last 5 minutes and beyond
find mmin 5 #files modified at exactly 5 minutes in the past
find –name ‘f*’ –o –size +5M #find files either beginning with f or larger
than 5M
find –not –name ‘f*’ or find \! –name ‘f*’ # will find files not beginning
with letter ‘f’
find –perm 664 #find files exactly with this permission
find –perm -664 #atleast
find –perm /664 #any of these permissions
Find files/directories under /var/log/ directory that the group can write to,
but others cannot read or write to it. Save the list of the files/directories
(with complete parent path) in /home/bob/data.txt file.
[bob@centos-host ~]$ sudo find /var/log -perm -g=w ! -perm -o=w ! -perm -
o=r > /home/bob/data.txt
[bob@centos-host ~]$ sudo find /var/log -perm -g=w -not -perm -o=w -not -
perm -o=r > /home/bob/data.txt
[bob@centos-host ~]$ chmod u+s /home/bob/datadir #set uid
[bob@centos-host ~]$ chmod g+s /home/bob/datadir #se gid
[bob@centos-host ~]$ chmod +t /home/bob/datadir #set sticky bit
ARCHIVE
tar -cvf archive.tar file1 #v shows verbose, creates a tar file called
archive.tar for file1
tar -rf archive.tar file2 #appends file2 to the archive.tar
tar -tf archive.tar #always use the tar list cmd to check where your file
will be extracted
Create a file.tar archive of /home/bob/file directory under /home/bob
location.
[bob@centos-host ~]$ tar -cvf /home/bob/file.tar -C /home/bob file
tar -xvf archive.tar –C /tmp #tells tar to extract the file in the /tmp dir
tar -cvzf archive.tar.gz file1 #creates a compressed tar using gzip
tar -cjvf archive.tar.bz2 file1 #bzip tar
Create a bzip archive under bob's home named file.txt.bz2 out of
/home/bob/file.txt, but preserve the original file. At the end of the exercise
you should have both:
bzip2 -k /home/bob/file.txt
Create a compressed tar archive logs.tar.gz (under bob's home) of /var/log/
directory.
[bob@centos-host ~]$ sudo tar -cvzf /home/bob/logs.tar.gz /var/log/
tar cJf archive.tar.xz file1 #xz tar compressed file
tar caf archive.xz file2 #autocompress
Execute /home/bob/script.sh script and save all command output (both
errors/warnings and normal output) in /home/bob/output.txt file.
/home/bob/script.sh > /home/bob/output.txt 2>&1
Execute /home/bob/script.sh script and save all normal output (except
errors/warnings) in /home/bob/output_stdout.txt file
[bob@centos-host ~]$ /home/bob/script.sh
1>/home/bob/output_stdout.txt 2>/dev/null
sort userinfo.text > sorted.txt #one > will over write each input
sort userinfo.text >> sorted.txt #two >> will not overwrite the input
egrep –r ‘^The’ /etc/ 2>/dev/null #redirect all error msg from this cmd
bc <<<1+2+3+4 = 10 #the here string redirects the 1+2+3+4 to the
bash calc bc, instead of typing it on a keyboard
rsync –a /etc/ [email protected] :/home/jane/pictures #-a ensures
subdir and permissions are sync-ed too
sudo dd if=/dev/vda of=diskimage.raw bs =1M status=progress
#creates an image
sudo dd if=diskimage.raw of=/dev/vda bs=1M status=progress
#restores an image
scp [email protected] :/var/log/myremote.log /var/log/mylocal.log
scp [email protected]:<remote_path> <local_path> #secure copy from
remote to local
scp /home/darl/mylocal [email protected]:/home/darl/myremote
scp <local_path> [email protected]:<remote_path> #copy from local to
remote
SCRIPTS
How to create a script
touch script.sh #creates a script file
vim script.sh #open the file in vim, i to edit, esc+:wq to save,
esc+:q! to exit vim, add the following lines to create a log script
#! /bin/bash #must be the first line in the script file
date >> /tmp/script.log #redirects and appends the date info to your
script file
cat /proc/version >> /tmp/script.log #appends the system info to your
script file, esc+:wq to save and exit
chmod u+x script.sh #makes the script executable, u+x means only
the user can run the script
chmod +x script.sh #+x everyone can run the script
./script.sh #to run the script
[ ab ] && [ bc ] #and operator
[ab ] || [ cd ] #or operator
echo $? #is used to get the exit code of a cmd, run it immediately after a
cmd is run
FILESYSTEMS
ls /mnt #/mnt dir used for temporarily mounting a random file systems
sudo mount /dev/sda1 /mnt #used to mount the file system
sudo umount -l /mnt #force unmount a file system mounted on /mnt
Superuser (root account): UID 0
System accounts (sshd, mail): home dir not under /home: UID < 100 and
500-100
Service account: (nginx)
Su <username> #to switch to another user
Su –c <cmd> #to run a cmd as root
Cat /ect/sudoers #only users listed here can use sudo
#to search for cmds that you can’t remember
Apropos <brief cmd phrase> eg apropos <director>
Su mandb
#to check local users or users logged on a system
W
Who #all logged in user
Cat /etc/password
Cat /etc/group #check groups
Red Hat removed the Docker container engine and the docker command
from RHEL 9.
#to confirm ownership of a directory
ls -ld /path/to/directory
#to check if a directory exist
test -d /path/to/directory && echo "Directory exists" || echo
"Directory does not exist"
# to navigate to localhost Home directory where you have desktop,
downloads, documents etc access it from the localhost session eg
[darl@localhost]# not from [root@localhost]#
Cd ~
#to access the desktop directory
cd ~/Desktop
# to create a directory called Alex in the Home directory
mkdir ~/Alex
#To access directories like Alex created in the user’s personal Home
directory eg like a Users Folder in windows use
cd ~/Alex
#To the general home directory like c drive in windows use, this is the
default home directory in bash
cd /Alex
#to check if an app is installed
rpm -qa | grep <name of the app or service>
#display installation logs
sudo tail -f /var/log/dnf.log
# monitor all package management activity (like apt, dnf, or rpm) by
checking the system journal.
sudo journalctl –f