Top 10 Linux Commands Anyone Can Use

As we continue to find Linux loaded on more and more consumer-based PC’s the reality of Linux everywhere has come to fruition. I’m relatively excited that Ubuntu is being endorsed by companies like Dell. As more and more PC’s ship image with Linux pre-loaded it’s almost necessary to become acquainted with the Linux shell. For those of you from the Microsoft days I would almost consider the Linux shell to be the DOS of Windows 3.1/95/98 (WinXP just emulated the shell). In the blurb below I try to make correlations to Windows commands to help you grasp what each command does.

Anyway, here ten of the commands I think you should know when using Linux as a first-time user. Note that when typing these commands the # sign symbolizes the shell prompt and you shouldn’t type it 🙂

Commands

  1. man
    I think the first command anyone should know is man. Man is short for "manual". This command provides you with details about any command (You can even "man man" from the command). Think of this as the F1 key in your standard Windows installation.

    # man <command>

    Example:
    # man ls

    image

    Type "q" on the keyboard to exit.

  2. ls
    Now that we know how to see what commands do, we need to know how to see files on the file system. The ls command shows you a listing of all files in a given directory. If no directory is specified, the command returns a listing of files and directories in the current working directory (CWD). Specify a directory after ls and receive a listing of that directory.

    # ls <directory,optional>

    Example:
    # ls /etc

    image

  3. vi
    If you want to hang with the pros you should probably take a short amount of time and pick up vi. VI is the Linux text editor of choice for the command line. Think of VI as your quick notepad in Windows. Specify a file after the vi command to open the file. Use the arrow keys to navigate the text file.

    Use the following basic commands to navigate vi:

    a.) /<search string> (searches for a string)
    b.) :w (writes the open file)
    c.) :q! (quit without saving changes)
    d.) :wq (write changes and quit)
    e.) :q (quit vi)

    Type "i" to enter editing mode. Hit escape after making your changes to enter the commands above.

    # vi <file to edit>

    Example:
    # vi /etc/fstab

  4. find
    Have you ever lost it? What if you never had it? Enter find. Find’s only similarity, to that annoying puppy in Windows XP, is that it will find your files. Other than that, this utility does its job effortlessly.

    # find <path> -name <name of file>

    Example:
    # find / -name foo.txt
    (This command finds "foo.txt" anywhere starting with the root "/" of the file system)

    image

  5. cat
    This utility shows the contents of a given file. Think of this command as the Windows equivalent "type". Specify the name of the file you wish to display after the command and … presto!

    # cat <file to output>

    Example:
    # cat /etc/fstab

    image

  6. less
    This command receives input from STDIN (Standard Input) and allows you to page through the output. This is useful with the command mentioned above. What if you have too much data for your shell’s output buffer? You can’t scroll up. Enter less. Simply pipe the output of your program to less and it’ll show you a scrollable, searchable result. Use the space part to page down. Use the arrow keys to navigate up and down and type "/<search string>" to search.

    # <command> | less

    Example:
    # cat /etc/exim.conf | less

    image

  7. grep
    Grep allows you to search a file for a string. It’s very simple to use. After executing your grep, each line is printed which matches your string.

    # grep "string" <file>

    Example:
    # grep "sda" /etc/fstab

    image

  8. rm, cp and mv
    Here are a few commands you can’t live without: rm, cp, and mv.

    Windows Command Linux Equivalent
    del rm
    copy cp
    rename/move mv

    To remove a file use this syntax:
    # rm <file>

    To remove a directory and its files contained:
    # rm -rf <path to directory>

    To copy a file simply use the following syntax:
    # cp <source> <destination>

    To rename or move a file use the following syntax:
    # mv <source> <destination>

  9. ps
    PS or "process list" shows you a list of all running processes. The commonly used (at least for me) command with flags is:

    # ps aux

    This command shows you a list of all processes, which user they are running as and the command (truncated).

  10. chmod/chown
    Linux is a very secure Operating System. Its roots of security lay in its strict ACL (Access Control List) settings. Linux uses chmod (change file access permissions) and chown (change file owner and group) to modify user settings and ownerships.

    There are many ways to use chmod but we’ll explain the simplest.

    # chmod <modes> <file/directory>

    If we were to create a shell script and we wanted to set it executable we would perform the following chmod:

    # chmod +x script.sh

    If we wanted to set the file read/write/execute we could use this command:

    # chmod +rwx script.sh

    Chown simply changes the owner and/or group of the file. The old way of changing the ownerships was like this:

    # chown user.group <file/directory>

    To the best of my knowledge this command works but it is a bit outdated. Now simply replace the "." with ":" so the command looks like this:

    # chown user:group <file/directory>

    I want to change /etc/fstab to be owned by user award and group foogroup. (Don’t do this really hehe!) I would use this command:

    # chown award:foogroup /etc/fstab

Conclusion

There are quite a few commands available in the Linux world. It’s always a good idea to be comfortable with the command line. The command line becomes easier as you use it (I’ve been living in it for almost 10 years now). The power and flexibility the command line offers is unmatched by any GUI. As with everything I recommend that you learn something from the inside out. That being said, start out with the command prompt and play around. Load up a VM (Virtual Machine) with Ubuntu or CentOS and play around. Check out this article to see how to load VMware Server Beta and get started today.

Linux Security: List Listening Ports

There are a few ways to list all ports open and to show which daemons are listening. Simply run one of the two commands:

# lsof -i | grep "(LISTEN)"

Example output

image

-or-

# netstat -tupan | grep "LISTEN"

I believe this to be a cleaner output
image

Using SED to replace newlines with commas

In the Linux world it is often necessary to manipulate data from commands in text form. Here’s a quick and easy way, using sed, to search for newlines in your output and insert commas where needed. This can be particularly useful if you intend on piping your output to an array for a script.

 

 

 

# sed -n ‘1h;2,$H;${g;s/\n/,/g;p}’ file.txt

Pretty ugly huh? That will get the job done with little effort. If you wish to replace the newlines in the file just use the -i flag with sed and your file will automagically be updated.

How to clear ESM logs in OpenManage

Hey all,

Sometimes it can be useful to clear the ESM logs in OpenManage if you do not have direct access to OMSA as a root user. To perform this action from the command line just enter the following:

omconfig system esmlog action=clear

The above command clears the ESM (hardware) logs in OpenManage. Sometimes this can clear amber lights on the front of the Dell servers.

Disable Console Screen Saver

By default most Linux installations come with the console screen saver on. This is generally a bad idea for a server system because if you do experience kernel panic issues you will not be able to see what caused the panic. The screen will remain blank.

Put the following lines in your /etc/rc.d/rc.local or in your login scripts:

setterm -blank 0
setterm -store

If you use X-Windows append one more line:

xset -dpms

This should allow for viewing of the screen in the even of kernel panic.

CPU2, CPU3 Machine Check Exception

Hey everyone, normally I don’t have issues with hardware in any production systems but, on occasion, I do see a few hardware failures. I would say the primary hardware failure I see on a day-to-day basis is hard disk failures. Today, however, I experienced a Machine Check Exception which had be puzzled. After turning to Google I found that this is most typically (at least on Linux) bad cache on the processor. This particular system was a dual processor system so I had virtually nothing to worry about if I had to fly solo until the part arrived.

The Machine Check Exception looks like this:

CPU 3: Machine Check Exception 0000000000000005
Bank 0: b200004010000400
Bank 5: b200121020000400

On Windows systems a MCE (Machine Check Exception) could also mean bad RAM, Motherboard or Processor. Reseating the processor is also another good option if you experience this error message.

Happy Linuxing!

NoClassDefFoundError – java.awt.Color

Hey everyone,

I recently ran in to an issue where calling functions from java.awt.Color caused a "NoClassDefFoundError" in the JSP page. I restarted resin and kept refreshing this JSP page. I saw a different error message that looked like this:

java.lang.UnsatisfiedLinkError: /usr/java/j2sdk1.4.2_02/jre/lib/i386/libawt.so:
libXp.so.6: cannot open shared object file: No such file or directory
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1560)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1477)
        at java.lang.Runtime.loadLibrary0(Runtime.java:788)
        at java.lang.System.loadLibrary(System.java:834)
        at sun.security.action.LoadLibraryAction.run(LoadLibraryAction.java:50)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.awt.Toolkit.loadLibraries(Toolkit.java:1437)
        at java.awt.Toolkit.<clinit>(Toolkit.java:1458)
        at java.awt.Color.<clinit>(Color.java:250)
        at _map._test2__jsp._jspService(/map/test2.jsp:5)
        at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
        at com.caucho.jsp.Page.subservice(Page.java:506)
        at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
        at com.caucho.server.http.Invocation.service(Invocation.java:315)
        at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:346)
        at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
        at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
        at java.lang.Thread.run(Thread.java:534)

This error was much different from the previous error but shows that when AWT was trying to initialize it could not locate "libXp.so.6". Through some more research I found that libXp.so.6 was part of the "xorg-x11-depreciated-libs" package in CentOS 4.5. I issued a "yum -y install xorg-x11-depreciated-libs" and a "ldconfig" to be safe and restarted resin. My java.awt.Color functions seemed to work perfectly after this.

Hopefully this helps someone!

The power of “lsof”

One great command to add to your arsenal is the “lsof” command. This command prints out all open files in Linux.

lsof can be used to help resolve issues like:

  • Can’t unmount device because it is busy; even if you believe it’s not busy.
    umount: /mountpoint: device is busy
  • A process is using a file but you have no idea which process
  • To view a list of active connections (netstat works better for this) and which program and PID (Process ID) is using this socket.

To successfully unmount a device which still complains about being in use simply run the following command:

# lsof | grep “/mountpoint”

This command returns a list of processes and associated PID’s and the user which has that directory or files open. Look for files (usually marked with “REG”) which will allow you to locate the service or program with the file open. Stop this service or at the very extreme kill -9 that process. (A funny video about kill -9)

To search for a file which is in use simply use an alteration of the command above:

# lsof | grep “openfile”

This allows you to locate the process and user using that file.

To view a list of active connections run this command:

# lsof | grep “IPv4”

This returns a list of all open IPv4 connections.

Also be aware that the “lsof” command can take quite some time to run on servers with very large file counts open (Oracle Servers, Web Servers) so please be patient. It’s not uncommon for the lsof command to take about 2-4 seconds to run.

Happy Linuxing!

How to read the iostat command

I believe that most performance issues related to slowness occur because of slow disks or poor application tuning. Memory is a big factor when it comes to OS-level caching and buffering but there’s nothing like a fast SCSI array or even a few WD Raptors in RAID-1.

The Linux utility "iostat" allows you to see a complete overview of disk utilization. The iostat utility does this by looking at the time the device is active in relation to the devices average transfer rate.

Using the iostat utility with the -x flag (-x is for extended statistics) will yield results that look like this:

image

If the iostat command is not available on your system perform one of the following commands to install the sysstat package.

CentOS/RHEL – # yum -y install sysstat
Ubuntu/Debian – # apt-get install sysstat

Pay special attention to the "%util" column of the results. In the example above the percentage of CPU time for I/O requests for /dev/sdb is quite high. This device is actually a large RAID-6 array and has not yet reached its 100% utilization mark. The closer the device or array is to 100% the closer you are to total saturation of that device.

If your utilization numbers are higher than expected take the following into consideration:

  • Tune the application (This is where you can gain the cheapest and most performance)
  • Obtain faster disks (10K+ SATA/SAS/SCSI)
  • Use a larger and more efficient RAID array for your application (RAID 0 for video editing, RAID-10 for databases, RAID-5 for file storage and general access and RAID-6 on newer controllers for increased redundancy)

Happy Linuxing!

OpenSource Mail Archiving

Hey everyone. I ran across a new open-source application called Mailarchiva. This software allows you to archive all of your email for long term storage. This software is easy to install and seems to be very efficient. The company claimed that the open source edition with a decent server (Dual Xeons and sufficient RAM) could archive 1,400,000 messages per day. I was very impressed with their performance claims.

image Mailarchiva provides full-text searching. The enterprise edition will allow for clustering search servers if your archive is significantly large. One main feature missing was the ability to rotate to long term storage such as a tape device. Although disk storage is becoming cheaper and cheaper; a long term storage solution almost always is needed.

Check out MailArchiva here and download the open-source version today. MailArchiva integrates with Exchange 2003, 2007, IpSwitch Imail, Postfix, sendmail, qmail, exim and more!