Using essential tools

New Page

Introduction

Free RHEL 8 License

https://developers.redhat.com/

Installing RHEL in VirtualBox

  1. Create the virtual network.
  2. Setup the hardware:
    • 2 CPU cores
    • 4 GB RAM
    • 20 GB disk
  3. Enable the 2nd network adapter ("Host Adapter").

Access to System

ssh user@machine-addr

Commands

Linux is case-sensitive.

Documentation is usually located here:

/usr/share/doc

Useful commands:

tty

Know what console is in use.

hostnamectl

Information on the host.

ip addr help
man ip

Press q to exit the manual page.

ip -4 addr show
ip -a
ss -ntl

Show listening TCP sockets.

ls /usr/share/doc/

Vagrant

mkdir -p vagrant/rhel8
cd vagrant/rhel8

vagrant init generic/rhel8
vagrant up
vagrant ssh

Register RHEL:

sudo subscription-manager register --username <username> --password <password>

Power off the VM:

vagrant halt

Text Files

touch

Create a file:

touch file1

Check the file type:

file file1

Write text to a file:

echo "hello" > file1

Shell Redirection

STDOUT (Standard Output)

Non-error output redirected with:

Example:

ls -l /etc/hosts /etc/hosts > log

STDERR (Standard Error)

Error output redirected with:

Example:

ls -l /etc/hosts /etc/Hosts 2> log

STDOUT and STDERR Combined

Redirect both output streams:

Example:

ls -l /etc/hosts /etc/Hosts &> log

HEREDOCs

Multiline text files:

cat > story.txt <<END
This is line 1
This is line 2
This is line 3
END

tee

Using the command tee output is sent both to the console and the file.

sudo echo "1.0.0.1 cloudflare" >> /etc/hosts # /etc/hosts : Permission denied

it fails since redirection is working at the shell level under the context of the user that started the shell

sudo echo "1.0.0.1 cloudflare" >> /etc/hosts # Fails as redirection from shell
echo "1.0.0.1 cloudflare" | sudo tee -a /etc/hosts # Works

Text editors

nano

Simple to use

sudo dnf repolist
sudo dnf install vim nano bash-completion
nano ./story.txt

vim

Powerful but need to be learned

vimtutor
vim ./story.txt

Normal mode

Editing mode

i to append before the cursor
a to append after the cursor
Shift + I to append at the start of the line
Shift + A to append at the end of the line

Esq brings back to Normal mode

Ex mode

Save or quit

Esq : x to save
Esq : q! to exit without saving

Changing directories

pwd # /home/bryan - print the working directory
cd /usr/share/doc # to move to the directory
cd # bring to the home directory
cd - # bring to the previous directory

mkdir my_directory # create a directory (need to have write permission)
mkdir ~/my_directory # create a directory at the specified location

mkdir -p parent_directory/child_directory # allow creation of a parent directory

cd ESC+. # shortcut keys to recall last argument

rmdir parent_directory # fails because not empty
rm -rf parent_directory # remove recusivly 

Files Operation

# cp copy a file (need read permission and write permission where you want to copy)

cp /etc/hosts .

# mv move or rename (both need write)

mv hosts hosts.old

# rm remove (need write in the directory where the file is)

rm hosts.old

Targeting a file

" * " refers to any character and any number of those characters

" ? " refers to any character but only one instance

touch files{1..12} # Create 12 files from files1 to files12

ls files* #return files1 to files12

ls files? # return files1 to files9
rm files? # remove files1 to files9

ls files?? # return files 10 to files 12
rm -i files?? # remove files 10 to files 12, -i let verify your interaction

Reading text file

cat

List the complete file.

cat /etc/hosts

head

Read top 10 or any specified number of lines of a file.

head -n2 /etc/passwd

tail

Read bottom 10 or any specified number of lines of a file.

tail -n2 /etc/passwd

less

Page through a file (used in man).

wc -l /etc/services 
# wc : word count
# wc -l : line count

less /etc/services

grep

Search through text in a file.

sudo grep Password /etc/sshd_config

sudo grep ^Password /etc/sshd_config # ^ : lines startting with

sudo wc -l /etc/sshd_config # return ~ 145 lines
sudo grep -vE '^(#|$)' /etc/sshd_config | wc -l
# -v : don't show me
# E : enhanced application
# '^(#|$)' : line that start with # OR $
# return ~ 16 lines