Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Wednesday, January 19, 2011

Limit Internet Bandwidth Usage (internet speed) in Ubuntu

There are many reasons one would want to limit internet bandwidth/speed on a system e.g you dont want your siblings to choke your internet or, if you are a network admin, you dont want one user, downloading a big movie over torrent, to hurt every one else's surfing.

To limit internet bandwidth on a Ubuntu machine install wondershaper. For this open a terminal and type following command:
sudo apt-get install wondershaper
Now use ifconfig command to find out the name of interface connected to internet. The name will be something like eth0, eth1, wlan0 or something similar. In my case it is eth0


Now issue a command similar to follwoing (relpacing eth0 with your interface name):
sudo wondershaper eth0 1024 256
this will limit the download speed to 1024 Kbps and upload to 256 Kbps. Remember that this limit is for all internet traffic i.e torrent, ftp, browsing and any thing else.

If you want to remove these limits use
sudo wondershaper clear eth0
again replacing eth0 with your interface name

Sunday, December 19, 2010

Track Your Internet Bandwidth Usage with NTM (Network Traffic Monitor)

For those of us with limited network connection Network Traffic Monitor (NTM) is a very helpful tool. It can monitor upload, download or total network traffic. It can also monitor time spent online and lot of other things. NTM is not available in Ubuntu repositories so you will have to download the .deb from here and install it. After installing start NTM from Applications->internet->NTM. Now right click on the NTM icon  in the taskbar and select preferences from the menu that appears. This will open the NTM preferences window. In the General tab specify the interface that you use to connect to internet. In my case it is wlan0


Now select Traffic tab and enter the traffic limit of your internet plan. I have entered 2G i.e 2048MB as an example value.


You can select Auto Disconnect if you want the connection to disconnect as soon as you reach your limit. This can be very helpful if extra MBs are charged at a high rate. Click Apply and you are all done. NTM will monitor your internet use and you can left click on the NTM icon in Taskbar to see your current speed, bandwidth you have consumed and lot of other information.


If you have time limited connection and not bandwidth limited i.e you buy internet hours and not GBs then you can also use NTM to keep track of time you spend on internet. Open the time tab to configure time related options.

Monday, December 13, 2010

Creating a screencast in Ubuntu Linux

To create a screen cast in Ubuntu we need three softwares
  • recordmydesktop for capturing and recording the desktop
  • gtk-recordmydesktop, a graphical frontend for recordmydesktop
  • mencoder for encoding the recorded video to xvid or any other format.

If you also need to record your voice in the screencast then you will need a properly configured sound card along with a decent microphone. You can test your microphone and sound card settings using Sound Recorder.

You can install the softwares using the following command:
sudo apt-get install mencoder recordmydesktop gtk-recordmydesktop
after installation completes run gtk-recordMyDesktop from Applications->Sound & Video menu

If you want to record your full desktop then simply press record and recording will start. You will see a small square in the Notification Area.
This shows that recordmydesktop is currently recording. When you are done and want to stop recording just click on this square and recording will stop. This square will then turn into a red circle and recordmydesktop will start encoding the captured video. It will take some time before it finishes, after that you can find the recorded video in your home directory.
But if you dont want to record complete desktop but only want to capture a single window then you can click on Select Window button and then click on the title bar of the window that you want to record and recordmydesktop will only record the selected window.
If you dont want a specific window but you want the recording to be limited to specific screen area then you can select that area from gtk-recordMyDesktop's preview area. Just left click and drag your mouse to select the desktop area for recording.
recordmydesktop produces files with .ogv extension. I dont know what codec it uses but the files are big e.g a 10 min recording my take upto 60-70 MB. To covert these files into divx use following commands
mencoder your_file.ogv -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -oac pcm -o /dev/null
mencoder your_file.ogv -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -alang en -oac mp3lame -lameopts br=96:cbr:vol=6 -o your_file.avi
This will convert the ogv files into avi with xvid video and mp3 audio. After that you are done and your screencast is ready. If you want to edit this avi file to add some effects or any thing you can use Pitivi video editor which comes by default with Ubuntu (10.04 onwards)

Saturday, December 11, 2010

Linux Tutorial 1 - Installing Ubuntu

This is my first attempt at creating a screencast, a video tutorial explaining the installation of Ubuntu Linux.

Part 1: Installation




Part 2: Post Install

Thursday, December 9, 2010

Simple network usage tracking with vnstat

If you use Linux, you can use vnstat to track your network usage.
Install vnstat using
sudo apt-get install vnstat
then configure it to monitor a network interface
sudo vnstat -u -i
you can find your network interface name using ifconfig or ip addr command. In my case its eth0

Now to see your network usage issue vnstat command


to view daily, weekly or monthly stats you can use -d, -w or -m switches with vnstat respectively i.e vnstat -m
To reset all the counters to zero you will need to delete the interface's database with vnstat and then recreate it. Use the following command for this purpose
rm /var/lib/vnstat/<interface>; vnstat -u -i <interface>
Please note that vnstat is not a packet sniffer, it simply uses the network stats exposed by kernel. This means there is  non-internet network traffic on your interface e.g file sharing, network gaming or etc than it will also be included in the stats. You can use vnstat as bandwidth monitor if you use a dedicated interface for internet usage like 3g usb modem.

Tuesday, November 30, 2010

About .bashrc .bash_profile .profile .bash_login and /etc/profile

Have you ever wondered what is the purpose of ~/.bashrc ~/.bash_profile ~/.bash_login ~/.profile and /etc/profile files in Linux.

Bash is the default shell on most of the Linux distributions like Fedora and Ubuntu. If you take a look at man pages for Bash, you will get the following explanation:
When bash is invoked as an interactive login shell, or as a  non-interactive  shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.  
...
When  an  interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exist. 
Interactive shell is the one which is not running because of a script and lets you enter commands to interact with it. Login shell is the one where you are asked for user name and password before you can enter commands. An example of an interactive login shell is the one that you get after you login to a system using ssh. An example of non-login interactive shell is a shell that you get when you run terminal from menu item or some icon.

So when you login to a Linux system, first commands from /etc/profile (if it exists) will get executed. Then the system will look for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and execute commands from the first of these files that it founds. And when you open Konsole/Terminal from KDE or GDM, first commands from /etc/bash.bashrc (if it exists) will get executed and then commands from ~/.bashrc (if it exists) will get executed.

So if you want to execute a command for all users then you will need to add it to /etc/profile but if you want a command executed for a specific user then you can add it to .profile, .bash_profile, .bash_login or .bashrc in his/her home directory.

Please keep in mind that KDE and GDM are not login shells so there behavior is different. I dont know about KDE but GDM first executes command from /etc/profile and then from ~/.profile.

Friday, July 23, 2010

Interprocess communications between a Linux Kernel Module (LKM) and a user program

More ofter than not a Kernel Module will need to communicate with a user program. We really have following three options for communication between a user program and Kernel Module:
  1. A device file in /dev
  2. A file in /proc 
  3. A file in /sysfs
  4. Netlink sockets
/dev is usually used for device driver files i.e files that represent devices but these don't have to be "real" devices, they can be pseudo devices. A device file in /dev/ may be acceptable, even for modules that aren't really device drivers. So if your module is a driver or exposes a simple open/read/write/close interface then this is the your best option.

A file in /proc or /sysfs is not an actual file. Read or write operations on these files actually go to the associated kernel module. /proc was originally intended for processes related information but over the years it has become dumping ground for lot of other stuff, which kernel developers are not happy about. So its better to stay away from it unless you are providing some processes related information. If you want a file based mechanism of communication but dont want to create a device file then /sysfs is the better option.

Netlink sockets, in my opinion, is the best option if you are working on a Kernel Module which is not a device driver. It provides user programs with a familiar socket interface and does not requires the creation of any kind of file.

I will soon be posting a working example of how to use Netlink socket for communication between a user program and Linux Kernel Module

Wednesday, May 20, 2009

Solving Skype audio problem in Ubuntu 9.04 (Jaunty)

I installed Skype on Jaunty but my audio was not working even though i was able to record my voice using Sound Recorder. After going through many web pages and trying many solutions the one that finally worked for me is this.

stop skype, open terminal and issue following commands
  1. sudo killall pulseaudio
  2. sudo apt-get remove pulseaudio
  3. sudo apt-get install esound
  4. sudo rm /etc/X11/Xsession.d/70pulseaudio
now start skype and every thing works fine.

Sunday, November 9, 2008

Installing Sun Java 6.0 on Ubuntu

Ubuntu is defaultly deployed with GNU libgcj JVM which doesn't work with some applications, for example Eclipse. Also in my experience GNU libgcj JVM is much slower than Sun JRE.
Installing Sun Java 6 JRE is simple. Just issue the following command from shell:
apt-get install sun-java6-jre sun-java6-bin sun-java6-plugin sun-java6-jdk
You will need to be root or use "sudo" to perform this operation. You can skip package sun-java6-jdk if you dont plan on doing development in Java.

Once these packages are installed you can use Alternatives to make sun java your default JVM. To do this issue following command from shell:
update-alternatives --config java
You will get a screen like following:

The * sign means that this JVM is default. Enter the number for Sun JRE which in my case is 3. Now you can check your java version using: java -version

Sunday, October 5, 2008

Installing and using knockd to allow temporary SSH access

knockd is a port-knock server which runs silently on your machine until it sees a specific port-sequence. When it sees the specific port sequence it performs an action associated with that sequence. Port sequence and the associated actions can be easily configured in knockd's configuration file /etc/knockd.conf.
In below example we will use knockd to, upon seeing a specific sequence of port knocks, open a "hole" in our firewall to allow ssh access from outside. Dont worry this is not insecure as you will be the only one knowing that port sequence, unless ofcourse you share that sequence with some one else :)
To accomplish this we will need to do following:
  • install knockd
  • configure it to allow ssh access on seeing a specific port sequence
  • configure it to close ssh access on seeing a different port sequence
Install knockd using: sudo apt-get install knockd. This installation will create a file /etc/knockd.conf.
Open this file using “sudo nano /etc/knockd.conf”.The file will look something like this:

----------------------------------------------------------------------------------------------------

[options]

logfile = /var/log/knockd.log

[openSSH]

sequence = 7000,8000,9000

seq_timeout = 5

command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCE$

tcpflags = syn

[closeSSH]

sequence = 9000,8000,7000

seq_timeout = 5

command = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCE$

tcpflags = syn

-----------------------------------------------------------------------------------------------------

we need to change the sequence and seq_timeout lines for both, [openSSH] and [closeSSH] sections. This is just a precautionary measure as sequence 7000,8000,9000 is a very common and well known. So change the file to look as one below. You can use any sequence that you like but avoid commonly used ports. You can even use ports that are closed/blocked on ur system as knockd works on link layer and will see all the traffic destined for your system even for closed or blocked ports. Dont change command, tcpflags or logfile options. After you have changed the file save it.


Now you need to edit your /etc/default/knockd. Open that file using “sudo nano /etc/default/knockd”. Change it to match the following. This will ensure that knockd is always run as daemon at init i.e at system startup.

The “-i eth0” will ensure that knockd listens on eth0 for port knock sequence. You can change it to whatever interface that you want knockd to listen at.

Now start knockd. You need to do this just this one time.

sudo /etc/init.d/knockd start

Now you are up and running :). knockd will open ssh access for you on seeing the specific port knock sequence. To try this try SSHing this computer from any other machine connected to this computer on interface that you selected in /etc/default/knockd which in my case is eth0. Your firewall will not allow access. To get access you will need to knock at the doors of your computer and for that you will need knock client on the machine that you are using to do ssh. If you are using a linux machine install knockd package and if you are using windows machine you can use windows client from here.

To knock issue following command ( replace 192.168.1.2 with the IP of your computer)

knock 192.168.1.2 5000 7000 9000

This will open ssh access for you. Now use ssh to access your gateway. When you are finished issue following command to close the ssh access.

knock 192.168.1.2 6000 7000 8000

Monday, September 1, 2008

Portable encrypted filesystem using Flash Drive, LInux and Cryptmount

Encrypted file-system can be easily created using cryptmount. On Ubuntu/Debian you can install cryptmount using “sudo apt-get install cryptmount”.

In this example I will demonstrate how to create an encrypted file-system on a flash drive and then use it on another machine. You will need root privileges on machines where you want to create or access this file-system. First we need to create the file system on the flash drive. To do this create an entry in the /etc/cryptmount/cmtab like this


cryptusb {
        dev=/media/flash/cryptusb.fs
        dir=/home/adnan/cryptusb
        fstype=ext3
        fsoptions=defaults
        cipher=aes
        keyfile=/media/flash/cryptusb.key
        keyformat=builtin
    }


This tells cryptmount that your file-system name is cryptusb and you want to store your file-system in /media/flash/cryptusb.fs and to mount it to /home/adnan/cryptusb. It also tells it that the filesystem type is ext3 and to use AES algorithm to encrypt/decrypt the file-system. You can change cipher filed to change the encryption algorithm. Its important that the keyfile and dev are flash drive paths because other wise your file system wont reside on your flash drive and hence you wont be able to use it on any other machine. man cmtab has more information on all the available fields and their valid values.


Now create the file(specified in dev field) which will contain this encrypted file system and the mount point.


$ dd if=/dev/zero of=/media/flash/cryptusb.fs bs=1M count=32

32+0 records in

32+0 records out

33554432 bytes (34 MB) copied, 0.673204 s, 49.8 MB/s

$ mkdir /home/adnan/cryptusb


Next generate the encryption key. You will need to specify key size in bytes


$ cryptmount --generate-key 32 cryptusb

generating random key; please be patient…
enter new password for target "manual":
confirm password:


Now issue following commands to prepare and format the file-system:


$ cryptmount --prepare cryptusb

enter password for target "cryptusb":


$ mkfs.ext3 /dev/mapper/cryptusb


$ cryptmount --release cryptusb


The file-system is now ready and can be mounted using command “cryptmount cryptusb”. To unmount use “cryptmount –u cryptusb”. To use this file-system on another machine just copy


cryptusb {
        dev=/media/flash/cryptusb.fs
        dir=/home/adnan1/cryptusb
        fstype=ext3
        fsoptions=defaults
        cipher=aes
        keyfile=/media/flash/cryptusb.key
        keyformat=builtin
    }


to /etc/cryptmount/cmtab on other machine. Change path in dev and keyfile fields to path to cryptusb.fs and cryptusb.key if flash drive on this machine is not mounted as /media/flash. You can also change dir field if you want the file system to be mounted some where else. After adding this to /etc/cryptmount/cmtab save the file and issue follwing commands


$ cryptmount --prepare cryptusb


$ cryptmount --release cryptusb


and you are done. you can now mount and unmount the file system using cryptmount. You may face some problem accessing files and directories because of file permissions. You can always change file and directory permissions using “chmod” and “chown” commands. A simple workaround is to make root to be the owner of this filesystem. To do this mount this file system using “cryptmount cryptusb” then issuing command “chown root:root /home/adnan1/cryptusb”. Then you can access this file system as root on both machines. This way you wont have to change permissions whenever you take this filesystem from one machine to other.

Wednesday, August 20, 2008

Installing cryptmount on Redhat Linux Enterprise 4

Cryptmount is a utility for mounting encrypted file systems. A nice tutorial on using cyptmount can be found here. But you first need to install cryptmount :-). I was installing it on Linux Enterprise 4 and was not able to make Yum or apt-get work so i had to use rpm and resolve dependecies my self :(. If u face the same problem try following.
Download Cryptmount, glibc, glibc-headers, glibc-devel and glibc-common and put them in the same directory. Now change ur working directory to that directory and issue command "rpm -Uvh glibc-2.8.90-11.i386.rpm glibc-common-2.8.90-11.i386.rpm glibc-devel-2.8.90-11.i386.rpm glibc-headers-2.8.90-11.i386.rpm"
This will install or update GNU C library on ur system. Now issue command "rpm -Uvh cryptmount-3.0-1.i586.rpm" and this will install cryptmount on ur system.