Category: Ubuntu

  • Ubuntu: How to update PHP’s timezone

     

    We came across this one recently when setting up a Zabbix server, where the installation would not proceed until the PHP timezone was set. This is quite straightforward – using your favourite editor edit:

     

    /etc/php5/apache2/php.ini

     

    Look for the Module Settings section or search for:

     

     date.timezone

     

    (in vi /date.timezone should get you there). Uncomment the line by removing the leading ; and then add in your desired timezone. For Sydney it would look like:

     

    [Date]

    ; Defines the default timezone used by the date functions

    ; http://php.net/date.timezone

    date.timezone = UTC+10

     

    Change UTC+10 to your desired timezone, save the file and restart Apache:

     

    sudo service apache2 restart

     

    Voila!

  • How to remove/delete old or unused kernels in Ubuntu

     

    If you have upgraded your kernel you will have found that Ubuntu keeps the older ones around, which can be handy if something breaks in the newer kernel and you have to boot from your old system. Over time, however, these can add up in terms of the amount of space consumed – at around 150MB per kernel you could easily find yourself with over a GB of old kernels if you’ve upgraded enough times. In this example the system we are using is Ubuntu 12.0.4.2. If you’re using Ubuntu Desktop, open up a terminal window – if you’re using Ubuntu Server, log in as usual and run:

     

    uname -r

    to see which kernel you are currently running. Make sure not to delete this one. Now, to see a list of the installed kernels, we run:

     

    dpkg –list | grep linux-image

    This command lists the installed packages and filters the list to include only those which have “linux-image” in them, which should only be your kernels. Your list should look something along the lines of:

     

    ii  linux-image-2.6.32-33-server 2.6.32-33.72     Linux kernel image for version 2.6.32 on x86_64
    ii  linux-image-3.2.0-35-generic 3.2.0-35.55      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
    ii  linux-image-3.2.0-38-generic 3.2.0-38.61      Linux kernel image for version 3.2.0 on 64 bit x86 SMP
    ii  linux-image-server           3.2.0.38.46      Linux kernel image on Server Equipment.

     

    To remove a kernel, run the following:

     

    sudo apt-get purge [kernel]

     

    Using the above list, say we want to get rid of 3.2.0-35. In that case the command would be:

     

    sudo apt-get purge linux-image-3.2.0-35-generic

     

    Once you have removed your unwanted kernels run:

     

    sudo update-grub2

     

    This will update the boot menu accordingly.

  • Monitoring network usage on Ubuntu

     

    If you want to see how much traffic is passing through your network port there’s a handy tool called vnstat which will tally the amount of data passing through. You can install it with:

     

    sudo apt-get install vnstat

     

    It will usually add the databases and network ports automatically like so:

     

    vnstat-0

     

    If it doesn’t and gives you an error you can create the database(s) with:

     

    sudo vnstat -u -i eth0

     

    If you have multiple network cards/ports you can add those in, too:

     

    vnstat -u -i eth1

    vnstat -u -i eth2

    …etc

     

    If it couldn’t create the databases you can start it with:

     

    sudo /etc/init.d/vnstat start

     

    If you need to change the maximum bandwidth from 100Mb you can edit the file:

     

    /etc/vnstat.conf

     

    Scroll down until you see the following:

     

    # maximum bandwidth (Mbit) for all interfaces, 0 = disable feature
    # (unless interface specific limit is given)
    MaxBandwidth 100

     

    and make MaxBandwidth the figure you require (e.g. 1000). If you make a change restart vnstat with:

     

    /etc/init.d/vnstat restart

     

    You can now see how much traffic has come through the NIC since vnstat started recording – at first it probably won’t be much (if any), but as it adds up you can check it with:

     

    vnstat

     

    The output should look like:

     

    vnstat-01

     

    You can watch how much traffic is flowing through in real-time by running:

     

    vnstat -i eth0 -l

     

    This will give you a screen showing you the current traffic:

     

    vnstat-02

     

    You can end this with CTRL+C, which shows you a summary screen:

     

    vnstat-03

     

    You can get an hourly summary with:

     

    vnstat -i eth0 -h

     

    vnstat-04 vnstat-05

     

    Daily summary with:

     

    vnstat -i eth0 -d

     

    vnstat-04

    Monthly summary with:

     

    vnstat -i eth0 -m

     

    vnstat-06

     

    This is a really handy way of keeping track of your network traffic – whether it’s out of curiosity, wanting to know how much stress your network is under or looking for a bottleneck this can be quite a valuable tool.

  • MythTV backend DVB adapter error: ERROR_OPEN

     

    If you see the above error when you’re trying to set up your DVB tuner card one cause is that MythTV can’t get access to the tuner card. This can be because there’s another application installed which has access to it, e.g. something like TVHeadend. On XBMCBuntu you can uninstall TVHeadend by running the following:

     

    apt-get remove tvheadend

    apt-get purge tvheadend

     

    …then retry the setup. If that was the problem it should pick the tuner up now without issue, showing the tuner name in place of ERROR_OPEN.

  • How to find number of cores in Ubuntu (or other versions of Linux)?

     

    If you’re using a virtual server (whether online or your own physical machine) it can be handy sometimes to check how many CPU cores are available; here are two easy methods of doing this. The first:

     

    nproc

     

    This will return a single number, whether it be 1, 2, 4 or otherwise. For a more detailed look, try:

     

    lscpu

     

    This will usually give a more complex readout, e.g.:

     

    root@server [/]# lscpu
    Architecture:          x86_64
    CPU op-mode(s):        32-bit, 64-bit
    Byte Order:            Little Endian
    CPU(s):                1
    On-line CPU(s) list:   0
    Thread(s) per core:    1
    Core(s) per socket:    1
    CPU socket(s):         1
    NUMA node(s):          1
    Vendor ID:             GenuineIntel
    CPU family:            6
    Model:                 45
    Stepping:              7
    CPU MHz:               2000.024
    BogoMIPS:              4000.04
    Hypervisor vendor:     Xen
    Virtualization type:   para
    L1d cache:             32K
    L1i cache:             32K
    L2 cache:              256K
    L3 cache:              15360K
    NUMA node0 CPU(s):     0

     

  • Samba log showing error: failed to retrieve printer list

     

    If you’re using Ubuntu and seeing the following error in your Samba log:

     

    Failed to retrieve printer list: NT_STATUS_UNSUCCESSFUL

     

    If you are a home user and using samba purely for file sharing from a server or NAS you’re probably not interested in sharing printers through it. If so, you can prevent this error from occurring by adding the following lines to your /etc/samba/smb.conf:

     

    printing = bsd

    printcap name = /dev/null

     

    Restart samba:

     

    /etc/init.d/smbd restart

     

    …check your logs:

     

    less /var/log/samba/log.smbd

     

    and the error should now no longer be appearing.

  • How to see individual CPU core loads in Ubuntu

     

    This is an interesting one – if you have the need to monitor your CPU usage individually across cores it’s actually quite easy with the top command. Simply run top and hit “1” – your output will go from:

     

    topSingleView

    to:

     

    topMultiCore

    In this case the server is a hexcore (0-5 cores shown, 6 in total) and we can clearly see the loads across each of them. To get colours – it can make top easier to read – hit Z.

     

    This can be quite handy for monitoring your CPU usage in more detail than basic load averages.

  • How to kill a process after a set period of time

     

    Knowing how to limit how long a process will run for is quite useful, particularly when you have daily backup scripts and the like which may at times run more than 24 hours; having multiple processes attempting to synchronize the same files can waste time, bandwidth and CPU power needlessly. The command we will use here is timeout. Ubuntu Server should have this pre-installed. It is used so:

     

    timeout [no. of seconds] [command]

     

    e.g.

     

    timeout 10 rsync /home/user/files/ /backups/user/files/

     

    would run the above rsync command but kill it after 10 seconds.

     

    This can be particularly useful with your daily scripts; simply set the timeout to be a few minutes less than 24 hours and you should hopefully avoid them running over each other. For reference there are 3600 seconds in an hour and 86400 seconds in 24 hours; setting a process to timeout after 86000 seconds would result in it running for 23 hours, 56 minutes and 20 seconds.

  • How to easily find the full path of a command in Ubuntu

     

    If you’re writing scripts or making cron jobs you will need to know the full path of the commands you’re using; rather than just being able to use “ls” you would have to use “/bin/ls” instead. You could use the find command here but there’s a quicker and more elegant way: which. Use it like so:

     

    which ls

     

    will return:

     

    /bin/ls

     

    Not everything will be in /bin, e.g.:

     

    which timeout

     

    will likely return:

     

    /usr/bin/timeout

     

    Simple but it will make your life quite a bit easier when writing scripts, particularly with new commands or command which you don’t use often.