Article by Keiran Holloway published on www.anchor.com.au
I’ve been working in this industry for a while now and naturally spend a lot of time using Linux on a daily basis. This gives lots of exposure to various Linux commands and tools. That said, I am sometimes surprised when I see, often very experienced system administrators, using somewhat convoluted commands to do something relatively simple using a different tool.
This is my opportunity to share some of these experiences:
1. pgrep and pkill – The first command ‘pgrep’ will return the process id based on a name or other attributes. pkill will signal a process with a matching name or attribute. Want to kill all processes being run by a given user? Issue a pkill -U USERNAME; sure beats the hell out of:
ps -U USERNAME|awk {'print $1'}|xargs kill
2. htop – Much like your regular ‘top’ command, on steriods. Gives an interactive view on your machine right now, but with an ascii visual representation of your CPU, memory and swap utilisation. Often not installed by default, but is packaged under both Red Hat Enterprise Linux and Debian and can be trivially installed on most dedicated and virtual private servers.
3. mytop – Similar to top, but designed specifically for MySQL. Gives you an interactive display of what is happening with your MySQL database, in real time. Information such as what queries are being run, amount of data which is being flowing in and out of the database, number of queries being run per second and number of slow queries. This application is once again packaged with most large common Linux distributions.
4. lsof – This cool little command comes with most Linux Distros as default and allows you to display any files which are currently opened on the system. Especially handy for finding files which have been deleted (and not appearing in the file system) but still resident due to being held open by a current running process.
5. iptraf – Want to know where all your network traffic is going to and coming from? iptraf is a really cool tool which can be used to track this information and let you know what is happening on your server.
I hope this information helps. Got some which I’ve missed? Please leave a comment and make this article more useful!
Anchor is a world-wide leader in providing comprehensive support on dedicated servers and virtual private servers both in Australia and around the world.
For when you’re calling support and they want to know what’s inside.
sudo lshw
ps -U USERNAME|awk {'print $1'}|xargs kill
=
slay USERNAME
There’s also iftop (like htop for ethernet), iotop (like htop for disk io).
Other good commands mostly unknown are “xd” and “autojump” which are like “cd” with a better autocompletion.
Thanks for these! I get almost giddy when I learn a new useful command-line tool.
Ever heard of tilt?
you mention pgrep and kill, but not pkill? that command is much easier put using pkill -U
pkill -U lcerezo
I also find iftop useful, more so than iptraf in some cases.
Good info to share, keep it up.
The locate command is awesome. Its the fastest way to find anything on your computer. Should be installed by default. Try “locate *.jpg” to find all the jpgs on your computer.
Thanks for the suggestions. It should be fun comparing iftop and iptraf to etherape. I really like these types of posts myself; there’s always something undiscovered and of use.
Get your NIC’s MAC without entering the router:
ifconfig | grep HWaddr
I also thought this was a cool find for getting your outside IP through terminal (copied from a script):
#!/bin/bash
wget -qO – http://myip.dk/ | egrep -m1 -o ‘[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}’
echo
sleep 10
#http://linux.byexamples.com/archives/307/what-is-my-public-ip-address/
# or $ wget -qO – http://cfaj.freeshell.org/ipaddr.cgi
I myself use half of the commands mentioned in this article, and was also surprised to see an amazingly good Unix system administrator I know use the long way with pipes, and asked him why he did not use the much easier commands instead. He answer was indeed very good – those commands work on any system – be it Sun Solaris, AIX, or whatever as well as Linux. The simplified commands are not always available. He would run commands on major heterogeneous unix clusters and there it is a pain to have to make scripts which had to check if each command was available and then use fall-back when not existing… Much easier to use what known to work everywhere. He then countered me with the exact opposite comment: Why do I use commands which might have different result in different unix environments, depend on tools that might not exist on a given system, when a much easier choice exist allowing your commands to run everywhere… Many of my commands would even break when in “sh” or “ksh” as I mainly used bash myself…
Having past experience myself with BSD, OpenSolaris, and Linux I kind of tend to agree with him. Professional system administrators essentially got to use trusted tools that work everywhere without many if/then/case statements, or dependencies to less common tools.
That said, mainly running Linux systems myself, I really like using the simplified tools such as pkill and pgrep and so on instead 😉
I’d add ngrep which can grep the network stream for a certain pattern. Also netsed which can alter network packets
I have to agree with Kenneth, there’s a lot to be said for using tools which are the lowest common denominator. Often I end up resorting to using perl scripts where other linux tools will do, just so that I know (or at least have some confidence) that it’s going to execute the same way on whichever platform I’m running it on.
There is of course nothing stopping you from building gnu coreutils and findutils on your chosen platform (providing it’s not too old and you have a compiler available).
pstree -p | less
Displays all processes in nested form with process ID, useful for killing a task that’s on another X session or to prune a branch of the process tree with
kill
@Kenneth: I agree. That’s why sysadmins should learn vi(1); it’s available on all systems.
Great article and even better replies, a wealth of information, thanks for sharing.
Hi,
I am new to Linux and I want to know all the commands in Linux.
Could you please send me the list or the links where i can get all the commands and if possible their explanations/illustrations.
Thanks in advance.
Search for some linux system administration book, there are some (free) online that you can find.
Some reference:
http://www.pixelbeat.org/cmdline.html
http://www.er.uqam.ca/nobel/r10735/unixcomm.html
http://www.reallylinux.com/docs/admin.shtml