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:

ln -s /etc/apache2/sites-enabled/mysite.lnk /etc/apache2/sites-available/mysite
Or with newer versions of Ubuntu:

a2ensite mysite

for enabling and

a2dissite mysite

for disabling

start/stop/restart apache
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)
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
mysql -u username -p databasename < dump.sql
It will ask you for that username password

Users

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

Files

Find files which have been modified today
find . -mtime -1 -print
Find all backup files in a directory
find . -name *~ -print
Find all backup files and delete them!
find . -name "*~" -exec rm {} \;
Change permissions for all folders only
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)
chmod g+s directory
Uncompress lots of zips with just one line of terminal commands
find *.zip -exec unzip {} \;
Find only files
find . -type f
Find only files … and delete them!
find . -type f -delete
Recursively find files which contain a given text
grep -lir "a given text" *
Find in files but do not search in .git directories
grep -Ir --exclude-dir=".git" "pattern" *
Move files from nested folders to current directory (“un-nest” them)
find . -type f -exec mv {} . \;
Available space in disk
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)
diff -w file1 file2
Get the md5 hash of a file
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
tar cvfz archive.tar.gz dname
backup a database
mysqldump db_name --user=username --password=password > database_dump.sql
backup all databases
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
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
zip outputfile.zip file1 file2 file3... fileN
Download a remote directory to current directory
scp -rv  yourlogin@yourhost:~/web/public_html .
Archive a directory in several files of 1Gb each
tar vcf - /path/to/dir | split --bytes=1024m -a 3 -d - output_prefix
And to join them and unarchive at the same time:
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
sudo fdisk -l
For example, /dev/sdb1 is the partition I want to mount. It can be different for you
Create mounting point
sudo mkdir /backup
Find UUID of the partition to mount
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:
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 😀
Give proper permissions – normal users can’t write in the new mount because it belongs to root.
In my case:

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:

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
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
sudo apt-get dist-upgrade
sudo gksu "update-manager -c"
Crisis!! X server doesn’t work after updating the distribution – boot in safe mode and run
sudo apt-get install --reinstall xserver-xorg
sudo dpkg -reconfigure xserver-xorg

System

Turn off
sudo shutdown
Reboot
sudo reboot
List mounted devices and disks and other info
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

sudo fsck /dev/sdb

, etc

Another option:

sudo e2fsck -p -f -v /dev/sda
Force fsck on boot
sudo touch /forcefsck

and reboot!

Change screen resolution using command line
xrandr -s new_widthxnew_height
example:

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
curl -o outputfile source_url
Mirror a website with wget
wget -m http://example.com
Or

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:
wget -nd -r -l1 --no-parent -A.mp3 http://www.example.com/mp3/
Simulate different bandwidth speeds for testing your site (aka Bandwidth Throttling)
trickle -u 10 -d 20 firefox
Thanks to mr.doob for this one!

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]

More tricks elsewhere

10 essential tricks for admins

10 Replies to “Ubuntu linux cheatsheet”

  1. Hi Sole!

    Love your cheatsheet, i’ve used it 1000 times, thanks for that!

    Anyway, it seems that in Ubuntu Hardy you need a different command to stop/start/restart Apache:

    sudo /usr/sbin/apache2 -k stop/start/restart

    If you try the commands you posted (that used to work, i know) you get something like this:

    “apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName”

    Cheers!

  2. funny, I just updated the sheet a few days ago – and it was actually that section that I updated 😉

    With your lines, I get the “apache2: bad user name ${APACHE_RUN_USER}” error.

    What works is:

    sudo /etc/init.d/apache2 start|stop|restart

  3. Sir,
    i thank you for explanations about the ‘find’ and ‘apt-get’ command

    have a good day.

  4. Not Sir but maybe Miss in any case 😉

    It’s great to see the sheet is helpful for someone else apart from me. This confirms I did well putting it here.

Comments are closed.