Linux Tricks & Hacks

1. Runnig top command in batch mode

Top is a very useful command we are using while working with linux for monitoring the utilization of our system.It is invoked from the command line and it works by displaying lots of useful information, including CPU and memory usage, the number of running processes, load, the top resource hitters, and other useful bits. By default, top refreshes its report every 3 seconds.
Most of us use top in this fashion; we run it inside the terminal, look on the statistics for a few seconds and then graciously quit and continue our work.
But what if you wanted to monitor the usage of your system resources unattended? In other words, let some system administration utility run and collect system information and write it to a log file every once in a while. Better yet, what if you wanted to run such a utility only for a given period of time, again without any user interaction?
There are many possible answers:
  • You could schedule a job via cron.
  • You could run a shell script that runs ps every X seconds or so in a loop, incrementing a counter until the desired number of interactions elapsed. But you would also need uptime to check the load and several other commands to monitor disk utilization and what not.
Instead of going wild about trying to patch a script, there's a much, much simpler solution: top in batch mode. 
top can be run non-interactively, in batch mode. Time delay and the number of iterations can be configured, giving you the ability to dictate the data collection as you see fit. Here's an example:

top -b -d 10 -n 3 >> top-file

We have top running in batch mode (-b). It's going to refresh every 10 seconds, as specified by the delay (-d) flag, for a total count of 3 iterations (-n). The output will be sent to a file. A few screenshots:
And that does the trick. Speaking of writing to files ...

2. Write to more than one file at once with tee

In general, with static data, this is not a problem. You simply repeat the write operation. With dynamic data, again, this is not that much of a problem. You capture the output into a temporary variable and then write it to a number of files. But there's an easier and faster way of doing it, without redirection and repetitive write operations. The answer: tee.

tee is a very useful utility that duplicates pipe content. Now, what makes tee really useful is that it can append data to existing files, making it ideal for writing periodic log information to multiple files at once.

Here's a great example:
ps | tee file1 file2 file3
That's it! We're sending the output of the ps command to three different files! Or as many as we want. As you can see in the screenshots below, all three files were created at the same time and they all contain the same data. This is extremely useful for constantly changing output, which you must preserve in multiple instances without typing the same commands over and over like a keyboard-loving monkey.

Now, if you wanted to append data to files, that is periodically update them, you would use the -a flag, like this:
ps | tee -a file1 file2 file3 file4

3. Unleash the accounting power with pacct

Did you know that you can log the completion of every single process running on your machine? You may even want to do this, for security, statistical purposes, load optimization, or any other administrative reason you may think of. By default, process accounting (pacct) may not be activated on your machine. You might have to start it:
/usr/sbin/accton /var/account/pacct
Once this is done, every single process will be logged. You can find the logs under/var/account. The log itself is in binary form, so you will have to use a dumping utility to convert it to human-readable form. To this end, you use the dump-acct utility.
dump-acct pacct
The output may be very long, depending on the activity on your machine and whether you rotate the logs, which you should, since the accounting logs can inflate very quickly.
And there you go, the list of all processes ran on our host since the moment we activated the accounting. The output is printed in nice columns and includes the following, from left to right: process name, user time, system time, effective time, UID, GID, memory, and date. Other ways of starting accounting may be in the following forms:
/etc/init.d/psacct start
Or:
/etc/init.d/acct start
In fact, starting accounting using the init script is the preferred way of doing things. However, you should note that accounting is not a service in the typical form. The init script does not look for a running process - it merely checks for the lock file under /var. Therefore, if you turn the accounting on/off using the accton command, the init scripts won't be aware of this and may report false results.
BTW, turning accounting off with accton is done just like that:
/usr/sbin/accton
When no file is specified, the accounting is turned off. When the command is run against a file, as we've demonstrated earlier, the accounting process is started. You should be careful when activating/deactivating the accounting and stick to one method of management, either via the accton command or using the init scripts.

4. Dump utmp and wtmp logs

Like pacct, you can also dump the contents of the utmp and wtmp files. Both these files provide login records for the host. This information may be critical, especially if applications rely on the proper output of these files to function.
Being able to analyze the records gives you the power to examine your systems in and out. Furthermore, it may help you diagnose problems with logins, for example, via VNC or ssh, non-console and console login attempts, and more.
You can dump the logs using the dump-utmp utility. There is no dump-wtmp utility; the former works for both.

You can also do the following:
dump-utmp /var/log/wtmp
Here's what the sample file looks like:

5. Monitor CPU and disk usage with iostat

Would you like to know how your hard disks behave? Or how well does your CPU churn?iostat is a utility that reports statistics for CPU and I/O devices on your system. It can help you identify bottlenecks and mis-tuned kernel parameters, allowing you to boost the performance of your machine.
On some systems, the utility will be installed by default. Ubuntu 9.04, for example, requires that you install sysstat package, which, by the way, contains several more goodies that we will soon review:
Then, we can start monitoring the performance. I will not go into details what each little bit of displayed information means, but I will focus on one item: the first output reported by the utility is the average statistics since the last reboot.
Here's a sample run of iostat:
iostat -x 10 10
The utility runs 10 times, every 10 seconds, reporting extended (-x) statistics. Here's what the sample output to terminal looks like:

6. Monitor memory usage with vmstat

vmstat does the similar job, except it works with the virtual memory statistics. For Windows users, please note the term virtual does not refer to the pagefile, i.e. swap. It refers to the logical abstraction of memory in kernel, which is then translated into physical addresses.
vmstat reports information about processes, memory, paging, block IO, traps, and CPU activity. Again, it is very handy for detecting problems with system performance. Here's a sample run of vmstat:
vmstat -x 10 10
The utility runs 10 times, reporting every 1 second. For example, we can see that out system has taken some swap, but it's not doing anything much with it, there's approx. 35MB free memory and there's very little I/O activity, as there are no blocked processes. The CPU utilization spikes from just a few percents to almost 90% before calming down.
Nothing specially exciting, but in critical situations, this kind of information can be critical.

7. Combine the power of iostat and vmstat with dstat

dstat aims to replace vmstat, iostat and ifstat combined. It also offers exporting data into .csv files that can then be analyzed using spreadsheet software. dstat uses a pleasant color output in the terminal:
Plus you can make really nice graphs. The spike in the graph comes from opening the Firefox browser, for instance.

8. Collect, report or save system activity information with sar

sar is another powerful, versatile system. It is a sort of a jack o' all trades when it comes to monitoring and logging system activity. sar can be very useful for trying to analyze strange system problems where normal logs like boot.msg, messages or secure under /var/log do not yield too much information. sar writes the daily statistics into log files under /var/log/sa. Like we did before, we can monitor CPU utilization, every 2 seconds, 10 times:
sar -u 2 10
Or you may want to monitor disk activity (10 iterations, every 5 seconds):
sar -d 5 10
Now for some really cool stuff ...

9. Create UDP server-client - version 1

Here's something radical: create a small UDP server that listens on a port. Then configure a client to send information to the server. All this without root access!

Configure server with netcat

netcat is an incredibly powerful utility that can do just about anything with TCP or UDP connections. It can open connections, listen on ports, scan ports, and much more, all this with both IPv4 and IPv6.
In our example, we will use it to create a small UDP server on one of the non-service ports. This means we won't need root access to get it going.
netcat -l -u -p 42000
Here's what we did:
-l tells netcat to listen, -u tells it to use UDP, -p specifies the port (42000).
We can indeed verify with netstat:
netstat -tulpen | grep 42000
And we have an open port:

Configure client

Now we need to configure the client. The big question is how to tell our process to send data to a remote machine, to a UDP port? The answer is quite simple: open a file descriptor that points to the remote server. Here's the actual BASH script that we will use to test our connection:
The most interesting bit is the line that starts with exec.
exec 104<> /dev/udp/192.168.1.143/$1
We created a file descriptor 104 that points to our server. Now, it is possible that the file descriptor number 104 might already be in use, so you may want to check first with lsof or randomize the choice of the descriptor. Furthermore, if you have a name resolution mechanism in place, you can use a hostname instead of an IP. If you wanted to use a TCP connection, you would use /dev/tcp.
The choice of the port is defined by the $1 variable, passed as a command-line argument. You can hard code it - or make everything configurable by the user at runtime. The rest of the code is unimportant; we do something and then send information to our file descriptor, without really caring what it is. Again, we need no root access to do this.

Test connection

Now, we can see the server-client connection in action. Our server is a Ubuntu 8.10machine, while our client is a Fedora 11. We ran the script on the client:
And watch the command-line on the server:
To make it even more exciting, I've created a small Flash demo with Wink. You are welcome to play the file, if you're interested:

Cool, eh?

10. Configure UDP server-client - version 2

The limitation with the exercise above is that we do not control over some of the finer aspects of our connection. Furthermore, the connection is limited to a single end-point. If one client connects, others will be refused. To make things more exciting, we can improve our server. Instead of using netcat, we will write one of our own - in Perl.
Perl is a powerful programming language, very flexible, very neat. I must admin I have only recently began dabbling in it, so do not expect any miracles, but here's one way of creating a UDP server in Perl - there are tons of other implementations available, better, smarter, faster, and more elegant.
The code is very simple. First, let's take a look at the entire file and then examine sections of code. Here it is:
#!/usr/bin/perl

use IO::Socket;

$server = IO::Socket::INET->new(LocalPort => '50060',
                                Proto => "udp")
or die "Could not create UDP server on port
$server_port : $@n";

my $datagram;
my $MAXSIZE = 16384; #buffer size

while (my $data=$server->recv($datagram,$MAXSIZE))
{
    print $datagram;

    my $logdate=`date +"%m-%d-%H:%M:%S"`;
    chomp($logdate);

    my $filename="file.$logdate";
    open(FD,">","$filename");
    print FD $datagram;
    close(FD);
}

close($server);
The code begins with the standard Perl declaration. If you want extra debugging, you can add the -w flag. If you want to use strict code, then you may also want to add use strict;declaration. I warmly recommend this.
The next important bit is this one:
use IO::Socket;
This one tells Perl to use the IO::Socket object interface. You can also use IO:Socket::INET specifically for domain sockets. For more information, please check the official Perl documentation.
The next bit is the creation of the socket, i.e. server:
$server = IO::Socket::INET->new(LocalPort => '50060',
                                Proto => "udp")
or die "Could not create UDP server on port
$server_port : $@n";
We are trying to open the local UDP port 50060. If this cannot be done, the script will die with a rather descriptive message.
Next, we define a variable that will take incoming data (datagram) and the buffer size. The buffer size might be limited by the network implementation or network restrictions on your router/switch or the kernel itself, so some values might not work for you.
And then, we have the server doing some hard work. It prints the data to the screen. But it also creates a log file with a time stamp and prints the data to the file as well.
The beauty of this implementation is that the server permits multiple incoming connections. Of course, you will have to decide how you want to differentiate the data sent by different clients, whether by a message header or using additional IO:Socket:INET objects like PeerAddr.
On the client side, nothing changes.

Conclusion

That's it for now. This crazy collection should help you impress friends evoke a smile with your peers or even your boss and help you be more detailed and productive when it comes to system administration tasks. Some of the utilities and tricks presented here are tremendously useful.
If you're wondering what distribution you may need to be running to get these things done, don't worry. You can get them working on all distros. Throughout this document, I demonstrated using Ubuntu 8.10, Ubuntu 9.04 and Fedora 11. Debian-based or RedHat-based, there's something for everyone.

Reuse an old router to bridge devices to your wireless network


Reuse an old router to bridge devices to your wireless network(Credit: Ed Rhee/CNET )
Many smart or connected devices come with wired-only connections (Ethernet), like your TV,game console, DVD player, TiVo, or other streaming-media device. Unless your Internet modem or wireless router happens to be at the same location as those devices, connecting them to your wireless network can be a challenge.
Possible solutions include, power-line adapters, dedicated wireless adapters for each device, or installing Ethernet jacks. Unfortunately, those options can get expensive and each has its unique drawbacks. Another option is using a wireless bridge. A wireless bridge connects two wired networks together over Wi-Fi. The wireless bridge acts as a client, logging in to the primary router and getting an Internet connection, which it passes on to the devices connected to its LAN Jacks. You can buy a dedicated wireless bridge, but if you have an old router lying around, you might be able to convert it into a wireless bridge using DD-WRT.

Client Bridge mode
A wireless bridge connects two wired networks together over Wi-Fi.


DD-WRT is free Linux-based firmware for routers that replaces the router's factory firmware. It can breathe new life into an old router, giving it enhanced performance and new features. One of the features of DD-WRT is the ability to switch the router's function to a wireless bridge.
This tutorial, based on the DD-WRT Wiki, will go over how to configure DD-WRT for use as a wireless bridge (client bridge mode), using a Linksys WRT54G router as an example. The process is fairly straightforward but will require some time and patience to complete. In the steps below, primary router refers to the main router that you'll connect to, while bridge routerrefers to the router you're configuring as a client bridge.
Step 1: Check the DD-WRT router database to see if your router is supported. If your router isn't supported, keep an eye out at The Cheapskate blog for router deals. Rick Broida recently found a refurbished router with DD-WRT support for just $9.99.
Step 2: If your router is supported, you'll see it listed in the database and it'll include links to the firmware. It may also include device-specific directions on how to install DD-WRT, but you'll want to read over the general installation notes as well. In some cases, you'll need to install a firmware prep, prior to installing the actual DD-WRT firmware. It's possible to brick your router during installation if not performed correctly, so read the directions carefully and follow each step as noted.
Step 3: Once you've successfully installed DD-WRT, perform a hard reset on the router. This usually involves holding a reset button/pin for 30 seconds or until you see the router's lights flashing. Refer to your router's user manual to figure out the exact method for a hard reset.
Step 4: Connect an Ethernet cable from your computer to one of the LAN jacks on the bridge router, then set a static IP address on your computer. Use something like 192.168.1.10, so you'll be on the same subnet as the bridge router, which will have an IP address of 192.168.1.1.
Step 5: Open a Web browser and enter 192.168.1.1 in the address bar. The DD-WRT Web interface will appear and you'll be prompted to change your username and password. Choose a username and password, then click the Change Password button.
Step 6: Go to Wireless > Wireless Security, then set the same Security ModeAlgorithm, andShared Key as your primary router. Click Save.

Step 7: Go to Wireless > Basic Settings, then change the Wireless Mode to Client Bridge. Set the same Network Mode and SSID as your primary router. If your bridge router is an N router, you may also need to set the same wireless channel width as your primary router. Click Save, then Apply Settings.
Step 8: Go to Setup > Basic Setup, then fill in the router IP info. For the local IP address of the router, choose an address in the same subnet as your primary router. If your primary router is on the 192.168.1.x subnet, choose something like 192.168.1.15. Leave the Subnet Mask as 255.255.255.0 and enter the IP address of your primary router as the Gateway. Leave Local DNS blank and select your time zone. Optionally, you can check the box to Assign WAN Port to Switch. This converts the WAN port to a LAN port, providing you with an extra port on your bridge router. Click Apply Settings.


Step 9: Log back in to your bridge router using the new address you set (192.168.1.15).
Step 10: Go to Security > Firewall, then under Block WAN Requests, make sure that only "Filter Multicast" is checked. Click Save.


Step 11: Under Firewall Protection, make sure "SPI Firewall" is disabled, then click Apply Settings.
Step 12: Go to Setup > Advanced Routing, then set the Operating Mode to "Router." Click Save.

Step 13: Remove the static IP address from your computer and re-enable automatic IP addressing. At this point, the bridge router should pass along an IP address from the primary router to your computer, giving you access to the Internet.
Step 14: Once you've verified that your computer can access the Internet while physically connected to the bridge router, you can disconnect your computer. You're now ready to connect your wired devices to the bridge router from anywhere in your home (within range of your primary router).
Notes:
  • If you're using MAC filtering on your primary router, you'll want to add the bridge router's wireless MAC address, not the LAN MAC address that's usually printed physically on the router. To find the wireless MAC address, go to Status > Sys-Info.
  • If your primary router is a Wireless-N router, but your bridge router is a Wireless-G router, you may need to set the wireless mode to a setting that supports b/g networks.
That's it. Now you can connect all your wired devices to your wireless network, even if they're not next to your primary router. It's worth mentioning that a wireless bridge is also useful when the scenario is reversed. If you have your Internet modem and primary router next to your TV, but need connectivity to your computer in another location, you can use the wireless bridge to provide a connection to your computer, printer, NAS, etc.

Courtsy : www.cnet.com

Best Boot CD Creator

Best Boot CD Creator
Best Boot CD Creator

Some malware can best be removed if the infected system, including the malware itself, is not active during the cleaning. To use another system, one needs either a second full installation, or a operating system bootable from CD. Boot CD Creator create a bootable CD with a minimum of interaction by the user required. Boot CD Creator automates the difficult parts and tries to be as simple as possible using these steps nearly every user should be capable of.

Download Boot CD Creater

Click here for other Boot CDs...

Lock and unlock computer with a USB drive


Lock and unlock computer with a USB drive
Lock and unlock computer with a USB drive

If you prevent access to your computer with just a password, you are missing out on an alternate (and more secure) way to lock down your computer.
Predator, a free Windows program, turns your USB drive into a key that locks your computer when it's removed. To unlock your computer, you'll have to plug the USB drive back in. 
Anyone who attempts to access your computer without the USB flash drive will be hit with an epic "Access Denied" message. To get started, follow this guide:
Step 1: Download and install Predator.
Step 2: Once Predator launches, plug in your USB flash drive. None of the contents of the drive will be deleted or altered in any way, so feel free to use your primary thumbdrive.
When you insert the drive, a dialog box will appear asking you to create a password. Click OK to continue.
Step 3: In the Preferences window, take note of a few key settings. First, enter a secure, unique password in the "New password" field. If you lose your USB drive, you'll use it to unlock your computer.
If you'd like, you can check the Always Required box and you'll be asked to enter the password each time you use your thumbdrive to unlock your PC.
Lock and unlock computer with a USB drive
Lock and unlock computer with a USB drive
Finally, in the section under Flash Drives, ensure that the correct USB flash drive is selected. When you're done, click "Create key" and then OK.

Step 4: Predator will exit. When it does, click the Predator icon in the taskbar to restart the program. A few seconds later, the icon will turn green, alerting you that Predator is running.
Every 30 seconds, Predator will check to see that your USB drive is plugged in. If it isn't, your computer will dim and lock down.
Here are some extra tips:
  • To pause Predator at any time select "Pause monitoring" from the taskbar menu.
  • If someone tried to access your PC while it was locked down, you'll see the activity log when you log back in. You can see the log at any time by clicking "View log" from the taskbar menu.
  • Predator's Web site has several cool how-tos, including one that shows you how to program your computer to take a snapshot each time someone tries and fails to log in to your computer. Check it out.


Linux Class Room: Introduction


Hi ,


Today I am going to start a new Session in this bog , focused on the people who are coming newly to Linux field.

Like every new guys in this field I also had a lot of doubts while changing to Linux field.At first it will not digest to new guys especially those who are changing from the windows background.Me to faced the same problem.
But , believe me ,when i changed to Linux background i felt that this is one of the simplest  OS for everyone.one thing i noticed was its power to change any files in its file system by the root user , as widows wil not allow this feature.In this way we can change our Linux to any form as per our wish, as widows wil not allow that.


ok...i am not going to give all my experience here ,though it will take a lot of time, i don't have that much tooo.



In this page i am going to give you classes from the basics of Linux( i will start with redhat , though it is more popular one).including the screenshots and video tutorials.



so Guys let start our new class room from my next posts......welcome to all with your feedbacks and comments

Click here to view all session ithis class room...

Best Linux OS in 2012


1. Ubuntu 12.04 LTS

Best Linux OS in 2012

Ubuntu is the #1 and the most popular distro out there. Even though Linux Mint appeals more to new users Ubuntu has a rigorous release cycle and tends to have more features implemented in each release. Ubuntu does not come with a load of software and codecs pre installed like Linux Mint. So new users may have trouble playing certain media formats and may require a few command line installations but due to the excellent community support they can be sorted out within minutes.
2. Linux Mint 13
Best Linux OS in 2012

Linux Mint is known as the second most popular linux distribution simply because of its user friendliness. It comes with loads of software carefully picked by the team, media codecs and drivers. The distro works so well out of the box you will not be spending any time trouble shooting. The Distro focuses on what is best for its users and provides what the mainstream linux users demand (most of the time). Linux Mint 13 comes in 2 editions. The Cinnamon edition includes a modern Gnome 3 desktop with a familiar and traditional layout. The MATE edition comes with a Gnome 2 desktop. Compared to the cinnamon edition, the MATE edition is more stable but is quite boring. Cinnamon is a fairly new desktop that is being developed by the mint team.
Linux mint is based on Canonical's Ubuntu.

3. Pinguy OS 12.04 LTS

Best Linux OS in 2012

Pinguy OS is an Ubuntu based distribution that comes with A LOT of software preinstalled. It is great for users who want to explore the extensive software that Linux has to offer. It is also very convenient because it includes almost all the software that a user may require. Pinguy OS is a fairly new distro but it is gaining popularity quickly. Pinguy OS includes two Docks by default and the overall look of the desktop leans toward OS X. Pinguy OS 12.04 includes a customized gnome-shell.
4. Zorin OS
Best Linux OS in 2012

Zorin OS is optimized for users who are transitioning From windows. It looks quite similar to Windows 7 and comes with "zorin look changer" that can make your desktop look similar to older Windows versions and Mac OS X. Zorin OS also offers four premium versions (Ultimate, Business, Multimedia, Gaming) which are available upon donating. There is also a free version that does not come with as much software preinstalled.

5. Peppermint OS 3

Best Linux OS in 2012

Peppermint OS a very light distro that comes with LXDE desktop envioronment. LXDE is very simple to use and many will find it to be a straightforward DE. The OS boots up quite fast which makes it ideal for older computers or netbooks. Peppermint OS 3 is based on Lubuntu 12.04. It includes an elegant theme by default and includes media-codecs out of the box.

6. Fedora

Best Linux OS in 2012

You simply cant go wrong with Fedora ( or Red Hat Enterprise Linux for a more server oriented usage ). The quality of Red Hat Linux distributions, and a great looking desktop. Perhaps a little less user-friendly than the two previous Debian based Linux distributions mentioned above. 







Windows XP no longer most popular desktop system

windows xp vs windows 7
windows xp vs windows 7


Windows 7 is now the world's most popular desktop operating system, according to the August report from Net Applications.In August, Windows 7 had a 42.76% market share, a fraction of a point more than Windows XP's 42.52%. Windows XP was released in 2001.The much-maligned Windows Vista sits at third place with a 6.15% market share, followed by Mac OS X 10.7 and Mac OS X 10.6 with 2.45% and 2.38%, respectively.

All in all, older versions included, Microsoft controls some 92% of the market.
It took three years for Windows 7 to become the world's most popular OS. Microsoft's desktop operating system, which was launched in October 2009, will soon be replaced by a newer version — Windows 8.
Windows 8, which is scheduled to go on sale on October 26, will be Microsoft's first operating system designed to work just as well on tablets as it does on desktop computers.
Users running Windows XP, Vista or Windows 7 will be able to upgrade to Windows 8 Professional for $39.99 via an online download.