How to Check Memory Use by Applications

 Command Line, Linux  Comments Off on How to Check Memory Use by Applications
Apr 232015
 

Once in a while I run into a situation where I feel one of the work computers running Linux (of course) might be running slow.

4GB Memory for Desktop PCs

The first thing I check is the memory installed and utilized through the use of free -m command.

The Free command is not perfect but good enough.

thomas@workpc ~ $ free -m
             total       used       free     shared    buffers     cached
Mem:          4798       1701       3097        147         70       1147
-/+ buffers/cache:        483       4315
Swap:         3930          0       3930

The data column under free in the second line is what you should look for.

With 4.31GB “free” under that column, no issues there!

Applications & Memory Use

Next, I look at applications that are drawing the most memory.

To check memory used by various applications, a plethora of options are available on the command line.

You can check memory use by all applications and processes or examine just the memory used by the top-5, top-10 or top-20 applications.

Here are a few commands to help you quickly check memory used by different applications. I have tested them on LinuxMint 17 but they should work on other Linux distros too.

1. Memory, CPU & others

The below command is one of my favorites because besides memory, it also provides CPU use and the PID (process ID).

$ ps -eo pmem,pid,pcpu,rss,vsz,time,args | sort -k 1 -r

2. Top 20 Processes
If you’re looking for memory use by the top 20 processes, go with the below command.

Unlike some of the other commands, this one will give you only memory use and percentage.

$ ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20

3. Top 10 Processes
Now if you’re looking for memory use by the top 10 processes, run either of the below commands.

ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 10

or

$ ps aux --sort=-%mem | awk 'NR<=10{print $0}'

4. Top 5 Processes
If you’re looking for memory use by just the top five processes, issue the below command in the terminal.

$ ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5

5. Top is Tops
For a lot of folks, the top command is tops when it comes to checking memory and other parameters of their computer.

Run top
Once inside top, press m

A big plus is that top comes installed with every Linux computer.

6. Htop Tops Top

Lately, htop, a gussied up, ‘colored’ version of top, has drawn a lot of defectors from top.

$ htop

On Debian (or its spawns like Ubuntu and Linux Mint you can install htop easily:

$ sudo apt-get install htop

Go ahead and try out these commands on your Linux system and see which of your applications is grabbing the most memory.

10 Crucial Yum Commands for CentOS 7

 Command Line, Linux  Comments Off on 10 Crucial Yum Commands for CentOS 7
Apr 222015
 

Yum (Yellowdog Updater, Modified) is at the core of the CentOSRed Hat and Fedora package management systems.

On Linux distros like CentOS 7, RedHat 7 or Fedora 21, if you want to update, install or remove packages, list installed packages or update the entire system while at the same time ensuring automatic dependency resolution then the command line tool you must deploy is yum.

Yum also comes in handy while enabling or disabling repositories (package sources).

To update, install or remove packages via yum, you must have superuser privileges (via su or sudo).

Now let’s take a look at some of the key Yum commands you ought to be familiar for CentOS 7.

1. Check for Updates

$ yum check-update

The above command only checks for and lists available updates but it will not install them.

There’s no need to be logged in as a superuser to check for available updates.

2. Update a Single Package

More often not, you’ll be required to update only a single package, not all the packages.

Use the below command if you wish to update just a single package and you know its name.

$ yum update package_name

3. Update All Packages

If you intend to update all packages along with their dependencies, go with either of the below commands.

$ yum update

or

$ yum upgrade

I usually go with yum update if I’m updating multiple packages.

4. Search Packages

You can check enabled repositories for packages you wish to install on your CentOS 7 or RedHat 7 system.

There’s no need to know the name of the package during the search.

Let’s say you heard about a good RSS package but forgot its name. So just use rss in the search string.

$ yum search search_string

Search Multiple Packages
Yum lets you search for multiple packages simultaneously.

In the below example, I’m searching for the Cherrytree notes app and the Quiterss RSS reader. Continue reading »

Linux Software Installation Made Easy

 Command Line, How To, Linux  Comments Off on Linux Software Installation Made Easy
Mar 122015
 

Don’t let Windows or Mac users scare you that adding or removing software on a Linux computer is only for bravehearts.

Installing, upgrading or removing software on Linux systems is no longer the daunting chore it used to be some years back.

Broadly speaking, there are two ways to install software packages on a Linux system.

You can install software packages either through a graphical user interface (GUI) or do it via the command line.

Major desktop Linux distros like Fedora Workstation 21, Linux Mint and Ubuntu now have slick GUIs that make searching for and installing new software a breeze.

The big advantage with GUI based installers is that they take care of dependency resolution so newbies won’t have to fret whether the package they’re installing or upgrading will work without having to install other software.

But you can be sure that hardcore Linux enthusiasts and system administrators will always opt for the command line to install, upgrade or remove software.

In large business environments (such as a web hosting companies) where headless servers are often the norm, Linux system administrators work exclusively on the command line.

Command Line Installation

On the command line, two kinds of tools (low level and high level) are available to install software packages on a Linux system.

The first method (dpkg for Debian and rpm for Red Hat) does not resolve dependencies and are therefore considered low level tools.

The high level installation tools (apt-get or yum) take care of dependencies.

In case you didn’t know, dependencies are other software that an application requires before it can work.

dpkg and rpm are low-level tools while apt-get and yum are considered high-level tools because they take care of dependencies as part of the installation process.

In this post, we’ll look at installing and removing software packages on both Debian based distributions and Red Hat style distros (like Fedora, CentOS and OpenSUSE that follow the Red Hat methodology) via the command line.

As you no doubt guessed, the commands for Debian and Red hat are completely different.

So we’ll have to consider them separately to avoid confusion.

We’ll consider Debian and its derivative distros first.

Installation on Debian

Commands mentioned in this section should work on Debian, Ubuntu, Linux Mint, Xandros and more.

Install a Package

Linux users opt for dpkg with software that are not downloaded from a repository.

Here’s how you install a package with dpkg.

# dpkg -i package_name

When you install a .deb package via the low-level dpkg tool, be aware that there is no dependency resolution.

So if the package you’re installing requires other software you’re stuck.

To avoid dependency issues, you must use higher-level tools like apt-get to install software on Debian and its derivatives. Strictly speaking, this is not always possible because not all packages are in repositories.

When you use the high level apt-get install tool, you’re downloading software from a repository.

The first step is to run update and then search for the package you’re interested in.

# apt-get update
# apt-cache search search_string

If you’re looking for an RSS reader, you’d run the below command first.

# apt-cache search rss

That’s how I discovered RSS readers like Quiterss and Liferea.

Finally, when you’ve found the package you want just install it with the following command.

You can skip the search step if you already know the package you want to install.

# apt-get install package_name
List Packages Installed

Your Ubuntu or Linux Mint computer has dozens of packages installed on it.

If you want the entire lengthy list, go with the following command.

$ dpkg -l

I recommend you save the output to a text file for leisurely analysis later.

$ dpkg -l > MyPackageList.txt
Total Packages Installed

Say you want to find out how many packages are installed on a Linux system without the long list of all the individual packages.

You use the dpkg with the -l option and then pipe it to wc -l.

On my Ubuntu system, I was curious about the total packages installed.

Here’s what I did:

$ dpkg -l | wc -l
2215
Remove a Package

Removing a package on a Debian, Ubuntu or Linux Mint computer is no sweat with the below command.

# dpkg -r package_name

Alternatively, if it’s a package downloaded from a repository you should go with either of the following commands.

# apt-cache remove package_name

But the above command leaves behind configuration files.

If your goal is to remove the configuration files too, you must use the purge option.

apt-cache purge package_name
Information About a Specific Package

If you’re looking for more information on a specific package, what command would you run?

In the below example, I sought information on the popular Cherrytree notes application.

$ dpkg -l cherrytree | tail -1 | tr -s ' '
ii cherrytree 0.35.7-1~ppa1~trusty1 all hierarchical note taking application
Verify if Package is Installed

You can check if a particular software package is installed on your Debian or Ubuntu computer.

Say you want to see if an rss package is installed on your Ubuntu computer.

Here’s the command you ought to run:

$ dpkg -l | grep rss
ii  quiterss  0.17.6-0ubuntu1~trusty  amd64 RSS/Atom feed reader written on Qt

If quiterss weren’t installed on my Ubuntu PC, I wouldn’t have got any response.

Red Hat & Red Hat Syle Distros

Let’s now take a dekko at how to install software on Red Hat and its derivative distributions like CentOS and Fedora.

Red Hat is the favorite Linux distro for scores of large corporations around the world. Continue reading »

How to Create Files Via Terminal

 Command Line  Comments Off on How to Create Files Via Terminal
Mar 042015
 

In this brief post, we’ll look at a few ways to quickly create files via the command line.

1. Use Redirection Operator >

Tammy@JacobPC $ > Test-File.txt

The above example will create a file called Test-File.txt in your current working directory.

What if the file already existed? In that case, the file would be truncated to zero bytes, which means it’ll be overwritten silently.

Important: So be careful and ensure that you don’t already have this file before you try this command.

Tammy@JacobPC $ >> Test-File.txt

2. Use the Append Command >>

Usually, the >> command is used when you wish to append the output to an existing file instead of overwriting it.

But if the file does not already exist, a new file is created.

Tammy@JacobPC $ >> Test-File.txt

3. Create Files with Content

Using the command line, you can also create a file with some content.

This is useful for creating short files when you’re in a hurry and works like a rudimentary word processor.

Tammy@Jacob $ cat > Test-File.txt
Jack and Jill went up the hill

Once you’re done, type Ctrl-D to signal cat the end of standard input

You can test the file you just created with the following command:

Tammy@JacobPC $ cat Test-File.txt
Jack and Jill went up the hill

4. Touch a File

Another way to create an empty file is via the touch command.

Here’s an example:

$Tammy@JacobPC touch Test-File.txt

The above command will create a file called Test-File.txt in your current working directory.

Alternatively, you can specify the directory where you want the new file to created.

$Tammy@JacobPC touch /home/Jackie/Documents/Test-File.txt

The marvels of Linux and the command line never cease to amaze me.

Master lsof Basics in 10 Minutes

 Command Line  Comments Off on Master lsof Basics in 10 Minutes
Feb 262015
 

A powerful command in the tool box of Linux (and Unix) systems administrators, lsof stands for list of open files.

Because of the sheer number of options available for this command line utility, some systems administrators prefer to use lsof instead of netstat or ps.

For those aspiring to a career in Linux systems administration, I recommend keeping lsof in your arsenal of frequently used command line tools.

Besides producing a single output list, lsof can also run in repeat mode.

For a complete list of options, I encourage you to check the lsof man pages at your leisure.

Without further ado, let’s take a deko at the key lsof commands.

1. Basic Command

If you don’t specify an option, lsof will list all open files belonging to all active processes.

And that’s a pretty long list of open files, I can assure you.

$ lsof

Just for the heck of it, I ran the basic lsof command and used the pipeline to count the number of lines via the wc command. Take a look at the output below:

$ lsof | wc -l
11814

Wow! That’s a lot of open files.

2. Display All Open Internet Sockets

The following command lists all connections (both listening and established).

lsof -i

3. List Processes Running on a Specific Port

For a list of all open Internet sockets on port 80, run the below command

lsof -i :80

I find the above command extremely useful because I can then use the PIDs to kill suspect or malicious IPs (from Brazil, China, Ukraine, Portugal etc) by running the below command:

kill -9 PID

Similarly, you can see open sockets on other ports too.

lsof -i :22

Let’s look at the SMTP port next.

lsof -i :25

4. List All udp or tcp Connections

lsof -i udp

If you want to display tcp connections, run the below command:

$ lsof -i tcp

Alternatively, run this command:

$ lsof -i tcp:80

5. List Files Opened by a Specific User

To see files opened by an user, we must use the -u option. Here’s the syntax:

$ lsof -u user_name

Now here’s an example:

$ lsof -u larry

Don’t be surprised to see a lengthy list of files.

6. List All Open Files Belonging to Processes Not Owned by a Specified User

The below command will list files opened by users other than Larry.

$ lsof -u ^larry

7. Lists only IPv4 or IPv6 Open Files

$ lsof -i 4

If you’re interested in IPv6 files, go with the below command:

$ lsof -i 6

8. Kill Everything a Specific User is Doing

You can use lsof to kill processes belonging to a particular user.

# kill -9 `lsof -t -u larry`

9. List Established Connections

We can restrict lsof to spit out only established connections with the following command.

$ lsof -i | grep ESTABLISHED

10. List All Open Files by a Specific Process

$ lsof -p PID

Here’s an example:

$ lsof -p 3672

11. List All Open Files EXCEPT Process with PID

$ lsof -p ^1

12. Display Opened Files Based on Process Names

You can use the process name or the first letter (say c, s or m) if you want to see all processes starting with hat letter.

$ lsof -c process_name

Here’s an example:

$ lsof -c ssh

13. List processes which Opened a Specific File

With the below command, you can list only those processes that opened a specific file, by using filename as arguments.

$ lsof /var/log/syslog

14. List Opened Files Under a Directory

$ lsof +D /var/log

15. Repeat Listing Files every 1 or 2 or 3 Seconds

You can get lsof to run at specified intervals of time.

$ lsof -r 2

In the above example, lsof will repeat every 2 seconds.

Rich Options for lsof

As we’ve noted at the outset, lsof comes with a bewildering array of options.

Treat the above examples as an lsof appetizer.

I hope they have whetted your appetite sufficiently enough to want to explore lsof in greater detail by checking the man pages.

WC – What Can You Do With It?

 Command Line  Comments Off on WC – What Can You Do With It?
Feb 262015
 

Let’s discuss about the wc command line tool today.

wc, which stands for word count, prints the count of lines, words and bytes for a file.

Here’s the basic syntax for wc:

wc [OPTION]… [FILE]…

By the way, wc can be used in command line pipelines as well. We’ll get to that toward the end of this post.

Let’s start by trying a few options that come with wc.

Before we do that, we’ll create a text file to use in our examples.

The text file we created is called testing-wc.txt.

$ cat > testing-wc.txt
Ingrid Bergman
Humphrey Bogart
Lauren Bacall
Cary Grant
Gregory Peck
Audrey Hepburn
Ava Gardner
Brad Pitt
Angelina Jolie
Julie Andrews
Shahrukh Khan
Hema Malini
Woody Allen
Christoph Waltz
Gérard Depardieu

1. Basic WC Command

The plain wc command without any options will provide the count for words, lines and characters.

$ wc testing-wc.txt
15  30 207 testing-wc.txt

2. Display Word Count

If you’re looking to print just the word count, use the -w option.

$ wc -w testing-wc.txt
30 testing-wc.txt

3. Display Line Count

The -l option prints out the line count.

$ wc -l testing-wc.txt
15 testing-wc.txt

4. Display Character Count

With -m option, you can display the character count.

$ wc -m testing-wc.txt
206 testing-wc.txt

5. Display Byte Count

Using the -c option gets you the byte count of the file. Continue reading »