1.1. System information
arch displays the machine's processor architecture(1)
uname -m displays the machine's processor architecture(2)
uname -r displays the kernel version in use
dmidecode -q displays hardware system components - (SMBIOS/DMI)
hdparm -i /dev/hda lists the architectural characteristics of a disk
hdparm -tT /dev/sda performs a test read operation on the disk
cat /proc/cpuinfo displays CPU info information
cat /proc/interrupts shows interrupts
cat /proc/meminfo checks memory usage
cat /proc/swaps shows which swaps are used
cat /proc/version displays the kernel version
cat /proc/net/dev displays network adapters and statistics
cat /proc/mounts shows mounted file systems
lspci -tv lists PCI devices
lsusb -tv displays USB devices
date displays system date
cal 2007 displays the calendar for 2007
date 041217002007.00 Set date and time - month, day, hour, minute, year, second.
clock -w saves time modifications to BIOS
Shutdown (system shutdown, restart and logout)
shutdown -h now shut down the system(1)
init 0 shut down the system(2)
telinit 0 shut down the system(3)
shutdown -h hours:minutes & amp; Shut down the system at scheduled time
shutdown -c cancels the scheduled shutdown of the system
shutdown -r now restart(1)
reboot(2)
logout logout
1.2. Files and Directories
cd /home enters the '/home' directory'
cd .. Return to the previous directory
cd ../.. returns to the upper two levels of directory
cd to enter your home directory
cd ~user1 enters the personal home directory
cd - Return to the last directory you were in
pwd shows working path
ls View files in a directory
ls -F View files in a directory
ls -l displays file and directory details
ls -a shows hidden files
ls [0-9] displays file and directory names containing numbers
tree displays the tree structure of files and directories starting from the root directory (1)
lstree displays the tree structure of files and directories starting from the root directory (2)
mkdir dir1 creates a directory called 'dir1'
mkdir dir1 dir2 creates two directories at the same time
mkdir -p /tmp/dir1/dir2 creates a directory tree
rm -f file1 deletes a file named 'file1''
rmdir dir1 deletes a directory called 'dir1'
rm -rf dir1 deletes a directory called 'dir1' and deletes its contents at the same time
rm -rf dir1 dir2 deletes two directories and their contents at the same time
mv dir1 new_dir rename/move a directory
cp file1 file2 copy a file
cp dir/* . Copy all files in a directory to the current working directory
cp -a /tmp/dir1 . Copy a directory to the current working directory
cp -a dir1 dir2 copy a directory
ln -s file1 lnk1 creates a soft link to a file or directory
ln file1 lnk1 creates a physical link to a file or directory
touch -t 0712250000 file1 Modify the timestamp of a file or directory - (YYMMDDhhmm)
file file1 outputs the mime type of the file as text
iconv -l lists known encodings
iconv -f fromEncoding -t toEncoding inputFile > outputFile creates anew from the given input file by assuming it is encoded in fromEncodingand converting it to toEncoding.
find . -maxdepth 1 -name *.jpg -print -exec convert "{}" -resize 80x60"thumbs/{}" \; batch resize files in the current directory and send themto a thumbnails directory (requires convert from Imagemagick)
1.3. File search
find / -name file1 Enter the root file system starting from '/' to search for files and directories
find / -user user1 searches for files and directories belonging to user 'user1'
find /home/user1 -name *.bin Search for files ending with '.bin' in the directory '/home/user1'
find /usr/bin -type f -atime 100 searches for executable files that have not been used in the past 100 days
find /usr/bin -type f -mtime -10 searches for files that were created or modified within 10 days
find / -name *.rpm -exec chmod 755 '{}' \; Search for files ending with '.rpm' and define their permissions
find / -xdev -name *.rpm Search for files ending with '.rpm', ignoring removable devices such as CD-ROM drives and USB drives
locate *.ps looks for files ending with '.ps' - run the 'updatedb' command first
whereis halt displays the location of a binary file, source code or man
which halt displays the full path to a binary or executable file
Mount a file system
mount /dev/hda2 /mnt/hda2 Mount a disk called hda2 - Make sure the directory '/mnt/hda2' already exists
umount /dev/hda2 unmounts a disk called hda2 - first exit from the mount point '/mnt/hda2'
fuser -km /mnt/hda2 Force uninstall when the device is busy
umount -n /mnt/hda2 runs the unmount operation without writing to the /etc/mtab file - useful when the file is read-only or when the disk is full
mount /dev/fd0 /mnt/floppy mount a floppy disk
mount /dev/cdrom /mnt/cdrom mount a cdrom or dvdrom
mount /dev/hdc /mnt/cdrecorder mount a cdrw or dvdrom
mount /dev/hdb /mnt/cdrecorder mount a cdrw or dvdrom
mount -o loop file.iso /mnt/cdrom Mount a file or ISO image file
mount -t vfat /dev/hda5 /mnt/hda5 mounts a Windows FAT32 file system
mount /dev/sda1 /mnt/usbdisk Mount a USB disk or flash memory device
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share Mount a windows network share
1.4. Disk space
df -h displays the list of mounted partitions
ls -lSr |more sorts files and directories by size
du -sh dir1 estimates the disk space used by directory 'dir1'
du -sk * | sort -rn displays the size of files and directories in sequence based on capacity
rpm -q -a --qf '{<!-- -->SIZE}t%{<!-- -->NAME}n' | sort -k1,1n Display the installed files in order based on size The space used by the rpm package (fedora, redhat type systems)
dpkg-query -W -f='${<!-- -->Installed-Size;10}t${<!-- -->Package}n' | sort -k1,1n based on size Display the space used by installed deb packages (ubuntu, debian system)
Users and groups
groupadd group_name creates a new user group
groupdel group_name deletes a user group
groupmod -n new_group_name old_group_name Rename a user group
useradd -c "Name Surname " -g admin -d /home/user1 -s /bin/bash user1 Create a user belonging to the "admin" user group
useradd user1 creates a new user
userdel -r user1 deletes a user ('-r' excludes home directory)
usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1 Modify user attributes
passwd change password
passwd user1 changes the password of a user (only allowed to be executed by root)
chage -E 2005-12-31 user1 Set the expiration date of user password
pwck checks '/etc/passwd' for file format and syntax fixes and existing users
grpck checks '/etc/passwd' for file format and syntax corrections and existing groups
newgrp group_name Log into a new group to change the default group for newly created files
Permissions on files - use " " to set permissions, use "-" to cancel
ls -lh shows permissions
ls /tmp | pr -T5 -W$COLUMNS Divides the terminal into 5 columns for display
chmod ugo rwx directory1 sets the directory owner (u), group (g) and others (o) to read (r), write (w) and execute (x) permissions
chmod go-rwx directory1 removes the read, write, and execute permissions of the group (g) and others (o) on the directory
chown user1 file1 changes the owner attribute of a file
chown -R user1 directory1 changes the owner attributes of a directory and changes the attributes of all files in the directory at the same time
chgrp group1 file1 changes the group of the file
chown user1:group1 file1 changes the owner and group attributes of a file
find / -perm -u s lists all files controlled by SUID in a system
chmod u s /bin/file1 sets the SUID bit of a binary file - the user running the file is also given the same permissions as the owner
chmod u-s /bin/file1 disables the SUID bit of a binary file
chmod g s /home/public sets the SGID bit of a directory - similar to SUID, but for directories
chmod g-s /home/public disables the SGID bit for a directory
chmod o t /home/public sets the STIKY bit on a file - only allows the legitimate owner to delete the file
chmod o-t /home/public disables the STIKY bit for a directory
Special properties for files - use " " to set permissions, use "-" for cancellation
chattr a file1 only allows reading and writing files in append mode
chattr c file1 allows this file to be automatically compressed/decompressed by the kernel
chattr d file1 When performing a file system backup, the dump program will ignore this file
chattr i file1 is set to an immutable file that cannot be deleted, modified, renamed or linked.
chattr s file1 allows a file to be safely deleted
chattr S file1 Once the application performs a write operation on this file, the system immediately writes the modified results to the disk.
chattr u file1 If the file is deleted, the system will allow you to restore the deleted file later.
lsattr displays special attributes
1.5. Packaging and compressing files
bunzip2 file1.bz2 Unzip a file called 'file1.bz2'
bzip2 file1 compresses a file called 'file1'
gunzip file1.gz decompresses a file called 'file1.gz'
gzip file1 compresses a file called 'file1'
gzip -9 file1 maximum compression
rar a file1.rar test_file Create a package called 'file1.rar'
rar a file1.rar file1 file2 dir1 Compress 'file1', 'file2' and directory 'dir1' at the same time
rar x file1.rar Unzip the rar package
unrar x file1.rar decompress rar package
tar -cvf archive.tar file1 creates an uncompressed tarball
tar -cvf archive.tar file1 file2 dir1 creates an archive file containing 'file1', 'file2' and 'dir1'
tar -tf archive.tar displays the contents of a package
tar -xvf archive.tar releases a package
tar -xvf archive.tar -C /tmp releases the compressed package to the /tmp directory
tar -cvfj archive.tar.bz2 dir1 creates a compressed package in bzip2 format
tar -xvfj archive.tar.bz2 Decompress a compressed package in bzip2 format
tar -cvfz archive.tar.gz dir1 creates a compressed package in gzip format
tar -xvfz archive.tar.gz decompresses a gzip format compressed package
zip file1.zip file1 creates a compressed package in zip format
zip -r file1.zip file1 file2 dir1 Compress several files and directories into a compressed package in zip format at the same time
unzip file1.zip Decompress a zip format compressed package
1.6.RPM package – (Fedora, Redhat and similar systems)
rpm -ivh package.rpm installs an rpm package
rpm -ivh --nodeeps package.rpm Install an rpm package ignoring dependency warnings
rpm -U package.rpm updates an rpm package without changing its configuration file
rpm -F package.rpm updates an rpm package that is already installed
rpm -e package_name.rpm deletes an rpm package
rpm -qa displays all installed rpm packages in the system
rpm -qa | grep httpd displays all rpm packages whose names contain the words "httpd"
rpm -qi package_name Get special information about an installed package
rpm -qg "System Environment/Daemons" displays the rpm package of a component
rpm -ql package_name displays a list of files provided by an installed rpm package
rpm -qc package_name displays a list of configuration files provided by an installed rpm package
rpm -q package_name --whatrequires displays a list of dependencies on an rpm package
rpm -q package_name --whatprovides displays the volume occupied by an rpm package
rpm -q package_name --scripts displays scripts executed during installation/removal
rpm -q package_name --changelog displays the modification history of an rpm package
rpm -qf /etc/httpd/conf/httpd.conf Confirm which rpm package the given file is provided by
rpm -qp package.rpm -l displays a list of files provided by an rpm package that has not yet been installed
rpm --import /media/cdrom/RPM-GPG-KEY Import public key digital certificate
rpm --checksig package.rpm confirms the integrity of an rpm package
rpm -qa gpg-pubkey confirms the integrity of all installed rpm packages
rpm -V package_name checks file size, permission, type, owner, group, MD5 check and last modified time
rpm -Va checks all installed rpm packages on the system - use with caution
rpm -Vp package.rpm confirms that an rpm package has not been installed yet
rpm2cpio package.rpm | cpio --extract --make-directories bin Run an executable file from an rpm package
rpm -ivh /usr/src/redhat/RPMS/arch/package.rpm Installs a built package from an rpm source
rpmbuild --rebuild package_name.src.rpm Build an rpm package from an rpm source code
1.7.YUM Package Upgrader – (Fedora, RedHat and similar systems)
yum install package_name download and install an rpm package
yum localinstall package_name.rpm will install an rpm package, using your own software repository to resolve all dependencies for you
yum update package_name.rpm updates all rpm packages installed in the current system
yum update package_name updates an rpm package
yum remove package_name deletes an rpm package
yum list lists all packages installed on the current system
yum search package_name searches for software packages in the rpm repository
yum clean packages cleans the rpm cache and deletes downloaded packages
yum clean headers deletes all header files
yum clean all deletes all cached packages and header files
**DEB packages (Debian, Ubuntu and similar systems)**
dpkg -i package.deb install/update a deb package
dpkg -r package_name deletes a deb package from the system
dpkg -l displays all installed deb packages in the system
dpkg -l | grep httpd displays all deb packages whose names contain the words "httpd"
dpkg -s package_name obtains information about a special package installed on the system
dpkg -L package_name displays the file list provided by a deb package that has been installed on the system
dpkg --contents package.deb displays a list of files provided by a package that has not yet been installed.
dpkg -S /bin/ping confirms which deb package the given file is provided by
1.8.APT Software Tool (Debian, Ubuntu and similar systems)
apt-get install package_name install/update a deb package
apt-cdrom install package_name Install/update a deb package from CD
apt-get update upgrades packages in the list
apt-get upgrade upgrades all installed software
apt-get remove package_name removes a deb package from the system
apt-get check confirms that the dependent software repository is correct
apt-get clean clean cache from downloaded packages
apt-cache search searched-package returns the package name containing the search string to be searched
View file contents
cat file1 View the contents of the file forward starting from the first byte
tac file1 View the contents of a file in reverse order starting from the last line
more file1 View the contents of a long file
less file1 is similar to the 'more' command, but it allows reverse operations on a file as well as forward operations
head -2 file1 View the first two lines of a file
tail -2 file1 View the last two lines of a file
tail -f /var/log/messages View the content being added to a file in real time
text processing
cat file1 file2 ... | command <> file1_in.txt_or_file1_out.txtgeneral syntax for text manipulation using PIPE, STDIN and STDOUT
cat file1 | command(sed, grep, awk, grep, etc...) > result.txt Merges the detailed description text of a file and writes the introduction into a new file
cat file1 | command(sed, grep, awk, grep, etc...) >> result.txt Merge the detailed description text of a file and write the introduction into an existing file
grep Aug /var/log/messages Search for the keyword "Aug" in the file '/var/log/messages'
grep ^Aug /var/log/messages searches for words starting with "Aug" in the file '/var/log/messages'
grep [0-9] /var/log/messages selects all lines containing numbers in the file '/var/log/messages'
grep Aug -R /var/log/* Searches the directory '/var/log' and subsequent directories for the string "Aug"
sed 's/stringa1/stringa2/g' example.txt Replace "string1" in the example.txt file with "string2"
sed '/^$/d' example.txt removes all blank lines from the example.txt file
sed '/ *#/d; /^$/d' example.txt removes all comments and blank lines from the example.txt file
echo 'esempio' | tr '[:lower:]' '[:upper:]' Merge the contents of upper and lower cells
sed -e '1d' result.txt excludes the first line from the file example.txt
sed -n '/stringa1/p' View lines containing only the word "string1"
sed -e 's/ *$//' example.txt deletes the last whitespace characters in each line
sed -e 's/stringa1//g' example.txt removes only the word "string1" from the document and retains the rest
sed -n '1,5p;5q' example.txt View the contents from the first line to the fifth line
sed -n '5p;5q' example.txt View line 5
sed -e 's/00*/0/g' example.txt replaces multiple zeros with a single zero
cat -n file1 indicates the number of lines in the file
cat example.txt | awk 'NR%2==1' deletes all even lines in the example.txt file
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 columns of a line
paste file1 file2 merges the contents of two files or two columns
paste -d ' ' file1 file2 merges the contents of two files or two columns, separated by " "
sort file1 file2 sorts the contents of two files
sort file1 file2 | uniq takes out the union of two files (only one copy of duplicate lines is kept)
sort file1 file2 | uniq -u removes intersections and leaves other lines
sort file1 file2 | uniq -d removes the intersection of two files (leaving only files that exist in both files)
comm -1 file1 file2 compares the contents of two files and deletes only the contents contained in 'file1'
comm -2 file1 file2 compares the contents of two files and deletes only the contents contained in 'file2'
comm -3 file1 file2 compares the contents of two files and only deletes the parts common to both files
1.9. Character settings and file format conversion
dos2unix filedos.txt fileunix.txt Converts the format of a text file from MSDOS to UNIX
unix2dos fileunix.txt filedos.txt Converts the format of a text file from UNIX to MSDOS
recode ..HTML < page.txt > page.html Convert a text file to html
recode -l | more displays all allowed conversion formats
1.10. File system analysis
badblocks -v /dev/hda1 Check bad blocks on disk hda1
fsck /dev/hda1 repair/check the integrity of the linux file system on the hda1 disk
fsck.ext2 /dev/hda1 Repair/check the integrity of the ext2 file system on the hda1 disk
e2fsck /dev/hda1 repair/check the integrity of the ext2 file system on the hda1 disk
e2fsck -j /dev/hda1 repair/check the integrity of the ext3 file system on the hda1 disk
fsck.ext3 /dev/hda1 Repair/check the integrity of the ext3 file system on the hda1 disk
fsck.vfat /dev/hda1 Repair/check the integrity of the fat file system on the hda1 disk
fsck.msdos /dev/hda1 Repair/check the integrity of the dos file system on the hda1 disk
dosfsck /dev/hda1 repair/check the integrity of the dos file system on the hda1 disk
Initialize a file system
mkfs /dev/hda1 creates a file system on the hda1 partition
mke2fs /dev/hda1 creates a linux ext2 file system in the hda1 partition
mke2fs -j /dev/hda1 creates a linux ext3 (journaled) file system in the hda1 partition
mkfs -t vfat 32 -F /dev/hda1 creates a FAT32 file system
fdformat -n /dev/fd0 formats a floppy disk
mkswap /dev/hda3 creates a swap file system
1.11.SWAP file system
mkswap /dev/hda3 creates a swap file system
swapon /dev/hda3 enables a new swap file system
swapon /dev/hda2 /dev/hdb3 enables two swap partitions
1.12. Backup
dump -0aj -f /tmp/home0.bak /home Make a full backup of the '/home' directory
dump -1aj -f /tmp/home0.bak /home Make an interactive backup of the '/home' directory
restore -if /tmp/home0.bak restore an interactive backup
rsync -rogpav --delete /home /tmp synchronizes directories on both sides
rsync -rogpav -e ssh --delete /home ip_address:/tmp rsync over SSH channel
rsync -az -e ssh --delete ip_addr:/home/public /home/local synchronizes a remote directory to a local directory through ssh and compression
rsync -az -e ssh --delete /home/local ip_addr:/home/public synchronizes the local directory to the remote directory through ssh and compression
dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz' Perform a backup of the local disk on the remote host through ssh
dd if=/dev/sda of=/tmp/file1 backup disk contents to a file
tar -Puf backup.tar /home/user performs an interactive backup operation on the '/home/user' directory
( cd /tmp/local/ & amp; & amp; tar c . ) | ssh -C user@ip_addr 'cd /home/share/ & amp; & amp; tar x -p' in a remote directory via ssh Copy the contents of a directory
( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home & amp; & amp; tar x -p' Copy a local directory to the remote directory via ssh
tar cf - . | (cd /tmp/backup ; tar xf - ) locally copies a directory to another place, retaining the original permissions and links
find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents Find and copy all files ending with '.txt' from a directory to another directory
find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2 Find all files ending with '.log' and make a bzip package
dd if=/dev/hda of=/dev/fd0 bs=512 count=1 Make an action to copy the contents of MBR (Master Boot Record) to the floppy disk
dd if=/dev/fd0 of=/dev/hda bs=512 count=1 Restore MBR contents from backup that has been saved to floppy disk
1.13. CD
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force clears the contents of a rewritable disc
mkisofs /dev/cdrom > cd.iso creates an iso image file of the CD on the disk
mkisofs /dev/cdrom | gzip > cd_iso.gz Create a compressed CD iso image file on the disk
mkisofs -J -allow-leading-dots -R -V "Label CD" -iso-level 4 -o ./cd.iso data_cd Create an iso image file of a directory
cdrecord -v dev=/dev/cdrom cd.iso Burn an ISO image file
gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom - Burn a compressed ISO image file
mount -o loop cd.iso /mnt/iso mounts an ISO image file
cd-paranoia -B Rip tracks from a CD to a wav file
cd-paranoia -- "-3" Rip tracks from a CD disc to a wav file (parameter -3)
cdrecord --scanbus scans the bus to identify scsi channels
dd if=/dev/hdc | md5sum Verifies the md5sum encoding of a device, such as a CD
1.14. Network – (Ethernet and WIFI wireless)
ifconfig eth0 displays the configuration of an Ethernet card
ifup eth0 enables an 'eth0' network device
ifdown eth0 disables an 'eth0' network device
ifconfig eth0 192.168.1.1 netmask 255.255.255.0 control IP address
ifconfig eth0 promisc sets 'eth0' into promiscuous mode for packet sniffing (sniffing)
dhclient eth0 enables 'eth0' in dhcp mode
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
hostname show hostname of system
host www.example.com lookup hostname to resolve name to ip address and viceversa(1)
nslookup www.example.com lookup hostname to resolve name to ip address and viceversa(2)
ip link show show link status of all interfaces
mii-tool eth0 show link status of 'eth0'
ethtool eth0 show statistics of network card 'eth0'
netstat -tup show all active network connections and their PID
netstat -tupl show all network services listening on the system and their PID
tcpdump tcp port 80 show all HTTP traffic
iwlist scan show wireless networks
iwconfig eth1 show configuration of a wireless network card
hostname show hostname
host www.example.com lookup hostname to resolve name to ip address and viceversa
nslookup www.example.com lookup hostname to resolve name to ip address and viceversa
whois www.example.com lookup on Whois database
Microsoft Windows networks (SAMBA)
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
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share mount a windows network share