Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

readme.md

Linux Short Notes

History of LINUX

Linux came from a Unix family, Linux is a free and open-source software operating system, which Linus Torvalds developed in September 1991.

In 1991, Linus Torvalds was a student at the University of Helsinki, Finland, USA.

He developed the first code of Linux 0.01 and post it on the Minix newsgroup on 17 Sep 1991, his code become so popular people encouraged him to develop new code and he was led to develop new code and release the first "official" version of Linux, version 0.02 on October 5, 1991.

Today many year pass and Linux become one of the most popular operating systems. Today 90% fastest Supercomputers out of 500 run on Linux variants including the top 10.


Linux File System Hierarchy

In Linux everything is represented as a file including a hardware program, the files are stored in a directory, and every directory contains a file with a tree structure. That is called File System Hierarchy.

Linux uses single rooted, inverted tree-like structure.

Root Directory represents with / (forward slash) It is a top-level directory in Linux.

/

The base of the Linux directory is the root. This is the starting point of FSH. Every directory arises from the root directory. It is represented by a forward slash (/).

If someone says to look into the slash directory, they refer to the root directory.


/root

It is the home directory for the root user (superuser).

/bin → User Binaries

  • Contains binary executable.
  • Common Linux commands you need to use in single-user modes are located under this directory.
  • Commands used by all the users of the system are located here.

/sbin → SystemBinaries

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

But, the linux commands located under this directory are used typically by system aministrator, for system maintenance purpose.

For example: iptables, reboot, fdisk, ifconfig, swapon

/dev → Device Files

  • it contains hardware device files,
  • Contains device files.
  • These include terminal devices, usb, or any device attached to the system.

For example: /dev/tty1, /dev/usbmon0

/var→ Variable Files

The variable data files such as log files are located in the /var directory.

File contents that tend to grow are located in this directory.

This includes

  • /var/log: System log files generated by OS and other applications.
  • /var/lib: Contains database and package files.
  • /var/mail: Contains Emails.
  • /var/tmp: Contains Temporary files needed for reboot.

/mnt→ Mount Directory

This directory is used to mount a file system temporarily.

/media→ Removable Media Devices

The /media directory contains subdirectories where removable media devices inserted into the computer are mounted.

/usr→ User Binaries

The /usr directory contains applications and files used by users, as opposed to applications and files used by the system.

/etc → Configuration files

It contains all configuration files of server

The core configuration files are stored in the /etc directory. It controls the behavior of an operating system or application. This directory also contains startup and shutdown program scripts that are used to start or stop individual programs.

/boot → Boot Loader Files

The /boot directory contains the files needed to boot the system — for example, the GRUB boot loader's files and your Linux kernels are stored here.


/opt → Optional Applications

The opt directory is used for installing the application software from third-party vendors that are not available in the Linux distribution. Usually, the software code is stored in the opt directory and the binary code is linked to the bin directory so that all users can run that software.

/home → Home Directory

It contains secondary users home directory.

/tmp → Temporary Files

Directory that contains temporary files created by system and users.

Files under this directory are deleted when system is rebooted.

Basic Commands

#pwd → it shows the present working directory
#ls → it shows available files and directory list in the present working directory.
#uname → it shows the name of the kernel (OS)
#uname -r → it shows version of the kernel
#cd → it use for change directory
#clear → it use for clear screen
#whoami → it show currently login user name
#history → it show list of previously used commands
#date → it show time and date

Create file or directory

1. For create single directory

mkdir directory_name

2. For create multiple directory

mkdir dir1 dir2 dir3

3. For create directory path (directory inside directory)

mkdir -p parent/child/grandchild

4. For create number of directory

mkdir dir{1..10}

Touch:

Touch command is use for create empty file, we can't write data in a file, can't edit or save file.


Create file

1. Create single file with touch command

touch filename

2. Create multilpe file

touch file1 file2 file3

3.Create number of files

touch file{1..10}

CP:

cp command is used for copy and paste file or directory

Syntax:

#cp <option> <source> <destination>

Options

  • -r for recursive
  • -v for verbose
  • -f for forcefully

For copy and paste


1. For copy file

cp source_file destination

2. For copy all data which start form D alphabet

cp D* destination

For remove file & directory

1. For delete file or directory

rm filename
rm -r directory_name

For move or rename file & directory

1. For move file or directory

mv source destination

2. For rename file or directory

mv old_name new_name

User Management

For create user account

useradd username

For check user account properties

id username

For create user account password

passwd username

For check user password properties

chage -l username

For switch user account

su username

For switch user account

su - username

For logout from user account

exit

Or

logout

For Delete user account

userdel username

For change user Login name

usermod -l new_name old_name

Group Management

A group is a collection of user accounts that is very use full to administrators for managing and applying permission to a number of users.

For add Group account

groupadd groupname

For check group account property

getent group groupname

For check group admin property

gpasswd -A admin_user groupname

For Delete group Account

groupdel groupname

For add single member in group

gpasswd -a username groupname

For add multiple member in group

gpasswd -M user1,user2,user3 groupname

For remove group member

gpasswd -d username groupname

For make group admin

gpasswd -A username groupname

Linux File System Permission

  • Basic Permission
  • Special Permission
  • Access Control List (ACL) Permission

Type of File Permission

For check file permission

ls -l filename
Permission Link Owner Group owner Size of file Date & time of file creation Name of file

For check directory permission

ls -ld directory_name

Permission Group

  • Owner (u) → Permissions used for the owner of the file
  • Group (g) → Permissions used by members of the group
  • Other (o) → Permissions used by all other users
Permission Description

Permission in details

  • Basic Permission
  • Special Permission
  • Access Control List (ACL) Permission

Type of File Permission

Permission Set

Permission with numeric & symbol


For change permission

For add read permission to owner

chmod u+r filename

For add read write permission to group

chmod g+rw filename

For remove read permission to others

chmod o-r filename

For change ownership

Syntax:

chown new_owner filename

For change group ownership

Syntax:

chgrp new_group filename

Set permission with a numeric value

  • r (read) = 4
  • w (write) = 2
  • x (execute) = 1

For set permission with a numeric value

chmod 755 filename

Access Control List (ACL)

Access control list (ACL) provides an additional, more flexible permission mechanism for file systems.

Access control list is a service which is used for providing special permission to specific users and group to particular directories and file

Use of ACL

Think of a scenario in which a particular user is not a member of group created by you but still you want to give some read or write access, how can you do it without making user a member of group, here comes in picture Access Control Lists, ACL helps us to do this trick.

For check ACL Permission

Syntax:

getfacl filename

For set ACL permission to user

setfacl -m u:username:permissions filename

For remove ACL permission of user

setfacl -x u:username filename

For set ACL permission to Group

setfacl -m g:groupname:permissions filename

For remove ACL permission of group

setfacl -x g:groupname filename

For remove all ACL permissions

setfacl -b filename

Regular Expressions

Regular expressions are special characters which help search data, matching complex patterns.

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern.

What are Regular Expressions?

GREP (Global Regular Expression Print)


Search a word (string in a file)

grep "string" filename

Search a string in multiple files

grep "string" file1 file2 file3

Search a string insensitive in file

grep -i "string" filename

Search a string in all files recursively

grep -r "string" directory

Inverting the string match

grep -v "string" filename

Displaying the string match total line no

grep -c "string" filename

Display the file names that match the string

grep -l "string" *.txt

Display the file names that do not contain the string

grep -L "string" *.txt

Displaying the string match line with number

grep -n "string" filename

Display the lines that start with a string

grep "^string" filename

Display the lines that end with a string

grep "string$" filename

Search and redirect output in a new file

grep "string" filename > newfile

Find

The Linux Find Command is one of the most important and much used command in Linux systems.

Find command used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

Find can be used in a variety of conditions like you can find files by permissions, users, groups, file type, date, size, and other possible criteria

Find files under Home directory

find /home -name filename

Find files with suid permission

find / -perm -4000

Find files with guid permission

find / -perm -2000

Find files with sticky bit permission

find / -perm -1000

Using Find command based on users

find / -user username

Using Find command based on groups

find / -group groupname

Search the file with less than 10MB in a folder

find /path -size -10M

Search the file with more than 10MB in a folder

find /path -size +10M

WC (Word Count)

The wc command is use for the count word and line numbers.

Count number of lines

wc -l filename

Count number of words

wc -w filename

head

Head command is used for to display top line of the file.

Display top 10 line of the file

head filename

Display top specific no line of the file

head -n 5 filename

tail

Tail command is used for to display the bottom line of the file.

Display bottom 10 line of the file

tail filename

Display bottom specific line of the file

tail -n 5 filename

Archive File in Linux

The Linux "tar" stands for tape archive, which is used by large number of Linux/Unix system administrators to compress size or drives backup. For create archive tar used some compression algorithms Such as gzip,bz2 and xz

Archiving is the process of combining multiple files and directories (same or different sizes) into one file. Archive process is very useful for backup and compression size of data in Linux.

What is Tar


Tar command syntax

#tar <options> <files>

Commonly used options

  • c -for create
  • x -for extract
  • v -for verbose
  • f -for forcefully
  • t -for test
  • z -for gzip
  • j -for bz2
  • J -for xz
  • C -for specific destination

To create a tar archive file

tar -cvf archive.tar files/

To show file size in human-readable format

ls -lh archive.tar

To extract a tar archive file on the default location

tar -xvf archive.tar

To extract a tar archive file on the specific location

tar -xvf archive.tar -C /path/to/destination/

To create a tar archive file with compress in size (gzip)

tar -czvf archive.tar.gz files/

To extract a tar archive file with compress in size (gzip)

tar -xzvf archive.tar.gz

To create a tar archive file with compress in size (bzip2/bz2)

tar -cjvf archive.tar.bz2 files/

To extract a tar archive file with compress in size (bzip2/bz2)

tar -xjvf archive.tar.bz2

To create a tar archive file with compress in size (xz)

tar -cJvf archive.tar.xz files/

To extract a tar archive file with compress in size (xz)

tar -xJvf archive.tar.xz

Job Automation

Job automation allows us to perform tasks automatically in OS by using tools.

This feature is very useful for the administrator to assign tasks to OS whenever he is not present or performing daily basis work.

Two type of job automation

  1. at — command is used to execute a job only one time.
  2. crontab — Crontab command is used to execute a job multiple times.

To a set job with at command

at time

To show pending a job

atq

To remove a job

atrm job_number

To restrict user from accessing at

echo "username" >> /etc/at.deny

To start crond service

systemctl start crond

To enable crond service (Permanent on)

systemctl enable crond

For set cron jobs

crontab -e

To show cron jobs of the current user

crontab -l

To remove cron jobs

crontab -r

Or,

Go to the crontab file and remove the job line

To the set cron job to other users

crontab -u username -e

To show cron job, other user

crontab -u username -l

To restrict user from crond service

echo "username" >> /etc/cron.deny

Sudo Command

sudo ("superuser do", or "switch user do") allows a user with proper permissions to execute a command as another user, such as the superuser.

sudo allows a permitted user to execute a command as another user, according to specifications in the /etc/sudoers file.

What is sudo?

For edit configuration file:

# vim /etc/sudoers

Provide sudo privilege to the user

# vim /etc/sudoers
root ALL=(ALL) ALL
amir ALL=(ALL) ALL (add this line appro. 101 lines)
:wq

Provide sudo privilege to the group

For edit configuration file:

# vim /etc/sudoers
%punegrp ALL=(ALL) ALL (line number 108)
:wq

By default, all members of punegrp group got sudo privileges

Wheel group

Wheel is a system group that by default has sudo privileges, if we add any member to that group then that user got sudo privilege

#grep wheel /etc/group
#useradd shub
#passwd shub
#gpasswd –a shub wheel

By default, all members of the wheel group got sudo privileges


Sudo without password

For edit configuration file:

# vim /etc/sudoers
amir ALL=(ALL) NOPASSWD: ALL
%punegrp ALL=(ALL) NOPASSWD: ALL
:wq

Managing networking based on Red Hat Enterprise Linux

To show ip address

ip a

Or,

ifconfig

Configure networking with nmcli

Network Manager is a daemon that monitors and manages network settings.

nmcli command used to manage networking

Manage IP configuration

For Show all list of connection

nmcli con show

To Show all list of connection

nmcli con show

To show a active connection

nmcli con show --active

To show a specific connection

nmcli con show "connection_name"

To show the device status

nmcli dev status

To create a new connection with nmcli

nmcli con add type ethernet con-name "citynet" ifname ens33

To show the device status

#nmcli con mod "citynet" ipv4.add 192.168.0.100
#nmcli con mod "citynet" gw4 192.168.0.254
#nmcli con mod "citynet" ipv4.dns 192.168.0.254
#nmcli con down citynet
#nmcli con up citynet
#nmcli con show — active
#nmcli con show citynet

To activate new connection

nmcli con up "connection_name"

To deactivate connection

nmcli con down "connection_name"

To start a new connection and stop the old connection

nmcli con up "new_connection"
nmcli con down "old_connection"

To remove existing connection

nmcli con delete "connection_name"

To remove the existing connection

nmcli con del "connection_name"

To set hostname

hostnamectl set-hostname new_hostname

To show hostname

hostname

Configuring networking with nmtui

nmtui

IP address configuration files

All created connections with nmcli and nmtui by default are stored in the following file

/etc/sysconfig/network-scripts/

We can also modify the connection using the above connection file, but it not recommended.

If update the file, restart NetworkManager service to update IP-configuration

systemctl restart NetworkManager