Geody Labs

# The Debian Linux Magic Spells File
# by Elf Qrin - http://labs.geody.com/
v0.51 r14may2008 fr--sep2005


# "One man cast a lingering spell of awe and wonder, of magical innocence overcoming evil, of simple courage conquering fear" -- J.R.R. Tolkien's The Lord of the Rings (1978 Ralph Bakshi animated film)
# "A script kiddie is someone who thinks of code as magical incantations and asks only 'what do I need to type to make this happen?'" -- anonymous

----- Notes: - This file needs to be edited and formatted. - In this file, jed is used as the default editor for it's more intuitive than others. you might want to use vi or vim instead. - Most commands work also on Ubuntu / Kubuntu / Xubuntu / Edubuntu - You may send contributions and suggestions to (see: http://www.geody.com/contacts.php ) ----- Install a software package using APT (Advanced Package Tool): apt-get install base-config apt-setup Set an ATP online source manually: (example from http://debian.fastweb.it/ ) The normal way of accessing the mirror is by putting this line in /etc/apt/sources.list : deb http://debian.fastweb.it/debian stable main contrib non-free (use unstable instead of stable if you want bleeding edge technology) Note: if you use the "stable" distribution, even if you use this mirror, you should also always keep this line: deb http://security.debian.org stable/updates main contrib non-free Update the list of packages: # After adding a new apt-get source, or before installing new packages, enter this command apt-get update Should a "Segmentation fault" error occur when updating the list of packages, you can fix it with these commands: rm -r /var/lib/apt/lists/* rm /var/cache/apt/*.bin # will delete pkgcache.bin and srcpkgcache.bin Upgrade a distribution (better after a fresh install): apt-get dist-upgrade Upgrade installed packages: apt-get upgrade Search for a package: apt-cache search PACKAGE # search for a package containing the specified word Install a package: apt-get install PACKAGE Install a package available locally (already downloaded) dpkg -i PACKAGE.deb Show information about a package and all its dependencies apt-cache show PACKAGE aptitude show PACKAGE # similar to apt-cache show PACKAGE but shows extra information apt-cache showpkg PACKAGE # show dependencies and reverse dependencies Check for broken dependencies: apt-get check Uninstall a package: apt-get remove PACKAGE aptitude purge PACKAGE # remove PACKAGE and its configuration files Debian Linux (sarge, kernel 2.6): workaround for E: This installation run will require temporarily removing the essential package e2fsprogs due to a Conflicts/Pre-Depends loop. This is often bad, but if you really want to do it, activate the APT::Force-LoopBreak option. apt-get remove e2fsprogs apt-get install e2fsprogs sysvinit initscripts upgrade glibc when requested. # note that this is a workaround, it should be fixed with apt-get dist-upgrade Note: when the Debian stable changes, if you don't upgrade your system accordingly, you'll have to modify the /etc/apt/sources.list file to specify you are using an old version of Debian (that is, you are no longer using the "current stable" version of Debian). For example, if you are using Sarge, and Debian Etch is released as stable, and you are not upgrading to Etch, you have to change all references to stable as sarge in your source.list file. Which means, you have to change "deb http://debian.example.com/debian stable main contrib non-free" as "deb http://debian.example.com/debian sarge main contrib non-free". If you don't make this change, when you'll try to install new packages, you'll receive the following message: The following packages will be REMOVED: base-config initrd-tools kernel-image-2.4.27-2-386 This is an example, the kernel image can be different in your case, however you'll not be able to install the package and it may damage your system. In fact, if you procede, you will be warned by this message: You are running a kernel (version 2.4.27-2-386) and attempting to remove the same version. This is a potentially disastrous action. Not only will /boot/vmlinuz-2.4.27-2-386 be removed, making it impossible to boot it, (you will have to take action to change your boot loader to boot a new kernel), it will also remove all modules under the directory /lib/modules/2.4.27-2-386. Just having a copy of the kernel image is not enough, you will have to replace the modules too. I repeat, this is very dangerous. If at all in doubt, answer no. If you know exactly what you are doing, and are prepared to hose your system, then answer Yes. If you get a message ending with the following request after a power outage: Give root password for maintenance (or type Control-D to continue): enter the root password then check the file system of the faulty device (read the whole message to identify the faulty driver, generally is /dev/hda0 on a single hard drive system or /dev/md0 on a RAID system): e2fsck -f FAULTY_DEVICE reply yes (y) to all questions. Note that if it asks to connect to lost+found more than once some of your files might be seriously damaged. However you can also use e2fsck -f -y FAULTY_DEVICE to reply automatically "yes" to all questions. then reboot Set keyboard layout # Enter this command to localize the keyboard for your country apt-get install console-common ----- Install a software package from the source: Packages in form of source code are generally stored into .tar.gz (.tgz) or .tar.bz2 (.tbz) files. You may need to download them from the Internet: wget http://www.example.com/source/package.tgz Once you have the package (from the Internet, a CD, or otherwise) in your work directory, you'll have to decompress it: tar -xjvf package.tbz # extract all files from a TAR+BZIP2 compressed archive or tar -xzvf package.tgz # extract all files from a TAR+GZIP compressed archive it should decompress inside a new directory. Move inside this new directory: cd PACKAGE_SOURCE_DIRECTORY_NAME and perform these commands: ./configure make su ROOT_PASSWORD # (if you're not logged as root) make install The executable file should be created inside /usr/local/bin , and you should be able to invoke it typing its name in the console and then hitting ENTER. To uninstall a package, you have to move inside the directory containing the source (if you kept it): cd PACKAGE_SOURCE_DIRECTORY_NAME and uninstall it: make uninstall If you haven't kept the source, you'll have to locate all files (try locate PACKAGE_NAME) and delete them manually. ----- Change root password without knowing the existing one You need physical access to the system. Add init=/bin/bash to boot parameters mount -o remount,rw # mount file system passwd # set new password ----- Writing text files for Linux using an editor running on another Operating System: When you edit a system file, a shell script, a fortune cookie text file, or another file meant to be processed by a Linux shell command or application, make sure that the lines are divided UNIX style (LF only, x0A; not CR/LF, x0D0A as in Windows), otherwise the line breaks will not be recognized as such and the whole file will be treated as a single line. ----- Install SSH: apt-get install ssh Get SSH version: ssh -V Start a SSH connection (port 22): ssh HOST Configure SSH: jed /etc/ssh/sshd_config # If your client can't authenticate to your server, it might be sending the password as a cleartext and your server is not accepting it. In this case you have to enable tunnelled clear text passwords setting PasswordAuthentication yes Start a Telnet (unencrypted) connection: telnet HOST # start a telnet connection on HOST at default port 23 telnet HOST:PORT # start a telnet connection on HOST at the specified PORT ----- Bash shells: Show BASH version: echo $BASH_VERSION Command Line Navigation: CURSOR LEFT / CURSOR RIGHT # move cursor one character left or right on command line LEFT ALT+[B/F] # move cursor to previous / next word on command line CURSOR UP / CURSOR DOWN # move to previous / next command in History CTRL+R # search for a command in History Switch bash shells LEFT ALT+[F1-F6] Cycle between bash shells LEFT ALT+[CURSOR LEFT / CURSOR RIGHT] Reset a "corrupted" shell: reset Clear shell terminal window: clear Open a new Bash shell: bash Exit from a non login shell: exit Change prompt: export PS1="[\t] \u@\h:\w\\\$ " # show time in 24 hours format between square brackets, the logged user name, the host name, the current path, and the root status ("#" if root, "$" otherwise) export PS1="\[\e[1;32m\][\t] \u@\h:\w\\\$ \[\e[0m\]" # same prompt, but colored in light green export PS1="\[\e[1;32m\][\$(date +%a\ %d%b%Y\ %H:%M:%S)] \u@\h:\w\\\$ \[\e[0m\]" # full date and time between square brackets, the logged user name, the host name, the current path, and the root status ("#" if root, "$" otherwise), colored in light green. Note that the shell command date is used, invoked with \$() . If $() (without the leading backslash) were used, the date would be generated only the first time and then stored into the variable and wouldn't be updated every time the prompt is shown. See current prompt: echo $PS1 Edit the script executed at the shell start-up (user login shell) # Custom Prompt and Aliases can be set here jed ~/.bash_profile Sample bash profile script: --- # /root/.bash_profile # BASH Shell Start-up # Prompt export PS1="\[\e[1;32m\][\$(date +%a\ %d%b%Y\ %H:%M:%S)] \u@\h:\w\\\$ \[\e[0m\]" # Aliases alias l="ls -laF" alias d="ls -aF" # Message echo "Welcome" --- Customize the MOTD (Message Of The Day), message shown after logging in, before than starting the shell: jed /etc/motd # edit the MOTD Customize the message shown before than the log in request: jed /etc/issue # edit the issue file ----- Description of a command: whatis COMMAND Manual of a command: man COMMAND Get information about related commands: apropos COMMAND Return the path of a command: which COMMAND type COMMAND # specifies if COMMAND is a shell builtin (internal) List internal commands: help help COMMAND # show information about an internal command Identify the type of a file: file PATH/FILE file -z PATH/FILE.zip # Attempts to check the file type of files contained inside a compressed file Create an alias: alias l='ls -laF' # makes "l" an alias for "ls -laF" List all existing aliases: alias Remove an alias: unalias ALIAS Start a comment (ignore following text): # Do nothing, successfully: true Do nothing, unsuccessfully: false Execute a random command between two: ((RANDOM % 2 == 0)) && echo "Yes" || echo "No" Process priority: nice -n0 COMMAND # execute a command with default priority (0) nice -n20 COMMAND # execute a command with the lowest priority (20) nice -n-20 COMMAND # execute a command with the highest priority (-20). Only root can assign negative (high) priority renice 10 PID # reassign priority to an existing process List running processes and their PIDs: ps # show all processes with a tty in current shell ps -a # show all processes with a tty in all shells ps -A # show all processes ps -A|grep "STRING" # show all processes that matches STRING Show running processes and CPU load: top # press "q" to quit Show jobs running in the background: jobs Show which user launched a process fuser PID terminate a process: kill -3 PID # quit a process kill -15 PID # term a process kill -9 PID # kill a process (most effective to quit an unresponsive process) terminate all process with a matching name: killall NAME killall -I NAME # ignore cases killall -i NAME # ask for confirmation before to kill /etc/init.d/COMMAND start # start a service, system daemon /etc/init.d/COMMAND stop # stop a service, system daemon /etc/init.d/COMMAND restart # restart a service, system daemon Show information about all loaded module: lsmod Show open files: apt-get install lsof lsof # Show all open files lsof PATH # Show all open files within PATH ----- Show system name: hostname Change system name as NEW_NAME: hostname NEW_NAME jed /etc/hostname # change it as NEW_NAME jed /etc/hosts # you'll probably need to add NEW_NAME as an alias hostname for 127.0.0.1 (localhost) Show system information: uname -a uname -r # show only kernel version cat /proc/sys/kernel/osrelease # kernel version Show the kernel ring buffer (to print out the bootup messages): dmesg Show last 20 lines of the system messages file tail --lines=20 /var/log/messages By default, on most Debian based distributions, every 20 minutes syslog marks the message file with "localhost -- MARK --" to log that the system is up and running. You can modify this behavior by editing the syslog startup file: jed /etc/init.d/sysklogd Locate the line that defines the variable SYSLOGD (generally, SYSLOGD="" ) and use the switch -m to set the delay between marks in minutes, or set it to 0 to disable the notification. Example: SYSLOGD="-m 60" marks the message file every hour, SYSLOGD="-m 0" disable the notification. Restart syslog to make the change effective: /etc/init.d/sysklogd restart Show the list of all commands entered in the shell: history # show all commands in history together with their offset history -d OFFSET # delete the specified history entry history -c # clear the whole history file List all PCI buses and devices: lspci Start Debian base system configuration tool: base-config Show CPU info: cat /proc/cpuinfo grep "model name" /proc/cpuinfo # return CPU model Show RAM info: cat /proc/meminfo grep MemTotal /proc/meminfo # return total RAM seen by the system Localization: dpkg-reconfigure locales # edit locale / add new locales locale # show locale settings locale -a # show available locales locale|cut -d= -f1|xargs locale -kc|less # list fields in locale database locale territory # show the locale territory # echo "This server is configured for `locale territory`" printf "%'d\n" 1234567 # print a number grouping thousands with the locale configuration Edit user creation preferences: jed /etc/adduser.conf Create a new user: adduser USER # add a new USER with a home directory in the default path ( /home/USER ) adduser --home DIR USER # add a new USER specifying a non standard path for the home directory adduser --no-create-home USER # add a new USER without a home directory Modify a user information: usermod [OPTIONS] USER Remove a user: deluser USER # remove the specified user deluser --remove-home USER # remove the specified user and his home directory and mail spool deluser --remove-all-files USER # remove the specified user and all files owned by the user (be careful) Change user contact information: chfn [OPTIONS] USER Change a user's default shell: chsh [OPTIONS] USER Change a user password: passwd USER Set a user password to expire: chage [OPTIONS] USER Remove a user: userdel USER Create a new user group: addgroup GROUP Modify a group information: groupmod [OPTIONS] GROUP Remove a group: groupdel GROUP Add a user to a group: adduser USER GROUP Execute a command with root privileges (root password will be asked): sudo COMMAND Change current user's password: passwd Show all existing users: # format: username:password (usually shadowed, a 'x' is shown instead):user id:group id:real name:home path:console cat /etc/passwd Show all logged users: users # list all logged users who # show information about all logged users w # show more information than "who" Show last logged users: last last USER # show last times when USER logged in last -n 10 # show last 10 logged users last -n 5 USER # show last 5 times when USER logged in Show current user's name: whoami Show information about a user: finger USER Show groups to which current user belongs: groups Show user and group IDs: id Send a message to a logged user: # Check the user device with a w or a who command first then redirect the output of an echo command to such device echo "Hello">/dev/pts/1 # Send "Hello" to the user logged with pts/1 Send a message to all logged users: wall PATH/FILE # show the content of FILE to all logged users (max 20 lines) wall # use standard input (normally the keyboard) to show a message to all logged users. Message must be terminated with an EOF (End Of File) character (usually CTRL+D) Execute a command as another user (impersonated user password will be requested): su USER COMMAND Start a console as another user (impersonated user password will be requested): su USER su # if no user is specified, then root is assumed by default ----- Search for a file: find PATH -name FILE_MASK # find all files with given mask inside PATH and all subdirectories. Example: find / -name *.php # find any PHP file in the server whereis FILENAME # return the paths of files with matching FILENAME locate NAME # return all paths and files with a matching NAME string Get information about a file: stat PATH/FILE Copy a file or a directory: cp -Rp SOURCE DESTINATION # copy a file or a directory tree preserving owners and properties Copy a file over a network: apt-get install netcat # On the Destination computer (listener). Make sure the PORT you are listening isn't blocked. Timeout (-w) is set to 3 seconds: nc -vvn -l -p PORT -w 3 > DEST_PATH/FILE # On the Source computer (sender). Make sure the PORT you are sending to isn't blocked. It will quit 2 seconds after receiving an EOF. If -q is not specified, then netcat must be killed manually: nc -vvn DESTINATION_IP PORT -q 2 < SOURCE_PATH/FILE Copy a file securely (through SSH) over a network (it will ask for passwords if needed for authentication on remote hosts): scp SOURCEPATH/NAME DESTUSER@DESTHOST:DESTPATH/NAME # copies a file from the local system to a remote host scp SOURCEUSER@SOURCEHOST:DSOURCEPATH/NAME DESTPATH/NAME # copies a file from a remote host to another remote host scp SOURCEUSER@SOURCEHOST:DSOURCEPATH/NAME DESTUSER@DESTHOST:DESTPATH/NAME # copies a file from a remote host to another remote host Move a file or a directory tree (or rename): mv SOURCEPATH/NAME DESTPATH/NAME for a in *; do mv -v "$a" "${a//STRING_SEARCH/STRING_REPLACE}"; done # Batch rename all files in the current directory for a in *; do mv -v "$a" "${a}_`date --iso-8601`"; done # Append date as YYYY-MM-DD to all file names in the current directory (note that it will be appended after any extension) Synchronize two directories: apt-get install rsync rsync -a -v /SOURCE_PATH/* /DEST_PATH/ # copy all files from /SOURCE_PATH that doesn't exist in /DEST_PATH rsync -a --delete -v /SOURCE_PATH/* /DEST_PATH/ # copy all files from /SOURCE_PATH that doesn't exist in /DEST_PATH, and delete all files in /DEST_PATH that doesn't (no longer) exist in /SOURCE_PATH rsync -a --delete -v rsync://www.example.com/dir/ /DEST_PATH # synchronize from an external rsync directory Create a link to a file: ln -s PATH/ACTUAL_FILE PATH/LINK_TO_CREATE Update the access date of a file: touch PATH/NAME Split a file in chunks: split -b=BYTES PATH/FILE PREFIX # splits FILE in chunks of BYTES bytes, using PREFIX to name generated chunks Rebuild a splitted file: cat PREFIX* > PATH/FILE Convert tabs to spaces in a text file: expand PATH/FILE # convert only tabs at the beginning of the line expand -a PATH/FILE # convert all tabs, not just the ones at the beginning of the lines Convert spaces to tabs in a text file: unexpand PATH/FILE # convert all tabs, not just the ones at the beginning of the lines unexpand -i PATH/FILE # convert only spaces at the beginning of the lines Replace all occurrences of a string inside a text file: sed "s/SOURCE_STRING/REPLACEMENT/g" PATH/SOURCE_FILE>PATH/DESTINATION_FILE Remove comments from a text file: sed "/ *#/d;" PATH/SOURCE_FILE>PATH/DESTINATION_FILE Remove blank lines from a text file: sed "/^ *$/d" PATH/SOURCE_FILE>PATH/DESTINATION_FILE Remove comments and blank lines from a text file: sed "/ *#/d; /^ *$/d" PATH/SOURCE_FILE>PATH/DESTINATION_FILE Remove trailing spaces and tabs from a text file: sed "s/[ \t]*$//" PATH/SOURCE_FILE>PATH/DESTINATION_FILE Add a text at the end of each line: sed "s/$/\;/g" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # append a semicolon (";") at the end of each line Add a text at the beginning of each line: sed "s/^/LINE : /g" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # add "LINE : " at the beginning of each line Case conversion: echo "Test"|tr '[:lower:]' '[:upper:]' # Return the text in Upper case echo "Test"|tr '[:upper:]' '[:lower:]' # Return the text in lower case cat PATH/SOURCE_FILE|tr '[:lower:]' '[:upper:]'>PATH/DESTINATION_FILE # Convert a text file to Upper case cat PATH/SOURCE_FILE|tr '[:upper:]' '[:lower:]'>PATH/DESTINATION_FILE # Convert a text file to lower case Extract items from a delimited text file: cut -d: -f1 PATH/SOURCE_FILE>PATH/DESTINATION_FILE # choose the first item of a series delimited by ":" Remove consecutive duplicate lines from a text file: uniq PATH/FILE uniq -u PATH/FILE # return only successive duplicated lines Wrap a text file at given coloumn: fold -w COLOUMN PATH/FILE Get the checksum of a file: sum PATH/FILE # return the checksum and block count for FILE cksum PATH/FILE # return the CRC checksum and byte count for FILE Compare and show differences between two binary files: cmp -b -l PATH/FILE1 PATH/FILE2 # compare two files and returns the position of the different bytes followed by the value and the corresponding ASCII character for each file cmp -b -l --bytes=10 PATH/FILE1 PATH/FILE2 # limit comparison to the first 10 bytes Compare and show differences between two text files: diff -abB PATH/FILE1 PATH/FILE2 # handy options: -i case insensitive Compare and show differences between three text files: diff3 -e MYFILE OLDFILE YOURFILE # output unmerged changes from OLDFILE to YOURFILE into MYFILE Type a file: cat PATH/FILE nl PATH/FILE # show line numbers for every line more PATH/FILE # type a file page by page less PATH/FILE # show a file page by page, and let scroll through pages (get it with apt-get install less) grep "TEXT" PATH/FILE # show only lines containing "TEXT" grep -v "TEXT" PATH/FILE # show only lines NOT containing "TEXT" grep -i "TEXT" PATH/FILE # show only lines containing "TEXT" (case insensitive) grep -vi "TEXT" PATH/FILE # show only lines NOT containing "TEXT" (case insensitive) grep -B 1 -A 1 "TEXT" PATH/FILE # show lines containing "TEXT" and show 1 line Before and 1 line After each match grep -B 2 -A 2 -n "TEXT" PATH/FILE # show lines containing "TEXT" and show 2 lines Before and 2 lines After each match, together with line numbers head PATH/FILE # show the head (first 10 lines) of FILE head --lines=20 PATH/FILE # show the first 20 lines of FILE head --bytes=1024 PATH/FILE # show the first 1024 bytes of FILE head --lines=-50 PATH/FILE # show all the FILE but the last 20 lines head --bytes=-2048 PATH/FILE # show all the FILE but the first 2048 bytes tail PATH/FILE # show the tail (last 10 lines) of FILE tail --lines=20 -F PATH/FILE # show the last 20 lines of FILE, and new lines as the file grows. If the files doesn't exist yet, it waits for it (useful for log files) tail --bytes=1024 PATH/FILE # show the last 1024 bytes of FILE wc PATH/FILE # line/word/byte count (handy options: --lines print only the newline counts, --words print only the word counts, --bytes print only the byte counts, --chars print the character counts) wc -L PATH/FILE # print the lenght of the longest line of FILE grep "STRING" PATH/FILE|wc --lines # count how many lines in FILE contains STRING sed -n "3p;3q" PATH/FILE # show line number 3 of FILE sed -n "2,4p;4q" test.txt # show lines 2 to 4 of FILE sed -n "6,12p;12q" test.txt # show lines 6 to 12 of FILE strings -a -n 4 PATH/FILE # extrapolates strings of text from binary files (a string of text is considered at least 4 bytes long, you can change this value with the option -n) (get it with apt-get install binutils ) strings -a -t x PATH/FILE # extrapolates strings of text from binary files and return the offset of the string (-t x specifies the offset as hex, -t d decimal, -t o octal) Return the average lenght in lines for all text files in the tree: find . -name "*.txt" -exec wc -l {} \; | awk 'BEGIN {x=0;y=0} {x+=1; y+=$1} END {print y/x}' Return a text string: echo STRING Colored text: # Note: Colors are not a POSIX (Portable Operating System Interface) feature in the Bash shell, so it might not be shown in some systems echo -e "\e[1;37m WHITE \e[0;37m LIGHT GRAY \e[1;30m GRAY \e[0;30m BLACK \e[0;31m RED \e[1;31m LIGHT RED \e[0;32m GREEN \e[1;32m LIGHT GREEN \e[0;33m BROWN \e[1;33m YELLOW \e[0;34m BLUE \e[1;34m LIGHT BLUE \e[0;35m PURPLE \e[1;35m PINK \e[0;36m CYAN \e[1;36m LIGHT CYAN \e[0m DEFAULT" Type a text string (y by default) until interrupted yes STRING Delete a file: rm PATH/NAME rm -i PATH/NAME # ask for confirmation before to delete Wipe a file (secure file deletion) # Important note: wiping overwrites a file one or more time before to delete it, to make it unrecoverable. This technique does not work with journaled file systems, like ReiserFS, Reiser4, or ext3 in journaled mode apt-get install wipe # if it doesn't work, try apt-get install secure-delete wipe PATH/FILE # wipe a file (asks for confirmation, write protected files will be spared) wipe -c -f PATH/FILE # wipe a file wipe -c -f -s PATH/FILE # wipe a file, quiet mode (no output returned except for fatal errors like "No such file or directory") wipe -c -f -k PATH/FILE # wipes the content of a file, but keeps the file in place (with a lenght of 0 bytes) wipe -c -f -e PATH/FILE # wipes the file up to its exact size, without wiping extra data in the last used block Create a directory: mkdir PATH Delete a directory (must be empty): rmdir PATH Delete a directory tree (recursive): rm -r PATH Wipe a directory tree (recursive secure file deletion): apt-get install wipe wipe -c -r PATH # wipe a directory tree (asks for confirmation) wipe -c -f -r PATH # wipe a directory tree wipe -c -f -r -s PATH # wipe a directory tree, quiet mode (no output returned except for fatal errors like "No such file or directory") Create a unique temporary file or directory with a pseudo random name: mktemp "tmp_XXXXXX" # Create a temporary file called for example tmp_92pIKB or tmp_Z1Pdz1 mktemp -d "tmp_XXXXXX" # Create a temporary directory called for example tmp_olD9rA or tmp_5Fw0JI Sample usage of mktemp (from mktemp man): TMPFILE=`mktemp /tmp/example.XXXXXX` || exit 1 echo "program output" >> $TMPFILE Show current directory: pwd Change directory: cd PATH Show the content of a directory: ls [PATH] ls -laF [PATH] # extended information ls -d */. # list only directories within current directory ls -d PATH/*/. # list only directories within given PATH Change access permissions: chmod MOD FILE # Change permissions for a file chmod -v MOD FILE # Change permissions for a file, with verbose output chmod -R MOD DIRECTORY # Change permissions for a directory tree (recursive) chmod -vR MOD DIRECTORY # Change permissions for a directory tree (recursive), with verbose output # Users: uuugggooo (ugo = u: owner user, g: all the users of the group, o: all other users) # Modes: 0: ---, 1: --x, 2: -w-, 3: -wx, 4: r--, 5: r-x, 6: rw- , 7: rwx Change owner: chown OWNER FILE # Change the owner of a file chown -R OWNER DIRECTORY # Change the owner of a directory tree (recursive) Change group: chgrp OWNER FILE # Change the group of a file chgrp -R OWNER DIRECTORY # Change the group of a directory tree (recursive) Raw copy: dd if=/dev/fd0 of=/dev/fd1 # copy a floppy disk (using two floppy disk drives) dd if=/dev/hda0 of=/dev/hda1 # copy a hard disk (or partition) into another one dd if=/dev/hda | gzip -9v | dd of=/mnt/hdb/hda.img # back up the whole hard disk as a gzipped file (you may need to mount a partition of another hard disk as hdb: mount /dev/hdb2 /mnt/hdb ) dd of=/mnt/hdb/hda.img | gzip | if=/dev/hda # restore the gzipped image of a hard disk dd if=/dev/fd0 of=/tmp/disk.bak # copy the whole floppy disk as a file dd if=/dev/hda of=/tmp/mbr.bin count=1 bs=512 # back up MBR (boot loader and partition table) Create and edit disk partitions: apt-get install parted parted # starts the partition utility parted commands: print # show current partitions mkpart primary START END # create a new primary partition mkpart extended START END # create a new extended partition mkpart logical START END # create a new logical partition (within the range of an extended one) resize PARTITON_N NEW_START NEW_END # resize an existing partition rm PARTITION_N # remove a partition quit # quit parted Show device partitions: fdisk -l /dev/DEVICE Make a file system (format) a disk (partition): mkfs.ext3 /dev/hda1 # Format hda1 as an ext3 partition mkfs -V -t ext3 /dev/hda1 # Format hda1 as an ext3 partition (Verbose output) mkfs -t msdos /dev/hda2 # Format hda1 as a MS-DOS partition mkfs -t msdos /dev/fd0 # Format a floppy disk using the MS-DOS file system mkswap /dev/hda2 # Format hda2 as the swap partition dd if=/dev/zero of=/dev/foo1 bs=512 count=1 # If you created or changed a DOS partition, you can use dd to zero the first 512 bytes Check and repair a file system: e2fsck -f -y FILE_SYSTEM # Note: Generally the device containing the file system to check can be /dev/hda0 on a single hard drive system or /dev/md0 on a RAID system); -y automatically reply yes (y) to all questions. Note that if it asks to connect to lost+found more than once, you may have some damaged files. Copy partitions from a device to another one: sfdisk -d /dev/sda | sfdisk /dev/sdb # copy partitions from sda to sdb Choose what partitions should be automatically mounted at system startup: jed /etc/fstab # edit static file system information Show free disk space: df df -h # show human readable values (in Megabytes, Gigabytes...) Check file system: fsck Summarize disk usage: # du shows size in KBytes by default (switch: -k), if you want to see the size in bytes, use the switch -b du PATH/FILE # disk usage for specified file du -s PATH # disk usage for the whole directory (recursively), total size du -s --exclude=*.mp3 PATH # disk usage for the whole directory (recursively), total size, excluding all files ending with .mp3 du PATH # disk usage for the whole directory (recursively) du -a PATH # disk usage for the whole directory (recursively), showing disk usage for all files and directories, not just directories du -S PATH # disk usage for the whole directory (recursively, but size is separated for each directory) Show directories and files sorted by size: du -ak | sort -rn | more Count files within directories: ls -1 /var/www/ancorviaggi.com/www/ | wc -l # count files within a directory ls -1R /var/www/ancorviaggi.com/www/ | wc -l # count files within a directory including subdirectories (recursively) Show memory usage: free free -m # show values in Megabytes Show mounted devices: mount Mount a device: mount -t DEVICE # example: mount -t /dev/cdrom Mount an ISO image of a CD: mount -o loop cd.iso /mnt/DIR_NAME/ Unmount a device: umount DEVICE # example: umount /dev/cdrom Unmount and eject a device (like a CD-ROM): eject # eject default device eject -d # show default device eject -r # eject CD-ROM Force changed blocks cached in RAM to disk: sync ---- Mount a remote directory tree: apt-get install sshfs fuse-utils To let other users to handle the remote directory tree: # create a group called fuse containing users allowed to mount the remote directory tree chgrp fuse /usr/bin/fusermount chmod u+s /usr/bin/fusermount adduser USER fuse Create the local directory where the remote directory tree will be mounted: mkdir /mnt/sshdir chown USER /mnt/sshdir Mount a remote directory tree: sshfs USER@HOST:/PATH /mnt/sshdir ----- Print a file on a printer: lpr -Pprinter PATH/FILE lpr -Pprinter -#N PATH/FILE # print N copies of FILE on printer List print queue: lpq Remove a job from the print queue: lprm JOB_ID lprm # remove all jobs from the print queue ----- Install VIM editor: atp-get install vim Install Jed editor: apt-get install jed ----- Indentation: Indent C code (or similarly formatted code): apt-get install indent indent PATH/SOURCE -o PATH/DEST indent -nprs PATH/SOURCE -o PATH/DEST # Doesn't add a space after an open bracket and before a closed one sed -i 's/^[[:space:]]*//g' PATH/FILE # Remove indentation from any text file Indent HTML code: apt-get install hindent hindent PATH/SOURCE.HTML>PATH/DEST.HTML hindent -i 0 PATH/SOURCE.HTML>PATH/DEST.HTML # Remove indentation hindent -c PATH/SOURCE.HTML>PATH/DEST.HTML # Force all tags to lowercase (otherwise all tags are forced to UPPERCASE by default) hindent -c -i 2 PATH/SOURCE.HTML>PATH/DEST.HTML # Set indentation level to 2 and force all tags to lowercase hindent -c PATH/SOURCE.HTML>PATH/DEST.HTML # Force all tags to lowercase hindent -f PATH/SOURCE.HTML>PATH/DEST.HTML # Flow: dump only HTML tags discarding any data between hindent -l PATH/SOURCE.HTML>PATH/DEST.HTML # List all HTML tags contained in the source document ----- Scheduled jobs with CronTab: # You can change the editor for Crontab (parameters in the following commands are case sensitive): export EDITOR=jed export VISUAL=jed # Edit CronTab file (using the specified editor, or the default editor, tipically "vi", otherwise) crontab -e Format: # Line of comments start with the dash character (#) minute (0-59) hour (0-23) day of month (1-31) month (1-12) day of week (0-6, Sunday=0) COMMAND # Use asterisks to specify any occurrence of the time parameter * * * * * echo "test" # execute the command every minute (not recommended) 30 * * * 1 echo "test" # execute the command every 30 minutes, every monday # separate multiple istances of the same time parameter with a comma (,) 15,30,45 * * * 1,3,5 echo "test" # execute the command every 15 minutes, every monday, wednesday, friday # separate two istances with a dash (-) to create a time range 0 0,12 * 6-9 1-5 echo "test" # execute the command at midnight amd at twelve, every day from monday to friday, during every month from June to September # Percent character (%) must be escaped (\) 30 * * * 1 touch /PATH/file_$(date +\%Y\%m\%d).txt # Normally, crontab send a mail to the user with the output of the command executed, including errors. Append ">/dev/null 2>&1" to the command line to prevent this behaviour * * * * * echo "test">/dev/null 2>&1 # list all scheduled jobs crontab -l # erase the crontab file (not recommended because remove all jobs, including ones that might be necessary for the system, you'd better edit the crontab files and delete entries manually) crontab -r ----- Networking: ifconfig # show local network interfaces ifconfig INTERFACE IP_ADDRESS # change the IP address for a network interface ifconfig INTERFACE down # disable a network interface ifconfig INTERFACE up # enable a network interface iwconfig # show local wireless network interfaces iwconfig INTERFACE IP_ADDRESS # change the IP address for a wireless network interface iwconfig INTERFACE down # disable a wireless network interface iwconfig INTERFACE up # enable a wireless network interface arp -a # show DNS, IP, and MAC address for all network interfaces jed /etc/hosts # assign hosts to specific IPs jed /etc/network/options jed /etc/network/interfaces /etc/init.d/networking restart Show active Internet connections: netstat -A inet -lnp # only servers netstat -A inet # without servers Show network sockets in use: lsof -i tcp:80 # Show all processes that are using port 80 TCP Ping host: ping HOST # ping a host until CTRL-C is pressed ping -c N HOST # ping a host N times Trace route to host: traceroute HOST Network Mapping: apt-get install nmap nmap -sS 192.0.2.0 # search for open ports in the given IP nmap -sS 192.0.2.0/24 # search for connected IPs (and related open ports) in the given IP mask nmap -sS -O -v 192.0.2.0 # Return Operating System and uptime for the given IP Port scanning: apt-get install netcat # scan TCP ports: nc -vvn -z 192.0.2.2 1-80 # scan TCP ports from 1 upto 80 in the given IP address # scan UDP ports: nc -u -vvn -z 192.0.2.8 1-255 # scan UDP ports from 1 upto 255 in the given IP address Calculate broadcast, network, Cisco wildcard mask, and host range apt-get install ipcalc ipcalc 192.0.2.0/24 ipcalc 192.0.2.0/255.255.255.0 ipcalc 192.0.2.0-192.0.2.100 # deaggregate address range DNS lookup: apt-get install host host www.example.com host 192.0.2.0 # Reverse DNS lookup. Note that nslookup is now deprecated Information about a domain name: whois example.com Download files from the Internet: # note: default ports: HTTP: 80, FTP: 21 wget "http://www.example.com/file.gz" # download the document at the given URL to the local directory wget "http://www.example.com/file.gz" -O PATH/downloaded_file.gz # download the document at the given URL to the specified directory and with the specified file name wget -S "http://www.example.com/file.gz" # download the document at the given URL in the local directory, and save the HTTP header at the beginning of the file wget -c --limit-rate=20k "http://www.example.com/file.gz" # continue an interrupted download, and limit speed to 20KBps wget -t 50 "http://www.example.com/file.gz" # retries up to 50 times if the download fails or it's incomplete wget -t 0 "http://www.example.com/file.gz" # retries forever if the download fails or it's incomplete, until it can get the requested file echo 'wget "http://www.example.com/file.gz"'|at 02:00 # start a download at 2:00am wget -q -O- "http://www.example.com/path/page.html"|grep 'a href' # show all links within given document wget -r -l1 "http://www.example.com/path/page.html" # download the first layer of links from given document wget -r -l1 --no-parent "http://www.example.com/path/page.html" # download the first layer of links from given document, ignoring links that are not within the given URL path wget -r -l0 "http://www.example.com/path/page.html" # download a whole website to the local directory wget --mirror "http://www.example.com/" # download a whole website to the local directory wget -r -l1 --no-parent -A "*.gif" "http://www.example.com/dir/" # download all files matching a certain pattern (all GIF files in this case) from the specified URL wget -U USER_AGENT_STRING "http://www.example.com/file.gz" # you may set the User Agent to pretend to get the file from the server with a normal browser Dump the content of a webpage on the screen: lynx -dump http://www.example.com/ Run a script on the web server wget -q -O- "http://localhost/script.php" # run a script on the local server lynx -dump http://localhost/script.php >/dev/null # run a script on the local server # If you receive this error message from the PPP Daemon (PPPD) # pppd: The remote system is required to authenticate itself but I couldn't find any secret (password) which would let it use an IP address. # You can fix it adding the string "noauth" (or changing the existing string "auth" to "noauth") in /etc/ppp/options ----- Mirror a disk through the network: If you have two computers with identical hardware you can mirror a system into the other machine through the network This is especially useful with clusters / virtual servers Start both machines from a live CD (like Knoppix - http://www.knoppix.org/ ) Connect both machine with a cross cable through their eth1 interfaces and set them this way: root@source_pc# ifconfig eth1 192.168.0.1 root@destin_pc# ifconfig eth1 192.168.0.2 Then execute these commands on the machines to start transfers: root@source_pc# dd if=/dev/sda bs=128M | nc _w2 192.168.0.2 9000 root@destin_pc# nc _l _p 9000 | dd of=/dev/sda Depending from the size of the disk it may need many hours ----- Maths: Return the result of an expression: # operators must be escaped and separated by a space # operators: + - * / % < > = >= <= != expr \( 1 \+ 2 \) \* 3 # Result = 20 expr 5 \> 3 # Returns 1 (true) expr 3 \> 5 # Returns 0 (false) Return the prime factors of a number: factor NUMBER factor NUMBER1 NUMBER2 NUMBER3... # return factors for the given numbers Random numbers: echo $RANDOM # Return a random number between 0 and 32767 (16-bit integer) expr \( 50 \+ $RANDOM \% \( 200 \- 50 \) \) # Return a random number between 50 and 200 Base conversion: apt-get install bc echo 'ibase=10; obase=16; 155'|bc # base conversion (155 from decimal to hexadecimal) echo 'ibase=16; obase=2; F'|bc # base conversion (F from hexadecimal to binary) echo '2*(3+4)'|bc -l # perform math operations Measure conversion: apt-get install units units # interactive units "1 in" "cm" # convert 1 inch to centimeters ----- Time: Show current date and time: date Set date and time: date MMDDhhmmYYYY.ss Set date from NTP (SNTP) server: apt-get install ntpdate ntpdate SERVER # set the clock ntpdate -q SERVER # query only for the time, don't set the clock SNTP servers: time.ien.it # Torino, Italy ntp.univ-lyon1.fr # Lyon, France vega.cbk.poznan.pl # Borowiec, Poland bitsy.mit.edu # Cambridge, Massachusetts, USA bonehed.lcs.mit.edu # Cambridge, Massachusetts, USA clock.isc.org # Palo Alto, California, USA Start a NTP server: apt-get install ntp-refclock (or another ntp* package) jed /etc/ntp.conf /etc/init.d/ntp-server restart Measure the resource time used by a program/command time COMMAND User and system times: times Show a calendar: # cal # show dayweeks in a row, Sunday first (use -m to have Monday first) # ncal # show dayweeks in a coloumn, Monday first cal # show a calendar for the current month cal -y # show a calendar for the current year cal -y YEAR # show a calendar for the given year cal MONTH YEAR # show a calendar for the given MONTH and YEAR ncal -e YEAR # show date of Easter (for Western Churches) ncal -o YEAR # show date of Orthodox Easter (Greek and Russian Orthodox Churches) Pause: sleep S # pause for S seconds Memorable dates: The directory /usr/share/calendar/ contains some collections of remarkable dates. For example: cat /usr/share/calendar/calendar.history # Important historical dates cat /usr/share/calendar/calendar.music # Important dates in music history cat /usr/share/calendar/calendar.lotr # Lord of the Rings dates ----- Archiving and Compression: Install archivers: apt-get install bzip2 apt-get install zip unzip apt-get install zoo apt-get install lha apt-get install rar apt-get install unp Archiving with ISO: dd if=/dev/dvd of=dvd.iso # Create an ISO file from a DVD dd if=/dev/cdrom of=cd.iso # Create an ISO file from a CD-ROM dd if=/dev/scd0 of=cd.iso # Create an ISO file from a SCSI CD-ROM mkisofs -o /PATH_DEST/cd.iso /PATH_SOURCE/ # Create an ISO file from a directory tree Archiving with TAR: tar -cvf file.tar text.txt # archive text.txt as file.tar tar -cvf file.tar *.txt # archive all files ending with ".txt" in the current directory as file.tar tar -xvf file.tar # extract all files from file.tar in the current directory Compressed TAR archives (tarball): # Use tar to compress a directory tree into a single file, and gzip or bzip2 to compress each file into a directory tree tar -cjvf file.tbz text.txt # TAR+BZIP2 compression (canonical extension .tbz or .tar.bz2) tar -xjvf file.tbz # extract all files from a TAR+BZIP2 compressed archive tar -czvf file.tgz text.txt # TAR+GZIP compression (canonical extension .tgz or .tar.gz) tar -xzvf file.tgz # extract all files from a TAR+GZIP compressed archive Compression with GZIP: gzip -9v text.txt > file.gz # compress text.txt as file.gz using highest compression and verbose output gzip -r9v file.txt file2 file3 /home/docs # compress file.txt file2 file3 and every file in /home/docs (if such directory exists), deleting original uncompressed files gzip -l file.gz # show the content of the compressed file gzip -d file.gz # extract the content of file.gz in the current directory and delete the compressed file (file.gz) Compression with BZIP2: bzip2 text.txt # compress text.txt as text.txt.bz2 bunzip2 file.bz2 # extract the content of file.bz2 into the current directory Compression with ZIP: zip file.zip file.txt # add file.txt to the compressed file file.zip (if it doesn't exist, it will created) zip -r file.zip /home/docs # compress the whole content of the directory /home/docs into file.zip zip -r filename.zip file.txt file2 file3 /home/docs # compress file.txt file2 file3 and the content of /home/docs (if such directory exists) into file.zip zip -F brokenfile.zip # try to fix a broken zipped file (use -FF to try even harder) unzip file.zip # extract the content of file.zip into the current directory Compression with LHa (LHarc) / LZH: lha a file.lha text.txt # compress text.txt as file.lha (or add text.txt to an already existing file.lha compressed archive) lha e file.lha # extract the content of file.lha into the current directory (will prompt for existing files) Compression with ZOO: zoo a file.zoo text.txt # compress text.txt as file.zoo (or add text.txt to an already existing file.zoo compressed archive) zoo e file.zoo # extract the content of file.zoo into the current directory (will ignore existing files) Compression with RAR (RAR is shareware): rar a file.rar text.txt # compress text.txt as file.rar (or add text.txt to an already existing file.rar compressed archive) rar e file.rar # extract the content of file.rar into the current directory (will prompt for existing files) Unpack most common formats: unp FILE # Supported extensions and formats: tar[.gz,.bz2], gz, bz2, Z, ar/deb, rpm, shar, rar, arj, zip, LHa, cab, ace, tnef, uu (mail, news), mime, hqx, sea, zoo, pmd, cpio, afio, lzop ----- Encoding: Install encoders/decoders: apt-get install mpack apt-get install uudeview apt-get install yencode MIME/Base64: mpack -s SUBJECT -o PATH/DEST_FILE PATH/SOURCE_FILE # encode (B64encode) within a MIME message with header munpack PATH/FILE # decode (B64decode) to the original file name uudeview -i PATH/FILE # decode (B64decode) to the original file name Base64: uuenview -b PATH/SOURCE_FILE>PATH/DEST_FILE # encode (B64encode). will not be decoded without MIME header. UU (Unix-to-Unix): uuenview -u PATH/SOURCE_FILE>PATH/DEST_FILE # encode (uuencode) uudeview -i PATH/FILE # decode (uudecode) to the original file name XX: uuenview -x PATH/SOURCE_FILE>PATH/DEST_FILE # encode (xxencode) uudeview -i PATH/FILE # decode (xxdecode) to the original file name Yenc: uuenview -y PATH/SOURCE_FILE>PATH/DEST_FILE # encode (yencode) uudeview -i PATH/FILE # decode (ydecode) to the original file name # uudeview handy options: +o don't overwrite existing files, -c autoclear (delete) successfully decoded files, -d desperate mode (process incomplete files) yencode PATH/SOURCE_FILE # encode (yencode) SOURCE_FILE as SOURCE_FILE.ync ydecode PATH/FILE # decode (ydecode) to the original file name ydecode -f PATH/FILE # decode (ydecode) to the original file name (force overwriting already existing files, without prompting) ----- Obfuscation: Text scrambling (anagrams): apt-get install wordplay wordplay "text string" # list all the possible anagrams of the given text string Alphabet scrambling: apt-get install rotix rotix # accept a text from standard input and return its ROT-13 to the standard output rotix -f PATH/SOURCE_FILE -o PATH/DEST_FILE # read SOURCE_FILE and return its ROT-13 to DEST_FILE (note: a ROT-13 can be deobfuscated performed the same operation again) rotix -r 15 -L -f PATH/SOURCE_FILE -o PATH/DEST_FILE # read SOURCE_FILE and return its ROT-15 to the Left to DEST_FILE (note: this can be deobfuscated performing a ROT-15 to the Right) rotix -r 12 -R -f PATH/SOURCE_FILE -o PATH/DEST_FILE # read SOURCE_FILE and return its ROT-12 to the Right to DEST_FILE (note: this can be deobfuscated performing a ROT-12 to the Left) Steganography: apt-get install steghide steghide --embed -p PASSWORD -ef SOURCE_FILE_TO_HIDE -cf DEST_OBFUSCATION_FILE # hide a file into another (AES encryption and compression are applied by default) steghide --embed -p PASSWORD -ef SOURCE_FILE_TO_HIDE -cf OBFUSCATION_FILE -sf DEST_FILE # hide a file into another (AES encryption and compression are applied by default), but save the result into a new file steghide --info STEGOFILE -p PASSWORD # return information about the file obfuscated into STEGOFILE steghide --extract -p PASSWORD -sf STEGOFILE -xf DESTFILE # extract the file obfuscated into STEGOFILE as DESTFILE # steghide applies AES encryption and compression by default. It can hide information into JPEG, BMP, WAV, and AU files. apt-get install stegdetect stegdetect FILE.jpg # try to find steganographed information into a JPEG file ----- Encryption with GnuPG (GPG) and Pretty Good Privacy (PGP) wrapper: apt-get install pgpgpg pgp -h # Help pgp -k # Key Manager functions help pgp -kg # Generate key pgp -kxa USER_ID DEST_FILE.ASC # Save User's public key to file pgp -ka FILE.ASC # Add public key from FILE.ASC to keyring pgp -ea PATH/FILE RECIPIENT_ID # Encrypt file for Recipient pgp PATH/FILE # Decrypt file (need password) # Handy options: # (Usage example: PGP -e = binary object file, PGP -ea = ASCII object file) # -a ASCII armour # -w Wipe (overwrite&delete) original file (when crypting) # -m No output file (on screen display, "eyes only") (when decrypting) ----- Passwords: Password generation: apt-get install apg apg -n 1 -M NL -a 1 -m 8 -x 12 # Generate a random password from 8 to 12 characters long made with only lower case letters and numbers apg -n 1 -M NL -a 0 -m 10 -x 15 # Generate a random but human readable password from 10 to 15 characters long made with only lower case letters and numbers apt-get install pwgen pwgen -1 -A -n -B # Generate a password of 8 characters made with only lower case letters (-A) and at least one number (-n, default behavior) without ambiguous characters (-B) ----- Extract audio tracks from a CD: apt-get install cdda2wav cdda2wav -B # extract tracks as WAVs from the default CD player cdda2wav -D /dev/cdrom -B # extract tracks as WAVs from the specified CD player Create an Audio CD writing WAVs as audio tracks: apt-get install cdrecord cdrecord dev=0,0,0 speed=4 -eject -pad -audio -raw -swab *.wav # write all WAVs from the current directory Create an Audio CD writing MP3s/OGGs/flacs as audio tracks: apt-get install mp3burn mp3burn *.mp3 # create an audio CD from all MP3s in the current directory mp3burn -p "PLAYLIST.m3u" # create an audio CD fetching files listed in a M3U playlist Play MP3 files: apt-get install mpg321 mpg321 FILE.mp3 Edit ID3v1 tags: apt-get install id3ed id3ed -i FILE.mp3 # return id3v1 tag id3ed -r FILE.mp3 # remove id3v1 tag id3ed -s SONGNAME -n ARTIST -a ALBUM -y YEAR -c COMMENT -k TRACKNUM -g GENRE_NUMBER FILE.mp3 # set id3v1 for FILE.mp3 id3ed -s SONGNAME -n ARTIST -a ALBUM -y YEAR -c COMMENT -k TRACKNUM -g GENRE_NUMBER *.mp3 # set id3v1 for all mp3 files in the current directory Edit ID3v1 and ID3v2 tags: apt-get install id3v2 id3v2 -l FILE.mp3 # return id3 tags id3v2 -1 -l FILE.mp3 # return id3v1 tag id3v2 -2 -l FILE.mp3 # return id3v2 tag id3v2 --delete-v1 FILE.mp3 # remove id3v1 tag id3v2 --delete-v2 FILE.mp3 # remove id3v2 tag id3v2 --delete-all FILE.mp3 # remove all tags (id3 v1 and v2) id3v2 -C FILE.mp3 # convert id3v1 to id3v2 tag id3v2 -a "ARTIST" -A "ALBUM" -t "SONG" -c "COMMENT" -g GENRE_NUMBER -y YEAR -T TRACK FILE.mp3 # set id3 for FILE.mp3 id3v2 -a "ARTIST" -A "ALBUM" -t "SONG" -c "COMMENT" -g GENRE_NUMBER -y YEAR -T TRACK FILE.mp3 # set id3 for all mp3 files in the current directory id3v2 -1 -a "ARTIST" -A "ALBUM" -t "SONG" -c "COMMENT" -g GENRE_NUMBER -y YEAR -T TRACK FILE.mp3 # set id3v1 for FILE.mp3 id3v2 -2 -a "ARTIST" -A "ALBUM" -t "SONG" -c "COMMENT" -g GENRE_NUMBER -y YEAR -T TRACK FILE.mp3 # set id3v2 for FILE.mp3 Check MP3 files: apt-get install mp3check mp3check -a FILE.mp3 # check crc and headers of mp3 files for consistency mp3check -a FILE.mp3 # check mp3 file for differences from layer 3, 44.1kHz, 128kB, joint stereo, no emphasis, has crc Lossless MP3 normalization (peak and loudness normalization): apt-get install mp3gain mp3gain FILE1.mp3 FILE2.mp3 FILE3.mp3 # normalize specified mp3 files (warning: overwrites source files) mp3gain -r FILE1.mp3 FILE2.mp3 FILE3.mp3 # normalize specified mp3 files all to equal loudness (warning: overwrites source files) mp3gain -a FILE1.mp3 FILE2.mp3 FILE3.mp3 # normalize specified mp3 files to the average loudness, intended for all audio files from a same album (warning: overwrites source files) mp3gain -u FILE.mp3 # undo changes made by mp3gain (based on info stored on tag) Convert MP3 to WAV: apt-get install mpg321 mpg321 -w FILE.wav FILE.mp3 # convert FILE.mp3 to FILE.wav Convert MP3 to OGG: apt-get install mp32ogg mp32ogg FILE.mp3 # convert FILE.mp3 to FILE.ogg mp32ogg --rename=DEST_FILE.ogg FILE.mp3 # convert FILE.mp3 to DEST_FILE.ogg mp32ogg --rename=%a_%l_%t.ogg *.mp3 # convert all mp3 files in the current directory renaming generated OGG files as ARTIST_ALBUM_TRACKTITLE.ogg (make sure that id3 tag contains all such information, otherwise files will be called as SOURCEFILE.ogg) Convert an audio format into another audio format apt-get install sox sox SOURCE_FILE.SOURCE_EXT DEST_FILE.DEST_EXT # convert SOURCE_FILE.SOURCE_EXT to DEST_FILE.DEST_EXT. File format conversion is based on standard extensions sox FILE.ogg FILE.mp3 # convert FILE.ogg into FILE.mp3 (note that MP3 support is optional. check sox --help for supported formats) ----- Manipulate video files apt-get install ffmpeg Convert a video format into another video format ffmpeg -i PATH/SOURCE_FILE.avi PATH/DEST_FILE.mpg # convert from AVI to MPEG ffmpeg -i PATH/SOURCE_FILE.avi -sameq FILE.avi PATH/DEST_FILE.mpg # convert from AVI to MPEG, keeping same quality of the source file ffmpeg -i PATH/SOURCE_FILE.avi -ss 00:00:00 -t 00:45:00 PATH/DEST_FILE.mpg # convert from AVI to MPEG, taking only the first 45 minutes of the source video (from 00:00:00 to 00:45:00) ffmpeg -i PATH/SOURCE_VIDEO.avi -i PATH/SOURCE_AUDIO.mp3 PATH/DEST_FILE.avi # create a video with audio taking video and audio from two different sources ffmpeg -i PATH/SOURCE_VIDEO.mpg -f mp3 AUDIO_TRACK.mp3 # extract the audio track as MP3 from a video ----- Play a video: apt-get install vlc vlc MOVIE.AVI # play a video (even in the text console) vlc http://www.example.com:8081/file.mpg # play a video from the web Streaming video server: vlc -vvv file.mpg --sout '#standard{access=http,mux=mpeg1,url=www.example.com:8081/file.mpg}' --ttl 12 --loop # Stream a MPEG file (loop) from the url http://www.example.com:8081/file.mpg # Note that you must have a streaming format like ASF to create a real stream of data vlc -vvv file.asf --sout '#standard{access=mmsh,mux=asf,url=www.example.com:8082}' --ttl 12 --loop # Stream an ASF file (loop) from the url mms://www.example.com:8082/file.asf ----- Convert images apt-get install imagemagick convert SOURCE.jpg DEST.png # Convert a JPEG image into PNG convert -resize 50% SOURCE.jpg DEST.jpg # resize SOURCE.jpg at 50% of the original size, and save it as DEST.jpg convert -resize 600x300 SOURCE.jpg DEST.jpg # resize SOURCE.jpg to 600x300 (width x height) pixels, and save it as DEST.jpg convert -resize 600 SOURCE.jpg DEST.jpg # resize SOURCE.jpg to a width of 600 pixels and relative height, and save it as DEST.jpg convert -resize x300 SOURCE.jpg DEST.jpg # resize SOURCE.jpg to a height of 300 pixels and relative width, and save it as DEST.jpg convert -thumbnail 150 SOURCE.jpg DEST.jpg # create a thumbnail of SOURCE.jpg with a width of 150 pixels and relative height, and save it as DEST.jpg # the difference between -resize and -thumbnail is that -thumbnail strips all meta data (like EXIF information) from the original image ----- EXIF information: apt-get install exif exif FILE.jpg # show EXIF tags inside a JPEG image exif -im FILE.jpg # return EXIF tags inside a JPEG image in a machine readable format (-i ID instead of tags, -m tabs delimited output) exif --ifd=0 -t TAG --set-value=VALUE SOURCE.jpg -o=DEST.jpg # set TAG as VALUE in SOURCE.jpg and save the resulting manipulated image as DEST.jpg exif --remove --ifd=0 SOURCE.jpg -o=DEST.jpg # remove EXIF information from SOURCE.jpg and save the resulting cleaned image as DEST.jpg exif -r SOURCE.jpg -o=DEST.jpg # remove the thumbnail from SOURCE.jpg and save the resulting cleaned image as DEST.jpg exif -e SOURCE.jpg -o=THUMBNAIL.jpg # extract the thumbnail from SOURCE.jpg and save it as THUMBNAIL.jpg exif FILE.jpg -n=THUMBNAIL.jpg # add THUMBNAIL.jpg to FILE.jpg and save the resulting manipulated image as FILE.jpg.modified.jpeg ----- Disk quotas: apt-get install quota quotatool quota # display disk usage and limits quotacheck # scan a file system for disk usage quotactl # set disk quotas ----- Quitting a session, turning off or rebooting system: CTRL+C # quit an application (break) exit # exit from a non login shell logout # logout from current session shutdown -t1 -h now # halt the system shutdown -t1 -r now # reboot the system reboot # reboot the system (like shutdown -r), might not work on older systems reboot -f # force reboot without using shutdown Show system reboot history: for wtmp in `ls -t /var/log/wtmp*`; do last reboot -f $wtmp; done|grep reboot # Edit system startup script # Commands that will be executed when the system boots. Different runlevels are allowed. jed /etc/inittab Choose what command must be performed when CTRl+ALT+DEL is pressed: jed /etc/inittab # What to do when CTRL-ALT-DEL is pressed. ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now Show uptime: uptime # a sysadmin will be judged by his uptime and by how many reboots he performed ----- Install LAMP: Install servers and related software: apt-get install apache2 apt-get install lynx apt-get install openssl apt-get install libapache2-mod-php4 php4 php4-gd php4-pear php4-mcrypt # PHP 4 apt-get install php4-mysql # MySQL support for PHP 4 apt-get install libapache2-mod-php5 php5 php5-gd php-pear php5-mcrypt # PHP 5 apt-get install php5-mysql # MySQL support for PHP 5 apt-get install mysql-server apt-get install phpmyadmin Apache 2 configuration: /usr/sbin/apache2 -v # return Apache 2 version jed /etc/apache2/apache2.conf jed /etc/apache2/ports.conf # Port listened by the webserver (normally 80) jed /etc/apache2/sites-available/default # set DocumentRoot and create Virtual Hosts (vhosts) Restart Apache after modifying its configuration: /etc/init.d/apache2 restart PHP configuration on Apache 2: jed /etc/php4/apache2/php.ini # PHP 4 configuration jed /etc/php5/apache2/php.ini # PHP 5 configuration To send e-mails from PHP scripts, you need to install sendmail apt-get install make apt-get install sendmail If you can't install sendmail, try to install sendmail-bin and rmail first: apt-get install sendmail-bin rmail sendmail You should check sendmail_path in PHP configuration (php.ini) and make sure it's either disabled or set to the default value of sendmail -t -i To send an e-mail from a PHP page you have to use the mail command, like in this example: mail($emailto, $emailsubject, $emailbody, "From: ".$emailfrom."\nReply-To: ".$emailreplyto."\nX-Mailer: ".$xmailer); Of course all sample variables used must be set properly. Show MySQL version: mysql -V Set MySQL root password: $ mysql mysql> SET PASSWORD FOR root@localhost = PASSWORD('newpassword'); Create a new mysql user and assign a database to him $ mysql -u root -pROOT_PASSWORD mysql mysql> insert into user (Host, User, Password, Select_priv) values ('localhost', 'USERNAME', password('USER_PASSWORD'), 'N'); Query OK, 1 row affected (0.00 sec) mysql> insert into db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Create_tmp_table_priv, Lock_tables_priv) values ('localhost', 'USER_DATABASE', 'USER_NAME', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'); Query OK, 1 row affected (0.00 sec) mysql> quit $ mysqladmin -u root -pROOT_PASSWORD reload The user can access the assigned database: $ mysql -u USERNAME -pUSER_PASSWORD USER_DATABASE Remove an user: mysql> drop user USERNAME Or mysql> delete from mysql.user where User='USERNAME' and Host='HOSTNAME'; mysql> revoke all privileges on *.* from USERNAME@HOSTNAME; mysql> revoke grant option on *.* from USERNAME@HOSTNAME; mysql> flush privileges; Show all MySQL users: mysql> select user, host from mysql.user; # show only user name and related host name for every user mysql> select * from mysql.user; # show detailed information for every user Show all databases: mysql> show databases; Show all tables in the current database: mysql> show tables; mysql> show table status; # provide information about every table Show all fields (coloumns) of a table: mysql> show fields from TABLE; # same as describe TABLE; mysql> show fields from TABLE from DB; # show fields of a table from another database mysql> show full fields from TABLE; # also lists privileges available to the current user Show all indexes (keys) of a table: mysql> show index from TABLE; mysql> show index from TABLE from DB; # show indexes of a table from another database Create an index (key) for a field (coloumn): mysql> alter table TABLE add index (`FIELD`) ; Count all records within a table: mysql> select count(*) from TABLE; Delete a database: mysql> drop database if exists DATABASE; Delete a table: mysql> drop table if exists TABLE; Delete a field: mysql> alter table TABLE drop FIELD; Delete an index: mysql> drop index INDEX on TABLE; Empty a table without deleting it: mysql> truncate table TABLE; Change engine type: mysql> alter table TABLE type = MYISAM; # change the engine tipe of TABLE to MyISAM Analyzes indexes of a table: # for MyISAM and BDB tables only mysql> analyze table TABLE; Check a table for errors: mysql> check table TABLE; Attempt to repair a corrupted table mysql> repair table TABLE; Optimize a table (defragment and rebuild indexes): mysql> optimize table TABLE; Show MySQL server status: mysql> show status; Check running MySQL processes: $ mysqladmin -u root -pROOT_PASSWORD processlist Or, from the MySQL console: mysql> show processlist; Kill a MySQL process: $ mysqladmin -u root -pROOT_PASSWORD kill ID Or, from the MySQL console: mysql> kill ID; Quit MySQL console: mysql> quit Show MySQL version: mysql -V To restart MySQL: /etc/init.d/mysql restart Run a MySQL file and store output into another file: mysql -u USER --password=PASSWORD DATABASE_NAME < SOURCE.sql > DESTINATION.txt Back up a MySQL database into a GZIP file: mysqldump --add-drop-table -u USER --password=PASSWORD DATABASE_NAME | gzip -9v > DATABASE_NAME`date +%Y%m%d`.sql.gz Upgrading from PHP 4 to PHP 5: # Note: this will also upgrade from MySQL 4 to MySQL 5, if MySQL 4 is installed # Warning: newer versions of MySQL often use different formats for tables than earlier versions. In some cases you can attempt to make restore such tables compatible using repair table TABLE; and optimize table TABLE; You'd better dump all your tables before to upgrade, in case you'll need to restore them after. In fact you may need to DROP tables which are no longer working as expected and rebuild them. A common issue are tables containing floating point fields: queries containing comparisons based on those fields will no longer work as they used to. # Edit the APT sources list jed /etc/apt/sources.list # Add the following sources deb http://packages.dotdeb.org stable all deb-src http://packages.dotdeb.org stable all # stop the servers /etc/init.d/apache2 stop /etc/init.d/mysql stop # remove PHP 4 apt-get remove libapache2-mod-php4 php4 php4-gd php4-pear php4-mcrypt php4-mysql # You have to remove MySQL as well apt-get remove mysql-server # Install PHP 5 apt-get install libapache2-mod-php5 php5 php5-gd php-pear php5-mcrypt # PHP 5 apt-get install php5-mysql # MySQL support for PHP 5 # Reinstall MySQL apt-get install mysql-server # Reinstall phpmyadmin (if you need it) apt-get install phpmyadmin # Removing PHP 4 will remove phpmyadmin as well, you'll have to reinstall it if you need it # Make sure that there are no references to PHP 4 left in the Apache configuration jed /etc/apache2/apache2.conf # Change <IfModule mod_php4.c> as <IfModule mod_php5.c> # Add .php5 to AddType application/x-httpd-php # Comment out <IfDefine HAVE_PHP4> ... </IfDefine> and everything between # restart the servers /etc/init.d/mysql restart /etc/init.d/apache2 restart # Web site statistics apt-get install webalizer jed /etc/webalizer.conf webalizer /var/log/apache2/access.log.1 # create a webalizer report for access.log.1 webalizer /var/log/apache2/example-com_access.log # create a webalizer report for example-com_access.log To have statistics available on the web for many websites, create a different access log, webalizer configuration file and output directory, for every website and a configuration file, and call webalizer from a script. Example: # 1. Set Apache log files jed /etc/apache2/sites-available/default <VirtualHost www.example.com> [...] CustomLog /var/log/apache2/example-com_www_access.log combined [...] </VirtualHost> <VirtualHost my.example.net> [...] CustomLog /var/log/apache2/example-net_my_access.log combined [...] </VirtualHost> /etc/init.d/apache2 restart # 2. Create output directories mkdir /var/www/webalizer mkdir /var/www/webalizer/www.example.com mkdir /var/www/webalizer/my.example.net # 3. Create custom Webalizer configuration files jed /etc/webalizer/webalizer_www.example.com.conf LogFile /var/log/apache2/example-com_www_access.log OutputDir /var/www/webalizer/www.example.com Incremental yes HostName www.example.com [...] jed /etc/webalizer/webalizer_my.example.net.conf LogFile /var/log/apache2/example-net_my_access.log OutputDir /var/www/webalizer/my.example.net Incremental yes HostName my.example.net [...] # 4. Create a script that invokes webalizer for every website mkdir /etc/scripts # You can have a verbose version to be invoked from a shell window, and a quiet version to be launched from CronTab: jed /etc/scripts/webalizev.sh #!/bin/bash # Webalize (verbose) echo -e "\Webalizer for www.example.com" webalizer -c /etc/webalizer/webalizer_www.example.com.conf echo -e "\nWebalizer for my.example.net" webalizer -c /etc/webalizer/webalizer_my.example.net.conf jed /etc/scripts/webalize.sh #!/bin/bash # Webalize (quiet) webalizer -c /etc/webalizer/webalizer_www.example.com.conf>/dev/null webalizer -c /etc/webalizer/webalizer_my.example.net.conf>/dev/null # If you want to reset information cached from previous run of Webalizer (especially if you've set Incremental yes in the webalizer configuration file), delete the files webalizer.current and webalizer.hist in the Webalizer OutputDir. # Example: # rm /var/www/webalizer/webalizer.current # rm /var/www/webalizer/webalizer.hist ----- FTP server: PROFTP: apt-get install proftpd (run as standalone) Create an user called "www" to access the www directory: adduser --home /var/www www Allow FTP access to the user "www": jed /etc/proftpd.conf Add the following lines in the user section of the proftpd configuration file: <Anonymous ~www> User www Group www DirFakeUser on ftp DirFakeGroup on ftp RequireValidShell off MaxClients 2 DisplayLogin welcome.msg DisplayFirstChdir .message <Directory *> <Limit READ WRITE STOR> AllowAll </Limit> </Directory> </Anonymous> /etc/init.d/proftpd restart VSFTP: # VSFTP allows virtual users, besides real shell users and anonymous users (you can simulate a chroot'ed FTP) apt-get install vsftpd apt-get install libpam-pwdfile Create a password file for virtual users: # Note: htpasswd only allows passwords up to 8 characters long htpasswd -bc /etc/vsftpd_passwd USERNAME PASSWORD Additional users can be created without the -c parameter: htpasswd -b /etc/vsftpd_passwd USERNAME PASSWORD If you want to enter the password manually, omit the -b parameter: htpasswd /etc/vsftpd_passwd USERNAME Use this command to remove a virtual user: htpasswd -D /etc/vsftpd_passwd USERNAME You'll need to create a directory for each virtual user (unless you configure vsftp otherwise): First, create the main directory for all virtual users: mkdir /var/www/ftp Then create a directory for each virtual user with proper read/write permissions: mkdir /var/www/ftp/USERNAME chmod 755 /var/www/ftp/USERNAME chown ftp /var/www/ftp/USERNAME Configure PAM (Pluggable Authentication Modules aka X/Open Single Sign-on) for vsftp: jed /etc/pam.d/vsftpd # Log in using htpasswd password file auth required pam_pwdfile.so pwdfile /etc/vsftpd_passwd account required pam_permit.so jed /etc/vsftpd.conf # Config file /etc/vsftpd.conf listen=YES #listen_ipv6=YES anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 anon_upload_enable=NO anon_mkdir_write_enable=NO dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES #chown_uploads=YES #chown_username=ftp xferlog_file=/var/log/vsftpd.log #xferlog_std_format=YES idle_session_timeout=600 data_connection_timeout=120 #nopriv_user=ftp #async_abor_enable=YES #ascii_upload_enable=YES #ascii_download_enable=YES ftpd_banner=Welcome #deny_email_enable=YES #banned_email_file=/etc/vsftpd.banned_emails chroot_local_user=YES #chroot_list_file=/etc/vsftpd.chroot_list #ls_recurse_enable=YES secure_chroot_dir=/var/run/vsftpd pam_service_name=vsftpd rsa_cert_file=/etc/ssl/certs/vsftpd.pem guest_enable=YES virtual_use_local_privs=YES #user_config_dir=/etc/vsftpd_user_conf #guest_username=ftp user_sub_token=$USER local_root=/var/www/ftp/$USER hide_ids=YES #cmds_allowed=PASV,BYE,LIST,ABOR,CWD,NLST,PORT,PWD,QUIT,RETR,SIZE,TYPE # Note that the option user_config_dir lets you create custom vsftp configuration files for each virtual users # You can create a config file with the name of the virtual user within the specified path jed /etc/vsftpd_user_conf/USERNAME # VSFTP extra config for user USERNAME local_root=/var/www/example.com/www/USERNAME Restart VSFTP to make it work with the new users or configuration: /etc/init.d/vsftpd restart ----- Samba: apt-get install samba smbfs smbclient samba-doc mkdir /public mkdir /public/shared chmod -v 0777 /public chmod -v 0777 /public/shared jed /etc/samba/smb.conf --- # samba configuration file [global] allow hosts = 192.168.0.0/255.255.255.0 workgroup = network server string = %h server (Samba %v) log file = /var/log/samba/log.%m max log size = 1024 ; security = user encrypt passwords = true passdb backend = tdbsam guest guest account = nobody invaluid users = root preserve case = yes short preserve case = yes ; include /home/samba/etc/smb.conf.%m socket options = TCP_NODELAY [homes] comment = Home directories browseable = no writable = no create mask = 0700 directory mask = 0700 [shared] comment = Public shared directory path = /public/shared public = yes browseable = yes ; read only = no writable = yes ; printable = no create mask = 0766 directory mask = 0766 guest ok = yes ; valid users = user_name --- testparm /etc/samba/smb.conf /etc/init.d/samba restart smbpasswd -a nobody New SMB password: Retype new SMB password: log file (default): /var/log/samba/log.%m smbtree # find Windows machines ----- Linux Shell scripts: First line headers: Bourne Shell (sh) #!/bin/sh Bourne Again Shell (sh) #!/bin/bash C-Shell (csh) #!/bin/csh Korn Shell (ksh) #!/bin/ksh Perl #!/usr/bin/perl PHP #!/usr/bin/php Python #!/usr/bin/python Turbo Shell (tcsh) #!/usr/bin/tcsh TCL/Tk #!/usr/bin/wish -f Set the executable flag: chmod +x FILE # declares the file as executable ----- Invoke Linux shell commands from PHP: PHP can perform calls to the Linux shell using the shell_exec() function: $output = shell_exec('ls -laF'); echo '<pre>'.$output.'</pre>'; or enclosing the Linux shell command between backticks: $output = `ls -laF`; echo '<pre>'.$output.'</pre>'; PHP also has a system() function that returns the value returned by the Linux shell command and the last line of output of such command: $last_line = system('ls -laF', $returned_value); ----- Fortune cookies: apt-get install fortunes /usr/games/fortune # return a fortune cookie (fetches a random sentence (% separated lines) from a random file in /usr/share/games/fortunes/ ) /usr/games/fortune PATH/FILE # return a fortune cookie fetching a random sentence (% separated lines) from the given FILE To create your own fortune files: - Create a text file separating each fortune cookie with lines containing only a percent (%) sign. - Create a database file for that text file, with the same file name followed by a .dat extension, and place it in the same directory: strfile PATH/FILENAME PATH/FILENAME.dat ----- Easter eggs: $ apt-get moo (__) (oo) /------\/ / | || * /\---/\ ~~ ~~ ...."Have you mooed today?"... $ apt-get -h [...] This APT has Super Cow Powers. $ aptitude -h [...] This aptitude does not have Super Cow Powers. $ aptitude moo There are no Easter Eggs in this program. $ aptitude -v moo There really are no Easter Eggs in this program. $ aptitude -vv moo Didn't I already tell you that there are no Easter Eggs in this program? $ aptitude -vvv moo Stop it! $ aptitude -vvvv moo Okay, okay, if I give you an Easter Egg, will you go away? $ aptitude -vvvvv moo All right, you win. /----\ -------/ \ / \ / | -----------------/ --------\ ---------------------------------------------- Happy? $ aptitude -vvvvvv moo What is it? It's an elephant being eaten by a snake, of course. # Note aptitude's easter egg is a reference to "The Little Prince" by Antoine de St. Exupery's. -----






Page issued on 15-May-2008 15:37 GMT
Copyright (c) 2008 Geody - Legal notices: copyright, privacy policy, disclaimer