Pages

Wednesday, August 19, 2009

Linux Command Line

System information - Shutdown, Restart and Logout of a system - Files and Directory - File search - Mounting a Filesystem - Disk Space - Users and Groups - Permits on Files - Special Attributes on files - Archives and compressed files - RPM Packages ( Fedora, Red Hat and like) - YUM packages tool (Fedora, RedHat and alike) - DEB packages (Debian, Ubuntu and like) - APT packages tool (Debian, Ubuntu and alike) - View file content - Text Manipulation - Character set and Format file conversion - Filesystem Analysis - Format a Filesystem - Filesystem SWAP - Backup - CDROM - Networking (LAN / WiFi) - Microsoft Windows networks (samba) - IPTABLES (firewall) - Monitoring and debugging - Others useful commands - Pacman packages tool (Arch, Frugalware and alike)



APT packages tool (Debian, Ubuntu and alike)

Command
Description
# apt-cache search [package]
returns list of packages which corresponds string "searched-packages"
# apt-cdrom install [package]
install / upgrade a deb package from cdrom
# apt-get install [package]
install / upgrade a deb package
# apt-get update
update the package list
# apt-get upgrade
upgrade all of the installed packages
# apt-get remove [package]
remove a deb package from system
# apt-get check
verify correct resolution of dependencies
# apt-get clean
clean up cache from packages downloaded

Archives and compressed files


Command
Description
# bunzip2 file1.bz2
decompress a file called 'file1.bz2'
# bzip2 file1
compress a file called 'file1'
# gunzip file1.gz
decompress a file called 'file1.gz'
# gzip file1
compress a file called 'file1'
# gzip -9 file1
compress with maximum compression
# rar a file1.rar test_file
create an archive rar called 'file1.rar'
# rar a file1.rar file1 file2 dir1
compress 'file1', 'file2' and 'dir1' simultaneously
# rar x file1.rar
decompress rar archive
# tar -cvf archive.tar file1
create a uncompressed tarball
# tar -cvf archive.tar file1 file2 dir1
create an archive containing 'file1', 'file2' and 'dir1'
# tar -tf archive.tar
show contents of an archive
# tar -xvf archive.tar
extract a tarball
# tar -xvf archive.tar -C /tmp
extract a tarball into / tmp
# tar -cvfj archive.tar.bz2 dir1
create a tarball compressed into bzip2
# tar -xvfj archive.tar.bz2
decompress a compressed tar archive in bzip2
# tar -cvfz archive.tar.gz dir1
create a tarball compressed into gzip
# tar -xvfz archive.tar.gz
decompress a compressed tar archive in gzip
# unrar x file1.rar
decompress rar archive
# unzip file1.zip
decompress a zip archive
# zip file1.zip file1
create an archive compressed in zip
# zip -r file1.zip file1 file2 dir1
compress in zip several files and directories simultaneously


Shutdown, Restart and Logout of a system

Command
Description
# init 0
shutdown system(2)
# logout
leaving session
# reboot
reboot(2)
# shutdown -h now
shutdown system(1)
# shutdown -h 16:30 &
planned shutdown of the system at 16:30
# shutdown -c
cancel a planned shutdown of the system
# shutdown -r now
reboot(1)
# telinit 0
shutdown system(3)

Backup
Command
Description
# find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2
find all files with '.log' extention and make an bzip archive
# find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents
find and copy all files with '.txt' extention from a directory to another
# dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz'
make a backup of a local hard disk on remote host via ssh
# dd if=/dev/sda of=/tmp/file1
backup content of the harddrive to a file
# dd if=/dev/hda of=/dev/fd0 bs=512 count=1
make a copy of MBR (Master Boot Record) to floppy
# dd if=/dev/fd0 of=/dev/hda bs=512 count=1
restore MBR from backup copy saved to floppy
# dump -0aj -f /tmp/home0.bak /home
make a full backup of directory '/home'
# dump -1aj -f /tmp/home0.bak /home
make a incremental backup of directory '/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
# rsync -az -e ssh --delete ip_addr:/home/public /home/local
synchronize a local directory with a remote directory via ssh and compression
# rsync -az -e ssh --delete /home/local ip_addr:/home/public
synchronize a remote directory with a local directory via ssh and compression
# tar -Puf backup.tar /home/user
make a incremental backup of directory '/home/user'
# ( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p'
copy content of a directory on remote directory via ssh
# ( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p'
copy a local directory on remote directory via ssh
# tar cf - . | (cd /tmp/backup ; tar xf - )
local copy preserving permits and links from a directory to another

CDROM

Command
Description
# cd-paranoia -B
rip audio tracks from a CD to wav files
# cd-paranoia --
rip first three audio tracks from a CD to wav files
# cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force
clean a rewritable cdrom
# 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
# cdrecord --scanbus
scan bus to identify the channel scsi
# dd if=/dev/hdc | md5sum
perform an md5sum on a device, like a CD
# mkisofs /dev/cdrom > cd.iso
create an iso image of cdrom on disk
# mkisofs /dev/cdrom | gzip > cd_iso.gz
create a compressed iso image of cdrom on disk
# mkisofs -J -allow-leading-dots -R -V
create an iso image of a directory
# mount -o loop cd.iso /mnt/iso
mount an ISO image

Character set and Format file conversion

Command
Description
# dos2unix filedos.txt fileunix.txt
convert a text file format from MSDOS to UNIX
# recode ..HTML <> page.html
convert a text file to html
# recode -l | more
show all available formats conversion
# unix2dos fileunix.txt filedos.txt
convert a text file format from UNIX to MSDOS

DEB packages (Debian, Ubuntu and like)


Command
Description
# dpkg -i [package.deb]
install / upgrade a deb package
# dpkg -r [package]
remove a deb package from the system
# dpkg -l
show all deb packages installed on the system
# dpkg -l | grep httpd
show all deb packages with the name "httpd"
# dpkg -s [package]
obtain information on a specific package installed on system
# dpkg -L [package]
show list of files provided by a package installed on system
# dpkg --contents [package.deb]
show list of files provided by a package not yet installed
# dpkg -S /bin/ping
verify which package belongs to a given file

Disk Space


Command
Description
# df -h
show list of partitions mounted
# dpkg-query -W -f='${Installed-Size;10}t${Package}n' | sort -k1,1n
show the used space by installed deb packages, sorting by size (debian, ubuntu and alike)
# du -sh dir1
estimate space used by directory 'dir1'
# du -sk * | sort -rn
show size of the files and directories sorted by size
# ls -lSr |more
show size of the files and directories ordered by size
# rpm -q -a --qf '%10{SIZE}t%{NAME}n' | sort -k1,1n
show the used space by rpm packages installed sorted by size (fedora, redhat and alike)

File search

Command
Description
# find / -name file1
search file and directory into root filesystem from '/'
# find / -user user1
search files and directories belonging to 'user1'
# find /home/user1 -name \*.bin
search files with '. bin' extension within directory '/ home/user1'
# find /usr/bin -type f -atime +100
search binary files are not used in the last 100 days
# find /usr/bin -type f -mtime -10
search files created or changed within 10 days
# find / -name *.rpm -exec chmod 755 '{}' \;
search files with '.rpm' extension and modify permits
# find / -xdev -name \*.rpm
search files with '.rpm' extension ignoring removable partitions as cdrom, pen-drive, etc.…
# locate \*.ps
find files with the '.ps' extension - first run 'updatedb' command
# whereis halt
show location of a binary file, source or man
# which halt
show full path to a binary / executable

Files and Directory


Command
Description
# cd /home
enter to directory '/ home'
# cd ..
go back one level
# cd ../..
go back two levels
# cd
go to home directory
# cd ~user1
go to home directory
# cd -
go to previous directory
# cp file1 file2
copying a file
# cp dir/* .
copy all files of a directory within the current work directory
# cp -a /tmp/dir1 .
copy a directory within the current work directory
# cp -a dir1 dir2
copy a directory
# cp file file1
outputs the mime type of the file as text
# iconv -l
lists known encodings
# iconv -f fromEncoding -t toEncoding inputFile > outputFile
converting the coding of characters from one format to another
# find . -maxdepth 1 -name *.jpg -print -exec convert
batch resize files in the current directory and send them to a thumbnails directory (requires convert from Imagemagick)
# ln -s file1 lnk1
create a symbolic link to file or directory
# ln file1 lnk1
create a physical link to file or directory
# ls
view files of directory
# ls -F
view files of directory
# ls -l
show details of files and directory
# ls -a
show hidden files
# ls *[0-9]*
show files and directory containing numbers
# lstree
show files and directories in a tree starting from root(2)
# mkdir dir1
create a directory called 'dir1'
# mkdir dir1 dir2
create two directories simultaneously
# mkdir -p /tmp/dir1/dir2
create a directory tree
# mv dir1 new_dir
rename / move a file or directory
# pwd
show the path of work directory
# rm -f file1
delete file called 'file1'
# rm -rf dir1
remove a directory called 'dir1' and contents recursively
# rm -rf dir1 dir2
remove two directories and their contents recursively
# rmdir dir1
delete directory called 'dir1'
# touch -t 0712250000 file1
modify timestamp of a file or directory - (YYMMDDhhmm)
# tree
show files and directories in a tree starting from root(1)

Filesystem Analysis

Command
Description
# badblocks -v /dev/hda1
check bad blocks on disk hda1
# dosfsck /dev/hda1
repair / check integrity of dos filesystems on disk hda1
# e2fsck /dev/hda1
repair / check integrity of ext2 filesystem on disk hda1
# e2fsck -j /dev/hda1
repair / check integrity of ext3 filesystem on disk hda1
# fsck /dev/hda1
repair / check integrity of linux filesystem on disk hda1
# fsck.ext2 /dev/hda1
repair / check integrity of ext2 filesystem on disk hda1
# fsck.ext3 /dev/hda1
repair / check integrity of ext3 filesystem on disk hda1
# fsck.vfat /dev/hda1
repair / check integrity of fat filesystem on disk hda1
# fsck.msdos /dev/hda1
repair / check integrity of dos filesystem on disk hda1

Filesystem SWAP


Command
Description
# mkswap /dev/hda3
create a swap filesystem
# swapon /dev/hda3
activating a new swap partition
# swapon /dev/hda2 /dev/hdb3
activate two swap partitions

Format a Filesystem

Command
Description
# fdformat -n /dev/fd0
format a floppy disk
# mke2fs /dev/hda1
create a filesystem type linux ext2 on hda1 partition
# mke2fs -j /dev/hda1
create a filesystem type linux ext3 (journal) on hda1 partition
# mkfs /dev/hda1
create a filesystem type linux on hda1 partition
# mkfs -t vfat 32 -F /dev/hda1
create a FAT32 filesystem
# mkswap /dev/hda3
create a swap filesystem

IPTABLES (firewall)


Command
Description
# 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
# iptables -t filter -A INPUT -j LOG --log-prefix
Logging on input chain
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
configure a PAT (Port Address Traslation) on eth0 masking outbound packets
# iptables -t nat -A PREROUTING -d 192.168.0.1 -p tcp -m tcp --dport 22 -j DNAT --to-destination 10.0.0.2:22
redirect packets addressed to a host to another host

Microsoft Windows networks (samba)


Command
Description
# mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share
mount a windows network share
# nbtscan ip_addr
netbios name resolution
# nmblookup -A ip_addr
netbios name resolution
# smbclient -L ip_addr/hostname
show remote shares of a windows host
# smbget -Rr smb://ip_addr/share
like wget can download files from a host windows via smb

Mounting a Filesystem


Command
Description
# fuser -km /mnt/hda2
force umount when the device is busy
# mount /dev/hda2 /mnt/hda2
mount disk called hda2 - verify existence of the directory '/ mnt/hda2'
# 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 //WinClient/share /mnt/share
mount a windows network share
# umount /dev/hda2
unmount disk called hda2 - exit from mount point '/ mnt/hda2' first
# umount -n /mnt/hda2
run umount without writing the file /etc/mtab - useful when the file is read-only or the hard disk is full

Monitoring and debugging

Command
Description
# free -m
displays status of RAM in megabytes
# kill -9 process_id
force closure of the process and finish it
# kill -1 process_id
force a process to reload configuration
# last reboot
show history reboot
# lsmod
display kernel loaded
# lsof -p process_id
display a list of files opened by processes
# lsof /home/user1
displays a list of open files in a given path system
# ps -eafw
displays linux tasks
# ps -e -o pid,args --forest
displays linux tasks in a hierarchical mode
# pstree
mostra un albero dei processi sistema Shows a tree system processes
# smartctl -A /dev/hda
monitoring reliability of a hard-disk through SMART
# smartctl -i /dev/hda
check if SMART is active on a hard-disk
# strace -c ls >/dev/null
display system calls made and received by a process
# strace -f -e open ls >/dev/null
display library calls
# tail /var/log/dmesg
show events inherent to the process of booting kernel
# tail /var/log/messages
show system events
# top
display linux tasks using most cpu
# watch -n1 'cat /proc/interrupts'
display interrupts in real-time

Networking (LAN / WiFi)

Command
Description
# dhclient eth0
active interface 'eth0' in dhcp mode
# ethtool eth0
show network statistics of eth0
# host www.example.com
lookup hostname to resolve name to ip address and viceversa
# hostname
show hostname of system
# ifconfig eth0
show configuration of an ethernet network card
# ifconfig eth0 192.168.1.1 netmask 255.255.255.0
configure IP Address
# ifconfig eth0 promisc
configure 'eth0' in promiscuous mode to gather packets (sniffing)
# ifdown eth0
disable an interface 'eth0'
# ifup eth0
activate an interface 'eth0'
# ip link show
show link status of all network interfaces
# iwconfig eth1
show wireless networks
# iwlist scan
wifi scanning to display the wireless connections available
# mii-tool eth0
show link status of 'eth0'
# netstat -tup
show all active network connections and their PID
# netstat -tupl
show all network services listening on the system and their PID
# netstat -rn
show routing table alike "route -n"
# nslookup www.example.com
lookup hostname to resolve name to ip address and viceversa
# route -n
show routing table
# route add -net 0/0 gw IP_Gateway
configure default gateway
# route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
configure static route to reach network '192.168.0.0/16'
# route del 0/0 gw IP_gateway
remove static route
# echo "1" > /proc/sys/net/ipv4/ip_forward
activate ip routing temporarily
# tcpdump tcp port 80
show all HTTP traffic
# whois www.example.com
lookup on Whois database

Others useful commands

Command
Description
# alias hh='history'
set an alias for a command - hh = history
# apropos ...keyword
display a list of commands that pertain to keywords of a program , useful when you know what your program does, but you don't know the name of the command
# chsh
change shell command
# chsh --list-shells
nice command to know if you have to remote into another box
# gpg -c file1
encrypt a file with GNU Privacy Guard
# gpg file1.gpg
decrypt a file with GNU Privacy Guard
# ldd /usr/bin/ssh
show shared libraries required by ssh program
# man ping
display the on-line manual pages for example on ping command - use '-k' option to find any related commands
# mkbootdisk --device /dev/fd0 `uname -r`
create a boot floppy
# wget -r www.example.com
download an entire web site
# wget -c www.example.com/file.iso
download a file with the ability to stop the download and resume later
# echo 'wget -c www.example.com/files.iso' | at 09:00
start a download at any given time
# whatis ...keyword
displays description of what a program does
# who -a
show who is logged on, and print: time of last system boot, dead processes, system login processes, active processes spawned by init, current runlevel, last system clock change

Pacman packages tool (Arch, Frugalware and alike)

Command
Description
# pacman -S name
Install package 'name' with dependencies
# pacman -R name
Delete package 'name' and all files of it



No comments: