Linux Commands For CLI
Linux Commands For CLI
System information
arch show architecture of machine
uname -r show used kernel version
show hardware system components -
dmidecode -q
(SMBIOS / DMI)
hdaparm -i /dev/hda displays the characteristics of a hard-disk
hdparm -tT /dev/sda perform test reading on a hard-disk
cat /proc/cpuinfo show information CPU info
cat /proc/interrupts show interrupts
cat /proc/meminfo verify memory use
cat /proc/swaps show file(s) swap
cat /proc/version show version of the kernel
cat /proc/net/dev show network adpters and statistics
cat /proc/mounts show mounted file system(s)
lspci -tv display PCI devices
lsusb -tv show USB devices
date show system date
cal 2007 show the timetable of 2007
set date and time -
date 041217002007.00
MonthDayhoursMinutesYear.Secondi
File search
search file and directory into root filesystem
find / -name file1
from '/'
search files and directories belonging to
find / -user user1
'user1'
search files with '. bin' extension within
find /home/user1 -name *.bin
directory '/ home/user1'
search bynary files are not used in the last
find /usr/bin -type f -atime +100
100 days
search files created or changed within 10
find /usr/bin -type f -mtime -10
days
search files with '.rpm' extension and modify
find / -name *.rpm -exec chmod 755 {} ;
permits
search files with '.rpm' extension ignoring
find / -name *.rpm -xdev removable partitions as cdrom, pen-drive,
etc.…
find files with the '.ps' extension - first run
locate *.ps
'updatedb' command
whereis halt show location of a binary file, source or man
which halt show full path to a binary / executable
Mounting a Filesystem
mount disk called hda2 - verify existence of
mount /dev/hda2 /mnt/hda2
the directory '/ mnt/hda2'
unmount disk called hda2 - exit from mount
umount /dev/hda2
point '/ mnt/hda2' first
fuser -km /mnt/hda2 force umount when the device is busy
run umount without writing the file
umount -n /mnt/hda2 /etc/mtab - useful when the file is read-only
or the hard disk is full
mount /dev/fd0 /mnt/floppy mount a floppy disk
mount /dev/cdrom /mnt/cdrom mount a cdrom / dvdrom
mount /dev/hdc /mnt/cdrecorder mount a cdrw / dvdrom
mount /dev/hdb /mnt/cdrecorder mount a cdrw / dvdrom
mount -o loop file.iso /mnt/cdrom mount a file or iso image
mount -t vfat /dev/hda5 /mnt/hda5 mount a Windows FAT32 file system
mount /dev/sda1 /mnt/usbdisk mount a usb pen-drive or flash-drive
mount -t smbfs -o username=user,password=pass
mount a windows network share
//winclient/share /mnt/share
Disk Space
df -h show list of partitions mounted
show size of the files and directories ordered
ls -lSr |more
by size
du -sh dir1 estimate space used by directory 'dir1'
show size of the files and directories sorted
du -sh * | sort -rn
by size
show space used by rpm packages installed
rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1n
sorted by size (fedora, redhat and like)
dpkg-query -W -f='${Installed-Size;10}\t${Package}\n' | show space used by deb packages installed
sort -k1,1n sorted by size (ubuntu, debian and like)
Users and Groups
groupadd group_name create a new group
groupdel group_name delete a group
groupmod -n new_group_name old_group_name rename a group
useradd -c "Nome Cognome" -g admin -d /home/user1
create a new user belongs "admin" group
-s /bin/bash user1
useradd user1 create a new user
delete a user ( '-r' eliminates home
userdel -r user1
directory)
usermod -c "User FTP" -g system -d /ftp/user1 -s
change user attributes
/bin/nologin user1
passwd change password
passwd user1 change a user password (only by root)
chage -E 2005-12-31 user1 set deadline for user password
check correct syntax and file format of
pwck
'/etc/passwd' and users existence
check correct syntax and file format of
grpck
'/etc/group' and groups existence
log in to a new group to change default
newgrp group_name
group of newly created files
Special Attributes on file - use "+" to set permissions and "-" to remove
allows write opening of a file only append
chattr +a file1
mode
allows that a file is compressed /
chattr +c file1
decompressed automatically by the kernel
makes sure that the program ignores Dump
chattr +d file1
the files during backup
makes it an immutable file, which can not be
chattr +i file1
removed, altered, renamed or linked
chattr +s file1 allows a file to be deleted safely
makes sure that if a file is modified changes
chattr +S file1
are written in synchronous mode as with
sync
allows you to recover the contents of a file
chattr +u file1
even if it is canceled
lsattr show specials attributes
Text Manipulation
cat file_test | [operation: sed, grep, awk, grep, etc] > syntax to elaborate the text of a file, and
result.txt write result to a new file
cat file_originale | [operazione: sed, grep, awk, grep, etc] syntax to elaborate the text of a file and
>> result.txt append result in existing file
look up words "Aug" on file
grep Aug /var/log/messages
'/var/log/messages'
look up words that begin with "Aug" on file
grep ^Aug /var/log/messages
'/var/log/messages'
select from file '/var/log/messages' all lines
grep [0-9] /var/log/messages
that contain numbers
search string "Aug" at directory '/var/log'
grep Aug -R /var/log/*
and below
grep Aug /var/log/messages write result of a search within a file
replace "string1" with "string2" in
sed 's/stringa1/stringa2/g' example.txt
example.txt
sed '/^$/d' example.txt remove all blank lines from example.txt
remove comments and blank lines from
sed '/ *#/d; /^ *$/d' example.txt
example.txt
echo 'esempio' | tr '[:lower:]' '[:upper:]' convert from lower case in upper case
sed -e '1d' result.txt eliminates the first line from file example.txt
view only lines that contain the word
sed -n '/stringa1/p'
"string1"
remove empty characters at the end of each
sed -e 's/ *$//' example.txt
row
remove only the word "string1" from text
sed -e 's/stringa1//g' example.txt
and leave intact all
sed -n '1,5p;5q' example.txt view from 1th to 5th row
sed -n '5p;5q' example.txt view row number 5
sed -e 's/00*/0/g' example.txt replace more zeros with a single zero
cat -n file1 number row of a file
cat example.txt | awk 'NR%2==1' remove all even lines from example.txt
echo a b c | awk '{print $1}' view the first column of a line
echo a b c | awk '{print $1,$3}' view the first and third column of a line
paste file1 file2 merging contents of two files for columns
merging contents of two files for columns
paste -d '+' file1 file2
with '+' delimiter on the center
sort file1 file2 sort contents of two files
sort contents of two files omitting lines
sort file1 file2 | uniq
repeated
sort contents of two files by viewing only
sort file1 file2 | uniq -u
unique line
sort contents of two files by viewing only
sort file1 file2 | uniq -d
duplicate line
compare contents of two files by deleting
comm -1 file1 file2
only unique lines from 'file1'
compare contents of two files by deleting
comm -2 file1 file2
only unique lines from 'file2'
compare contents of two files by deleting
comm -3 file1 file2
only the lines that appear on both files
Filesystem Analysis
badblocks -v /dev/hda1 check bad blocks in disk hda1
repair / check integrity of linux filesystem on
fsck /dev/hda1
disk hda1
repair / check integrity of ext2 filesystem on
fsck.ext2 /dev/hda1
disk hda1
repair / check integrity of ext2 filesystem on
e2fsck /dev/hda1
disk hda1
repair / check integrity of ext3 filesystem on
e2fsck -j /dev/hda1
disk hda1
repair / check integrity of ext3 filesystem on
fsck.ext3 /dev/hda1
disk hda1
repair / check integrity of fat filesystem on
fsck.vfat /dev/hda1
disk hda1
repair / check integrity of dos filesystem on
fsck.msdos /dev/hda1
disk hda1
repair / check integrity of dos filesystems on
dosfsck /dev/hda1
disk hda1
Format a Filesystem
create a filesystem type linux on hda1
mkfs /dev/hda1
partition
create a filesystem type linux ext2 on hda1
mke2fs /dev/hda1
partition
create a filesystem type linux ext3 (journal)
mke2fs -j /dev/hda1
on hda1 partition
mkfs -t vfat 32 -F /dev/hda1 create a FAT32 filesystem
fdformat -n /dev/fd0 format a floppy disk
mkswap /dev/hda3 create a swap filesystem
SWAP filesystem
mkswap /dev/hda3 create a swap filesystem
swapon /dev/hda3 activating a new swap partition
swapon /dev/hda2 /dev/hdb3 activate two swap partitions
Backup
dump -0aj -f /tmp/home0.bak /home make a full backup of directory '/home'
make a incremental backup of directory
dump -1aj -f /tmp/home0.bak /home
'/home'
restore -if /tmp/home0.bak restoring a backup interactively
rsync -rogpav --delete /home /tmp synchronization between directories
rsync -rogpav -e ssh --delete /home ip_address:/tmp rsync via SSH tunnel
synchronize a local directory with a remote
rsync -az -e ssh --delete ip_addr:/home/public /home/local
directory via ssh and compression
synchronize a remote directory with a local
rsync -az -e ssh --delete /home/local ip_addr:/home/public
directory via ssh and compression
dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd make a backup of a local hard disk on
of=hda.gz' remote host via ssh
make a incremental backup of directory
tar -Puf backup.tar /home/user
'/home/user'
( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd copy content of a directory on remote
/home/share/ && tar x -p' directory via ssh
( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup- copy a local directory on remote directory via
home && tar x -p' ssh
local copy preserving permits and links from
tar cf - . | (cd /tmp/backup ; tar xf - )
a directory to another
find /home/user1 -name '*.txt' | xargs cp -av --target- find and copy all files with '.txt' extention
directory=/home/backup/ --parents from a directory to another
find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > find all files with '.log' extention and make
log.tar.bz2 an bzip archive
make a copy of MBR (Master Boot Record) to
dd if=/dev/hda of=/dev/fd0 bs=512 count=1
floppy
restore MBR from backup copy saved to
dd if=/dev/fd0 of=/dev/hda bs=512 count=1
floppy
CDROM
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast
clean a rewritable cdrom
-force
mkisofs /dev/cdrom > cd.iso create an iso image of cdrom on disk
create a compressed iso image of cdrom on
mkisofs /dev/cdrom | gzip > cd_iso.gz
disk
mkisofs -J -allow-leading-dots -R -V "Label CD" -iso-level 4
create an iso image of a directory
-o ./cd.iso data_cd
cdrecord -v dev=/dev/cdrom cd.iso burn an ISO image
gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom - burn a compressed ISO image
mount -o loop cd.iso /mnt/iso mount an ISO image
cd-paranoia -B rip audio tracks from a CD to wav files
rip first three audio tracks from a CD to wav
cd-paranoia -- "-3"
files
cdrecord --scanbus scan bus to identify the channel scsi
IPTABLES - firewall
iptables -t filter -L show all chains of filtering table
iptables -t nat -L show all chains of nat table
iptables -t filter -F clear all rules from filtering table
iptables -t nat -F clear all rules from table nat
iptables -t filter -X delete any chains created by user
iptables -t filter -A INPUT -p tcp --dport telnet -j ACCEPT allow telnet connections to input
iptables -t filter -A OUTPUT -p tcp --dport http -j DROP block HTTP connections to output
iptables -t filter -A FORWARD -p tcp --dport pop3 -j ACCEPT allow POP3 connections to forward chain
logging sulla chain di input Logging on chain
iptables -t filter -A INPUT -j LOG --log-prefix "DROP INPUT"
input
configure a PAT (Port Address Traslation) on
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
eth0 masking outbound packets
iptables -t nat -A PREROUTING -d 192.168.0.1 -p tcp -m redirect packets addressed to a host to
tcp --dport 22 -j DNAT --to-destination 10.0.0.2:22 another host