Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
System information
Command # arch # cal 2007 # cat /proc/cpuinfo # cat /proc/interrupts # cat /proc/meminfo # cat /proc/swaps # cat /proc/version # cat /proc/net/dev # cat /proc/mounts # clock -w # date # date 041217002007.00 Description show architecture of machine(1) show the timetable of 2007 show information CPU info show interrupts verify memory use show file(s) swap show version of the kernel show network adpters and statistics show mounted file system(s) save date changes on BIOS show system date set date and time MonthDayhoursMinutesYear.Seconds show hardware system components - (SMBIOS / DMI) displays the characteristics of a hard-disk perform test reading on a hard-disk display PCI devices show USB devices show architecture of machine(2) show used kernel version
# dmidecode -q
# hdparm -i /dev/hda # hdparm -tT /dev/sda # lspci -tv # lsusb -tv # uname -m # uname -r
# cp -a /tmp/dir1 . # cp -a dir1 dir2 # cp file file1 # iconv -l # iconv -f fromEncoding -t toEncoding inputFile > outputFile # find . -maxdepth 1 -name *.jpg -print -exec convert
# mkdir dir1 # mkdir dir1 dir2 # mkdir -p /tmp/dir1/dir2 # mv dir1 new_dir # pwd # rm -f file1 # rm -rf dir1
create a directory called 'dir1' create two directories simultaneously create a directory tree rename / move a file or directory show the path of work directory delete file called 'file1' remove a directory called 'dir1' and contents recursively remove two directories and their contents recursively delete directory called 'dir1' modify timestamp of a file or directory (YYMMDDhhmm) show files and directories in a tree starting from root(1)
# tree
File search
Command # find / -name file1 Description search file and directory into root filesystem from '/' search files and directories belonging to 'user1' search files with '. bin' extension within directory '/ home/user1' search binary files are not used in the last 100 days search files created or changed within 10 days search files with '.rpm' extension and modify permits search files with '.rpm' extension ignoring removable partitions as cdrom, pen-drive, etc. find files with the '.ps' extension - first run 'updatedb' command show location of a binary file, source or man show full path to a binary / executable
# find /usr/bin -type f -mtime -10 # find / -name *.rpm -exec chmod 755 '{}' \;
# locate \*.ps
Mounting a Filesystem
Description force umount when the device is busy mount disk called hda2 - verify existence of the directory '/ mnt/hda2' mount a floppy disk mount a cdrom / dvdrom mount a cdrw / dvdrom mount a cdrw / dvdrom mount a file or iso image mount a Windows FAT32 file system mount a usb pen-drive or flash-drive mount a windows network share unmount disk called hda2 - exit from mount point '/ mnt/hda2' first run umount without writing the file /etc/mtab useful when the file is read-only or the hard disk is full
# mount /dev/fd0 /mnt/floppy # mount /dev/cdrom /mnt/cdrom # mount /dev/hdc /mnt/cdrecorder # mount /dev/hdb /mnt/cdrecorder # mount -o loop file.iso /mnt/cdrom # mount -t vfat /dev/hda5 /mnt/hda5 # mount /dev/sda1 /mnt/usbdisk # mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share # umount /dev/hda2
# umount -n /mnt/hda2
Disk Space
Command # df -h # dpkg-query -W -f='${InstalledSize;10}t${Package}n' | sort -k1,1n # du -sh dir1 # du -sk * | sort -rn Description show list of partitions mounted show the used space by installed deb packages, sorting by size (debian, ubuntu and alike) estimate space used by directory 'dir1' show size of the files and directories sorted by size
# ls -lSr |more
show size of the files and directories ordered by size show space used by rpm packages installed sorted by size (fedora, redhat and like)
delete a group rename a group from moon to sun check correct syntax and file format of '/etc/group' and groups existence log into a new group to change default group of newly created files change password change a user password (only by root) check correct syntax and file format of '/etc/passwd' and users existence create a new user "user1" belongs "admin" group create a new user delete a user ( '-r' eliminates home directory) change user attributes
# newgrp - [group]
# useradd -c "User Linux" -g admin -d /home/user1 -s /bin/bash user1 # useradd user1 # userdel -r user1 # usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1
Permits on Files
Command # chgrp group1 file1 # chmod ugo+rwx directory1 Description change group of files set permissions reading (r), write (w) and (x) access to users owner (u) group (g) and others (o)
remove permits reading (r), write (w) and (x) access to users group (g) and others (or set SUID bit on a binary file - the user that running that file gets same privileges as owner disable SUID bit on a binary file set SGID bit on a directory - similar to SUID but for directory disable SGID bit on a directory set STIKY bit on a directory - allows files deletion only to legitimate owners disable STIKY bit on a directory change owner of a file change user owner of a directory and all the files and directories contained inside change user and group ownership of a file
view all files on the system with SUID configured show permits on files divide terminal into 5 columns
# chattr +d file1
# chattr +i file1
# chattr +u file1
# lsattr
# tar -xvf archive.tar -C /tmp # tar -cvfj archive.tar.bz2 dir1 # tar -xvfj archive.tar.bz2 # tar -cvfz archive.tar.gz dir1 # tar -xvfz archive.tar.gz # unrar x file1.rar # unzip file1.zip # zip file1.zip file1 # zip -r file1.zip file1 file2 dir1
extract a tarball into / tmp create a tarball compressed into bzip2 decompress a compressed tar archive in bzip2 create a tarball compressed into gzip decompress a compressed tar archive in gzip decompress rar archive decompress a zip archive create an archive compressed in zip compress in zip several files and directories simultaneously
# rpm -U [package.rpm]
# rpm -F [package.rpm]
# rpm -e [package] # rpm -qa # rpm -qa | grep httpd # rpm -qi [package] # rpm -qg "System Environment/Daemons" # rpm -ql [package]
# rpm -q [package] --whatprovides # rpm -q [package] --scripts # rpm -q [package] --changelog # rpm -qf /etc/httpd/conf/httpd.conf
show list of files provided by a rpm package not yet installed import public-key digital signature verify the integrity of a rpm package verify integrity of all rpm packages installed check file size, permissions, type, owner, group, MD5 checksum and last modification check all rpm packages installed on the system use with caution verify a rpm package not yet installed install a package built from a rpm source extract executable file from a rpm package build a rpm package from a rpm source
# rpm --import /media/cdrom/RPM-GPG-KEY # rpm --checksig [package.rpm] # rpm -qa gpg-pubkey # rpm -V [package]
# rpm -Va
# rpm -Vp [package.rpm] # rpm -ivh /usr/src/redhat/RPMS/`arch`/[package.rpm] # rpm2cpio [package.rpm] | cpio --extract -make-directories *bin* # rpmbuild --rebuild [package.src.rpm]
# yum -y update # yum update [package] # yum remove [package] # yum list # yum search [package] # yum clean [package] # yum clean headers
# dpkg -s [package]
obtain information on a specific package installed on system show list of files provided by a package installed on system show list of files provided by a package not yet installed verify which package belongs to a given file
# dpkg -L [package]
# dpkg -S /bin/ping
# apt-cdrom install [package] # apt-get install [package] # apt-get update # apt-get upgrade # apt-get remove [package] # apt-get check # apt-get clean
view last two lines of a file view in real time what is added to a file
Text Manipulation
Command # cat example.txt | awk 'NR%2==1' # echo a b c | awk '{print $1}' # echo a b c | awk '{print $1,$3}' # cat -n file1 # comm -1 file1 file2 Description remove all even lines from example.txt view the first column of a line view the first and third column of a line number row of a file compare contents of two files by deleting only unique lines from 'file1' compare contents of two files by deleting only unique lines from 'file2' compare contents of two files by deleting only the lines that appear on both files find differences between two files look up words "Aug" on file '/var/log/messages' look up words that begin with "Aug" on file '/var/log/messages' select from file '/var/log/messages' all lines that contain numbers search string "Aug" at directory '/var/log' and below merging contents of two files for columns merging contents of two files for columns with '+' delimiter on the center find differences between two files and merge interactively alike "diff" replace "string1" with "string2" in example.txt remove all blank lines from example.txt remove comments and blank lines from example.txt eliminates the first line from file example.txt view only lines that contain the word "string1" remove empty characters at the end of each row remove only the word "string1" from text and
# sed 's/string1/string2/g' example.txt # sed '/^$/d' example.txt # sed '/ *#/d; /^$/d' example.txt
# sed -e '1d' exampe.txt # sed -n '/string1/p' # sed -e 's/ *$//' example.txt # sed -e 's/string1//g' example.txt
leave intact all # sed -n '1,5p' example.txt # sed -n '5p;5q' example.txt # sed -e 's/00*/0/g' example.txt # sort file1 file2 # sort file1 file2 | uniq # sort file1 file2 | uniq -u print from 1th to 5th row of example.txt print row number 5 of example.txt replace more zeros with a single zero sort contents of two files sort contents of two files omitting lines repeated sort contents of two files by viewing only unique line sort contents of two files by viewing only duplicate line convert from lower case in upper case
Filesystem Analysis
Command # badblocks -v /dev/hda1 # dosfsck /dev/hda1 Description check bad blocks on disk hda1 repair / check integrity of dos filesystems on disk hda1 repair / check integrity of ext2 filesystem on disk hda1 repair / check integrity of ext3 filesystem on disk hda1 repair / check integrity of linux filesystem on disk hda1 repair / check integrity of ext2 filesystem on disk hda1 repair / check integrity of ext3 filesystem on disk hda1 repair / check integrity of fat filesystem on disk hda1 repair / check integrity of dos filesystem on disk
# e2fsck /dev/hda1
# e2fsck -j /dev/hda1
# fsck /dev/hda1
# fsck.ext2 /dev/hda1
# fsck.ext3 /dev/hda1
# fsck.vfat /dev/hda1
# fsck.msdos /dev/hda1
hda1
Format a Filesystem
Command # fdformat -n /dev/fd0 # mke2fs /dev/hda1 Description format a floppy disk create a filesystem type linux ext2 on hda1 partition create a filesystem type linux ext3 (journal) on hda1 partition create a filesystem type linux on hda1 partition create a FAT32 filesystem create a swap filesystem
# mke2fs -j /dev/hda1
Filesystem SWAP
Command # mkswap /dev/hda3 # swapon /dev/hda3 # swapon /dev/hda2 /dev/hdb3 Description create a swap filesystem activating a new swap partition activate two swap partitions
Backup
Command # find /var/log -name '*.log' | tar cv --filesfrom=- | bzip2 > log.tar.bz2 # find /home/user1 -name '*.txt' | xargs cp av --target-directory=/home/backup/ -parents # dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz' # dd if=/dev/sda of=/tmp/file1 # dd if=/dev/hda of=/dev/fd0 bs=512 count=1 # dd if=/dev/fd0 of=/dev/hda bs=512 count=1 # dump -0aj -f /tmp/home0.bak /home # dump -1aj -f /tmp/home0.bak /home # restore -if /tmp/home0.bak # rsync -rogpav --delete /home /tmp # rsync -rogpav -e ssh --delete /home ip_address:/tmp Description find all files with '.log' extention and make an bzip archive find and copy all files with '.txt' extention from a directory to another make a backup of a local hard disk on remote host via ssh backup content of the harddrive to a file make a copy of MBR (Master Boot Record) to floppy restore MBR from backup copy saved to floppy make a full backup of directory '/home' make a incremental backup of directory '/home' restoring a backup interactively synchronization between directories rsync via SSH tunnel
# rsync -az -e ssh --delete ip_addr:/home/public /home/local # rsync -az -e ssh --delete /home/local ip_addr:/home/public # tar -Puf backup.tar /home/user
synchronize a local directory with a remote directory via ssh and compression synchronize a remote directory with a local directory via ssh and compression make a incremental backup of directory '/home/user' copy content of a directory on remote directory via ssh copy a local directory on remote directory via ssh local copy preserving permits and links from a directory to another
# ( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p' # ( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p' # tar cf - . | (cd /tmp/backup ; tar xf - )
CDROM
Command # cd-paranoia -B # cd-paranoia -# cdrecord -v gracetime=2 dev=/dev/cdrom eject blank=fast -force # cdrecord -v dev=/dev/cdrom cd.iso # gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom # cdrecord --scanbus # dd if=/dev/hdc | md5sum # mkisofs /dev/cdrom > cd.iso # mkisofs /dev/cdrom | gzip > cd_iso.gz # mkisofs -J -allow-leading-dots -R -V # mount -o loop cd.iso /mnt/iso Description rip audio tracks from a CD to wav files rip first three audio tracks from a CD to wav files clean a rewritable cdrom burn an ISO image burn a compressed ISO image scan bus to identify the channel scsi perform an md5sum on a device, like a CD create an iso image of cdrom on disk create a compressed iso image of cdrom on disk create an iso image of a directory mount an ISO image
configure 'eth0' in promiscuous mode to gather packets (sniffing) disable an interface 'eth0' activate an interface 'eth0' show link status of all network interfaces show wireless networks wifi scanning to display the wireless connections available show link status of 'eth0' show all active network connections and their PID show all network services listening on the system and their PID show routing table alike "route -n" lookup hostname to resolve name to ip address and viceversa show routing table configure default gateway configure static route to reach network '192.168.0.0/16' remove static route activate ip routing show all HTTP traffic lookup on Whois database
# ifdown eth0 # ifup eth0 # ip link show # iwconfig eth1 # iwlist scan
# route -n # route add -net 0/0 gw IP_Gateway # route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 # route del 0/0 gw IP_gateway # echo "1" > /proc/sys/net/ipv4/ip_forward # tcpdump tcp port 80 # whois www.example.com
IPTABLES (firewall)
Command Description
# iptables -t filter -L # iptables -t nat -L # iptables -t filter -F # iptables -t nat -F # iptables -t filter -X # iptables -t filter telnet -j ACCEPT # iptables -t filter http -j DROP # iptables -t filter pop3 -j ACCEPT # iptables -t filter -A INPUT -p tcp --dport -A OUTPUT -p tcp --dport -A FORWARD -p tcp --dport -A INPUT -j LOG --log-prefix
show all chains of filtering table show all chains of nat table clear all rules from filtering table clear all rules from table nat delete any chains created by user allow telnet connections to input block HTTP connections to output allow POP3 connections to forward chain Logging on input chain configure a PAT (Port Address Traslation) on eth0 masking outbound packets redirect packets addressed to a host to another host
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE # 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
show system events display linux tasks using most cpu display interrupts in real-time
# chsh # chsh --list-shells # gpg -c file1 # gpg file1.gpg # ldd /usr/bin/ssh # man ping # mkbootdisk --device /dev/fd0 `uname -r` # wget -r www.example.com # wget -c www.example.com/file.iso # echo 'wget -c www.example.com/files.iso' | at 09:00 # whatis ...keyword # who -a