You are on page 1of 14

Lesson 02 Using Essential

Tools & Cockpit


Created @December 5, 2021 8:20 PM

Class

Type

Materials

Reviewed

Last Update @March 26, 2022 8:08 AM


Linux Commands
Bash Shell
2.3 I/O Redirection and Piping
2.4 I/O Redirection and Piping
2.5 FHS Linux (File System Hierarchy Structure)
/boot
/dev (devices)
Major and Minor Numbers
/etc
/home
/sbin : Essential System binaries
/usr (like program files in Windows)
/var (for dynamic data like logs)
2.6 Using man
2.7 Finding the Right man page
2.8, 2.9 vim
2.10 Globbing and Wildcards
2.11 Cockpit

Linux Commands
pwd
whoami
ls -l
ip addr show, ip a
free -m
df -h // (disk free, human readable format)

Lesson 02 Using Essential Tools & Cockpit 1


cat /etc/hosts
findmnt // (mounted file systems, tree structure)
useradd
system-machine-id-setup
history // (!11, repoeat history number 11)
!f // run last command starts with "f"
CTRL+R // (start typing part of the command, the shell will complete
the command for you

Bash Shell
// Piping
# ps -aux | less
ps -aux | wc

// Redirection
ls > lsfiles
cat lsfiles
whoami > lsfiles
!cat
ls >> lsfiles
ls qwert // error apears since no file with this name
ls qwert 2> erros.txt // redirect errors to the file errors.txt
ls qwert * 2> errors.txt // will print the output without errors, errors will be re
directed to errors.txt
ls qwert * 2> /dev/null // no errors appear

// Environment Variables
env
env | less
LANG=fr_FR.utf-8
ls --help | less // the man page will appear in French!
LANG=en_EN.utf-8 // change it back to English

// Aliases
alias
alias h=history

2.3 I/O Redirection and Piping


STDIN ————> CMD ————> STDOUT

(Keyboard) (screen)

file < > filename

STDIN ————> CMD ————> STDERR

Lesson 02 Using Essential Tools & Cockpit 2


(Keyboard) (screen)

2> errfile.txt

2.4 I/O Redirection and Piping


>

>> // append

2> /dev/null // Redirecting to SDTERR

< // redirecting STDIN

💡 In piping: STDOUT of the first command is used as STDIN of the second


command

ls /etc > etcfiles.txt


less etcfiles.txt

who > etcfiles.txt


cat /etcfiles.txt
ls >> etcfiles.txt

grep -R student /etc // Recursively look for the text "student"


grep -R student /etc 2> /dev/null // will print only useful output, and redirect
errors to /dev/null

ls -l /etc | wc
ls -l /etc | less
ls -l /etc | grep host

2.5 FHS Linux (File System Hierarchy


Structure)

Lesson 02 Using Essential Tools & Cockpit 3


mount
/ /dev/sda2
|— boot /dev/sda1
|— home server:/home
|— var /dev/sdb
| ....

All these files are spread out over multiple devices.

$ findmnt
TARGET SOURCE FSTYPE OPTIONS
/ /dev/mapper/cl-root
│ xfs rw,relatime,seclabel,a
ttr2,inode64,logbufs=8,logbsize=32k,noquota
├─/sys sysfs sysfs rw,nosuid,nodev,noexe
c,relatime,seclabel
│ ├─/sys/kernel/security securityfs securityfs rw,nosuid,nodev,noexe
c,relatime
│ ├─/sys/fs/cgroup tmpfs tmpfs ro,nosuid,nodev,noexe
c,seclabel,mode=755
│ │ ├─/sys/fs/cgroup/systemd cgroup cgroup rw,nosuid,nodev,noexe

Lesson 02 Using Essential Tools & Cockpit 4


c,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-
│ │ ├─/sys/fs/cgroup/net_cls,net_prio cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,net_cls,net_prio
│ │ ├─/sys/fs/cgroup/devices cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,devices
│ │ ├─/sys/fs/cgroup/cpu,cpuacct cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,cpu,cpuacct
│ │ ├─/sys/fs/cgroup/cpuset cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,cpuset
│ │ ├─/sys/fs/cgroup/rdma cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,rdma
│ │ ├─/sys/fs/cgroup/freezer cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,freezer
│ │ ├─/sys/fs/cgroup/blkio cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,blkio
│ │ ├─/sys/fs/cgroup/perf_event cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,perf_event
│ │ ├─/sys/fs/cgroup/hugetlb cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,hugetlb
│ │ ├─/sys/fs/cgroup/memory cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,memory
│ │ └─/sys/fs/cgroup/pids cgroup cgroup rw,nosuid,nodev,noexe
c,relatime,seclabel,pids
│ ├─/sys/fs/pstore pstore pstore rw,nosuid,nodev,noexe
c,relatime,seclabel
│ ├─/sys/fs/bpf bpf bpf rw,nosuid,nodev,noexe
c,relatime,mode=700
│ ├─/sys/kernel/tracing none tracefs rw,relatime,seclabel
│ ├─/sys/fs/selinux selinuxfs selinuxfs rw,relatime
│ ├─/sys/kernel/debug debugfs debugfs rw,relatime,seclabel
│ ├─/sys/fs/fuse/connections fusectl fusectl rw,relatime
│ └─/sys/kernel/config configfs configfs rw,relatime
├─/proc proc proc rw,nosuid,nodev,noexe
c,relatime
│ └─/proc/sys/fs/binfmt_misc systemd-1 autofs rw,relatime,fd=34,pgrp
=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=19497
├─/dev devtmpfs devtmpfs rw,nosuid,seclabel,siz
e=1882904k,nr_inodes=470726,mode=755
│ ├─/dev/shm tmpfs tmpfs rw,nosuid,nodev,seclab
el
│ ├─/dev/pts devpts devpts rw,nosuid,noexec,relat
ime,seclabel,gid=5,mode=620,ptmxmode=000
│ ├─/dev/mqueue mqueue mqueue rw,relatime,seclabel
│ └─/dev/hugepages hugetlbfs hugetlbfs rw,relatime,seclabel,p
agesize=2M
├─/run tmpfs tmpfs rw,nosuid,nodev,seclab
el,mode=755
│ └─/run/user/1000 tmpfs tmpfs rw,nosuid,nodev,relati
me,seclabel,size=382636k,mode=700,uid=1000,gid=1000
│ └─/run/user/1000/gvfs gvfsd-fuse fuse.gvfsd-fus rw,nosuid,nodev,relati
me,user_id=1000,group_id=1000
├─/boot /dev/sda1 xfs rw,relatime,seclabel,a
ttr2,inode64,logbufs=8,logbsize=32k,noquota
└─/var/lib/nfs/rpc_pipefs sunrpc rpc_pipefs rw,relatime

Lesson 02 Using Essential Tools & Cockpit 5


/boot
Everything that is
needed to boot the
Linux system.

vmlinuz-4.18.0-
32.el8.x86_64

Linux Kernel, the


heart of the OS,
that allow to
interact with the
HW on your
computer.

/dev
(devices)
Interface files to
talk to specific HW
devices.

/dev/sda
/dev/null

By addressing a
/dev file, you are

addressing directly
the Kernel.

Major and Minor


Numbers

Lesson 02 Using Essential Tools & Cockpit 6


Kernel Level
Identifier of which
device you want to
address.

Major and Minor Numbers


Char devices are accessed through
names in the filesystem. Those names crw-rw-rw- 1 root root 1, 3 Feb
23 1999 null
are called special files or device files or crw------- 1 root root 10, 1 Fe
simply nodes of the filesystem tree; b 23 1999 psaux
crw------- 1 rubini tty 4, 1 Au
they are conventionally located in g 16 22:22 tty1
the  /dev  directory. Special files for char crw-rw-rw- 1 root dialout 4, 64 Ju
n 30 11:19 ttyS0
drivers are identified by a “ c ” in the first crw-rw-rw- 1 root dialout 4, 65 Au
column of the output of  ls -l . g 16 00:00 ttyS1
crw------- 1 root sys 7, 1 Fe
Block devices appear in  /dev  as well, b 23 1999 vcs1
crw------- 1 root sys 7, 129 Fe
but they are identified by a “b.” b 23 1999 vcsa1
crw-rw-rw- 1 root root 1, 5 Fe
b 23 1999 zero

The kernel uses the major number at open time to dispatch (send) execution to the
appropriate driver.
The minor number is used only by the driver specified by the major number; other
parts of the kernel don’t use it, and merely pass it along to the driver.

It is common for a driver to control several devices (as shown in the listing); the
minor number provides a way for the driver to differentiate among them.

/proc/partitions
paul@RHELv4u4:~$ cat /proc/partitions
The  /proc/partitions  file contains a major minor #blocks name
table with:
3 0 524288 hda
3 64 734003 hdb
major
8 0 8388608 sda
8 1 104391 sda1
and minor number of partitioned
8 2 8281507 sda2
devices, 8 16 1048576 sdb
8 32 1048576 sdc
their number of blocks 8 48 1048576 sdd
253 0 7176192 dm-0
and the device name in /dev. 253 1 1048576 dm-1

Lesson 02 Using Essential Tools & Cockpit 7


Verify with  /proc/devices  to link the
major number to the proper device.

The major number corresponds to the


device type (or driver) and can be found
in  /proc/devices . In this case 3
corresponds to ide and 8 to sd.
The major number determines
the device driver to be used with this
device.

The minor number is a unique
identification of an instance of this
device type. The  devices.txt  file in the
kernel tree contains a full list of major
and minor numbers.

/etc
Contains configuration files.

cd /etc
cat passwd
cat redhat-releas
// Redhat Enterprise linux 8.0
cat os-release

/home useradd linda

/sbin : Essential System binaries


e.g., fsck, init, route.

Just like /bin, /sbin also contains binary executables.

Lesson 02 Using Essential Tools & Cockpit 8


The linux commands located under this directory are used typically by system
administrator, for system maintenance purpose.

Example: iptables, reboot, fdisk, ifconfig, swapon

/usr (like program files in Windows)


|
|— bin // user binary // run buy normal user
|— sbin // system binaries // needs root privilege
| ...

/usr : Secondary hierarchy for read-only user data; contains the majority of
(multi-)user utilities and applications.  
Contains binaries, libraries, documentation, and source-code for second level
programs.

contains binary files for user programs. If you can’t find a user binary
/usr/bin

under /bin , look under /usr/bin . For example: at, awk, cc, less, scp

/usr/sbincontains binary files for system administrators. If you can’t find a


system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd,
useradd, userdel

/usr/lib contains libraries for /usr/bin and /usr/sbin

/usr/localcontains users programs that you install from source. For example,
when you install apache from source, it goes under /usr/local/apache2/usr/src
holds the Linux kernel sources, header-files and documentation.

/var (for dynamic data like logs)

cat /var/log/messages
less /var/log/messages

man hier // description of the filesystem hierarchy


man file-hierarchy

2.6 Using man


The table below shows the section numbers of the manual followed by the types of
pages they contain.

Lesson 02 Using Essential Tools & Cockpit 9


1 Executable programs or shell commands (commands run as normal
user)
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd (for configuration files)
6 Games
7 Miscellaneous (including macro packages and conventions), e.g. man(7),
groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]

man lvcreate

G —> (Ground) End of the manual page.

g —> Top of the manual page.

Space —> scroll ne page

ENTER —> scroll one line

2.7 Finding the Right man page


man pages are indexed in the mandb

mandb // mandb - create or update the manual page index caches, to rebuil
d the man database
man -k list // search for commands contains the word "list"
apropos list // apropos list
man -k user | wc // word count, wc - print newline, word, and byte counts for each f
ile
man -k user | grep 8 // filter for "8", section "8"
grep // to filter the results
man man // see how man works
man apropos
man mandb
useradd --help

2.8, 2.9 vim

Lesson 02 Using Essential Tools & Cockpit 10


Esc // from insert mode to command mode
i, a // insert, append
o // open new line
:wq
:q!
dd // delete a line
d$ // delete until the end of the file
yy // copy line
p // paste
G // end of the file
gg // top of the file
v // visual mode
v then gg // select text up to the top of the file
y // to copy the selected text
u // undo
ctrl-r // redo
/text // search from the current curser positio downwards for text
?text // search in the oposite direction
^ // go to the beginning of the line
$ // go to the end of the current line
:%s/old/new/g // substitute global
:%s/old/new/ // substitute one by one

2.10 Globbing and Wildcards

man 7 glob
ls host*
ls ?ost*
ls [hm]ost // start with either h or m
ls [!hm]ost // does NOT start with either h or m
ls script[0-9][0-9]
touch script{0..100} // create range of files
ls script[0-9][0-9]
ls script[0-5][0-9]
ls *[0-9]* // show also the contents of the directories
ls -d *[0-9]* // show only the directories without their contents

Lesson 02 Using Essential Tools & Cockpit 11


cp /etc/[a-c]* /tmp/files // copy files that start with a, b, or c
mv [ab]* photos/ // move files that start with a or b

2.11 Cockpit

[student@localhost test]$ systemctl enable cockpit.socket // better to make it as r


oot
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ====
Authentication is required to manage system service or unit files.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ====
Created symlink /etc/systemd/system/sockets.target.wants/cockpit.socket → /usr/lib/sys
temd/system/cockpit.socket.
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ====
Authentication is required to reload the systemd state.
Authenticating as: root
Password:
==== AUTHENTICATION COMPLETE ====

[student@localhost test]$ systemctl status cockpit.socket


● cockpit.socket - Cockpit Web Service Socket
Loaded: loaded (/usr/lib/systemd/system/cockpit.socket; enabled; vendor preset: dis
abled)
Active: inactive (dead)
Docs: man:cockpit-ws(8)
Listen: [::]:9090 (Stream)

systemctl restart cockpit.socket // must restart the cockpit

[root@localhost ~]# systemctl status cockpit.socket


● cockpit.socket - Cockpit Web Service Socket
Loaded: loaded (/usr/lib/systemd/system/cockpit.socket; enabled; vendor preset: dis
abled)
Active: active (listening) since Tue 2021-12-07 01:03:48 EST; 3s ago
Docs: man:cockpit-ws(8)
Listen: [::]:9090 (Stream)
Process: 34397 ExecStartPost=/bin/ln -snf active.motd /run/cockpit/motd (code=exite
d, status=0/SUCCESS)
Process: 34389 ExecStartPost=/usr/share/cockpit/motd/update-motd localhost (code=ex
ited, status=0/SUCCESS)
Tasks: 0 (limit: 23536)
Memory: 708.0K

Lesson 02 Using Essential Tools & Cockpit 12


CGroup: /system.slice/cockpit.socket

Dec 07 01:03:47 localhost.localdomain systemd[1]: Starting Cockpit Web Service Socket.


Dec 07 01:03:48 localhost.localdomain systemd[1]: Listening on Cockpit Web Service Soc
ket.

Before systemctl restart cockpit.socket

After systemctl restart cockpit.socket

https://192.168.1.7:9090/

Lesson 02 Using Essential Tools & Cockpit 13


Lesson 02 Using Essential Tools & Cockpit 14

You might also like