Table of Contents

Marc Verhaar 2021/11/02 19:08

====== Uitreksel RHCSA 8 ======

File Maintenance Commands:

Command Function Example
cp Copy file cp file1 file2
rm Remove file rm file2
mv Move file (also rename!) mv file /path/newfile
mkdir Make directory mkdir newdir
rmdir Remove directory (must be empty!) rm newdir
rm -rf Remove file or directory rm -rf /bin
chgrp Change group of file or directory '' sudo chgrp root file 1''
chown Change ownership of file or directory chown user file
chown user:group file
chown -R user: dir

Inode:

This means:

Command Function Example
ln create a hardlink
ln target linkname
ln -s create a soft link
ln -s target linkname
ls -i show files with inode numbers
ls -li

Example:

'''' touch original
echo “some stuff” > original
ln -s original softlink
ln original hardlink

ls -li
46271969 -rw-rw-r– 2 marcv marcv 24 Nov 2 20:54 hardlink
46271969 -rw-rw-r– 2 marcv marcv 24 Nov 2 20:54 original
46271970 lrwxrwxrwx 1 marcv marcv 8 Nov 2 20:54 softlink → original

rm original
ls -l
-rw-rw-r– 1 marcv marcv 24 Nov 2 20:43 hardlink
lrwxrwxrwx 1 marcv marcv 8 Nov 2 20:44 softlink → original (broken link)
''''

Redirecting:

Standard redirecting:

There are 3 redirects in Linux:

  1. stdin: standard input has file descriptor number as 0 (keyboard, mouse, ..?)
  2. stdout: standard output has file descriptor number as 1 (monitor)
  3. stderr: standard error has file descriptor number as 2 (monitor)

We can redirect these output using >, <, and 2>:

Command Redirects
ls -l > output output to “output” (not overrides output to stdout which is terminal)
ls -l » output same but appends to existing content instead of overwriting
'' cat < file'' redirect content of file to cat (quite useless as cat file will do the same)
mail user@here.nl < content using mailprogram, send content
ls -l /root 2> errorfile redirect errors to errorfile (valid output to stdout)

Using redirect in combination with EOF:

Assign multi-line string to a shell variable:

sql=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='baz'
EOF
)

The $sql variable now holds the new-line characters too. You can verify with echo -e “$sql”

Pass multi-line string to a file in Bash:

cat <<EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF

The double “«” is necessary! It does not matter if you use EOF, eof or any other string for closing

The print.sh file now contains:

#!/bin/bash
echo $PWD
echo /home/user

Pass multi-line string to a pipe in Bash:

cat <<EOF | grep 'b' | tee b.txt
foo
bar
baz
EOF

The b.txt file contains bar and baz lines. The same output is printed to stdout.

Pipes (|):

command what it does
ls -ltr | less
feed less with output of ls -ltr
 find . *file | grep name
find files and grep in filenames(!)
 cat file* | grep string
cat files and grep inside content(!) This can also be done using
grep -R string ./*

Getting help:

File editors:

For RHCSA you'll be using vi because it's present on almost all *nix systems.

Common keys in command mode:

User account management:

commands:

files:

Some examples:

Command What it does
useradd -d /homedir -g group username add user, assign to group and define homedir
usermod -a -G wheel user add user to additional group wheel
useradd -r nonuser create system account
useradd -s /bin/zsh user create user and define shell
chage -m 5 user user can change password in 5 days
chage -M 90 -W 70 user user must change password every 90 days and gets warning 7 days prior
chage -I 7 user account will be disabled 7 days after password expires

Switch users and sudo access:

Commands:

Log monitoring:

Log directory is /var/log/ (unless specified other per application)

Accurate system time is critical!

RedHat 8 uses NTP for time synchronization.

Root privileges:

Become root using:

su -

The minus ensures that correct environment (variables) is loaded so don't use su without it. If configured properly (sudo is installed and user is member of group wheel), you can also do:

sudo su -

Setting up a rhel server:

CLI commands:

Understanding the shell:

FHS; File Hierarchy Standard:

01-02-6