# New Page

# Introduction

## Free RHEL 8 License

[https://developers.redhat.com/](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

```bash
ssh user@machine-addr

```

## Commands

Linux is case-sensitive.

Documentation is usually located here:

```bash
/usr/share/doc

```

Useful commands:

```bash
tty

```

Know what console is in use.

```bash
hostnamectl

```

Information on the host.

```bash
ip addr help
man ip

```

Press `q` to exit the manual page.

```bash
ip -4 addr show
ip -a

```

```bash
ss -ntl

```

Show listening TCP sockets.

```bash
ls /usr/share/doc/

```

## Vagrant

```bash
mkdir -p vagrant/rhel8
cd vagrant/rhel8

vagrant init generic/rhel8
vagrant up
vagrant ssh

```

Register RHEL:

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

```

Power off the VM:

```bash
vagrant halt

```

# Text Files

## touch

Create a file:

```bash
touch file1

```

Check the file type:

```bash
file file1

```

Write text to a file:

```bash
echo "hello" > file1

```

## Shell Redirection

### STDOUT (Standard Output)

Non-error output redirected with:

- `>` overwrite
- `>>` append

Example:

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

```

### STDERR (Standard Error)

Error output redirected with:

- `2>` overwrite
- `2>>` append

Example:

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

```

### STDOUT and STDERR Combined

Redirect both output streams:

- `&>` overwrite
- `&>>` append

Example:

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

```

### HEREDOCs

Multiline text files:

```bash
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.

```bash
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

```bash
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

```

- tee file1 (overwrite)
- tee -a file1 (append)

## Text editors

### nano

Simple to use

```bash
sudo dnf repolist
sudo dnf install vim nano bash-completion

```

```bash
nano ./story.txt

```

### vim

Powerful but need to be learned

```bash
vimtutor

```

```bash
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

```bash
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

```bash
# 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

- " \* " would target all files
- file\* would target any files starting with "file"

" ? " refers to any character but only one instance

- " ? " would target all files with only 1 character in their name
- "file?" would target any files that has 5 character and start with "file"

```bash
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.

```bash
cat /etc/hosts

```

### head

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

```bash
head -n2 /etc/passwd

```

### tail

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

```bash
tail -n2 /etc/passwd

```

### less

Page through a file (used in man).

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

less /etc/services

```

### grep

Search through text in a file.

```bash
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

```