GNU/Linux System Administration Fundamentals

Submitted on Sat, 06/12/2021 - 10:47

This is not a conventional tutorial on GNU/Linux system administration. Rather, it is a shorthand note for those who participated in an online training conducted by Provincial Program Implementation Unit, Lumbini Province.


Training environment configuration

Virtualbox Configuration

  • CPU (half ot total), Memory (half ot total), 3D acceleration, Video Memory (maximum/128 MB)

 

Regarding networking, you have two choices:

NAT: In this case, the guest OS and the host OS will be in separate networks.

Bridge: In this case, both the guest and host OS will be in same network.

Guest OS Full Screen configuration

  • Go to terminal (ctrl+alt+T)
  • sudo bash
  • apt update
  • apt upgrade -y
  • apt install linux-headers-$(uname -r) build-essential dkms
  • Click on Device menu and select Install Guest Addition CD images 
  • Go to terminal
  • cd /media/<username>/VBox_GAs_6.1.22 {replace username with your username}

    For example: If your username is apurwa, the command will be:

    cd /media/apurwa/VBox_GAs_6.1.22

     
  • sh ./VBoxLinuxAdditions.run 
  • Restart and Click full screen from menu

Linux History

  • GNU/Linux (The official name of the first OS released using Linux kernel and GNU components.)
  • Richard Stallman and Free Software Foundation
  • GNU is Not Unix (Recursive acronym)
  • GNU GPL License
  • Linus Torvalds and Linux kernel
  • Linux Distros 
  • Debian vs Red Hat

Linux Shell

  • A command interpreter. Takes input from the user and responds accordingly. Used for Linux administration.
  • Shell flavours: sh (Shell), bash (Bourne-again shell)

Linux as a multi user operating system

Concepts:

  • Multi user environment
  • Users and groups
  • Root and non-root users
  • User switching
  • Shell prompt (dollar and hash)
  • Sudoers list
  • Manual pages

 

Commands:

  • su
  • sudo
  • exit
  • man
  • passwd


 

Multi user environment

Linux is a multi user operating system. Multiple users can access the system simultaneously.

Users and groups

Every user is a member of some group.

Root and non-root users

Root has unrestricted access to the whole system while non-root users have limited access.

User switching

In order to switch to root user, there are two options:

  • sudo bash or,
  • su root

sudo vs su

sudo stands for “superuser do”.

su stands for “switch user”.

In sudo, we have to provide the current user’s password. 

In su, we have to provide the password of that user who we want to switch to.

For example, 

su root

This will require root’s password.

sudo bash

This will require apurwa’s password.

Sudo can be used in two ways:

  • sudo <command>
  • sudo bash 

sudo <command>

Example: 

apt update

This command shall fail due to insufficient permission.

sudo apt update

This will ask for the current user’s password and the previous command will be run by root user’s privilege. Instead of switching to the root user, only the root user’s privileges are used for that single command.

sudo bash 

This will ask for the current user’s password and switch to the root user. After the switch, the user stays as root until exit is typed.

For above example,

sudo bash

apt update

exit

Shell prompts

Dollar vs Hash: Non-root users have a dollar sign in prompt while root has a hash sign.

Sudoers list

Sudo privilege is not provided to all non root users. The sudoers list is defined in a file: /etc/sudoers

If the file contains the following: %sudo   ALL=(ALL:ALL) ALL, users belonging to the  group sudo are only allowed this privilege.

Man pages

man <command>

Command synopsis

Command arguments


Linux Directory Structure and Navigation

Concepts:

  • Root directory (/) and subdirectories
  • “Everything in Linux is a file.” 
  • Relative path and absolute path
  • Symbolic links
  • Wildcards (* and ?)
  • . and ..

Commands:

  • cd (with and without argument)
  • pwd (find out about current location)
  • ls ( list content of the current folder)
  • man (read about command details) 
  • mkdir (create directory)
  • rm (remove file and folder)
  • cp (copy files and folders)
  • mv (move or rename files and folders)
  • ln (create a link)
  • find (find files and folders)
  • whoami (find out about current user)

 

Root directory (/) and subdirectories

  • /bin : system commands
  • /etc: configuration files
  • /home: user personal data (Downloads, Desktop, Documents)
  • /dev: device files (files representing a physical device)
  • /var: server data files, log files 
  • /root: root user’s home directory
  • /media: external removable media (USB, optical drives)
  • /proc: system and process information

 

“Everything in Linux is a file.”

  • Not accurate ! 
  • Everything is accessible as a file.
  • Text files and special files
  • Device files
  • /dev/
  • /proc/

 


Working with text files

Concepts:

  • Creating text files
  • Reading text files
  • Pattern matching

Commands:

  • cat (read text file content)
  • touch (create empty file)
  • less (scroll output)
  • tail (read the bottom 10 lines of a text file)
  • head ((read the top 10 lines of a text file))
  • grep (pattern matching)

 


Text editing with vim

Concepts:

  • Modes: (Normal, Insert, Command, Visual, Replace)
  • Cut, copy and paste (delete, yank and put)
  • search
  • replace
  • jump (to line, top and bottom)
  • save
  • exit

Vim Commands:

  • i switch to insert mode
  • esc switch to normal mode
  • : switch to command mode (only possible from normal mode)
  • R switch to replace mode (only possible from normal mode)
  • v select a character (visual mode: select right/left for multiple characters)
  • V select a line (visual mode: select up/down for multiple lines)
  • y copy
  • d cut
  • p paste
  • w write
  • q quit
  • q! forcefully quit
  • Jump :number
  • gg go to top
  • G to to bottom
  • / search
  • Find and replace all :%s/apple/mango/g

Compression and extraction

Commands:

For compression:

  • tar -cvzf <tar.gz file> directory
  • c for compress
  • v for verbose
  • z for tar.gz compression 
  • f for create compressed file with given name

 

For extraction:

  • tar -xvzf <tar.gz file>
  • x for extract

Piping and Redirection

 

In piping, we pass output of one command to another. Example:

cat /home/apurwa/fruits | grep -i 'apple'

 

By using redirection, we can send output of one command to a file. For example:

find . -type f -size +5M > /home/apurwa/report


User, Permission and Ownership Management

Concepts:

  • User Attributes
  • Login Shell
  • User management
  • Permission management
  • Ownership management

 

Commands:

  • useradd
  • usermod
  • chmod
  • chown
  • chgrp
  • groups
  • id

 

User Attributes

Every user has following attributes:

  • Username / UID
  • Password
  • Group / GID
  • Home folder
  • Login Shell

 

Login Shell types:

  • /bin/sh
  • /bin/bash
  • /usr/sbin/nologin
  • /bin/false (safest)

 

List of all users in the system

  • /etc/passwd

List of all the groups in the system

  • /etc/group

 

Linux permissions types:

  • Read (4)
  • Write (2)
  • Execute (1)

 

Ownership management

  • Owner
  • Group
  • Other 

 

Minimum permission to :

  • access a file and 
  • enter a folder 

 


Package Management

Concepts:

  • Packaging system
  • Package manager
  • Repository (/etc/apt/source.list)

Commands:

  • apt update/upgrade/search/show/purge/autoremove/list --installed


 

dpkg stands for debian package.

dpkg is a packaging system used by debian based distros.

dpkg works with .deb files

apt is a package manager based on dpkg.

 


Apache Webserver Configuration

Packagename: apache2

Commands:

  • apt install apache2
  • systemctl start/stop/status/enable/is-enabled apache2 
  • a2ensite/a2dissite/a2enmod/a2dismod

 

PHP module configuration

Package name: libapache2-mod-php

Commands:

  • apt install libapache2-mod-php
  • php -v (Check php version and confirm php is running)
  • apache2ctl -M (list apache2 modules installed)
  • apache2ctl -t (test apache2 configuration files for any syntactic errors)

 

Mariadb database server configuration

Commands:

  • apt install mariadb-server
  • mysql_secure_installation (for security configuration)

 

By running mysql_secure_installation script, root's password can be set. However, in some cases, the root's password still may not be changed. 

In such casess, do the following:

Log into mysql terminal without providing password:

# mysql -u root

Select mysql database:

mysql >use mysql

Update root user's password by running following query

mysql > alter user 'root'@'localhost' identified by 'password';

mysql > flush privileges;

Now, mysql -u root should not work. In order to login, use the following command:

mysql -u root -p

This command will ask for the root password. Type the password and login.

 

 

Mysql commands:

  • mysql -u root -p (enter mysql with root user and providing password in next line)
  • show databases; (show databases in the database server)
  • use <databasename>; ( to enter database)
  • show processlist; (show what the database is doing)
  • flush privileges;
  • alter user....

 

Phpmyadmin configuration

Package name: phpmyadmin

Commands:

  • apt install phpmyadmin

 

After installation, go to localhost/phpmyadmin. If page not found error is seen, do the following:

Open apache2 configuration file with vim:

# vim /etc/apache2/apache2.conf

Add the following line at the top of the configuration file:

Include /etc/phpmyadmin/apache.conf

Save and exit.

Restart apache2 service with following command:

systemctl restart apache2

 

Go to browser and test the path again.


Web application deployment

Example: Drupal

  • Download compressed drupal tar.gz file 
  • Extract the file into apache’s document root at /var/www/html
  • Create a black database in mysql for drupal
  • Follow the installation process from browser

 

Commands:

  • mysql create <database name>;


 


Virtual Host Configuration

  • Configuration file: /etc/apache2/sites-available/<file>
  • ServerName
  • ServerAdmin
  • DocumentRoot

 


Status Monitoring and other commands

  • df
  • top/htop
  • netstat 
  • free
  • who
  • du
  • history
  • who
  • fdisk
  • uptime
  • curl
  • kill
  • poweroff 
  • reboot

Tags