7.6 KiB
🎬 Introduction to Linux for beginners
💡 Preface
This module is part of a course on DevOps.
Checkout the course introduction for more information
This module is part of chapter 2
What is Linux
Linux is a family of operating systems based on the Linux kernel. It's the most popular operating system on the planet that powers most of the internet today as well as Android which is powered by the Linux operating system.
It's important to note that Linux comes in many distributions or Distros. a Distro is the Linux kernel, packaged alongside many supporting operating system software and libraries.
There are many popular Linux Distros, including Ubuntu, Debian, Arch Linux, Fedora and more.
Setup a Linux Server
For this module, you will need access to a Linux server.
See the Preface section above for a link to the chapter overview page where you will find the module on Servers & Virtualization for creating a Virtual Server.
Checkout chapter 2: module: Introduction to Servers & Virtualization if you need to create a Linux server
Terminal, Shell & Command Line
The first thing we see when we start this server is that our software gives us a window where we can type commands.
The window we see is called the Terminal. The terminal is just a window that accepts inputs and outputs
The Terminal is responsible for running what's called a Shell.
A Shell is a program that exposes the operating system to a user or a program. So we talk to the operating system by typing commands into the Shell. The Shell is the command intepreter which processes our commands and outputs the results.
Users & Security
When we created our server, we had to provide details of a username and password for our default user.
This is a standard practice for setting up Linux.
Linux by default is shipped with a root
user. The root
user is the administrative account of the operating system which have very high privileges.
You are discouraged from using the root
user because it can make almost any change to the operating system which can be disastrous or have catastrophic effects.
SUDO & Administrative tasks
In order to do administrative tasks on a server, instead of using the root
account, we can setup whats called sudo
sudo
is a program that enables users to run programs with privileges of other users, by default the super user.
SUDO originally stood for "superuser do" as it allowed us to do something as a super user.
Then it became "su do", as in "Substitute user, do", because sudo
can run commands as other users as well.
To see sudo
, type sudo --help
We can run sudo -i
to switch to root
and exit
to go back to our account
Add a user
To add user: (We see only root account can do this)
adduser bob
Fatal: Only root may add a user or group to the system.
We elevate our privilege by using sudo
in the front (and follow the prompts)
sudo adduser bob
Bob is now an unprivileged user.
We can make Bob privileged by adding him to the SUDOers group then Bob can use sudo
to elevate privileges just like our user.
# see the help text
usermod --help
# we are going to use -a which means append
# we are going to use -G which means we want to append to a group
usermod -aG sudo bob
# we get permission denied! remember to use sudo
sudo usermod -aG sudo bob
Delete a user
To delete a user, we simply run userdel
userdel --help
#remove bob
sudo userdel -r bob
List users
User information in Linux is stored in the passwd
file which exists in the /etc
directly
View the content of the file with the cat
command
cat /etc/passwd
Another useful command to see who we are logged in as, is the whoami
command
The File system & Navigation
In order to navigate around, we will learn a couple of commands to navigate, but we also need to know what we are navigating
-
ls
is a command we use to list contents in a directory./
id the root directory in Linux, which is the equivalent ofC:\
in Windows. Typels /
in the terminal to list out contents in the/
root directory. -
Home directories: Under the
/
directory you will see a directory called/root
which is the$HOME
directory of the root user.
All other user folders are under/home
The~
also represents the full path to the home directory for your current user -
The
.
is a short-hand for the current directory. Example:ls .
-
echo
prints stuff to the terminal. One example is to see the content of the $HOME environment variable, we can typeecho $HOME
to see it -
Environment variables are something we will dive into an our command line and scripting modules, but some variables are important as we have already seen with
$HOME
. Another one is$PWD
which contains the current working directory. -
Create a directory: we can create a directory with
mkdir
Create a directory in your HOME directory:mkdir $HOME/testdir
-
Change directory: We can change the current working directory by using the
cd
command.
I.Ecd $HOME
-
Delete a directory: we can create a directory with
rm
Let's delete our test directory:rm -rf testdir
File management
Let's change directory to our $HOME folder
cd $HOME
We can see where we are
echo $PWD
Create a new folder where we can keep our scripts
# create directory
mkdir scripts
# view directory
ls -l
# change directory
cd scripts
Create a new script file with the touch
command
We can use the touch
command to create any type of file
touch my-first-script.sh
We can see the file is empty
ls
cat my-first-script.sh
We can edit the file using nano
which is a simple text editor for Linux
nano my-first-script.sh
We can now write content in the file
Let's add the following content to the script file:
echo "This is our first script running from directory $PWD !"
To exit nano
and save the file, we press CTRL+x and type Y
to save the file.
In our scripting module, we will look at running scripts in more details , but quickly:
# try run the script
./my-first-script.sh
-bash: ./my-first-script.sh: Permission denied
# needs exectute permissions
chmod +x my-first-script.sh
# try run the script again
./my-first-script.sh
This is our first script running from directory /home/devopsguy/scripts !
To copy a file, we can use the cp
command. Lets make a copy of our script
cp --help
cp my-first-script.sh my-second-script.sh
ls -l
We can delete the second file with the rm
command
rm my-second-script.sh
We can move or rename a file with the mv
command
cp my-first-script.sh my-final-script.sh
Package managers
To install things on Linux we need to learn about the package managers.
Ubuntu Linux maintains sources list for where it downloads packages by default
The sources list contains URLs or Links to Mirror sites or Repository sites where packages are looked for when trying to install things
To install a package :
sudo apt-get install -y <packagename>
To remove a package :
sudo apt-get remove <packagename>