jump to navigation

Using “tee” to write to files and the terminal May 17, 2007

Posted by Carthik in administration, commands, snippets, ubuntu.
37 comments

The utility “tee” is very useful for plumbing on the command line. Curiously enough, it gets its name from the T-splitter used in plumbing, shown below:
T-splitter used in Plumbing

Say you want to run a command, and be able to see the output and errors on the screen, and be able to save them to a file. That’s where tee comes in, so you could do a:
$sudo apt-get upgrade 2>&1 | tee ~/apt-get.log
…to run apt-get upgrade and save the output and errors to the file apt-get.log in your home directory.

Purists please excuse the following explanation 🙂 The “2” refers to the “tap” from which the errors pour out (called stderr). The “1” refers to the tap from which the output pours out. The 2>&1 makes the errors to also pour out of the output tap. So then stderr goes to stdout. The pipe “|” redirects the output to tee. Now tee splits the output of the previous command two ways, and puts it both in ~/apt-get.log and in the standard output, which happens to be your screen/terminal.

tee is also handy when you have a small permissions problem. Say you want to write some text to a file “filename.txt” owned by the “root” user – you would just use something like:
$sudo vim filename.txt
and then change the file, right?

Now suppose you want to echo what you write, and write the file, all in one command, you then can use tee thusly:
$echo "localhost 127.0.0.1" | sudo tee filename.txt > /dev/null

This would write the text “localhost 127.0.0.1” to the file filename.txt which is not owned by you. The output of tee itself will go to /dev/null (nothingness) instead of the standard output, which is your terminal.

Don’t lose sleep over this, but someday it will come handy, and when you can figure out why the “sudo” does not apply after the “>” in your command, remember tee and come back here.

For all your command line redirecting needs, and to learn to wield pipes and tees like nunchakus read this excellent page.

Local DNS Cache for Faster Browsing August 2, 2006

Posted by Carthik in administration, guides, packages, ubuntu.
332 comments

A DNS server resolves domain names into IP addresses. So when you request “google.com” for example, the DNS server finds out the address for the domain, and sends your request the right way.

You can run a DNS cache on your computer. This will speed up the process of looking up domain names when browsing. The difference is about 30-60 ms for me. Multiply that difference by the number of websites you visit a day for an approximate estimate of the speed improvement. Of course, all this would be worth it if it weren’t for the fact that setting this up is way too easy.

The following instructions are for someone with a cable (broadband) internet connection, where the computer gets it’s local IP address using DHCP from the router in your house/office:

The package we will be using for caching nameserver lookups is called dnsmasq. So first, install it using:
$sudo apt-get install dnsmasq
(If you can’t find then, then you probably haven’t added the Universe repository to your list of repositories.)

No uncomment the following line (that is edit the line to NOT have a “#” in the beginning) in the file /etc/dnsmasq.conf:
listen-address=127.0.0.1

Now edit /etc/dhcp3/dhclient.conf and make sure the section below exactly like this, especially the line that says “prepend domain-name-servers 127.0.0.1;”

#supersede domain-name "fugue.com home.vix.com";
prepend domain-name-servers 127.0.0.1;
request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, host-name,
netbios-name-servers, netbios-scope;

Explanation for the above change: In the normal case, when you get a new dhcp lease, the dhcp3 client (tool) on your computer gets a new lease, and updates the /etc/resolv.conf file on your computer with the right values for the DNS servers to use (usually some machine in the network of your hosting provider). Adding the “prepend” option as we did above ensures that “127.0.0.1” will appear on the top of the list of DNS servers. That magic number refers to your own computer. So in the future, whenever your computer needs to resolve a domain name, it will forward that request to dnsmasq (which is running at 127.0.0.1 – your computer). If the details for the domain name are already in you cache, well and good, dnsmasq will serve it up and make the process real fast. If it is not in the cache, then dnsmasq will look at the /etc/resolv.conf file and use the nameservers listed below the “127.0.0.1”. I hope that explains things.

Now open the file /etc/resolv.conf in your text editor. It probably looks like:

search yourisp.com
nameserver 217.54.170.023
nameserver 217.54.170.024
nameserver 217.54.170.026

The 127.0.0.1 is missing right now since you haven’t renewed your lease after you edited the /etc/dhcp3/dhclient.conf file. So, let us add that in manually this one time. After you do, your /etc/resolv.conf file will look like the following:

search yourisp.com
nameserver 127.0.0.1
nameserver 217.54.170.023
nameserver 217.54.170.024
nameserver 217.54.170.026

Don’t worry if the numbers are different – if they are not, then hey – we must be neighbours 😉

Okay. We are almost done here. All we have to do now is to restart dnsmasq so that the changes we made to the configuration file take effect. You can do that using the command:
$sudo /etc/init.d/dnsmasq restart.

Now you are running a local DNS cache. If you want to measure your speed improvement, type the command:
$dig google.com
You will see something like “;; Query time: 38 msec” there.
Now type the command again, and you should see something like:”;; Query time: 2 msec”

See, the first time, since google.com’s details were not in your cache (you are using it for the first time), the query took 38 ms. The second time, the cache speeds up the lookup. I have been using this for over a month now, and haven’t had a problem.

The following is ONLY for dsl customers
Note: If you have a dsl connection, the following may work:
Basically, the differences are in how the “conf” files are edited and used.
Copy the /etc/resolv.conf file to /etc/resolv.dnsmasq.conf
Then, edit the /etc/dnsmasq.conf file as follows:

# Change this line if you want dns to get its upstream servers from
# somewhere other that /etc/resolv.conf
resolv-file=/etc/resolv.dnsmasq.conf

You also have to uncomment the line that says listen-address=127.0.0.1
Now, edit /etc/resolv.conf to have ONLY the following line in it:
nameserver 127.0.0.1
Next, edit /etc/ppp/peers/dsl-provider and change the line:
usepeerdns to
#usepeerdns
(that is, comment out that line)
The ppp client does not allow you to prepend the 127.0.0.1 entry to your /etc/resolv.conf file. So what we did in the above was to create a copy of your previous resolv.conf for dnsmasq to use for lookups, update the file to use a local cache, and then prevent the ppp client from overwriting the resolv.conf file the next time. Now you can restart the dnsmasq service as I explained above, and start enjoying faster name resolution.
I don’t have a dsl connection, and so all the above is to the best of my knowledge.

To those of you still on dial-up – THANK YOU for visiting my blog! (I’m too ignorant to know how to change things to get dnsmasq to work on dial-up 🙂 )

Participate in the Popularity Contest July 16, 2006

Posted by Carthik in administration, applications, packages, ubuntu.
22 comments

The popularity-contest (popcon) package in Ubuntu lets you vote on your favorite, most-used apps and packages every week. This is an automated process, so once you set it up, you will never have to worry about it again. Below is a description of the package:

Vote for your favourite packages automatically. When you install this package, it sets up a cron job that will anonymously inform the developers about your most used packages.
This information helps us make decisions such as which packages should be promoted and so be in standard installs.

The results are available to everyone at popcon.ubuntu.com. Seeing as there are less than 2000 i386 (pentium) users that currently submit popcon information, I thought we could do better. So setup popcon and start submitting info about your usage of applications now – this is vital feedback for those who architect the distro – not to mention a lot of fun for everyone.

Here’s how you set it up. It is already installed, but inactive:
Execute the following command to reconfigure it:
$sudo dpkg-reconfigure popularity-contest

You can choose to submit stats via http, or via email. Both are automated processes, and I’d choose http for the convenience. If, however, you do choose email, note that you might have to take a small additional step to enable postfix to send the email properly. You can follow the instructions to setup a “smarthost” in postfix to ensure that your email gets sent and received without problems. I have my popcon set up to use the http protocol to send information. Since it is anonymous, it is not a threat to your privacy. So go ahead, and give ’em some feedback.

Installing Packages on Computers with Slow Connections Redux July 8, 2006

Posted by Carthik in administration, commands, guides, maintenance, ubuntu.
15 comments

Earlier, I had written about using apt-zip to upgrade computers on slow internet connections by using a faster machine to do the downloads. However, since that involves understanding how apt-zip works, and a small learning curve, here is a hack that should work pretty well, in spite of it’s hackish nature.

The tip here will let you install new packages (and their dependencies) or upgrade a system by using a faster internet connection to do the downloading. First we get the URIs for the pacakges to be downloaded, then we download the packages and transport them to the computer with the slower connection. Thanks to Ewan for this tip posted to the ubuntu-users list a long time ago.
The steps:
1) On the computer you wish to install something new on (or upgrade), do a $sudo apt-get update

2) Then use apt-get to generate a list of the packages it needs to download, in order to install the package that you need, but not download them:
$sudo apt-get -qq --print-uris install name-of-package | cut -d\' -f 2 > urilist
this gives you a file ‘urilist’ in the same directory as the one in which you ran the previous command, with a list of files to download.

3) Take your list to a machine with a fast internet connection and download the packages using wget:
$wget -i < urilist

4) Take your newly downloaded debs home and copy them into the
/var/cache/apt/archives/ directory

5) Rerun the same apt-get command, but without any special parameters:
$ sudo apt-get install name-of-package

apt-get should tell you the quantity of packages that it will install,
and how much it will download. The download amount should be zero since
the packages are already downloaded.

“expert” Mode Install and Admin (sudo) for Users May 26, 2006

Posted by Carthik in administration, ubuntu.
10 comments

If you chose to install Ubuntu in the expert mode, then you can create an use a root user, just like in any other distro. However, this might leave normal users with no admin privileges, so they’ll have trouble running Synaptic, configuring the network etc. In the normal mode, the first user is automatically setup as an admin, so you can use sudo (or gksudo) to execute programs as an administrator (root user equivalent).

If, however, you installed Ubuntu in the expert mode, and still want some users to be admins, you can use this solution. You can create a group called admin, and give this group admin privileges. Then you can add all the users you want to have these privileges to this group.

The command for this are as follows:

#addgroup --system admin
#visudo
The sudoers file should then be edited to include:
%admin ALL=(ALL) ALL
#adduser username admin

The above commands are to be executed as the root user, of course. Replace username with the username of the user that you want to give admin privileges to.

Read Emails from your System using Evolution May 25, 2006

Posted by Carthik in administration, maintenance, ubuntu.
19 comments

The system installed on your PC sends you mails. You usually see a message saying “You have new mail.” when you open a new terminal window. Some of these emails report errors, when things didn’t go right. Some list things that need to be done after some software is installed/upgraded. In any case, the assumption is that you read these mails. You can also send other users on the system mail messages using this system, but since I assume this is only for a standalone PC with users you can talk to if needed (I am sure your significant other would rather hear what you have to say, that read it in a system mail message 😉 )

You can read them using pine or mutt (after you install them) from the command line, but what’s better is that you can read them using Evolution. Follow the instructions below to do so:

In Evolution:

  1. Select Edit->Preferences
  2. Select the “Mail Accounts” tab and click “Add”
  3. Fill in the required fields, when asked for e-mail address, input USERNAME@HOSTNAME (where USERNAME is your username and HOSTNAME is the hostname of the machine (try “localhost” if you aren’t sure)
  4. For “Server Type in Receiving Email”, select “Local Delivery”. In input box Path enter: /var/spool/mail/USERNAME (replace USERNAME with your username).
  5. Follow throw and complete creating the account

You’re done. Now you can read the local system mail using Evolution.

Disable Shutdown For Normal Users March 20, 2006

Posted by Carthik in administration, guides, ubuntu.
24 comments

Sometimes, when you have one computer shared among multiple users, and you don’t want normal users (users who are not admins), that is, anyone who is not you 😉 to be able to shutdown the computer, then you can follow the following steps. Some of this is from the useful fedora mailing list email, and some from the ubuntu-users mailing list (thanks to Olafur Arason). I tried the instructions below on my computer, and this should work for you.

Again, this will allow only admin users with sudo privileges to shutdown the computer, for other “normal” users, the logout menu will allow them to do only that, log out!

Step1:
Open /etc/X11/gdm/gdm.conf in a text editor and find the [greeter] section. Make sure that there is a line which says SystemMenu = false. This line will ensure that the gdm login screen will not have the option for shutting down the system etc.

Step 2:
If you have a laptop, or an acpi system on your computer, then go to /etc/acpi and disable the power button, so that, when someone presses the power button, the system does not shutdown. You can disable this easily by doing chmod 000 /etc/acpi/powerbtn.sh

Step 3:
Now edit /etc/inittab

and find the lines that say:

#Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now

And change it to read:

#Disallow CTRL-ALT-DELETE
ca::ctrlaltdel:/bin/echo "ctrl-alt-delete has been disabled"
.

This will effectively prevent users from changing to a console screen and using ctrl+alt+delete to shutdown the system.

Step 4:
Execute the following commands:
sudo chgrp admin /sbin/halt /sbin/shutdown
sudo chmod 550 /sbin/halt /sbin/shutdown

Step 5:
Use the Configuration Editor (Applications -> System Tools -> Configuration Editor) to edit the preference apps->gnome-session->options->logout_option to “logout” instead of “shutdown”.

That’s it! Now only you, or another superuser (Admin) can shutdown the system, using the command:
$sudo shutdown -t3 -r now

Editing FAT32 Partition Labels using mtools March 1, 2006

Posted by Carthik in administration, applications, guides, ubuntu.
49 comments

I wanted to rename the fat32 partitions that get automounted when they are plugged in to the USB drive. Two were exactly similar external hard disk drives, and one was an iPod. The exactly similar hard disk drives (one each at home and work) were both getting mounted at /media/sda1 or sda2 etc, and it was impossible to distinguish one from the other easily. Also, I found that it wasn’t that easy to edit the partition labels for FAT32 partitions. So I thought I would summarize how I named my fat32 partitions to have consistent names. This has the benefit that when these drives are automounted, they will be at the location /media/partition-label, where partition-label is the label that you give the partition.

Step by step instructions to re-label FAT partitions follow:

1) Install mtools:
$sudo apt-get install mtools

2) After the usb drive is automounted after plugging in, find out the device descriptor using:
$mount
and Note down where it says “sda1” or similar

3) copy the mtools.conf to ~/.mtoolsrc
$cp /etc/mtools.conf ~/.mtoolsrc

4) Edit ~/.mtoolsrc to add one line at the very end:
drive i: file="/dev/sda2"
–you may have to change sda2 to something else depending on what you got in step 2 above.

5) Change to the “drive” i:
$mcd i:

6) Check what the label for the drive is currently:
$sudo mlabel -s i:

7) Change the label to something pretty:
$sudo mlabel i:my-ipod

8) Check if the label has changed:
$sudo mlabel -s i:
I got the following output —
Volume label is MY-IPOD

You’re all set!! The next time that partition gets automounted, it will be at /media/MY-IPOD

Restoring the Ubuntu usplash after a Kubuntu Install February 20, 2006

Posted by Carthik in administration, maintenance, ubuntu.
58 comments

If you, like me, installed kubuntu-desktop to try it out, then afterward, you must have noticed that when you boot up, the kubuntu splash screen appears with “kubuntu” in blue (instead of the Ubuntu brown) while the computer is booting up.

While this is not a major problem, it is a minor irritant, since you will have to field questions from people regarding why you use kde instead of the superior gnome. Even if it is not a problem, I found a solution, and so it deserves to be called a problem, just so I can write an entertaining article regarding how to fix this.

To get back your familiar Ubuntu usplash image and screen, do a:
$sudo update-alternatives --config usplash-artwork.so

Now you will get to answer a question regarding whether to use the Kubuntu, Ubuntu (or if you have it installed xubuntu) – desktop usplash imag. Select the one you want and rest at ease.

For those who don’t want to muck around with the terminal, may I suggest a quick read of the galternatives article I authored previously?

P.S. Regular Daily readers might like to know that an article on this site got dugg. I apologize for not having published anything since then – was busy reinstalling Ubuntu on a machine, and life interfered, too.

Clean Up Old Thumbnails February 15, 2006

Posted by Carthik in administration, commands, maintenance, snippets, ubuntu.
43 comments

The nautilus file manager shows you thumbnails of images, pdf files etc when you are browsing your directories. But you already know that. Nautilus also saves a copy of the thumbnails for later, to speed things up. The thumbnails are stored in your ~/.thumbnails directory. Over time, thumbnails keep accumulating, since, even if you delete a file (an image, say), the thumbnail remains. Cleaning this up might save you some space. It saved me about 650MB!

I found this neat command that you can execute to find and delete thumbnails that have not been accessed in the last 7 days. Deleting a thumbnail should not affect anything much, since if Nautilus cannot find a thumbnail, it will just create one anew.

$find ~/.thumbnails -type f -atime +7 -exec rm {} \;

You can put that code in your cron if you like, to have it run every month or so.