Linux Foundation Certified System Administrator (LFCS) : Basics of Linux (Part 1)

When you first use the Terminal, you can see something like this

If we check the location using "pwd", we can see this

Basically, in general every used is assigned a home directory. For example, Michael will have an address like this:

If we have another user named Allen, he will also have a default address:

Basically within home directory, you keep your data and others can't get access to it.

Also, keep in mind that this ~icon means home directory

Commands and arguments

commands serve a purpose like functions in code, for example "echo"

echo <Text> ; here echo is the command and Text is the argument

here the Text gets printed.

Command types

There are built in commands and again external commands which we can download or use externally

Basic Linux Commands

Let's target to build these folders under michael's account

Here Mumbai folder is within India folder. And India Folder is within Asia.

Let's start:

To see where we are currently, write "pwd"

In my terminal, if I apply this, I get this:

To see what is inside that location, write 'ls'

In my terminal, I got this

Now, if we want to create a folder within the location , we will use "mkdir"

We can also create all folders just like this

for my practice, I have created a folder called "linuxpractice" and will create all of these folders within there. Remember, this was not part of our plan. I just did it for my benefit.

In my terminal,

Now, if I check out, I can see all folders within the location

Let's create the country list now

we went into the Asia location using "cd" and then created folder named China and India using mkdir

if we now want to create the city Mumbai within India, without going within the location India, we case use this "mkdir India/Mumbai"

In my terminal,

Now if we want to go to the upper folder/location location , we can use cd ..

Absolute and Relative Path

When we want to go from root directory to Asia, you can use the absolute path or relative path.

Pro way:

Assume that , you want to go from home directory to a folder called "/etc". So, we can push that in queue

We need to use the pushd command

The home directory (~) is now in the queue

Now, if we wish to switch to other folders, like /var and then /tmp, we can go there.

But if we want to go to home directory, we just need to use popd

Now, let's make it possible

Here we can see in left, we have several mistakes like 'Munbai' instead of Mumbai, Morocco in Europe instead of Africa and Cairo city without any City.txt file

Let's make it possible

So, starting with Morocco folder. We have to move it to Africa folder using "mv" command and then location/file name and then destination.

Again, to change the name Munbai to Mumbai, we can again use the mv command

To, copy City.txt file within Cairo, we can use cp location/file destination

FInally, to remove the Tottenham.txt file, use rm location/file name

To see content within a file , we can use cat command

To add lines, use can use cat > location/file name

Then you can give input. Here , we used 'Cairo' as input and then used Ctrl + d to get out of it

To create an empty file under China named Country.txt, we can use touch location//file name command

Long list

to see list of files with information use ls -l

To see hidden files use ls -a

Single dot . means current working directory

Double dots .. mean previous directory

To see list of files in order, use ls -lt and in reverse use ls -ltr

How to get help when stuck with a command?

use whatis <command> and man <command>

You can also use --help

You can also use apropos keyword to search a keyword from the whole guideline

Bash shell

There are multiple shell types

to use shell from terminal, we need to write this

In my terminal, I got this

Bash supports autocomplete. So, you just need to write few words and press Tab for suggestions and accept that

You can also set variables (alias). here we have set dt as date

You can also check all the previous commands using history command

We have previously used $SHELL command which was an environment variable

You can check all from here

To set environment variable, use export command

You can also use this

But in this way, we can't use OFFICE's value for a long time. To do that, we need to add them to these files .profile or, .pam_environment

User profile scripts, such as ~/.profile, ~/.bash_profile, ~/.bashrc, and others, are executed when a user logs in, allowing the setup of the environment according to personal preferences.

To make changes persistent in Unix-like operating systems, variables, aliases, and other configurations can be added to these profile files.

For example, to add a variable using the command line:

echo 'export MY_VARIABLE="example_value"' >> ~/.profile

This command appends the export statement to the end of the ~/.profile file, making the variable MY_VARIABLE persistent across sessions.

To add an alias, like ll for ls -l, use:

echo 'alias ll="ls -l"' >> ~/.profile

Practice 1

For example, If you are asked to create a new environment variable called PROJECT=MERCURY and make it persistent by adding the variable to the ~/.profile file.

It should be

echo 'export PROJECT=MERCURY' >> /home/bob/.profile

Here, /home/bob is where we have the .profile file and we are exporting the variable PROJECT as MERCURY

Practice 2

Set an alias called up for the command uptime and make it persistent by adding to ~/.profile file.

echo 'export up=uptime' >> /home/bob/.profile

Path variable

Anything that's present must be added to PATH variable

Now assume that you have installed obs and want to see where is obs-studi in your system . For that, use which obs-studio

it that's not present in the $PATH variable, you can't see it here.

To solve this issue we set PATH equal to $PATH variable and append the obs studios binary file location using :/opt/obs/bin

Customize Bash prompt

In general, we have working directory and then user prompt symbol when we use the bash

But we can customize it and make it this

we can change the prompt to 'ubuntu-server' by setting $PS1

We can also use these to modify the bash

For example , I have these in my terminal

Practice:

Update Bob's prompt so that it displays the date as per the format below:

Example: [Wed Apr 22]bob@caleston-lp10:~$
Make sure the change is made persistent.

Run:PS1='[\d]\u@\h:\w$' and add this to the ~/.profile file to keep it persistent over time.

echo 'PS1="[\d]\u@\h:\w$"' >> ~/.profile

Linux Kernel

You can compare it to a librarian

Librarian tracks and manages all stuffs.

Linux kernel has these 4 duties

manage memory, manage who is doing which process, security check and others.

It's Monolithic (kernel carries out CPU, Memory management scheduling etc all by itself) & Modular (it can extend it's capability through dynamically loaded kernel modules)

Kernel versions

We check it by using uname

also, to be specific, we can use uname -r

In my terminal,

check out all version record

Kernel space

Process that's within kernel remains here and basically handle administrative stuff

User space

Process outside of kernel is called user land. Example of user space applications includes programs that is written in C,Java, python, ruby etc.

Most UNIX like interface comes with all sort of prepackages called user space applications.

how does all of these work?

Basically, from the user space, if we want to reach memory etc. we go for the system calls and that goes to kernel.

kernel then finds our device drivers and finds out the memory to write a file and many more.

How does Linux work with Hardware?

Let's assume that we have a pen drive that got attached to the PC.

As soon as it's done, the device driver within kernel generates an uevents

The uevents is passed in to user space and it created a /dev/<identity> for the usb disk

To check messages from kernel, we can use dmesg

We can also find special keywords using grep -i "keyword to search"

udevadm info queries from the database about hard disk information

udevadm monitor listens to uevents such as which device got added or removed and their location

lspci lists down all pci cards like video card etc.

lsblk lists down all blocks of a disk. here sda (ssd disk) has been partitioned in 5 parts (blocks)

lscpu lists down the cpu details

lsmem shows us the memory size and remianing

lshw gives detailed information about the pc

Linux boot process

BIOS Post test

checks if hardware etc are ok or not

Boot ladder

Kernel initialization

systemd now works and initialize the process

Systemd targets

now assume , we have 2 person. Bob and Dave both using Ubuntu.

But when they load their laptop, one gets graphical view and one directly goes to terminal.

Why??

Basically , Dave has modified and instructed systemd in such a manner that it directly shows terminal rather than graphical user interface (gui) like Bob.

There are system like runlevel which is 5 for Bob and 3 for Dave

depending on the value of runlevel, systemd's target does change.

If we want to change the target from graphical.target to multiuser.target, we need to do this

The term runlevels is used in the sysV init systems. These have been replaced by systemd targets in systemd based systems.

The complete list of runlevels and the corresponding systemd targets can be seen below:

runlevel 0 -> poweroff.target

runlevel 1 -> rescue.target

runlevel 2 -> multi-user.target

runlevel 3 -> multi-user.target

runlevel 4 -> multi-user.target

runlevel 5 -> graphical.target

runlevel 6 -> reboot.target