Ticker

6/recent/ticker-posts

Linux Fundamental 2






Mounting and Unmounting Existing File systems.
  • mount - Mount a filesystem if ran without options, it will display currently mounted filesystem
  • umount - Unmount a filesystem


find a filesystem. will display mounted filesystem information in a tree view by default
$findmnt -D



Filesystem commands:
  • df - display filesystem disk usage. display the filesystem, mount point, and utilization detials
  • lsblk - list block devices. Display information about block storage devices, including the devices size and any mounted filesystem. display colums can be customized
  • blkid - list block devices. Display information about blocl storage devices, including the device size. and any mounted filesystem. Display
  • /etc/fstab system file that contains information about file tthat are available for the opOS to mount at startup


The type command is used to display information about the command type. It will show you how a given command would be interpreted if typed on the command line.
$type cp
$type mv


Make multiple directory using mkdir command.
$mkdir a b c


apropos command can help you with the command, if you don't remember the exact syntx of command only if you remember it partially.
It is equivalent to man command with -k option
$apropos mv


Change the shell of current user
$usermod --shell /bin/bash satish
$chsh --shell /bin/sh satish
$vi /etc/passwd

Print the environment variables.
$env


If you want to customize basic prompt of shell
$PS1="[\d \t \u@\h:\w ] $ "
echo $PS1

\d : the date in "Weekday Month Date" format (e.g., "Mon May 04")
\t : the current time in 24-hour HH:MM:SS format
\w : the current working directory, with $HOME abbreviated with a tilde



kernal log you can see using dmesg
$dmesg | grep -i usb
$lspci


peripeheral interconnect
ethernet controller, video card, etc


If you need to see the detailed information of a system's hardware configuration you can use following command
## List hardware
$ lshw

If you want to see the current settings of console, you can use systemctl get-default command also you can set default setting
$systemctl get-default
$systemctl set-default multi-user


Ref - here
  • lsmem lists memory blocks and their state; these reflect physical memory and are counted in units of memory blocks, i.e. 128MiB on your system. To do this, lsmem reads information made available by the kernel in /sys/devices/system/memory. On your system, the kernel tracks 64 memory blocks for a total of 8GiB.
  • free lists memory that’s usable by the system; “total” is the amount of physical memory, minus memory reserved by the system (for the firmware’s purposes mostly) and the kernel’s executable code. free reads this information from /proc/meminfo.
  • The difference in output is explained by this difference in what is measured. In all cases, free’s total memory will be smaller than lsmem’s total online memory.


hostnaectl command is used to check the current hostname of the system
$hostnamectl
$hostnamectl set-hostname NAME

Add terrafrom to PATH of Linux so that you don't need to use full path
$export PATH=$PATH:/usr/local/bin/terraform



IO readirection(in details here)
Redirect standard error use the following symbol 
$cat missing_file 2> error.txt
$cat missing_file 2>> error.txt
$cat missing_file 2> /dev/null
$echo $SHELL | tee shell.txt


tee command
tee -a for appending in file instead of overriding .
$dmesg | grep -i usb
$echo $SHELL | tee -a shell.txt

curl command in Linux :

curl

tr 


uniq command in Linux :

uniq

sort command in Linux :

sort

date command in Linux:

$(date +%s)


Reference:
https://stackoverflow.com/questions/1951742/how-can-i-symlink-a-file-in-linux



Set timezone in Linux:

$ timedatectl
$ timedatectl list-timezones
$sudo timedatectl set-timezone Asia/Kolkata


Create a user with non interactive shell.

$ adduser pankaj -s /sbin/nologin


Linux Access Control List:

ACL gives you flexibility and grenular level permission technique to manage file systems.

$ mkdir -p /home/root/PRE-PROD

## Now suppose you don't want user pankaj to have access to PRE-PROD folder
## you only want vikas to access it

$ cd /home/root
$ getfacl PRE-PRD

## add permission to specific user
$ setfacl -m "u:pankaj:rwx" /home/root/PRE-PROD

## for group
$ setfacl -m "g:pankaj:rwx" /home/root/PRE-PROD

$ setfacl -m "u:vikas:---" /home/root/PRE-PROD


Create a user with expiry date:

$ useradd pankaj
$ chage -l pankaj

## set expiry of the account
$ chage -E "2022-07-15" pankaj

## set number of days to expiry
$ chage -M 120 pankaj



SELINUX installation:

$ sudo yum install selinux* -y
$ sestatus
$ cat /etc/selinux/config | grep SELINUX 
$ cat /etc/selinux/config
  SELINUX=disabled
$cat /etc/selinux/config | grep SELINUX
$sestatus



Create Group in Linux :

$sudo useradd -m -d /users/username -s /bin/bash -g sysadmin -G wheel username
$
sudo useradd -u 1098 -d /users/username -s /bin/bash -g sysadmin -G wheel -m   username


Change user home directory, Primary Group, Shell, UID and GID:

$usermod -d /var/users/ -m username
$usermod -g sysadmin username  
$usermod -s /bin/ksh username
$usermod -u 666 -g 777 username


Create User in Linux by Specifying Home Directory, Primary group, Secondary group, Shell  :

$sudo useradd -m -d /users/username -s /bin/bash -g sysadmin -G wheel username

Change user home directory, Primary Group, Shell, UID and GID:

$usermod -d /var/users/ -m username
$usermod -g sysadmin username  
$usermod -s /bin/ksh username
$usermod -u 666 -g 777 username

See the user information:

$id username

killall command in Linux - To kill running process with the given name

killall PROCESS_NAME || echo "print this if Process was not running"

Synchronize Time on Linux Machine using Network Time Protocol (NTP) service:

ntpdate -u ntp_server_hostname
#Add ntp server details in this file 
/etc/ntp.conf

cat /etc/ntp.conf

server ntp_server.domain.com

See the file type of a file/directory:

$file file_name
#-s, --special-files
$file -sL 
/dev/sda1
$file file_names
$file directory_name/*

cut command in Linux:
cut is mainly used when you want to cut a part of a line based on some delimiter and send that data to standard output using pipe(| ) command.

$cat file.txt
hello amit
world in
java point

$cut -c 3 file.txt
l
r
v

$cut -b 1,2,3 file.txt
hel
wor
jav

$cat file.txt | cut -d " " -f 1 | sort -r
world
java
hello

$cat file.txt | head -n 2
hello amit
world in

$cat file.txt | tail -n 2
world in
java point

sdiff or vimdiff command in Linux:
When you want to compare file side by side we use sdiff command.
and when you want to only see the differ lines then use it with -s parameter

$sdiff file1.txt file2.txt

$sdiff -s file1.txt file2.txt

$vimdiff file1.txt file2.txt 

echo command to write a file:

echo "My name is satish kumar" > file1.txt

echo -e command to(-e enables interpretation) :
echo -e "\n$var1\n$var2\n"

tee command in Linux:
tee is very useful when you want to read the data on standard output and also want to write data to a file.

$df -h | tee disk_info_usage.txt

cat command in Linux to write a file:

$ cat << EOF > newfilename.txt
> My Name is satish kumar
> I am 21 years old
> I am from Mumbai
> EOF

$ cat newfilename.txt
 My Name is satish kumar
 I am 25 years old
 I am from Mumbai

When do we use colon (:-) in bash variable:
This will set the default variable value as '' if you are not providing any value to then either by declaring or by passing argument.

export username='ec2-user'
export USER_NAME=$(username:-'')




syncing two directories
rsync = remote synchronization
ssh deamon must be running
$ rsync -a Pictures/ satish@1.2.34.4:/home/rahul/

## Reverse
$rsync -a satish@1.2.34.4:/home/satish Pictures/

## Local
$ rsync -a Pictures/ /backups/Pictures


Boot, reboot, and shutdown a system safely

## For reboot
$
systemctl reboot    
$
systemctl reboot --force
$systemctl reboot --force --force

## For shutdown
$
systemctl poweroff
$systemctl poweroff --force
$systemctl poweroff --force --force

## Shutdown 00:00 to 23:59
$
sudo shutdown 02:00

## Shutdown after 15 minutes
$
sudo shutdown +15


$
sudo shutdown -r 02:00 ##reboot
$
sudo shutdown -r +15 ##reboot

### 
wall message
$
sudo shutdown -r +1 'Scheduled restart to do an offline-backup of our database'





Boot or change system into different operating modes





Install, configure and troubleshoot bootloaders







Use scripting to automate system maintenance tasks




Manage the startup process and services (In Services Configuration)

When we boot up linux some important application start automatically and can be in very specific order.
How does all this happen?
Well all this happens with the help init = intialization system
so how does this init system know to start up the system, what to do what program crashes and so on, it needs specific instruction in order to do its job, yup that's right we have Units

Init does many jobs and the logic part of it is written in Units

Units can be of various types such as
  • service
  • socket
  • device
  • timer


Service Units

Service Units has clear instructions about things such as what command to issue to startup the program, what to do if the program crashes, what command to issue when the program is restarted and so on, basically to manage lifecycle of certain application.

Startup processes and services


Diagnose and manage processes



Locate and analyze system log files



Schedule tasks to run at a set date and time

There are 3 ways to schedule tasks to run at a set time
  1. cron job
  2. anacron - small unit is -> day, week, month and also useful when you want to run the cron no matter when the power was on, cron job won't work if the system get power on after scheduled task time. cron can miss jobs when computer is power off.
  3. at - is foucused on task that should only runs once

cron



cron job
* = match all possible values (i.e., every hour)
, = match multiple values (i.e., 15,45)
- = range of values (i.e., 2-4)
/ = specific steps (i.e., */4)



anacron

daily, monthly, weekly, yearly
sudo vin /etc/anacrontab
3 10 test_job /usr/bin/touch /root/anacron_created_this
once every 3 days 
10 is delayed  mins that means if job has been missed and all the missed job should run on the delay of minutes

test_job is job identifier and then the command with full path
@weekly 10 test_job /usr/bin/touch /root/anacron_created_this
@monthly 10 test_job /usr/bin/touch /root/anacron_created_this

To check syntax
anacron -T 

at command
To run a job at 3:30 PM
$at 15:30
run some command at 15:30 PM 
/usr/bin/touch testfile
##ctrl+d
$ at 'August 20 2022'
$ at 'now + 30 minutes'

see what jos are schedule to run
$ atq
return job_number

#so next run at with -c parameter to see the command
$ at -c job_number
$ atrm job_number ## to remove cron


Verify completion of scheduled jobs




Update software to provide required functionality and security

Upgrading with package manager



Post a Comment

0 Comments