Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kuldeephry/cb883be324653be03cc885c8ceffe24e to your computer and use it in GitHub Desktop.

Select an option

Save kuldeephry/cb883be324653be03cc885c8ceffe24e to your computer and use it in GitHub Desktop.
Linux_Administrator_Daily_Tasks
----------------------------------------------------------------------------------------------------
#KVM
grep -i vmx /proc/cpuinfo #check if the CPU supports virtualization
lsmod | grep kvm #check if the kvm kernel module is loaded
To run KVM, you need a processor that supports hardware virtualization.
Intel and AMD both have developed extensions for their processors, deemed respectively Intel VT-x (code name Vanderpool) and AMD-V (code name Pacifica)
#If 0 it means that your CPU doesn't support hardware virtualization.
#If 1 or more it does - but you still need to make sure that virtualization is enabled in the BIOS.
$egrep -c '(vmx|svm)' /proc/cpuinfo
$cat /sys/hypervisor/properties/capabilities #if it is enabled or not from xen
$kvm-ok #If you see You can still run virtual machines, but it'll be much slower without the KVM extensions
INFO: Your CPU does not support KVM extensions
KVM acceleration can NOT be used
$egrep -c ' lm ' /proc/cpuinfo #If 0 is printed, it means that your CPU is not 64-bit. If 1 or higher it is 64-bit
$ uname -m
x86_64
----------------------------------------------------------------------------------------------------
$ wc -c file1.txt #the number of characters in a file
$ wc -l file1.txt #the number of lines in a file
----------------------------------------------------------------------------------------------------
# terminal1
$ ls > pipe2
$ mkfifo pipe5 -m700
$ ls -l > pipe5
$ rm pipe5
# terminal2
$ ls -lart pipe2 # list hidden files in the current directory
prw-rw-r-- 1 vagrant vagrant 0 Feb 25 15:33 pipe2
$ ls -lart pipe5
prwx------ 1 vagrant vagrant 0 Feb 25 20:52 pipe5
$ cat < pipe5
total 15828
prw-rw-r-- 1 vagrant vagrant 0 Feb 25 15:33 pipe2
prw-rw-r-- 1 vagrant vagrant 0 Feb 25 20:51 pipe4
prwx------ 1 vagrant vagrant 0 Feb 25 20:52 pipe5
-rw-rw-r-- 1 vagrant vagrant 16207833 Jan 22 22:02 terraform_0.12.20_linux_amd64.zip
----------------------------------------------------------------------------------------------------
pushd #stores a directory path in the directory stack
popd #removes the top directory path from the same stack
dirs #check directory stack
----------------------------------------------------------------------------------------------------
# unix/windows file editing
"/bin/bash^M: bad interpreter: No such file or directory"
fix: sed -i -e 's/\r$//' build_all.sh
----------------------------------------------------------------------------------------------------
The Bash shell feature that is used for matching or expanding specific types of patterns is called globbing
$ ls -l ????.txt #files whose names are four characters long
$ ls -l foot????.doc # files whose names are 8 characters long, first 4 characters are f, o, o and t and extension is doc
$ ls -l best.??? #all files with the name ‘test’ having any extension of three characters long
$ ls –lt #list down files /folders sorted with modified time
$ ls -l *.pl #all files of ‘pl’ extension
$ ls -l [p-s]* #all files and folders whose name contains p or q or r or s
$ ls -l [1-5]* #all files and folders whose name starts with any digit from 1 to 5
$ grep '^[P-R]' list.txt #lines from list.txt file that starts with P or Q or R
$ grep '[^A-C]' list.txt #lines from list.txt file that starts with A or B or C
$ grep [!P-R] list.txt #from list.txt file that starts with ‘P’ or Q or R
$ grep [!4-8] list.txt #lines from list.txt file that starts with any digit from 4 to 8.
$ grep a$ list.txt #lines from list.txt file that ends with ‘a’
$ grep 50$ list.txt #lines from list.txt file that end with the number 50
$ ls -l {?????.sh,*st.txt} #files whose names are 5 characters long and the extension is ‘sh’ or the last two characters of the files are ‘st’ and the extension is ‘txt’
$ rm {*.doc,*.docx} #delete all files whose extensions are ‘doc’ or ‘docx’
$ ls a*+(.bash|.sh) #filenames which are starting with character ‘a’ and has the extension ‘bash’ or ‘sh’
----------------------------------------------------------------------------------------------------
echo test > >(cat) #the output of echo would be redirected to the file that serves as the input to cat, and cat would produce the contents of that file on standard output
echo foo | cat -n
echo foo > >(cat -n) # emulate pipe above
The process substitution >(command) will be replaced by a file name.
This file name corresponds to a file that is connected to the standard input of the "command" inside the substitution
$ cat .profile | while read line; do ((counter1++)); done
$ echo $counter1
$ while read line; do ((count++)); done < <(cat ~/.profile)
$ echo $count
101
----------------------------------------------------------------------------------------------------
#Network Troubleshooting
Step 1: Check if your interface is configured
$ ifconfig
sudo resolvconf -u
Step 2: Setting up your interface
check if the drivers are loaded
$ dmesg | grep -3 -i eth
configure the interface
ifconfig eth0 128.42.14.176 netmask 255.255.255.0 up
Assign a Broadcast to Network Interface
ifconfig eth0 netmask 255.255.255.224
If the loopback interface is not up
ifconfig lo up
now be able to ping your own machine
$ ping -c 3 127.0.0.1
Step 3: Check if you can ping the gateway
ping the DNS server
Step 6: Setting up routing
# route add -net <naddr> netmask <maddr> eth0
# route add default gw <gaddr> eth0
setup the loopback route if it's missing
# route add -host 127.0.0.1 lo
$ ip route show
default via 10.0.2.2 dev eth0 proto dhcp metric 100
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100
192.168.18.0/24 dev eth1 proto kernel scope link src 192.168.18.9 metric 101
traffic to anywhere else should be forwarded through eth0 to the gateway at 10.0.2.2
traffic to 10.0.2.2 (the gateway to the public Internet) should be forwarded directly to its destination through eth0
traffic to anywhere within 192.168.18.0/24 (the local area network) should be forwarded directly to its destination through eth1
$ ip route get to 192.168.18.12 from 192.168.18.9 iif eth1
$ ip route get to 192.168.18.9 from 192.168.18.12 iif eth1
$ route -n
Step 7: Name resolution
3 files: /etc/host.conf, /etc/hosts, /etc/resolv.conf
/etc/host.conf:
order hosts,bind
multi on
/etc/hosts:
127.0.0.1 localhost loopback
<IPaddr> this.host.name
/etc/resolv.conf:
domain yourdept.yourcompany.com
search yourdept.yourcompany.com yourcompany.com
nameserver <domainaddr>
----------------------------------------------------------------------------------------------------
# ping a host
ping 192.168.0.2
#show routing table without resolving domain names
netstat -nr
# show informations about errors/collisions
netstat -ni
# show statistics about your network card
netstat -i -I em0
netstat -a
netstat -at
netstat -s
netstat -au
netstat -l
netstat -lu
netstat -lt
netstat -tulpn
netstat -plan
netstat -plan | grep ":80"
netstat -tunlp | grep ":80 "
List all TCP sockets and related PIDs
netstat -antp
netstat -anp
List all UDP sockets and related PIDs
netstat -anup
# find out on which port a program is running
netstat -ap | grep ssh
# find route to example.com
traceroute www.example.com
#find route to example.com using tcptraceroute (which uses tcp to discover path)
tcpdraceroute www.example.com
# The maximum number of hops can be adjusted with the -m flag.
traceroute -m 255 obiwan.scrye.net
# adjust the size of the packet that is sent to each hop by giving the integer after the hostname
traceroute google.com 70
Specify Gateway
sudo traceroute -g 10.0.2.2 yahoo.com
Specify Source Interface
sudo traceroute -i eth0 yahoo.com
Autonomous Systems
traceroute -A yahoo.com
tracepath yahoo.com
tracepath -n yahoo.com
tracepath -b yahoo.com
sets the initial packet length
tracepath -l 28 yahoo.com
set maximum hops (or maximum TTLs) to max_hops
tracepath -m 5 yahoo.com
set the initial destination port to use
tracepath -p 8081 yahoo.com
show fully qualified domain name (FQDN)
$ hostname -f
short hostname
$ hostname
real-time view of the current state of your system
$ htop
$ timedatectl
$ timedatectl list-timezones
$ sudo timedatectl set-timezone 'Africa/Lubumbashi'
sudo apt-get install -y mtr
mtr www.google.com
mtr --report google.com
# show connected sockets
sockstat -c
# show listening sockets and processes
sockstat -l
# show arp table
arp -a
arp -na
# delete a record from arp table
arp -d 192.168.0.2
# add a static record in arp table
arp -s 192.168.0.2 00:10:b5:99:bf:c4
# listen on em0 network interface and sniff packets that pass via em0
$ sudo arp -i eth0
find out reachability of an IP on the local Ethernet with arping i.e send ARP request 192.168.1.1:
$ sudo arping -I eth0 -c 3 192.168.18.12
$ sudo arping -I eth1 -c 3 192.168.18.12
Find duplicate IP
$ sudo arping -D -I eth1 -c 3 192.168.18.12
$ file *
20:30: empty
file1: ASCII text
file2: ASCII text
$ file -b symbolic_test1.txt
symbolic link to test1.txt
Check what ethernet devices exist currently
# ls -al /sys/class/net
# ls -Rl
list hidden files and the contents of all subdirectories
ls -aR /home/username
# ls -pu
see eth* devices
# ls -al /sys/class/net
Get the PCI address of the NIC
# lspci | grep Mellanox
# lspci | grep Eth
iw distinguishes between wireless LAN hardware devices (the physical layer, referred to as phy) and the network interface configured to use that hardware (e.g. wlan0,
similar to an Ethernet eth0 interface). To see the list of devices, and interfaces for each device
#iw dev
**MAC_ADDRESS 08:00:27:e3:b0:01
$ ip link show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:e3:b0:01 brd ff:ff:ff:ff:ff:ff
$ ip addr show eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:e3:b0:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.253/24 brd 192.168.1.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fee3:b001/64 scope link
valid_lft forever preferred_lft forever
#temporarily set the IP address
ifconfig eth0 192.168.8.185
ifconfig eth0 192.168.8.185 netmask 255.255.255.0 up
#temporarily change the MAC address
ifconfig eth0 down hw ether AA:BB:CC:DD:EE:FF && ifconfig eth0 up
ifconfig eth0 netmask 255.255.255.0
ifconfig eth0 broadcast 192.168.70.255
ip addr show -> List IP address of the server
ip addr show eth0
ip addr show eth1 | grep "inet "
ip addr add 10.132.1.1/24 dev eth1 -> Add a new IP4 address
ip addr show eth1 -> confrm that the new address is available on the interface
ip link set eth2 down -> bring an interface down
ip link set eth2 up
ip -s link->view basic network statistics on all interfaces
ip -s link ls eth0 ->see the statistics for the eth0 interface
ip -s -s link ls eth0 ->see additional info
ss -t ->show established TCP connections
ss -u ->show established UDP connections
ss -A tcp
ss -x
ss -ltn ->see which ports are listening for connections
ss -nt
ss -ltn
ss -ua
ss -a -A udp
ss -lun ->udp
ss -s->prints out the statistics
#Install sysstat package
# /etc/default/sysstat ENABLED="true"
#sudo service sysstat restar
vmstat 1 99999 ->the system statistics every second, for the number of times specifed (99999 in this instance)
vmstat –a 1 99 ->show memory usage information
vmstat -a -S M 1 9 -> reformat in Mega Bytes
vmstat 1 99999 ->gather information for disks and other block devices
vmstat -d -w
iostat 1 9 ->CPU information and disk information for all devices
iostat -d -p sda 1 9-> show information for device sda with disk statistics
sar -u 1 30 -> display CPU statistics every second for 30 seconds
sar -r 1 30 -> display memoru statistics every second for 30 seconds
sar -b 1 30 -> display block device statistics every second for 30 seconds
#add the appropriate BeeGFS repositories
wget -o /etc/yum.repos.d/beegfs-rhel7.repo http://www.beegfs.com/release/beegfs_2015.03/dists/beegfs-rhel7.repo
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | apt-key add -
sudo permission denied
The redirection to a file is handled by bash. It does therefore not inherit permissions granted by sudo.
"sudo tee" for writing to a file as root.
lsblk -f -> when used with the -f option, it prints file system type on partitions
sudo file -sL /dev/sdb1 -> file system type on partitions
lsblk -f
lsblk -l
lsblk --scsi
lsblk -o name,type,fstype,label,partlabel,model,mountpoint,size
# generate traces of the i/o traffic on block devices
"sudo blktrace -d /dev/sda -o - | blkparse -i -"
#writing ISO usb bootable
sudo umount /dev/sdX
sudo dd if=/path/to/ubuntu.iso of=/dev/sdX bs=4M && sync
sdx-> lslbk command
sync-> sync bit is important as dd can return before the write operation finishes.
# mount all file systems on /etc/fstab
mount -a
mount -fav
cat /proc/mounts
# format linux swap partition
mkswap
man -K date -> display a list of all manual pages containing the keyword "date"
# Clone / Compile specific kernel
sudo git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git linux -> latest stable kernel to "linux" directory
git tag -l | grep v4.9.1 -> find specific kernel version
git checkout -b kernel490 v4.9.1 -> switch to kernel with custom name "kernel490"
#crontab
Display scheduled jobs for the specified user
crontab -l -u vagrant
# Display Cron Table
ls -la /etc/cron*
crontab -l
# Delete All Cron Jobs
crontab -r
crontab -e
This will append the current date to a log file every minute.
* * * * * /bin/date >> /tmp/cron_output
at specific time
00 15 * * 4 sh /root/test.sh
35 21 * * 7 /bin/date >> /tmp/cron_output
every 5 minutes
*/5 * * * * mycommand
an hourly cron job but run at minute 15 instead (i.e. 00:15, 01:15, 02:15 etc.):
15 * * * * [command]
once a day, at 2:30am:
30 2 * * * [command]
once a month, on the second day of the month at midnight (i.e. January 2nd 12:00am, February 2nd 12:00am etc.):
0 0 2 * * [command]
on Mondays, every hour (i.e. 24 times in one day, but only on Mondays):
0 * * * 1 [command]
three times every hour, at minutes 0, 10 and 20:
0,10,20 * * * * [command]
# Stop download Mon-Fri, 6am
0 6 * * 1,2,3,4,5 root virsh shutdown download
monitor whether cron job runs
tail -f /var/log/syslog | grep CRON
tail /var/log/cron
#the login history of users
last | grep sysadmin
#the system last rebooted
last reboot
#brctl show -> Bridge connections
--------------------------------------------------------------------------------------------------------------------
#LVM
pvdisplay
pvck
pvs
lvscan
lvdisplay
lvmdiskscan
vgchange
vgscan -a y
e4defrag -cv /path/to/myfiles (defrag folder )
$ sudo pvcreate /dev/sdb
$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <63.00g 4.00m
/dev/sdb vg_iscsi lvm2 a-- <30.00g 0
$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name vg_iscsi
PV Size 30.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 7679
Free PE 0
Allocated PE 7679
PV UUID hG93NW-gvRB-njUP-pgj8-omRF-YzFe-rTMWOz
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <63.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 16127
Free PE 1
Allocated PE 16126
PV UUID rFHI2D-fvZw-Mf2P-gKTC-ZTwt-vdiY-TEQc14
$ sudo vgcreate vg_iscsi /dev/sdb
$ sudo vgdisplay
--- Volume group ---
VG Name vg_iscsi
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <30.00 GiB
PE Size 4.00 MiB
Total PE 7679
Alloc PE / Size 7679 / <30.00 GiB
Free PE / Size 0 / 0
VG UUID j63noX-S9I0-5Gp0-3FPg-IZ23-oZNK-6qpb7X
$ sudo lvcreate -l 100%FREE -n lv_iscsi vg_iscsi
[vagrant@vg-suricata-30 ~]$ sudo lvscan
ACTIVE '/dev/vg_iscsi/lv_iscsi' [<30.00 GiB] inherit
ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
ACTIVE '/dev/centos/home' [<20.01 GiB] inherit
ACTIVE '/dev/centos/root' [40.98 GiB] inherit
$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/vg_iscsi/lv_iscsi
LV Name lv_iscsi
VG Name vg_iscsi
LV UUID exEdIG-s2bK-vFEa-fD3X-dplu-q2W3-1rTXsE
LV Write Access read/write
LV Creation host, time vg-suricata-30, 2019-12-18 12:35:56 +0000
LV Status available
# open 0
LV Size <30.00 GiB
Current LE 7679
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
$ sudo vgremove vg_iscsi
$ sudo pvremove /dev/sdb
--------------------------------------------------------------------------------------------------------------------
#list open files
lsof
#list open files owned by user1
lsof -u user1
#list open file via tcp
lsof -i TCP:1-1024
lsof -i TCP:80
PID 27808
lsof -Pan -p 27808 -i
lsof -p 2
# troubleshooting #1
find all the opened files and processes along with the one who opened them
# lsof –p PID
Count number of files & processes
# lsof -p 4271 | wc -l
Check the currently opened log file
lsof –p | grep log
Find out port number used by daemon
# lsof -i -P |grep 4271
# find out what running processes are associated with each open port on Linux
netstat -nlp|grep 9000
sudo ss -lptn 'sport = :80'
sudo netstat -nlp | grep :80
sudo lsof -n -i :80 | grep LISTEN
fuser 3306/tcp
fuser 80/tcp
ss -tanp | grep 6379
fuser -v -n tcp 22
sudo netstat -ltnp | grep -w ':80'
netstat -tulpn | grep :80
netstat -tulpn
ls -l /proc/1138/exe
sudo ss -tulpn
sudo ss -tulpn | grep :3306
fuser 7000/tcp
ls -l /proc/3813/exe
man transmission
whatis transmission
# find out current working directory of a process pid 3813
ls -l /proc/3813/cwd
pwdx 3813
# Find Out Owner Of a Process on Linux
cat /proc/3813/environ
grep --color -w -a USER /proc/3813/environ
lsof -i :80 | grep LISTEN
# The file /etc/services is used to map port numbers and protocols to service names
grep port /etc/services
grep 443 /etc/services
#Start a Linux Process or Command in Background
$ tar -czf home.tar.gz .
$ bg
$ jobs
OR
$ tar -czf home.tar.gz . &
$ jobs
#Keep Linux Processes Running After Exiting Terminal
$ sudo rsync Templates/* /var/www/html/files/ &
$ jobs
$ disown -h %1
$ jobs
OR
$ nohup tar -czf iso.tar.gz Templates/* &
$ jobs
#Detach a Linux Processes From Controlling Terminal
firefox </dev/null &>/dev/null &
count &
jobs
fg
bg
fg %#
jobs -l
count 2> /dev/null &
2 is the file descriptor of stderr
the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively.
a number 0 = standard out (i.e. STDIN)
a number 1 = standard out (i.e. STDOUT)
a number 2 = standard error (i.e. STDERR)
if a number isn't explicitly given, then number 1 is assumed by the shell (bash)
"/dev/null" is the null device it takes any input you want and throws it away.
It can be used to suppress any output
"2>/dev/null"
Redirect STDERR to /dev/null (nothing shows up on console)
The general form of this one is "M>/dev/null", where "M" is a file descriptor number.
This will redirect the file descriptor, "M", to "/dev/null".
"2>&1"
The general form of this one is "M>&N", where "M" & "N" are file descriptor numbers.
It combines the output of file descriptors "M" and "N" into a single stream.
"2>&-"
closing a file descriptor used with redirection
The general form of this one is "M>&-", where "M" is a file descriptor number.
This will close output for whichever file descriptor is referenced, i.e. "M"
"|&"
Redirect STDERR and STDOUT to STDIN
This is just an abbreviation for "2>&1 |"
"&>/dev/null"
Redirect both STDERR & STDOUT to /dev/null (nothing shows up on console)
This is just an abbreviation for >/dev/null 2>&1.
It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null
">/dev/null"
Redirect STDOUT to /dev/null (only STDERR shows on console)
This is just an abbreviation for 1>/dev/null.
It redirects file descriptor 1 (STDOUT) to /dev/null.
"command > /dev/null 2>&1 &"
Run command in the background, discard stdout and stderr
"command >> /path/to/log 2>&1 &"
Run command and append stdout and stderr to a log file
Hide standard and error outputs
./command >/dev/null 2>&1
Hide standard output
./command >/dev/null
sends 2 (stderr) into 1 (stdout), and sends stdout to file.log
command > file.log 2>&1
Hide standard and error outputs and release terminal (run the command in background)
./command >/dev/null 2>&1 &
prevent standard output and error output, redirecting them both to /dev/null
script > /dev/null 2>&1
#see how long every program and service takes to start ups
systemd-analyze blame
#Identify processes using files, directories, or sockets.Who is Using a File or Directory
$ fuser .
$ fuser -v ./
Check Processes Using TCP/UDP Sockets
fuser -v -n tcp 5000
the processes that are using my 'home' directory
$ fuser ~
$ fuser ~ -v
check for the root directory
$ fuser /
$ fuser / -v
$ fuser -v /home/ismail
$ fuser -v -m /home/ismail/.bashrc
$ fuser -v -n tcp 8080
$ fuser -v -n udp 53
kill this TCP listener, you can use option -k
$ fuser -i -k 8080/tcp
shows all processes at the (local) TELNET port
$ fuser telnet/tcp
list signals
$ fuser -l
STOP a process
$ fuser -i -k STOP [FILE/DIRECTORY]
kills all processes accessing the file system /home
$ fuser -km /home
configure it to start Automatically while system start-up
# chkconfig tgtd on
verify that the run level configured correctly for the tgtd service
# chkconfig --list tgtd
$ who -r
run-level 5 2018-07-14 17:16
$ runlevel ->‘N’ indicates that the runlevel has not been changed since the system was booted. And, "number" is the current runlevel
The last run level, and the current run level.
$ runlevel
md5sum ubuntu-6.10-desktop-i386.iso
sha256sum ubuntu-9.10-dvd-i386.iso
#watch is used to run any designated command at regular intervals.
watch -n 5 "ls -l | wc l"
# detect driver hardware problems
dmesg | more
The output of dmesg is maintained in the log file
/var/log/dmesg
cat /var/log/dmesg | less
data from /dev/kmsg
use syslog
# dmesg -S
# limit the output to only error and warnings
dmesg --level=err,warn
# dmesg produce timestamps
dmesg --level=err -T
dmesg -T | grep -i eth0
dmesg --level=err,warn -T | grep -i eth0
# limit dmesg's output only to userspace messages
dmesg -u
# timestmaps along with decode facility and levels in dmesg command output
dmesg -Tx
Supported log levels (priorities):
emerg - system is unusable
alert - action must be taken immediately
crit - critical conditions
err - error conditions
warn - warning conditions
notice - normal but significant condition
info - informational
debug - debug-level messages
dmesg -TL -f kern
dmesg -TL -f daemon
Supported log facilities:
kern - kernel messages
user - random user-level messages
mail - mail system
daemon - system daemons
auth - security/authorization messages
syslog - messages generated internally by syslogd
lpr - line printer subsystem
news - network news subsystem
# verify vt-d is ON
"dmesg | grep Virtualization"
# dmesg | grep -i memory
# dmesg | grep -i dma
# dmesg | grep -i usb
# dmesg | grep -E "memory|dma|usb|tty"
# dmesg | grep -E "sda|dma"
Clear dmesg logs
# dmesg -C
Display colored messages
# dmesg -L
Monitor real time dmesg logs
# dmesg --follow
# dmesg -Tx --follow
# watch "dmesg | tail 7-20"
Display raw message buffer
# dmesg -r
#virtual machine check
$ dmesg |grep -i hypervisor
$ dmidecode -s system-manufacturer
#32x 64x query
uname –m
arch
#linux version
lsb_release -a
cat /etc/issue
cat /etc/os-release
cat /etc/lsb-release
cat /etc/*-release
cat /proc/version
hostnamectl set-hostname server1
$ hostname --ip-address
$ hostname --all-ip-addresses
#sudo user
echo "stack ALL=(ALL) NOPASSWD: ALL" |sudo tee -a /etc/sudoers
# append text with non-root user
echo "deb http://research.cs.wisc.edu/htcondor/ubuntu/8.8/bionic bionic contrib" |sudo tee -a /etc/apt/sources.list
#extracting entire archive
tar -zxvf backup.tar.gz
tar -xf file_name.tar.gz --directory /target/directory
tar -zxvf /tmp/onos-1.12.0.tar.gz --strip-components 1 --directory /opt --one-top-level=onos
tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz --one-top-level=mysql57 --strip-components 1
tar zxvf ugly_name.tgz --one-top-level=pretty_name
#extract .xz file
unxz tor-browser-linux32-5.5.4_en-US.tar.xz
tar xvf tor-browser-linux32-5.5.4_en-US.tar
#extract .bz2 file
bzip2 -dk FileZilla_3.29.0_x86_64-linux-gnu.tar.bz2
tar xvf FileZilla_3.29.0_x86_64-linux-gnu.tar
#extract .zip file
unzip terraform_0.11.7_linux_amd64.zip -d terraform
#extract .rar file
unrar e extract.rar r
# create user home directory backup
tar cvf filename.tar /home/vagrant/
# show which files were changed
tar dvf filename.tar
# update the changed files
tar uvf filename.tar
# make smaller backup
gzip filename.tar
#format a USB storage device with FAT32 file system
mkfs –t vfat <USB-device-mount-point>
# mount -o loop,offset=$((10860003 * 512)) disk.img /mnt
#find out the USB device mount point
fdisk -l
#unmount the drive,you can’t format a mounted drive.
sudo umount /dev/sdb1
sudo mkfs.vfat /dev/sdb1
sudo mkfs.ntfs /dev/sdb1
mkfs.ext4 <USB-device-mount-point>
mkfs.ntfs <USB-device-mount-point>
#Set label name to USB drives
sudo mkfs.vfat /dev/sdb1 -n sk
#without entering the root password
"su: keeps the environment of the old/original user even after the switch to root
"su -" creates a new environment (as dictated by the ~/.bashrc of the root user), similar to the case when you explicitly log in as root user from the log-in screen.
"su -l" pass more arguments
"su -c" ,su [target-user] -c [command-to-run] a command that you want to run after switching to the target user.
run "sudo -s" or "sudo -i" to mimic "su" or "su -l"
# switching to root using sudo -i (or sudo su) cancels auditing/logging
# when a sudo command is executed, the original username and the command are logged
"sudo su"
"sudo -i"
su is equivalent to sudo -i
gives you the root environment, i.e. your ~/.bashrc is ignored.
simulates a login into the root account
Your working directory will be /root
will read root's .profile
"sudo -s"
gives you the user's environment, so your ~/.bashrc is respected.
launches a shell as root
doesn't change your working directory
"sudo bash"
runs bash as a super user
list user vagrant's full command line of processes
$ top -c -u vagrant
ignore idle processes
$ top -i -u vagrant
updated with 5 secs intervals, including child processes
$ top -u vagrant -c -d 5 -S
The load averages shown by these tools is read /proc/loadavg file
cat /proc/loadavg
mount -l
lshw -short
file -Ls
dmesg
denyhosts
vmstat
w
uptime
ps
free
iostat
pmap
paste
uname
sudo
mkdir
chown
ptree
pkill
killall
kill -KILL PID
all signals
$ kill -l
stop and restart process
$ kill -1 13980
1 SIGHUP
9 SIGKILL stop process without letting gracefully
15 SIGTERM stop process
$ touch mylog
$ ls -lai mylog
3145761 -rw-rw-r-- 1 vagrant vagrant 0 Mar 27 21:01 mylog
update the access time of existing file
$ touch -c mylog
Change file access time - 'a' of existing file
$ touch -a mylog
Change the modified time '-m' of existing file
$ touch -m mylog
$ touch -am mylog
Set a specific access/modify time instead of current time, specify the datetime in format [[CC]YY]MMDDhhmm[.ss]
$ touch -c -t 201603051015 mylog
$ touch -c -d '5 Jan 2009' mylog
$ touch -c -d '20:30' myfile
Use the timestamp of another file as reference
$ touch myfile -r mylog
use the timestamps of 'apl' for 'apl.c'
$ touch apl.c -r apl
$ ls -lai mylog
3145761 -rw-rw-r-- 1 vagrant vagrant 0 Mar 27 21:06 mylog
$ stat mylog
File: 'mylog'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fc00h/64512d Inode: 3145761 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ vagrant) Gid: ( 1000/ vagrant)
Access: 2019-03-27 21:07:54.953000000 +0000
Modify: 2019-03-27 21:07:54.953000000 +0000
Change: 2019-03-27 21:07:54.953000000 +0000
Birth: -
force touch to not create any new file
$ touch -c newfile
$ ls -l newfile
ls: cannot access 'newfile': No such file or directory
$ stat newfile
stat: cannot stat 'newfile': No such file or directory
# create symbolic link
ln -s test1.txt symbolic_test1.txt
stat symbolic_test1.txt
touch -c -d '5 Jun 2001' -h symbolic_test1.txt
stat symbolic_test1.txt
# detect symbolic link
touch a.xt
ln -s a.txt b.txt
stat b.txt
file b.txt
lscpiu
stat file.txt
#standard output activities for each available processor
mpstat
mpstat -P ALL 2 2
df –h # view the amount of free disk space
df -i # view number of inodes in the system
df -kl # get a detail description on disk space usage
#see how much space /some/dir is consuming
du -sh /some/dir
#List all running processes containing the string stuff
cat /proc/cpuinfo
grep "physical id" /proc/cpuinfo | wc -l
cat /proc/meminfo
grep MemTotal /proc/meminfo | awk '{FS=":"}{print $2 }' | awk '{print $1/1024/1024}'
cat /proc/zoneinfo
cat /proc/mounts
cat /etc/issue
# missing files
$ sudo ls -lai /lost+found/
# errors about a corrupt superblock on the drive
$ e2fsck -b 8193
$list files sorted by size
ls –lSr
ls -il
#get pid of my_app
my_app & $!
pidof lighttpd -> Find PID of A Program/Command
pidof -s php5-cgi
pidof -x fetch-data -> Get pids of scripts
pidof -o 4242 lighttpd -> ignore or omit processes,useful to ignore calling shell or shell script or specific pid.
# history
The !! command history expansion executes the previous command
#curl -IL http://localhost
HTTP/1.1 200 OK
Server: nginx/1.10.2
curl -Is http://www.google.com | head -n 1
curl -sSf http://example.org > /dev/null
curl -XGET 'localhost:9200/?pretty'
curl -X PUT "http://127.0.0.1:9200/mytest_index"
# check if apache is running
curl -sf http://webserver/check_url
# process was holding a particular port open
ss -tp state listening sport = :80 | grep httpd
# check a particular process id
lsof -p 1036 -P | grep 'TCP \*:80'
$ echo "The process id is" $$
$ echo "The process id is" $$$$
# check what process is listening
$ sudo fuser -n tcp 22
22/tcp: 1088 14324 14354
echo $SHELL -> determine current shell type
cat /proc/cpuinfo | grep 'vmx\|svm' -> VT-x/AMD-v virtualization is enabled in BIOS
# testing nginx
journalctl -u nginx.service
nginx -t
sudo ss -tulpn # Verify that port 80 or 443
curl -I http://10.21.136.13
curl http://10.21.136.13
dig +short localhost @8.8.8.8
journalctl -b ->all of the journal entries that have been collected since the most recent reboot
journalctl --list-boots
journalctl -b -1 -> see the journal from the previous boot
journalctl --since "2015-01-10 17:15:00"
journalctl --since "2015-06-26 23:15:00" --until "2015-06-26 23:20:00"
journalctl --since yesterday
journalctl --since 09:00 --until "1 hour ago"
journalctl -u nginx.service -> see all of the logs from an Nginx unit on our system
journalctl -u nginx.service --since today
journalctl _PID=8088
id -u www-data
33
journalctl _UID=33 --since today
journalctl /usr/bin/bash
journalctl -k ->Kernel messages, those usually found in dmesg output
journalctl -n 20 ->see with a number after the -n
journalctl --disk-usage
sudo journalctl --vacuum-size=1G
journalctl -f -> continuously prints log messages
journalctl -u mysql.service -f
------------------------------------------------------------------------------------------------
Job for autofs.service failed because a configured resource limit was exceeded. See "systemctl status autofs.service" and "journalctl -xe" for details.
# systemctl start autofs
# systemctl is-active autofs
# ps -aux | grep -i autofs | grep -v grep
------------------------------------------------------------------------------------------------
scp [email protected]:foobar.txt /some/local/directory-> Copy the file "foobar.txt" from a remote host to the local host
scp foobar.txt [email protected]:/some/remote/directory -> Copy the file "foobar.txt" from the local host to a remote host
scp [email protected]:/some/remote/directory/foobar.txt [email protected]:/some/remote/directory/ ->Copy the file "foobar.txt" from remote host "rh1.edu" to remote host "rh2.edu"
top -> Checking the Priority of Running Processes
ps -o pid,comm,nice -p 594 -> Checking the Priority of Running Processes
ps -fl -C "perl test.pl" -> The “NI” column in the ps command output indicates the current nice value (i.e priority) of a process.
ps -p 2053 -o comm=
list services running as root
$ ps aux | grep root
ps aux | grep 3813
ps -eo pid,user,group,args,etime,lstart | grep '[3]813'
ps aux | grep '[1]616'
ps -eo pid,user,group,args,etime,lstart | grep '[1]616'
ps aux | grep stuff
The init process, with process ID 1, which does nothing but wait around for its child processes to die.
Usually started for /etc/inittab
$ ps -ef| grep init
# see the name of the process
$ sudo ps 1088 14324 14354
# CPU time, page faults of child processes
ps -Sla
$ ps -lu vagrant
memory information long format
$ ps -lma
signal format
$ ps -sx
controlling terminal
$ ps --tty 1 -s
list of command line arguments
pstree -a
show PIDS for each process name
pstree -p
sort processes with the same ancestor by PID instead of by name,numeric sort
pstree -n
pstree -np
find out the owner of a process in parenthesis
pstree -u
pstree -u vagrant
pstree -unp vagrant
highlight the current process and its ancestors
pstree -h
highlight the specified process
pstree -H 60093
find ID of a process owned by a specific user
$ pgrep -u vagrant sshd
$ pgrep -u vagrant -d:
list process names
$ pgrep -u vagrant -l
$ pgrep -u vagrant -a
count of matching processes
$ pgrep -c -u vagrant
# priority levels between -20 and 19
nice -10 perl test.pl -> test.pl is launched with a nice value of 10 when the process is started
nice --10 perl test.pl -> Launch a Program with High Priority
renice -n -19 -p 3534 -> Change the Priority of a Running Process
/etc/security/limits.conf -> set the default nice value of a particular user or group
gpg --verify gnupg-2.2.3.tar.bz2.sig gnupg-2.2.3.tar.bz2 -> check the signature of the file gnupg-2.2.3.tar.bz2
$ systemd-analyze blame ->see how long every program and service takes to start up
-----------------------------------------------------------------------------------------------------
PARSING JSON FILE
sudo apt-get install -y jq
curl -s 'https://api.github.com/users/lambda' | jq -r '.name'
grep -w \"key_name\" /vagrant/test.json |tail -1 | cut -d\" -f4
grep -w \"author\" /vagrant/test.json |tail -1 | cut -d\" -f4
$ FOOBAZ="tester"
$ jq -n --arg foobaz "$FOOBAZ" '{"foobaz":$foobaz}' > test1.json
$ cat test1.json
export $(jq -r '@sh "FOO=\(.foo) BAZ=\(.baz)"') #fill environment variables from JSON object keys (e.g. $FOO from jq query ".foo")
echo '{ "foo": 123, "bar": 456 }' | jq '.foo' #print out the foo property
apod_url=$(curl -s https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY | jq -r '.hdurl') #get the URL of the current Astronomy Picture of the Day (APOD)
echo '{ "Version Number": "1.2.3" }' | jq '."Version Number"' #if a property has a spaces or weird characters
echo '[1,2,3]' | jq '.[]' #how iteration works
echo '[ {"id": 1}, {"id": 2} ]' | jq '.[].id' #access a property on each item
echo '{ "a": 1, "b": 2 }' | jq '.[]' #the value of each key/value pair
----------------------------------------------------------------------------------------------------
bootstrap.sh
parted /dev/sdb mklabel msdos
parted /dev/sdb mkpart primary 512 100%
mkfs.xfs /dev/sdb1
mkdir /mnt/disk
mount /mnt/disk
# Format the /dev/sdb partition with XFS filesystem and with a GPT partition table
sudo parted -s /dev/sdb mklabel gpt mkpart primary xfs
sudo mkfs.xfs /dev/sdb -f
sudo blkid -o value -s TYPE /dev/sdb
# list disk UUIDs
ls -l /dev/disk/by-id
$ fdisk -v
$ sudo fdisk -l
$ sudo fdisk -l /dev/sda1
-----------------------------------------------------------------------------------------------------
cut -c3 -> print the character from each 3rd line as a new line of output.
cut -c2,7 -> Display the 2nd and 7th character from each line of text
cut -c-4 -> Display the first four characters from each line of text
cut -c13- -> Print the characters from thirteenth position to the end.
cut -d' ' -f4 -> Given a sentence, identify and display its fourth word. Assume that the space (' ') is the only delimiter between words.
cut -d' ' -f1-3 -> Given a sentence, identify and display its first three words. Assume that the space (' ') is the only delimiter between words.
cut -f 1-3 -> Given a tab delimited file with several columns (tsv format) print the first three fields.
cut -f2- -> Given a tab delimited file with several columns (tsv format) print the fields from second fields to last field.
-----------------------------------------------------------------------------------------------------
uniq -ci -> count the number of times each line repeats itself (only consider consecutive repetions).compare consecutive lines in a case insensitive manner
uniq -u -> display only those lines which are not followed or preceded by identical replications
Given a text file, count the number of times each line repeats itself (only consider consecutive repetions).
Display the count and the line, separated by a space.
uniq -ci | cut -c7-
-----------------------------------------------------------------------------------------------------
head -n 20 -> Display the first lines of an input file.
head -c20 -> Display the first characters of an input file.
head -n 22 | tail -n +12 -> Display the lines (from line number 12 to 22, both inclusive) of a given text file
# print the lines between 5 and 10, both inclusive
cat filename | head | tail -6
-----------------------------------------------------------------------------------------------------
tail -n 20 | tail -n +12 -> Display the last lines of an input file.
tail -c 20 -> Display the last characters of an input file
-----------------------------------------------------------------------------------------------------
tr '()' '[]' -> In a given fragment of text, replace all parentheses with box brackets
tr -d [:lower:] -> In a given fragment of text, delete all the lowercase characters
tr -s ' ' -> In a given fragment of text, replace all sequences of multiple spaces with just one space
-----------------------------------------------------------------------------------------------------
sort -> Given a text file, order the lines in lexicographical order.
sort -r -> Given a text file, order the lines in reverse lexicographical order
sort -n -> the lines reordered in numerically ascending order
sort -nr -> The text file, with lines re-ordered in descending order (numerically).
given a file of text,in TSV (tab-separated) format.Rearrange the rows of the table in descending order of the values
sort -t$'\t' -rnk2
given a file of tab separated weather data (TSV). There is no header column in this data file.Sort the data in ascending order
sort -nk2 -t$'\t'
given a file of pipe-delimited weather data (TSV). There is no header column in this data file.
sort -nrk2 -t$'|'
-----------------------------------------------------------------------------------------------------
paste -s -> Given a CSV file where each row contains the name of a city and its state separated by a comma.replace the newlines in the file with tabs
paste - - - -> given a CSV file where each row contains the name of a city and its state separated by a comma, restructure the file in such a way, that three consecutive rows are folded into one, and separated by tab.
paste -s -d ";" -> given a CSV file where each row contains the name of a city and its state separated by a comma.replace the newlines in the file with semicolon
paste - - - -d ";" -> given a CSV file where each row contains the name of a city and its state separated by a comma. restructure the file so that three consecutive rows are folded into one line and are separated by semicolons
-----------------------------------------------------------------------------------------------------
grep -v -c -e "that" -> find out how many lines that does not match the pattern
grep -w "the" -> Output only those lines that contain the word 'the'.
grep -iw "the" -> Output only those lines that contain the word 'the'. The search should NOT be case sensitive.
grep -viwe "that" -> Only display those lines that do NOT contain the word 'that'.
grep -Eiw "th(e|ose|en|at)" < /dev/stdin -> display all those lines which contain any of the following words "the,that,then,those" .The search should not be sensitive to case. Display only those lines of an input file, which contain the required words.
grep '\([0-9]\) *\1' -> Given an input file, with N credit card numbers,grep out and output only those credit card numbers which have two or more consecutive occurences of the same digit (which may be separated by a space, if they are in different segments). Assume that the credit card numbers will have 4 space separated segments with 4 digits each
top 10 IP addresses in the log file.
grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" access.log | uniq -ci | sort -nr | head -n10
ifconfig -a | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
-----------------------------------------------------------------------------------------------------
user@host: $ cat<<EOF > file.txt
$ > 1 line
$ > other line
$ > n line
$ > EOF
user@host:
-----------------------------------------------------------------------------------------------------
# append text
cat<<EOF | sudo tee -a ceph.conf
public network = 192.168.18.0/24
osd pool default size = 2
EOF
-----------------------------------------------------------------------------------------------------
sudo install consul /usr/bin/consul
(
cat <<-EOF
[Unit]
Description=consul agent
Requires=network-online.target
After=network-online.target
[Service]
Restart=on-failure
ExecStart=/usr/bin/consul agent -dev
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
) | sudo tee /etc/systemd/system/consul.service
-----------------------------------------------------------------------------------------------------
cat <<EOT | sudo tee /lib/systemd/system/procenv.service
[Unit]
Description=Display systemd environment
[Service]
Type=oneshot
ExecStart=/usr/bin/procenv --file=/tmp/procenv-systemd.log
EOT
-----------------------------------------------------------------------------------------------------
> outputs to a file
>> appends to a file
< reads input
<<Here tells the shell that you are going to enter a multiline string until the "tag" Here. You can name this tag as you want, it's often EOF or STOP.
"EOF" is known as a "Here Tag"
The redirection operators "<<" and "<<-" both allow redirection of lines contained in a shell input file,
known as a "here-document", to the input of a command.
The format of here-documents is:
<<[-]word
here-document
delimiter
If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter.
This allows here-documents within shell scripts to be indented in a natural fashion.
-----------------------------------------------------------------------------------------------------
# Assign multi-line string to a shell variable
# The $sql variable now holds the new-line characters
# verify with echo -e "$sql"
sql=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='baz'
EOF
)
-----------------------------------------------------------------------------------------------------
#Pass multi-line string to a file in Bash
$ cat <<EOF > print.sh
#!/bin/bash
echo \$PWD
echo $PWD
EOF
-----------------------------------------------------------------------------------------------------
# Pass multi-line string to a pipe in Bash
$ cat <<EOF | grep 'b' | tee b.txt
foo
bar
baz
EOF
-----------------------------------------------------------------------------------------------------
$ sudo tee <<EOF /etc/somedir/foo.conf >/dev/null
# my config file
foo=bar
EOF
-----------------------------------------------------------------------------------------------------
echo -e "
Home Directory: $HOME \n
hello world 1 \n
hello world 2 \n
line n... \n
" > file.txt
-----------------------------------------------------------------------------------------------------
echo write something to file.txt | cat > file.txt
cat >file.txt <<< Write something here
# see the line numbers
cat -n song.txt
# shows at the end of line and also in space showing ‘$‘ if there is any gap between paragraphs
# useful to squeeze multiple lines in a single line.
cat -e test
# all output will be redirected in a newly created file
cat test test1 test2 > test3
# Sorting Contents of Multiple Files in a Single File
cat test test1 test2 test3 | sort > test4
# Display Multiple Files at Once
cat test; cat test1; cat test2
# append your text to the end of the file
cat >> ~/.bashrc <<EOF
# my config file
foo=bar
EOF
cat > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor <<EOF
performance
EOF
-----------------------------------------------------------------------------------------------------
curl -sSL https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_linux_amd64.zip -o nomad.zip
https://releases.hashicorp.com/packer/${PACKER_VERSION}/packer_${PACKER_VERSION}_linux_amd64.zip
curl -sSL https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}_linux_amd64.zip -o vagrant.zip
curl -sSL https://releases.hashicorp.com/vagrant/2.2.2/vagrant_2.2.2_linux_amd64.zip -o vagrant.zip
unzip vagrant.zip
$ curl -L https://raw.githubusercontent.com/do-community/ansible-playbooks/master/docker/ubuntu1804.yml -o /vagrant/docker_ubuntu.yml
------------------------------------------------------------------------------------------
split --bytes=2048m WinXP.img WinXP_img_
# four files (2GB each) appeared
WinXP_img_aa
WinXP_img_ab
WinXP_img_ac
WinXP_img_ad
cat WinXP_img_* > WinXP.img
# join smaller files into a larger one
cat partfilename* > outputfilename
video.avi.01
video.avi.02
video.avi.03
cat video.avi.* > video1.avi
$ cat file1
1. Asia:
2. Africa:
3. Europe:
4. North America:
$ tac file1
4. North America:
3. Europe:
2. Africa:
1. Asia:
$ cat file2
1. India
2. Nigeria
3. The Netherlands
4. The US
$ join file1 file2
1. Asia: India
2. Africa: Nigeria
3. Europe: The Netherlands
4. North America: The US
# create a new file “new.txt” that is a concatenation of “file1.txt” and “file2.txt”
cat file1.txt file2.txt > new.txt
format text and convert it to a different width
$ fmt --width=20 test.txt
------------------------------------------------------------------------------------------
g++ -v
g++ temp.cpp
# run
./a.out
------------------------------------------------------------------------------------------
# Installing software from source
tar xvzf package.tar.gz
tar xvjf package.tar.bz2
cd package
./configure
make
make install
# Cleaning up
make clean
make uninstall
# "make clean" runs as expected even if you do have a file named clean.
# There are two reasons to use a phony target: to avoid a conflict with a file of the same name, and to improve performance.
.PHONY: clean
clean:
rm -rf *.o
# case1
$ cat make
hello : hello.o
gcc -Wall hello.o -o hello
$ cat hello.c
#include<stdio.h>
int main(void)
{
printf("\n Hello World!!!\n");
return 0;
}
$ cp make mon-makefile
$ ls
hello.c make mon-makefile
make -C makefile-test1/ hello
make -f mon-makefile
make -s
# case2
$ cat file2.h
void add(int a, int b, void (*f)(int));
$ cat file2.c
#include<stdio.h>
#include"file2.h"
void add(int a, int b, void(*f)(int))
{
int c = a+b;
f(c);
}
$ cat file1.c
#include<stdio.h>
#include"file2.h"
void callback (int result)
{
printf("\n Result is : [%d] \n", result);
}
int main(void)
{
int a=0,b=0;
printf("\n Enter two numbers to add: ");
scanf("%d %d",&a, &b);
add(a,b,callback);
return 0;
}
$ cat makefile
file : file1.o file2.o
gcc -Wall file2.o file1.o -o file
file1.o : file1.c file2.h
gcc -c -Wall file1.c -o file1.o
file2.o : file2.c file2.h
gcc -c -Wall file2.c -o file2.o
$ cp make mon-makefile
$ ls
file1.c file2.c file2.h makefile mon_makefile
------------------------------------------------------------------------------------------
# linux system management
top
sar
vmstat
iostat
free
uptime
ps
tcpdump
iptraf
nestat
# /Proc file system - Various Kernel Statistics
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/zoneinfo
cat /proc/mounts
------------------------------------------------------------------------------------------
# same inodes
ls -ldi /. /..
2 drwxr-xr-x 24 root root 4096 Feb 21 20:28 /.
2 drwxr-xr-x 24 root root 4096 Feb 21 20:28 /..
# different inodes
ls -ldi /home/vagrant/. /home/vagrant/..
3145730 drwxr-xr-x 7 vagrant vagrant 4096 Feb 22 10:14 /home/vagrant/.
3145729 drwxr-xr-x 3 root root 4096 Aug 24 08:48 /home/vagrant/..
------------------------------------------------------------------------------------------
# The user file-creation mode mask (umask)
/etc/profile
~/.bashrc
~/.bash_profile
# By default
0022 (022)
0002 (002)
files
666
directories
777
The default umask 002 used for normal user
directory permissions
775
file permissions
664
The default umask for the root user is 022
directory permissions
755
file permissions
644
base permissions
directory permissions
(rwxrwxrwx) 0777
file permissions
(rw-rw-rw) 0666
# No other user can read or write your data
umask 077
# when you share data with other users in the same group
# Members of your group can create and modify data files
# those outside your group can read data file, but cannot modify it.
umask 022
# exclude users who are not group members
umask 007
# The octal umasks are calculated via the bitwise AND of the unary complement of the argument using bitwise NOT
Octal value : Permission
0 : read, write and execute
1 : read and write
2 : read and execute
3 : read only
4 : write and execute
5 : write only
6 : execute only
7 : no permissions
------------------------------------------------------------------------------------------
# testing webpages
$ telnet control01 80
GET /index.html HTTP/1.1
Host: control01
GET /index.html HTTP/1.1
Host: control01
If-modified-since: Sun, 24 Feb 2019 12:59:37 GMT
GET /telnet-send-get-head-http-request HTTP/1.1
HOST: control01
HEAD / HTTP/1.1
Host: control01
------------------------------------------------------------------------------------------
# delete files containing special chars
$ cat>>"-f"<<EOF
> test
> EOF
$ ls
-f test test2 videos
$ ls -li
total 260
3145770 -rw-rw-r-- 1 vagrant vagrant 5 Mar 5 17:02 -f
find / -name wget 2>/dev/null
find /home/vagrant -name file1
find /home/vagrant -user root
find /home/vagrant -group root
find . -inum 3145770 -delete
find . -inum 3145770 -exec rm -i {} \;
ls -il {file-name}
find . -inum [inode] -exec rm -i {} \;
------------------------------------------------------------------------------------------
# steganography, attaching a .rar file to a .jpg etc.
cat pic.jpg file.rar > result.jpg
------------------------------------------------------------------------------------------
# view used IRQs
cat /proc/interrupts
#determine the IRQ number associated with the Ethernet driver
$ grep enp0s3 /proc/interrupts
19: 24006 IO-APIC-fasteoi enp0s3
$ grep eth0 /proc/interrupts
19: 13247 IO-APIC 19-fasteoi eth0
# troubleshoot network card etc. hardware conflicts, what addresses are used or free, or move conflicting hardware to free resource
# listing IRQs currently being used.not listed IRQs are considered free.used when devices alert CPU to take action
cat /proc/interrupts
# listing used DMA channel.when devicess access memory directly without going through CPU
cat /proc/dma
# listing I/O ports currently being used.any range not listed is free and can be used by other devices.
# devices have unique I/O addresses
cat /proc/ioports
------------------------------------------------------------------------------------------
# view default shell for each user
$ cat /etc/passwd
# There are seven fields in the /etc/passwd file
# username, UID, GID, comment, home directory, command
# add an asterisk at the beginning of the password field in the /etc/passwd file, that user will not be able to log in
/etc/passwd
# useradd tecmint
# useradd -m tecmint
# passwd tecmint
create admin user
sudo useradd vagrant -s /bin/bash -g sudo -m
modify a existing user to admin user
sudo usermod -aG sudo vagrant
create a user ‘anusha‘ with a home directory ‘/data/projects‘.
# useradd -d /data/projects anusha
create a user ‘navin‘ with custom userid ‘999‘.
# useradd -u 999 navin
add a user ‘tarunika‘ with a specific UID and GID
# useradd -u 1000 -g 500 tarunika
add a user ‘tecmint‘ into multiple groups like admins, webadmin and developer
# useradd -G admins,webadmin,developers tecmint
# id tarunika
uid=995(tarunika) gid=1001(vboxadd) groups=1001(vboxadd)
create a user ‘aparna‘ with account expiry date i.e. 27th April 2014 in YYYY-MM-DD
# useradd -e 2014-03-27 aparna
verify the age of account
# chage -l aparna
set a account password expiry date i.e. 45 days on a user ‘tecmint’
# useradd -e 2014-04-27 -f 45 tecmint
insert that user’s full name, Manis Khurana
# useradd -c "Manis Khurana" mansi
add a user ‘tecmint‘ without login shell
# useradd -s /sbin/nologin tecmint
create a user ‘ravi‘ with home directory ‘/var/www/tecmint‘, default shell /bin/bash and adds extra information about user
# useradd -m -d /var/www/ravi -s /bin/bash -c "TecMint Owner" -U ravi
# useradd -m -d /var/www/tarunika -s /bin/zsh -c "TecMint Technical Writer" -u 1000 -g 1000 tarunika
disabling login shell to a user called ‘avishek‘
# useradd -m -d /var/www/avishek -s /usr/sbin/nologin -c "TecMint Sr. Technical Writer" -u 1019 avishek
tecmint:x:504:504:tecmint:/home/tecmint:/bin/bash
Username: User login name used to login into system. It should be between 1 to 32 charcters long.
Password: User password (or x character) stored in /etc/shadow file in encrypted format.
User ID (UID): Every user must have a User ID (UID) User Identification Number. By default UID 0 is reserved for root user and UID’s ranging from 1-99 are reserved for other predefined accounts. Further UID’s ranging from 100-999 are reserved for system accounts and groups.
Group ID (GID): The primary Group ID (GID) Group Identification Number stored in /etc/group file.
User Info: This field is optional and allow you to define extra information about the user. For example, user full name. This field is filled by ‘finger’ command.
Home Directory: The absolute location of user’s home directory.
Shell: The absolute location of a user’s shell i.e. /bin/bash.
# delete user account
userdel -r member2
# lock user account
usermod -L member1
# unlock user account
usermod -U member1
# configure user default shell
usermod -s /bin/bash member1
# add the user geek to the group sudo
usermod -a -G sudo geek
# create new user, new group with the same name
sudo adduser sdn --system --group
make sure username is added to the group libvirtd
$ sudo adduser `id -un` libvirtd
$ sudo adduser $(id -un) libvirtd
------------------------------------------------------------------------------------------
cat /etc/group
cat /etc/gshadow
cut -d: -f1 /etc/group | sort
$ groupadd admins
$ useradd -G admins member1
gpasswd -a devops1 sudo
Adding user devops1 to group sudo
gpasswd -d devops1 sudo
Removing user devops1 from group sudo
# list groups
groups
whoami
id -Gn
id vagrant
$ sudo groupadd --system --gid 1002 appuser
$ cat /etc/group | grep appuser
appuser:x:1002:
$ sudo useradd --no-log-init --system --uid 1001 --gid 1002 appuser
$ cat /etc/passwd | grep appuser
appuser:x:1001:1002::/home/appuser:/bin/sh
# list a group's members
$ sudo lid -g sales
$ cut -d: -f1,4 /etc/passwd | grep $(getent group sales | cut -d: -f3) | cut -d: -f1
grep -i --color 'root' /etc/group
getent group -> List all groups
cat /etc/group -> List all groups
getent group vboxusers
groups -> View the Groups a User Account
groupmod -g 3000 foo -> assign a new GID to group called foo
------------------------------------------------------------------------------------------
“skeleton” directory is defined in /etc/default/useradd file.
# ls -lart /etc/skel
# ls -ldi /etc/skel
33554552 drwxr-xr-x. 2 root root 62 Mar 24 2018 /etc/skel
------------------------------------------------------------------------------------------
valid login shells
# cat /etc/shells
# chsh -l
change user vagrant's bash
# chsh vagrant -s /bin/rbash
verify user vagrant's bash
# cat /etc/passwd | grep vagrant
# echo $SHELL
Reading Library editor
$ cat /etc/inputrc
keyboard bindings
$ bind -v
command substitution; output from pwd works as the argument for echo command
$ echo `pwd`
------------------------------------------------------------------------------------------
semicolon ";" multiple commands on the same line
backaslash "\" run commands longer than one line
press tab key or twice ESC key, command completion
"./" run a command from pwd
------------------------------------------------------------------------------------------
history file size setting
$ cat /etc/profile
$ echo $HISTSIZE
$ echo $HISTFILE
$ fc -l
------------------------------------------------------------------------------------------
echo $PATH
view all the env variables
$ export -p
$ set
Set an Environment Variable
$ export MYAPP=1
holds the list of all directories that are searched by the shell when you type a command name
$PATH
system-wide
$ cat /etc/profile
single user
$ cat .bash_profile
# add PATH
vi .bash_profile
export PATH=$PATH:$HOME/Downloads/terraform
system-wide prompt setting
$ cat /etc/bashrc
$ echo $PS1
------------------------------------------------------------------------------------------
Display current libraries from the cache
# ldconfig -p | head -5
Display libraries from every directory
ldconfig -v | head
# cat /etc/ld.so.conf
------------------------------------------------------------------------------------------
# number the lines in a file
nl alphaservices | tee alphabetservices
$ cat file1
1. Asia:
2. Africa:
3. Europe:
4. North America:
Display the contents of file.txt in octal format (one byte per integer)
$ od -b file1
0000000 061 056 040 101 163 151 141 072 012 062 056 040 101 146 162 151
0000020 143 141 072 012 063 056 040 105 165 162 157 160 145 072 012 064
0000040 056 040 116 157 162 164 150 040 101 155 145 162 151 143 141 072
0000060 012
0000061
Display the contents of file.txt in ASCII (character) format, with byte offsets displayed as hexadecimal.
$ od -Ax -c file1
000000 1 . A s i a : \n 2 . A f r i
000010 c a : \n 3 . E u r o p e : \n 4
000020 . N o r t h A m e r i c a :
000030 \n
000031
------------------------------------------------------------------------------------------
# merge all files in the directory and split
ls | xargs cat | tee file1 | split -5
# printing
pr -h "title" file1
list mounted file systems
$ cat /etc/mtab
------------------------------------------------------------------------------------------
configuration files on the system
/etc
files copied to each user's home
/etc/skel
user by user and supersuser accounts.Stores application programs
/usr
executables used by users, in user's PATH statement
/usr/bin
executables used by superusers
/usr/sbin
applications which are not part of linux
/usr/local
applications installed after initial linux installation,in user's PATH statement
administrative applications installed after initial linux installation
/usr/local/bin
the default location for application documentation is in a directory named for the application
/usr/doc
stores log files, mails and other data
/var
log files
/var/log
last logins
/var/log/lastlog
By default, the main system log
/var/log/messages
mail & printing files
/var/spool
binaries run during system startup
/bin
administrative binaries run by superusers
/sbin
home for superuser
/root
user home dirs
/home
files run by boot loader and kernel
/boot
peripheral access files
/dev
virtual dir contains system info
/proc
stores temporary files
/tmp
------------------------------------------------------------------------------------------
# find all files, SUID bit enabled
find / -perm -4000 -exec ls -l {} \;
find /usr/bin/ -perm -4000 -exec ls -l {} \;
find /bin/ -perm -4000 -exec ls -l {} \;
# find all files, SGID bit enabled
find / -perm -2000 -exec ls -l {} \;
-----------------------------------------------------------------------------------------------------
u stands for user.
g stands for group.
o stands for others.
a stands for all.
same output:
chmod +x somefile (Based on umask value)
chmod a+x somefile, chmod ugo+x somefile (Without considering umask value), add the execute permission for everyone
------------------------------------------------------------------------------------------
Applying SUID Permission Numerically
# chmod 4755 /bin/ping
Removing SUID by Numerically
chmod 0755 /bin/ping
Applying SUID Permission to ping binary file Alphabetically
# chmod u+s /bin/ping
Removing SUID Permission
chmod u-s /bin/ping
------------------------------------------------------------------------------------------
# Applying SGID Permission
chmod g+s /database/
chmod 2775 database/
# Remove SGID Alphabetically
chmod g-s /database/
chmod 0775 /database
------------------------------------------------------------------------------------------
a sticky bit is now in place and only root, file or directory owners can rename and delete files
# chmod +t /var/share/
# ls -ld /var/share/
drwxrwxrwt. 2 root root 4096 Mar 5 11:21 /var/share/
chmod 0777 somefile (octal)
chmod 777 somefile (decimal)
chmod 0710 mydir ; ls -ld mydir
chmod 00710 mydir ; ls -ld mydir
------------------------------------------------------------------------------------------
# quota settings
sudo apt update ; sudo apt install quota -y
quota --version
find /lib/modules/`uname -r` -type f -name '*quota_v*.ko*'
sudo mount -o remount /
cat /proc/mounts | grep ' /
sudo quotacheck -ugm /
sudo quotaon -v /
sudo setquota -u member1 200M 240M 0 0 /
sudo quota -vs member1
sudo setquota -t 864000 864000 /
sudo repquota -s /
------------------------------------------------------------------------------------------
# ldd (Unix) ldd (List Dynamic Dependencies)
ldd /bin/ls
# display unused direct dependencies
ldd -u /bin/ping
# more information
ldd -v /bin/ping
------------------------------------------------------------------------------------------
# rename root
$ head -2 /etc/passwd
root:x:0:0:root:/root:/bin/nologin
rootmon:x:0:0:root:/root:/bin/bash
$ sudo passwd rootmon
$ su - rootmon
# pwd
/root
awk -F ":" '{print $5}' /etc/passwd #print the fifth field
getent passwd $UID| awk -F ":" '{print $5}'
GECOS fields (which stands for "General Electric Comprehensive Operating System")
username:password:userid:groupid:gecos:home-dir:shell
GECOS are divided as:
:FullName,RoomAddress,WorkPhone,HomePhone,Others:
------------------------------------------------------------------------------------------
# enable the root account
sudo passwd root
------------------------------------------------------------------------------------------
# send an email from command line
mail -s “Hello world” [email protected]
echo “This will go into the body of the mail.” | mail -s “Hello world” [email protected]
df -h | mail -s “disk space report” [email protected]
------------------------------------------------------------------------------------------
# check a file system for errors?
fsck
fsck.ext3
fsck.nfs
fsck.ext2
fsck.vfat
fsck.reiserfs
fsck.msdos
In order to run fsck on the root partition, the root partition must be mounted as readonly
------------------------------------------------------------------------------------------
# list of drives that are mounted at boot
/etc/fstab
# runs as a daemon and typically has PID 1
# change the default runlevel upon boot up.
/etc/inittab
# list of all runlevels and services used by them
chkconfig --list
# daemon is responsible for tracking events on the system
syslogd
# set which window man-ager you want to use when logging in to X from that account
# edit in your home directory to change which window manager you want to use
~/.xinitrc
------------------------------------------------------------------------------------------
find the number of processing units available on a system
nproc
lscpu
grep 'model name' /proc/cpuinfo | wc -l
cat /proc/cpuinfo
------------------------------------------------------------------------------------------
$ seq -s";" -w 1 12
01;02;03;04;05;06;07;08;09;10;11;12
------------------------------------------------------------------------------------------
protection from inadvertently overwriting files when copying
~/.bashrc
alias cp='cp -i'
------------------------------------------------------------------------------------------
sudo apt-get install tree -y
tree -a
tree -d
tree -da
tree -daC
------------------------------------------------------------------------------------------
ipcs (InterProcess Communication System) provides a report on the semaphore, shared memory & message queue
ipcs -u
ipcs -m
------------------------------------------------------------------------------------------
nslookup github.com
nslookup 140.82.118.4
nslookup -query=mx github.com
nslookup -query=ns github.com
nslookup -query=any github.com
nslookup -query=soa github.com
nslookup -query=soa port=54 github.com
nslookup -debug github.com
------------------------------------------------------------------------------------------
Command Substitution
# `backquotes` also known as `backticks`
KERNEL_VERSION=`uname -r`
#(parentheses)
KERNEL_VERSION=$( uname -r )
$ echo $KERNEL_VERSION
4.15.0-29-generic
------------------------------------------------------------------------------------------
The command shell interprets the && as the logical AND.the second command will be executed only when the first one has been succcefully executed
A double ampersand && in Bash means AND and can be used to separate a list of commands to be run sequentially.
Commands separated by a double ampersand && are to be run synchronously, with each one running only if the last did not fail (a fail is interpreted as returning a non-zero return status).
&& AND – execute both, return true of both succeed
; sequential execution, return status is that of the last in the list
$ mkdir /workspace ; mkdir /entrypoint
mkdir: cannot create directory ‘/workspace’: Permission denied
mkdir: cannot create directory ‘/entrypoint’: Permission denied
$ mkdir /workspace && mkdir /entrypoint
mkdir: cannot create directory ‘/workspace’: Permission denied
------------------------------------------------------------------------------------------
disable and stop service
$ sudo systemctl disable --now zabbix-server.service
enable and start service
$ sudo systemctl enable --now zabbix-server.service
------------------------------------------------------------------------------------------
ls -laZ ~/.ssh
# change the security context to system_u:object_r:usr_t:s0
chcon -R -v system_u:object_r:usr_t:s0 ~/.ssh/
------------------------------------------------------------------------------------------
problem: sleep: invalid time interval `2\r'
fix: sudo cat test.sh | sudo tr -d '\r' | sudo tee test2.sh
------------------------------------------------------------------------------------------
$ diff 1.txt 2.txt # display the differences in the files by comparing the files line by line
$ diff -c 1.txt 2.txt
$ diff 1.txt 2.txt -u
$ diff 1.txt 2.txt -i
$ diff 1.txt 2.txt --color
$ diff 1.txt 2.txt -s
vagrant@lampstack-01:~$ echo "file" > file1.txt
vagrant@lampstack-01:~$ cp file1.txt file2.txt
vagrant@lampstack-01:~$ cmp file1.txt file2.txt #cmp command reports the byte and line number if a difference is found
vagrant@lampstack-01:~$ sudo cmp file1.txt file2.txt
vagrant@lampstack-01:~$ echo "identical file1" >> file1.txt
vagrant@lampstack-01:~$ cat file1.txt
file
identical file1
vagrant@lampstack-01:~$ cat file2.txt
file
vagrant@lampstack-01:~$ cmp file1.txt file2.txt #cmp command reports the byte and line number if a difference is found
cmp: EOF on file2.txt after byte 5, line 1
vagrant@lampstack-01:~$ diff file1.txt file2.txt
2d1
< identical file1
------------------------- -----------------------------------------------------------------
# generate load
$ yes > /dev/null &
# maximum number of processes available to a single user.
$ ulimit -u
# The limit is set with the -S option
$ ulimit -S -u 500
## Stress using CPU-bound task
stress -c 4
## Stress using IO-bound task
stress -i 2
# a load average of four is imposed on the system by specifying two CPU-bound processes, one I/O-bound process, and one memory allocator process
stress -c 2 -i 1 -m 1 --vm-bytes 128M -t 10s
# The restriction can be made permanent by configuring the nproc value
/etc/security/limits.conf
# initiate a fork bomb
":(){ :|:& };:"
# run ./$0& twice
#!/bin/sh
./$0&
./$0&
------------------------- -----------------------------------------------------------------
# approval trick
$ yes | sudo yum install puppet
# user has to type 'y' for each query
$ yes | rm -ri test
------------------------- -----------------------------------------------------------------
#oracle java download
wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
"https://download.oracle.com/otn-pub/java/jdk/13.0.1+9/cec27d702aa74d5a8630c65ae61e4305/jdk-13.0.1_linux-x64_bin.tar.gz" \
-O jdk-7-linux-x64.tar.gz
curl -LO -H "Cookie: oraclelicense=accept-securebackup-cookie" \
https://download.oracle.com/otn-pub/java/jdk/13.0.1+9/cec27d702aa74d5a8630c65ae61e4305/jdk-13.0.1_linux-x64_bin.tar.gz
------------------------------------------------------------------------------------------
find -type f -exec md5sum -t {} \; | cut -d ' ' -f 1 | sort | md5sum #compute checksum
------------------------------------------------------------------------------------------
hostnamectl set-hostname vg-checkmk-client
echo "172.28.128.15 vg-checkmk-client.local vg-checkmk-client" |sudo tee -a /etc/hosts
echo "name: nameserver, ip: 8.8.8.8 " |sudo tee -a /etc/resolv.conf
------------------------------------------------------------------------------------------
#File Creation Times
vagrant@lampstack-01:/tmp/nexus$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 205M 0 205M 0% /dev
tmpfs 48M 7.8M 41M 17% /run
/dev/mapper/vagrant--vg-root 62G 4.3G 55G 8% /
tmpfs 240M 0 240M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 240M 0 240M 0% /sys/fs/cgroup
vagrant 420G 375G 46G 90% /vagrant
tmpfs 48M 0 48M 0% /run/user/1000
vagrant@lampstack-01:/tmp/nexus$ ls -i Dockerfile
3808483 Dockerfile
vagrant@lampstack-01:/tmp/nexus$ sudo debugfs -R 'stat <3808483>' /dev/mapper/vagrant--vg-root
debugfs 1.44.6 (5-Mar-2019)
vagrant@lampstack-01:/tmp/nexus$ vagrant@lampstack-01:/tmp/nexus$ sudo debugfs -R 'stat <3808483>' /dev/mapper/vagrant--vg-root | grep crtime
debugfs 1.44.6 (5-Mar-2019)
crtime: 0x5e4f0f92:b6a11330 -- Thu Feb 20 23:00:34 2020
------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment