Ubuntu articles

Ubuntu linux cheatsheet

Apache

htdocs folder
/var/www
apache configuration files
/etc/apache2/
vhosts definitions
/etc/apache2/sites-available
Create a link to each definition in /etc/apache2/sites-enabled: bash ln -s /etc/apache2/sites-enabled/mysite.lnk /etc/apache2/sites-available/mysite
Or with newer versions of Ubuntu: bash a2ensite mysite for enabling and bash a2dissite mysite for disabling
start/stop/restart apache
bash sudo /etc/init.d/apache2 start/restart/stop
Logs
/var/log/apache2

PHP

php ini
/etc/php5/apache2/php.ini
Sessions temp dir
/var/lib/php5
Pear and all of that stuff
/usr/share/php5

mysql

config file (my.cnf)
/etc/mysql/my.cnf
Delete tables with a certain pattern (drop tables like)
bash mysql --user=theuser --password=thepassword -N -e "show tables like 'whatever%'" db_name | perl -e 'while(<>){chomp; push @tables, $_;}print "drop table " . join ("," ,@tables) . "\n";' | mysql --user=theuser --password=thepassword db_name
Restore a dump
bash mysql -u username -p databasename < dump.sql
It will ask you for that username password

Users

Add user to group
bash sudo adduser <username> <groupname>

Files

Find files which have been modified today
bash find . -mtime -1 -print
Find all backup files in a directory
bash find . -name *~ -print
Find all backup files and delete them!
bash find . -name "*~" -exec rm {} \;
Change permissions for all folders only
bash find . -type d -exec chmod g+x {} \;

Set the group id bit (so files created later in the folder belong to the folder's group)
bash chmod g+s directory
Uncompress lots of zips with just one line of terminal commands
bash find *.zip -exec unzip {} \;
Find only files
bash find . -type f
Find only files ... and delete them!
bash find . -type f -delete
Recursively find files which contain a given text
bash grep -lir "a given text" *
Find in files but do not search in .git directories
bash grep -Ir --exclude-dir=".git" "pattern" *
Move files from nested folders to current directory ("un-nest" them)
bash find . -type f -exec mv {} . \;
Available space in disk
bash df -h (in fact this return available space in each mount in the system)
Show differences between two files without taking into account whitespace (very useful when line returns and spaces/tabs are messing up normal diffs)
bash diff -w file1 file2
Show differences between directories
bash diff -rq dir1 dir2
Get the md5 hash of a file
bash md5sum filename

Sharing folders

Right click over the folder to share, select 'Sharing options', click 'Share this folder' and 'Allow other people to write in this folder'. For setting the samba user and password, open a terminal and run sudo smbpasswd -a username , where username is the username you'll use when asked by Samba. The password you'll set is the one you want to use for accessing that folder remotely. It does not need to be your system password. This way when you do changes in the folder, the changes are done by username, not by nobody.

Backups

archive and compress a whole directory
bash tar cvfz archive.tar.gz dname
backup a database
bash mysqldump db_name --user=username --password=password > database_dump.sql
backup all databases
bash mysqldump -u username -p --all-databases >/tmp/databases.dump
All-in-one: get a remote database dump, compress it, download and uncompress in your local machine
bash ssh your_host "cd dumps_dir; mysqldump --user your_user --password=your_pass --host=db_host database_name | gzip > database_name.gz" scp your_login@your_host:dumps_dir/database_name.gz ./sql/ gunzip ./sql/database_name.gz
Compress a file with zip
bash zip outputfile.zip file1 file2 file3... fileN
Download a remote directory to current directory
bash scp -rv yourlogin@yourhost:~/web/public_html .
Archive a directory in several files of 1Gb each
bash tar vcf - /path/to/dir | split --bytes=1024m -a 3 -d - output_prefix
And to join them and unarchive at the same time:
bash cat /path/to/archived_files/output_prefix* | tar xvf

Mounting internal drives

Let's say I want to create a mount point for a secondary backup disk, so that it is always mounted without having to do it manually each time I want to use it.
Find out name of disk/partition
bash sudo fdisk -l
For example, /dev/sdb1 is the partition I want to mount. It can be different for you
Create mounting point
bash sudo mkdir /backup
Find UUID of the partition to mount
bash sudo vol_id /dev/sdb1
It returns both the filesystem type (ext3 in this case) and the UUID: ID_FS_USAGE=filesystem ID_FS_TYPE=ext3 ID_FS_VERSION=1.0 ID_FS_UUID=4ae128f5-b8a5-46ca-a27b-ddc03af18171
Edit /etc/fstab file to include the new partition in the list of mounts
Add a line like UUID={UUID} /{mount_point} {fs_type} defaults 0 0. In my case: UUID=4ae128f5-b8a5-46ca-a27b-ddc03af18171 /backup ext3 defaults 0 2 Note that the last '2' is for telling fsck that it should check this disk after it checked the first one (which is the root one and should have an '1' instead of '2'). If you enter a '0' this partition will never be checked when starting the system; that's probably not a good idea.
Did we do it right? Try to refresh the mounts with this:
bash sudo mount -a
If there are no errors, you should be able to access the new mount point with the File Browser. If you get something like mount: special device 4ae128f5-b8a5-46ca-a27b-ddc03af18171 does not exist you probably forgot to add UUID= before the actual UUID, like I did :D
Give proper permissions - normal users can't write in the new mount because it belongs to root.
In my case: bash sudo chown -R sole:sole /backup sudo chmod -R 755 /backup These can take a long time -- specially if there are lots of files in the disk and it is large :)
Note: mostly taken from this fab tutorial

NOT mounting floppy drives

To remove the Floppy drive entry in Nautilus and etc:
Edit /etc/fstab and comment (with a '#') the following line: /dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
In a terminal: bash sudo modprobe -r floppy to unload the floppy module

Shredding data in a hard drive

If you are going to dispose of a hard drive, it's a MUST! --unless you want your old data scrutinized by identity thieves and curious people in general.


sudo shred /dev/sdc -f -v -z --iterations=35

WARNING: THIS WILL DESTROY ALL DATA IN THE SELECTED DISK (/dev/sdc)

Use at your own discretion!!

You can find out which disk to shred using Disk Utility. It needs to be unmounted first--and for obvious reasons it won't allow you to unmount the system disk, so you're relatively safe if you perform this with a normal boot (not a LiveCD).

shred is included in the Live CD so you can choose the try Ubuntu option and wipe out all data in a computer before disposing of it, selling it, recycling it, etc...

Checking a drive for bad blocks / sectors


sudo badblocks /dev/sda -s

would run a read only test, also showing progress info while the test is performed

Updates

Remove unused packages
bash sudo apt-get autoremove
Manually update greyed out entries in the update manager
Go to Synaptic Package Manager, order by the status column (i.e. the first one), select all the packages with a star (*) over a green background, and select "Mark for upload".
Distribution update
bash sudo apt-get dist-upgrade
bash sudo gksu "update-manager -c"
Crisis!! X server doesn't work after updating the distribution - boot in safe mode and run
bash sudo apt-get install --reinstall xserver-xorg
bash sudo dpkg -reconfigure xserver-xorg

System

Turn off
bash sudo shutdown
Reboot
bash sudo reboot
List mounted devices and disks and other info
bash sudo fdisk -l
Static file system information
/etc/fstab
Fcsk - boot from live CD (it won't allow you to fsck a mounted drive)
open a console with ctrl+alt+f1
then bash sudo fsck /dev/sdb , etc
Another option: bash sudo e2fsck -p -f -v /dev/sda
Force fsck on boot
bash sudo touch /forcefsck and reboot!
Change screen resolution using command line
bash xrandr -s new_widthxnew_height
example: bash xrandr -s 1920x1200

Xorg

Restart xorg
press ctrl+alt+backspace (doesn't work on latest versions of Ubuntu)

Net stuff

Download a file with curl
bash curl -o outputfile source_url
Mirror a website with wget
bash wget -m http://example.com
Or bash wget -H -r --level=2 -k -p http://example.com to download files up to 2 levels recursively
Cheap downthemall with wget
Downloading all linked mp3 files from a page or directory:
bash wget -nd -r -l1 --no-parent -A.mp3 http://www.example.com/mp3/
Simulate different bandwidth speeds for testing your site (aka Bandwidth Throttling)
bash trickle -u 10 -d 20 firefox

Subversion

List info for a remote repository
svn info svn://repository_url (or http://repository_url, etc)
svn info also works with local resources: svn info . lists info for current directory
List files in a repository path
svn list svn://repository/path
Relocate a server location
svn switch --relocate svn://svnserver svn://svnserver/yellow_dog (taken from here)
Fire up svn server daemon
svnserve -d -r /home/svn/path_to_repositories_root

VirtualBox

Recompiling kernel module after upgrading the kernel:
sudo aptitude install linux-headers-$(uname -r)
sudo /etc/init.d/vboxdrv setup
Some people suggest using "sudo aptitude install virtualbox-ose-modules-generic" which is "a metapackage". I haven't tested it.

PulseAudio

Stop and restart
pkill pulseaudio; pulseaudio &

PGP & co

Clearsign a file with a non-default key
gpg --default-key [KEYID] --clearsign [FILENAME]