One place for hosting & domains

      An Overview of Open Source Data Visualization Tools


      Updated by Linode Contributed by Mihalis Tsoukalos

      Creating graphic visualizations for a data set is a powerful way to derive meaning from vast amounts of information. It provides a way to extract meaningful relationships between different aspects of your data depending on how the data is mapped and which graphic representations are chosen. Data visualization is a common practice in many sectors, including various scientific disciplines, business settings, the government sector, and education.

      There are many open source tools available to create sophisticated data visualizations for complex data sets. This guide will provide an introductory exploration of data analysis and 2D graphics rendering packages that can be used with R, Python, and JavaScript to generate data visualizations.

      In this guide you will complete the following steps:

      Before You Begin

      Ensure you have the following programs and packages installed on your computer:

      1. The R programming language
      2. The RStudio Desktop application
      3. Python 3.7.0 or higher
        1. pandas Python package
        2. Matplotlib Python package
        3. wordcloud Python package
      4. The Golang programming language

      Assumptions

      This guide assumes you have some basic familiarity with the following concepts and skills:

      1. Basic programming principles and data structures
      2. Are able to read code written in Go, Python, HTML, CSS, and JavaScript

      Create Your Data Sets

      In this section, you will create a data set using the contents of your Bash history file and optionally, your Zsh history file. You will then create a third data set using a Perl script that will extract information from the first two data sets. In the Create Visualizations for your Data section of the guide, you will use these various data sets to create corresponding visualizations.

      Data Set 1 – Bash History File

      A Bash history file stores all commands executed in your command line interpreter. View your 10 most recently executed commands with the following command:

      head ~/.bash_history
      

      Your output should resemble the following:

        
      git commit -a -m "Fixed Constants links"
      git push
      git diff
      docker images
      brew update; brew upgrade; brew cleanup
      git commit -a -m "Cleanup v2 and v3"
      git push
      git commit -a -m "Added examples section"
      git push
      cat ~/.lenses/lenses-cli.yml
      
      

      Create a new directory named data-setsto store your data and copy your Bash history file to the directory:

      mkdir data-sets && cp ~/.bash_history data-sets/data-1
      

      Data Set 2 – Zsh History File

      If you are using the Zsh shell interpreter, you can use its history file as a second data set. Zsh’s history file format includes data that you will need to exclude from your data set. Use AWK to clean up your Zsh history file and save the output to a new file in the data-sets directory:

      awk -F ";" '{$1=""; print $0}' ~/.zsh_history | sed -e "s/^[ t]*//" -e "/^$/d" > data-sets/data-2
      

      Data Set 3 – Perl Script

      To create your third data set, you will use a Perl script that categorizes the contents of your data set files. The categorization is based on the word count for each line of text in your data files or, in other words, the length of each command stored in your Bash and Zsh history files. The script creates 5 categories of command lengths; 1 – 2 words, 3 – 5 words, 6 – 10 words, 11 – 15 words, and 16 or more words.

      1. Create a file named command_length.pl in your home directory with the following content:

        ~/command_length.pl
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        58
        59
        60
        61
        62
        63
        
        #!/usr/bin/perl -w
        
        use strict;
        
        my $directory = "";
        my $filename = "";
        
        my $CAT1 = 0;
        my $CAT2 = 0;
        my $CAT3 = 0;
        my $CAT4 = 0;
        my $CAT5 = 0;
        
        if ( @ARGV != 1 ) {
           die <<Thanatos
              usage info:
                 Please use exactly 1 argument!
        Thanatos
        }
        
        ($directory) = @ARGV;
        opendir(BIN, $directory)
            || die "Error opening directory $directory: $!n";
        
        while (defined ($filename = readdir BIN) ) {
            # The following command does not process . and ..
            next if( $filename =~ /^..?$/ );
            process_file($directory."/".$filename);
        }
        
        print "Category1tt$CAT1n";
        print "Category2tt$CAT2n";
        print "Category3tt$CAT3n";
        print "Category4tt$CAT4n";
        print "Category5tt$CAT5n";
        exit 0;
        
        sub process_file {
            my $file = shift;
            my $line = "";
        
            open (HISTORYFILE, "< $file")
                || die "Cannot open $file: $!n";
        
            while (defined($line = <HISTORYFILE>)) {
                chomp $line;
                next if ( ! defined($line) );
                check_category($line);
            }
        }
        
        sub check_category {
            my $command = shift;
            chomp $command;
            my $length = length($command);
        
            if ( $length <= 2 ) { $CAT1 ++; }
            elsif ( $length <= 5 ) { $CAT2 ++; }
            elsif ( $length <= 10 ) { $CAT3 ++; }
            elsif ( $length <= 15 ) { $CAT4 ++; }
            else { $CAT5 ++; }
        }
            
      2. Run the Perl script. The script expects a single argument; the directory that holds all data files that you want to process. The file’s output will be saved in a new file command_categories.txt. The command_categories.txt file will be used later in this guide to create visualizations using R.

        ./command_length.pl data-sets > ~/command_categories.txt
        

        Note

        Your Perl script must be executable in order to run. To add these permissions, execute the following command:

        chmod +x command_length.pl
        

        Open the .command_categories.txt file to view the categorizations created by your Perl script. Your file should resemble the following example:

        command_categories.txt
        1
        2
        3
        4
        5
        6
        7
        
        "Category Name" "Number of Times"
        Category1 5514
        Category2 2381
        Category3 2624
        Category4 2021
        Category5 11055
            

      You now have three sources of data that you can use to explore data visualization tools in the next sections.

      • ~/data-sets/data-1
      • ~/data-stes/data-2
      • ~/command_categories.txt

      Create Visualizations for your Data

      In this section you will use the data sets you created in the previous section to generate visualizations for them.

      Visualize your Data with R and RStudio

      R is a specialized programming language used for statistical computing and graphics. It is especially good for creating high quality graphs, like density plots, line charts, pie charts, and scatter plots. RStudio is an integrated development environment (IDE) for R that includes debugging and plotting tools that make it easy to write, debug, and run R scripts.

      In this section, you will use the command_categories.txt file created in the Data Set 3 – Perl Script section and RStudio to create a pie chart and simple spreadsheet of your data.

      1. Open RStudio Desktop and create a data frame. In R, a data frame is a table similar to a two-dimensional array.

        DATA <- read.table("~/command_categories.txt", header=TRUE)
        
        • This command will read the command_categories.txt file that was created in the Data Set 3 – Perl Script section of the guide and create a data frame from it that is stored in the DATA variable.

        • The header=TRUE argument indicates that the file’s first row contains variable names for column values. This means that Category Name and Number of Times will be used as variable names for the two columns of values in command_categories.txt.

      2. Next, create a pie chart visualization for each column of values using R’s pie() function.

        pie(DATA$Number.of.Times, DATA$Category.Name)
        
        • The function’s first argument, DATA$Number.of.Times and DATA$Category.Name, provides the x-vector numeric values to use when creating the pie chart visualization.

        • The second argument, DATA$Category.Name, provides the labels for each pie slice.

        RStudio will display a pie chart visualization of your data in the Plots and Files window similar to the following:

        Pie Chart

      3. Visualize your data in a spreadsheet style format using R’s View() function.

        View(DATA, "Command Lengths")
        

        RStudio will display a spreadsheet style viewer in a new window pane tab.

        Spreadsheet

      Explore R’s graphics package to discover additional functions you can use to create more complex visualizations for your data with RStudio.

      Create a Word Cloud using Python

      Word clouds depict text data using varying font sizes and colors to visually demonstrate the frequency and relative importance of each word. A common application for word clouds is to visualize tag or keyword relevancy. In this section you will use Python3 and your Bash and Zsh history files to generate a word cloud of all your shell commands. The Python packages listed below are commonly used in programs for data analysis and scientific computing and you will use them to generate your word cloud.

      1. Create a file named create_wordcloud.py in your home directory with the following content:

        ~/create_wordcloud.py
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        
        #!/usr/bin/env python3
        
        import pandas as pd
        import matplotlib.pyplot as plt
        import sys
        import os
        from wordcloud import WordCloud, STOPWORDS
        from random import randrange
        
        path = sys.argv[1]
        
        if os.path.exists(path):
            print("Creating word cloud for file: " + path)
        
            data = pd.read_table(path, header = None, names=["CMD"], encoding = "iso-8859-1")
        
            skipWords = []
            skipWords.extend(STOPWORDS)
        
            words = ''.join(data['CMD'])
        
            w = WordCloud(
                    stopwords=set(skipWords),
                    background_color='gray',
                    width=4000,
                    height=2000
                    ).generate(words)
            filename = "word_cloud_" + str(randrange(100)) + ".png"
            print("Your wordcloud's filename is: " + filename)
        
            plt.imshow(w)
            plt.axis("off")
            plt.savefig(filename, dpi=1000)
        
        else:
            print("File" + path +  "does not exist")
            
      2. Run your Python script and pass the path of one of your data set files as an argument. The script will read the contents of the file using panda’s read_table() function and convert it into a data frame with a column name of CMD. It will then use the data in the CMD column to create a concatenated string representation of the data that can be passed to wordcloud to generate a .png wordcloud image.

        ./create_wordcloud.py ~/data-sets/data-1
        

        You should see a similar output from the Python script:

          
        Creating word cloud for file: /Users/username/data-sets/data-2
        Your word cloud's filename is: word_cloud_58.png
            
        
      3. Open the word_cloud_58.png image file to view your word cloud.

        Word Cloud

      You could use a similar process to create a word cloud visualization for any text you want to analyze.

      Visualize Data using D3.js

      D3.js is a JavaScript library that helps you visualize JSON formatted data using HTML, SVG, and CSS. In this section you will us D3.js to create and embed a pie chart visualization into a web page.

      To convert your data set into JSON, you will create a Golang command line utility that generates JSON formatted plain text output. For more complex data sets, you might consider creating a similar command line utility using Golang’s json package.

      1. Create a file named cToJSON.go in your home directory with the following content:

        ./cToJSON.go
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        58
        59
        60
        61
        62
        63
        64
        65
        66
        67
        68
        69
        70
        71
        72
        73
        74
        75
        76
        77
        78
        79
        80
        81
        82
        83
        84
        85
        86
        87
        88
        89
        90
        91
        92
        
        package main
        
        import (
            "bufio"
            "flag"
            "fmt"
            "io"
            "os"
            "regexp"
            "sort"
        )
        
        var DATA = make(map[string]int)
        
        func lineByLine(file string) error {
            var err error
            f, err := os.Open(file)
            if err != nil {
                return err
            }
            defer f.Close()
        
            r := bufio.NewReader(f)
            for {
                line, err := r.ReadString('n')
                if err == io.EOF {
                    break
                } else if err != nil {
                    fmt.Printf("error reading file %s", err)
                    break
                }
        
                r := regexp.MustCompile("[^\s]+")
                words := r.FindAllString(line, -1)
                if len(words) == 0 {
                    continue
                }
        
                if _, ok := DATA[words[0]]; ok {
                    DATA[words[0]]++
                } else {
                    DATA[words[0]] = 1
                }
        
            }
            return nil
        }
        
        func main() {
            flag.Parse()
            if len(flag.Args()) == 0 {
                fmt.Printf("usage: cToJSON <file1> [<file2> ...]n")
                return
            }
        
            for _, file := range flag.Args() {
                err := lineByLine(file)
                if err != nil {
                    fmt.Println(err)
                }
            }
        
            n := map[int][]string{}
            var a []int
            for k, v := range DATA {
                n[v] = append(n[v], k)
            }
        
            for k := range n {
                a = append(a, k)
            }
        
            fmt.Println("[")
            sort.Sort(sort.Reverse(sort.IntSlice(a)))
        
            counter := 0
            for _, k := range a {
                if counter >= 10 {
                    break
                }
        
                for _, s := range n[k] {
                    if counter >= 10 {
                        break
                    }
                    counter++
                    fmt.Printf("{"command":"%s","count":%d},n", s, k)
                }
            }
            fmt.Println("];")
        }
            
        • The utility expects file paths to your Bash and Zsh data sets as arguments.
        • It will then read the files and find the 10 most popular commands and output it as JSON formatted data.
        • Several Golang standard library packages are used in the utility to perform operations liking reading files, using regular expressions, and sorting collections.
      2. Run the command line utility and pass in the paths to each command history data set:

        go run cToJSON.go data-set/data-1 data-set/data-2
        

        Your output should resemble the following:

          
        [
        {"command":"ll","count":1832},
        {"command":"git","count":1567},
        {"command":"cd","count":982},
        {"command":"brew","count":926},
        {"command":"unison","count":916},
        {"command":"gdf","count":478},
        {"command":"ssh","count":474},
        {"command":"rm","count":471},
        {"command":"sync","count":440},
        {"command":"ls","count":421},
        ];
            
        

        You are now ready to create your pie chart visualization and embed it into a web page using D3.js

      3. Create an HTML file named pieChart.html and copy and paste the following content. The DATA variable on line 31 contains the JSON data that was created by the cToJSON.go script in the previous step. Remove the JSON data in the example and replace it with your own JSON data.

        Note

        In this example, your JSON data is hardcoded in pieChart.html for simplicity. Web browser security constraints restrict how a document or script loaded from one origin can interact with a resource from another origin. However, you may consider using the d3-fetch module to fetch your JSON data from a specific URL.
        ~/pieChart.html
          1
          2
          3
          4
          5
          6
          7
          8
          9
         10
         11
         12
         13
         14
         15
         16
         17
         18
         19
         20
         21
         22
         23
         24
         25
         26
         27
         28
         29
         30
         31
         32
         33
         34
         35
         36
         37
         38
         39
         40
         41
         42
         43
         44
         45
         46
         47
         48
         49
         50
         51
         52
         53
         54
         55
         56
         57
         58
         59
         60
         61
         62
         63
         64
         65
         66
         67
         68
         69
         70
         71
         72
         73
         74
         75
         76
         77
         78
         79
         80
         81
         82
         83
         84
         85
         86
         87
         88
         89
         90
         91
         92
         93
         94
         95
         96
         97
         98
         99
        100
        101
        102
        103
        104
        105
        
        <!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="utf-8">
            <title>History Visualization</title>
        
            <style type="text/css">
              * { margin: 0; padding: 0; }
        
              #chart {
                background-color: white;
                font: 14px sans-serif;
                margin: 0 auto 50px;
                width: 600px;
                height: 600px;
              }
              #chart .label{
                fill: #404040;
                font-size: 12px;
              }
            </style>
          </head>
        
          <body>
            <div id="chart"></div>
          </body>
        
          <script src="https://d3js.org/d3.v3.min.js"></script>
          <script type="text/javascript">
        
          var DATA = [
              {"command":"ll","count":1832},
              {"command":"git","count":1567},
              {"command":"cd","count":982},
              {"command":"brew","count":926},
              {"command":"unison","count":916},
              {"command":"gdf","count":478},
              {"command":"ssh","count":474},
              {"command":"rm","count":471},
              {"command":"sync","count":440},
              {"command":"ls","count":421}
              ];
        
            var width  = 600;
                height = 600;
                radius = width / 2.5;
        
            var pie = d3.layout.pie()
                        .value(function(d) { return d.count; })
        
            var pieData = pie(DATA);
            var color = d3.scale.category20();
        
            var arc = d3.svg.arc()
                        .innerRadius(0)
                        .outerRadius(radius - 7);
        
            var svg = d3.select("#chart").append("svg")
                        .attr("width", width)
                        .attr("height", height)
                        .append("g")
                        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
        
            var ticks = svg.selectAll("line")
                           .data(pieData)
                           .enter()
                           .append("line");
        
            ticks.attr("x1", 0)
                 .attr("x2", 0)
                 .attr("y1", -radius+4)
                 .attr("y2", -radius-2)
                 .attr("stroke", "black")
                 .attr("transform", function(d) {
                   return "rotate(" + (d.startAngle+d.endAngle)/2 * (180/Math.PI) + ")";
                 });
        
            var labels = svg.selectAll("text")
                            .data(pieData)
                            .enter()
                            .append("text");
        
            labels.attr("class", "label")
                  .attr("transform", function(d) {
                     var dist   = radius + 25;
                         angle  = (d.startAngle + d.endAngle) / 2;
                         x      = dist * Math.sin(angle);
                         y      = -dist * Math.cos(angle);
                     return "translate(" + x + "," + y + ")";
                   })
                  .attr("dy", "0.35em")
                  .attr("text-anchor", "middle")
                  .text(function(d){
                    return d.data.command + " (" + d.data.count + ")";
                  });
        
            var path = svg.selectAll("path")
                          .data(pieData)
                          .enter()
                          .append("path")
                          .attr("fill", function(d, i) { return color(i); })
                          .attr("d", arc);
          </script>
        </html>
        
      4. Navigate to your preferred browser and enter the HTML file’s absolute path to view the pie chart. For a macOS user that has stored the HTML file in their home directory, the path would resemble the following: /Users/username/pieChart.html

        JS Pie Chart

      Next Steps

      Now that you are familiar with some data visualization tools and simple techniques, you can begin to explore more sophisticated approaches using the same tools explored in this guide. Here are a few ideas you can consider:

      • Create a new data set by extracting all git related commands from your history files; analyze and visualize them.
      • Automate some of the techniques discussed in this guide using Cron jobs to generate your data sets automatically.
      • Explore the Python for Data Science eBook’s data visualization section for a deeper dive into using pandas.

      Find answers, ask questions, and help others.

      This guide is published under a CC BY-ND 4.0 license.



      Source link

      How to List Open Files with lsof


      Updated by Linode

      Contributed by

      Mihalis Tsoukalos

      Introduction

      lsof was created by Victor A. Abell and is a utility that lists open files. As everything in Linux can be considered a file, this means that lsof can gather information on the majority of activity on your Linode, including network interfaces and network connections. lsof by default will output a list of all open files and the processes that opened them.

      The two main drawbacks of lsof are that it can only display information about the local machine (localhost), and that it requires administrative privileges to print all available data. Additionally, you usually do not execute lsof without any command line parameters because it outputs a large amount of data that can be difficult to parse. This happens because lsof will natively list all open files belonging to all active processes – for example, the output of wc(1) (a word count utility) when applied to lsof on a test instance shows the size of the output is extremely large:

      sudo lsof | wc
      
        
          7332   68337 1058393
      
      

      Before You Begin

      Note

      Running lsof without root privileges will only return
      the results available to the current user. If you are not familiar with the sudo command,
      see the Users and Groups guide.

      On most major distributions, lsof will come pre-installed and you can begin using it immediately. If for any reason it is not found, you can install lsof using your preferred package manager.

      Command Line Options

      The lsof(8) binary supports a large number of command line options, including the following:

      OptionDescription
      -h and -?Both options present a help screen. Please note that you will need to properly escape the ? character for -? to work.
      -aThis option tells lsof to logically ADD all provided options.
      -bThis option tells lsof to avoid kernel functions that might block the returning of results. This is a very specialized option.
      -lIf converting a user ID to a login name is working improperly or slowly, you can disable it using the -l parameter.
      –PThe -P option prevents the conversion of port numbers to port names for network files.
      -u listThe -u option allows you to define a list of login names or user ID numbers whose files will be returned. The -u option supports the ^ character for excluding the matches from the output.
      -c listThe -c option selects the listing of files for processes executing the commands that begin with the characters in the list. This supports regular expressions, and also supports the ^ character for excluding the matches from the output.
      -p listThe -p option allows you to select the files for the processes whose process IDs are in the list. The -p option supports the ^ character for excluding the matches from the output.
      -g listThe -g option allows you to select the files for the processes whose optional process group IDs are in the list. The -g option supports the ^ character for excluding the matches from the output.
      -sThe -s option allows you to select the network protocols and states that interest you. The -s option supports the ^ character for excluding the matches from the output. The correct form is PROCOTCOL:STATE. Possible protocols are UDP and TCP. Some possible TCP states are: CLOSED, SYN-SENT, SYN-RECEIVED, ESTABLISHED, CLOSE-WAIT, LAST-ACK, FIN-WAIT-1, FIN-WAIT-2, CLOSING, and TIME-WAIT. Possible UDP states are Unbound and Idle.
      +d sThe +d option option tells lsof to search for all open instances of directory s and the files and directories it contains at its top level.
      +D directoryThe +D option tells lsof to search for all open instances of directory directory and all the files and directories it contains to its complete depth.
      -d listThe -d option specifies the list of file descriptors to include or exclude from the output. -d 1,^2 means include file descriptor 1 and exclude file descriptor 2.
      -i4This option is used for displaying IPv4 data only.
      -i6This option is used for displaying IPv6 data only.
      -iThe -i option without any values tells lsof to display network connections only.
      -i ADDRESSThe -i option with a value will limit the displayed information to match that value. Some example values are TCP:25 for displaying TCP data that listens to port number 25, @google.com for displaying information related to google.com, :25 for displaying information related to port number 25, :POP3 for displaying information related to the port number that is associated to POP3 in the /etc/services file, etc. You can also combine hostnames and IP Addresses with port numbers and protocols.
      -tThe -t option tells lsof to display process identifiers without a header line. This is particularly useful for feeding the output of lsof to the kill(1) command or to a script. Notice that -t automatically selects the -w option.
      -wThe -w option disables the suppression of warning messages.
      +wThe +w option enables the suppression of warning messages.
      -r TIMEThe -r option causes the lsof command to repeat every TIME seconds until the command is manually terminated with an interrupt.
      +r TIMEThe +r command, with the + prefix, acts the same as the -r command, but will exit its loop when it fails to find any open files.
      -nThe -n option prevents network numbers from being converted to host names.
      -F CHARACTERThe -F command instructs lsof to produce output that is suitable as input for other programs. For a complete explanation, consult the lsof manual entry.

      Note

      By default, the output of lsof will include the output of each one of its command line options,
      like a big logical expression with multiple OR logical operators between all the command line
      options. However, this default behavior can change with the use of the -a option.

      Note

      For the full list of command line options supported by lsof and a more detailed
      explanation of the presented command line options, you should consult its manual page:

      man lsof
      

      Anatomy of lsof Output

      The following command uses the -i option to display all open UDP files/connections:

      sudo lsof -i UDP
      
        
      COMMAND   PID USER  FD    TYPE DEVICE SIZE/OFF NODE NAME
      rpcbind   660  root  6u    IPv4  20296  0t0      UDP  *:sunrpc
      rpcbind   660  root  7u    IPv4  20298  0t0      UDP  *:836
      rpcbind   660  root  9u    IPv6  20300  0t0      UDP  *:sunrpc
      rpcbind   660  root  10u   IPv6  20301  0t0      UDP  *:836
      avahi-dae 669 avahi   12u   IPv4  20732  0t0      UDP  *:mdns
      avahi-dae 669 avahi   13u   IPv6  20733  0t0      UDP  *:mdns
      avahi-dae 669 avahi   14u   IPv4  20734  0t0      UDP  *:54087
      avahi-dae 669 avahi   15u   IPv6  20735  0t0      UDP  *:48582
      rsyslogd  675  root  6u    IPv4  20973  0t0      UDP  li10-121.members.linode.com:syslog
      dhclient  797  root  6u    IPv4  21828  0t0      UDP  *:bootpc
      ntpd      848   ntp   16u   IPv6  22807  0t0      UDP  *:ntp
      ntpd      848   ntp   17u   IPv4  22810  0t0      UDP  *:ntp
      ntpd      848   ntp   18u   IPv4  22814  0t0      UDP  localhost:ntp
      ntpd      848   ntp   19u   IPv4  22816  0t0      UDP  li10-121.members.linode.com:ntp
      ntpd      848   ntp   20u   IPv6  22818  0t0      UDP  localhost:ntp
      ntpd      848   ntp   24u   IPv6  24916  0t0      UDP  [2a01:7e00::f03c:91ff:fe69:1381]:ntp
      ntpd      848   ntp   25u   IPv6  24918  0t0      UDP  [fe80::f03c:91ff:fe69:1381]:ntp
      
      

      The output of lsof has various columns.

      • The COMMAND column contains the first nine
        characters of the name of the UNIX command associated with the process.
      • The PID column
        shows the process ID of the command.
      • The USER column displays the name of the
        user that owns the process.
      • The TID column shows the task ID. A blank TID indicates a
        process. Note that this column will not appear in the output of many lsof commands.
      • The FD column stands for file descriptor. Its values can be cwd, txt, mem, and
        mmap.
      • The TYPE column displays the type of the file: regular file, directory, socket, etc.
      • The DEVICE column contains the device numbers separated by commas.
      • The value of the SIZE/OFF
        column is the size of the file or the file offset in bytes. The value of the NODE column
        is the node number of a local file.
      • Lastly, the NAME column shows the name of the mount point
        and file system where the file is located, or the Internet address.

      The Repeat Mode

      Running lsof with the –r option puts lsof in repeat mode, re-running the command in a loop every few seconds. This mode is useful for monitoring for a process or a connection that might only exist for a short time. The -r command will run forever, so when you are finished you must manually terminate the command.

      The +r option will also put lsof in repeat mode – the difference between -r and +r is that +r will
      automatically terminate lsof when a loop has no new output to print.

      When lsof
      is in repeat mode, it prints new output every t seconds (a loop); the default value
      of t is 15 seconds, which you can change by typing an integer value after -r or +r.

      The following command tells lsof to display all UDP connections every 10 seconds:

      sudo lsof -r 10 -i UDP
      

      Choosing Between IPv4 and IPv6

      lsof lists both IPv4 and IPv6 connections by default, but you can choose the kind
      of connections you want to display. The following command displays IPv4 connections
      only:

      sudo lsof -i4
      

      Therefore, the next command will display all TCP connections of the IPv4 protocol:

      sudo lsof -i4 -a -i TCP
      

      An equivalent command to the above is the following command that uses grep:

      sudo lsof -i4 | grep TCP
      

      On the other hand, the following command will display IPv6 connections only:

      sudo lsof -i6
      

      Therefore, the next command will display all UDP connections of the IPv6 protocol:

      sudo lsof -i6 | grep UDP
      
        
      avahi-dae  669  avahi  13u  IPv6  20733  0t0  UDP  *:mdns
      avahi-dae  669  avahi  15u  IPv6  20735  0t0  UDP  *:48582
      ntpd       848  ntp    16u  IPv6  22807  0t0  UDP  *:ntp
      ntpd       848  ntp    20u  IPv6  22818  0t0  UDP  localhost:ntp
      ntpd       848  ntp    24u  IPv6  24916  0t0  UDP  [2a01:7e00::f03c:91ff:fe69:1381]:ntp
      ntpd       848  ntp    25u  IPv6  24918  0t0  UDP  [fe80::f03c:91ff:fe69:1381]:ntp
      
      

      Logically ADD All Options

      In this section of the guide you will learn how to logically ADD the existing options
      using the -a flag. This provides you enhanced filtering capabilities. Take the following command as an example:

      sudo lsof -Pni -u www-data
      

      The above command would print out all network connections (-i), suppressing network number conversion (-n) and the conversion of port numbers to port names (-P), and it would also print out all files pertaining to the www-data user, without combining the two options into one logical statement.

      The following command combines these two options with the -a logical AND option and finds all open sockets belonging to the www-data user:

      lsof -Pni -a -u www-data
      
        
      COMMAND  PID    USER      FD  TYPE  DEVICE   SIZE/OFF  NODE  NAME
      apache2  6385   www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  6385   www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  6386   www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  6386   www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  6387   www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  6387   www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24567  www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24567  www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24570  www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24570  www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24585  www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24585  www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  25431  www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  25431  www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  27827  www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  27827  www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  27828  www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  27828  www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  27829  www-data  4u  IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  27829  www-data  6u  IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      
      

      Note

      You are allowed to place the -a option wherever you like as lsof will still detect the relevant options.

      Using Regular Expressions

      lsof has support for regular expressions. Regular expressions begin and end with a
      forward slash (/) character. The ^ character denotes the beginning of a line whereas $
      denotes the end of the line. Each dot (.) character represents a single character in
      the output.

      The following lsof command will find all commands that have precisely five characters:

      lsof -c /^.....$/
      
        
      COMMAND  PID  USER  FD   TYPE     DEVICE  SIZE/OFF  NODE  NAME
      netns    18   root  cwd  DIR      8,0     4096      2     /
      netns    18   root  rtd  DIR      8,0     4096      2     /
      netns    18   root  txt  unknown                          /proc/18/exe
      jfsIO    210  root  cwd  DIR      8,0     4096      2     /
      jfsIO    210  root  rtd  DIR      8,0     4096      2     /
      jfsIO    210  root  txt  unknown                          /proc/210/exe
      kstrp    461  root  cwd  DIR      8,0     4096      2     /
      kstrp    461  root  rtd  DIR      8,0     4096      2     /
      kstrp    461  root  txt  unknown                          /proc/461/exe
      
      

      Output For Other Programs

      Using the -F option, lsof generates output that is suitable for processing by scripts
      written in programming languages such as awk, perl and python.

      The following command will display each field of the lsof output in a separate line:

      sudo lsof -n -i4 -a -i TCP:ssh -F
      
        
      p812
      g812
      R1
      csshd
      u0
      Lroot
      f3
      au
      l
      tIPv4
      .
      .
      .
      
      

      Providing various arguments to the -F option allows you to generate less output – notice that the process ID
      and the file descriptor are always printed in the output. As an example, the following command
      will only print the process ID, which is preceded by the p character, the file descriptor, which
      is preceded by the f character, and the protocol name of each entry, which is preceded by
      the P character:

      sudo lsof -n -i4 -a -i TCP:ssh -FP
      
        
      p812
      f3
      PTCP
      p22352
      f3
      PTCP
      p22361
      f3
      PTCP
      
      

      Note

      For the full list of options supported by -F, you should visit the manual page of lsof.

      Additional Examples

      Show All Open TCP Files

      Similar to the aforementioned UDP command, the following command will display all open TCP files/connections:

      sudo lsof -i TCP
      
        
      COMMAND   PID     USER     FD   TYPE  DEVICE  SIZE/OFF NODE NAME
      sshd      812     root     3u   IPv4  23674   0t0      TCP  *:ssh (LISTEN)
      sshd      812     root     4u   IPv6  23686   0t0      TCP  *:ssh (LISTEN)
      mysqld    1003    mysql    17u  IPv4  24217   0t0      TCP  localhost:mysql (LISTEN)
      master    1245    root     13u  IPv4  24480   0t0      TCP  *:smtp (LISTEN)
      sshd      22352   root     3u   IPv4  8613370 0t0      TCP  li10-121.members.linode.com:ssh->ppp-2-8-23-19.home.otenet.gr:60032 (ESTABLISHED)
      sshd      22361   mtsouk   3u   IPv4  8613370      0t0      TCP  li10-121.members.linode.com:ssh->ppp-2-8-23-19.home.otenet.gr:60032 (ESTABLISHED)
      apache2   24565   root     4u   IPv6  8626153      0t0      TCP  *:http (LISTEN)
      apache2   24565   root     6u   IPv6  8626157      0t0      TCP  *:https (LISTEN)
      apache2   24567   www-data 4u   IPv6  8626153      0t0      TCP  *:http (LISTEN)
      apache2   24567   www-data 6u   IPv6  8626157      0t0      TCP  *:https (LISTEN)
      apache2   24568   www-data 4u   IPv6  8626153      0t0      TCP  *:http (LISTEN)
      apache2   24568   www-data 6u   IPv6  8626157      0t0      TCP  *:https (LISTEN)
      apache2   24569   www-data 4u   IPv6  8626153      0t0      TCP  *:http (LISTEN)
      apache2   24569   www-data 6u   IPv6  8626157      0t0      TCP  *:https (LISTEN)
      apache2   24570   www-data 4u   IPv6  8626153      0t0      TCP  *:http (LISTEN)
      apache2   24570   www-data 6u   IPv6  8626157      0t0      TCP  *:https (LISTEN)
      apache2   24571   www-data 4u   IPv6  8626153      0t0      TCP  *:http (LISTEN)
      apache2   24571   www-data 6u   IPv6  8626157      0t0      TCP  *:https (LISTEN)
      
      

      Listing All ESTABLISHED Connections

      Internet Connections

      If you process the output of lsof with some traditional UNIX command line tools, like grep and awk,
      you can list all active network connections:

      sudo lsof -i -n -P | grep ESTABLISHED | awk '{print $1, $9}' | sort -u
      
        
      sshd 109.74.193.253:22->2.86.23.29:60032
      
      

      Note

      The lsof -i -n -P command can be also written as lsof -i -nP or alternatively as
      lsof -nPi – writing it as lsof -inP would generate a syntax error because lsof
      thinks that np is a parameter to -i.

      SSH Connections

      The following command finds all established SSH connections to the local machine:

      sudo lsof | grep sshd | grep ESTABLISHED
      
        
      253.members.linode.com:ssh->ppp-2-86-23-29.home.otenet.gr:60032 (ESTABLISHED)
      sshd  22361  mtsouk  3u  IPv4  8613370  0t0  TCP li140-253.members.linode.com:ssh->ppp-2-86-23-29.home.otenet.gr:60032 (ESTABLISHED)
      
      

      The following command produces the same output as the previous command, but will do so more quickly because the -i TCP
      option limits the amount of information lsof prints, which mean that grep will have less data
      to process:

      sudo lsof -i TCP | grep ssh | grep ESTABLISHED
      

      Alternatively, you can execute the following command to find all established SSH
      connections:

      sudo lsof -nP -iTCP -sTCP:ESTABLISHED | grep SSH
      

      Showing Processes that are Listening to a Particular Port

      The following command shows all network connections that listen to port number 22
      (ssh) using either UDP or TCP:

      sudo lsof -i :22
      
        
      COMMAND  PID    USER    FD  TYPE  DEVICE   SIZE/OFF  NODE  NAME
      sshd     812    root    3u  IPv4  23674    0t0       TCP   *:ssh (LISTEN)
      sshd     812    root    4u  IPv6  23686    0t0       TCP   *:ssh (LISTEN)
      sshd     22352  root    3u  IPv4  8613370  0t0       TCP   li140-253.members.linode.com:ssh->ppp-2-86-23-29.home.otenet.gr:60032 (ESTABLISHED)
      sshd     22361  mtsouk  3u  IPv4  8613370  0t0       TCP   li140-253.members.linode.com:ssh->ppp-2-86-23-29.home.otenet.gr:60032 (ESTABLISHED)
      
      

      Determine Which Program Listens to a TCP port

      One of the most frequent uses of lsof is determining which program listens to a given TCP port.
      The following command will print TCP processes that are in the LISTEN state by using the -s option to provide a protocol and protocol state:

      sudo lsof -nP -i TCP -s TCP:LISTEN
      
        
      COMMAND  PID    USER      FD   TYPE  DEVICE   SIZE/OFF  NODE  NAME
      sshd     812    root      3u   IPv4  23674    0t0       TCP   *:22 (LISTEN)
      sshd     812    root      4u   IPv6  23686    0t0       TCP   *:22 (LISTEN)
      mysqld   1003   mysql     17u  IPv4  24217    0t0       TCP   127.0.0.1:3306 (LISTEN)
      master   1245   root      13u  IPv4  24480    0t0       TCP   *:25 (LISTEN)
      apache2  24565  root      4u   IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24565  root      6u   IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24567  www-data  4u   IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24567  www-data  6u   IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24568  www-data  4u   IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24568  www-data  6u   IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24569  www-data  4u   IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24569  www-data  6u   IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24570  www-data  4u   IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24570  www-data  6u   IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24571  www-data  4u   IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24571  www-data  6u   IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      apache2  24585  www-data  4u   IPv6  8626153  0t0       TCP   *:80 (LISTEN)
      apache2  24585  www-data  6u   IPv6  8626157  0t0       TCP   *:443 (LISTEN)
      
      

      Other possible states of a TCP connection are CLOSED, SYN-SENT, SYN-RECEIVED,
      ESTABLISHED, CLOSE-WAIT, LAST-ACK, FIN-WAIT-1, FIN-WAIT-2, CLOSING, and TIME-WAIT.

      Finding Information on a Given Protocol

      The next lsof command shows open UDP files that use the NTP (Network Time Protocol) port only:

      sudo lsof -i UDP:ntp
      
        
      COMMAND  PID  USER  FD   TYPE  DEVICE  SIZE/OFF  NODE  NAME
      ntpd     848  ntp   16u  IPv6  22807   0t0       UDP   *:ntp
      ntpd     848  ntp   17u  IPv4  22810   0t0       UDP   *:ntp
      ntpd     848  ntp   18u  IPv4  22814   0t0       UDP   localhost:ntp
      ntpd     848  ntp   19u  IPv4  22816   0t0       UDP   li140-253.members.linode.com:ntp
      ntpd     848  ntp   20u  IPv6  22818   0t0       UDP   localhost:ntp
      ntpd     848  ntp   24u  IPv6  24916   0t0       UDP   [2a01:7e00::f03c:91ff:fe69:1381]:ntp
      ntpd     848  ntp   25u  IPv6  24918   0t0       UDP   [fe80::f03c:91ff:fe69:1381]:ntp
      
      

      The output displays connections that use either IPv4 or IPv6. If you want to display
      the connections that use IPv4 only, you can run the following command:

      sudo lsof -i4 -a -i UDP:ntp
      
        
      COMMAND  PID  USER  FD   TYPE  DEVICE  SIZE/OFF  NODE  NAME
      ntpd     848  ntp   17u  IPv4  22810   0t0       UDP   *:ntp
      ntpd     848  ntp   18u  IPv4  22814   0t0       UDP   localhost:ntp
      ntpd     848  ntp   19u  IPv4  22816   0t0       UDP   li140-253.members.linode.com:ntp
      
      

      Disabling DNS and port Number Resolving

      lsof uses the data found in the /etc/services file to map a port number to a
      service. You can disable this functionality by using the –P option as follows:

      lsof -P -i UDP:ntp -a -i4
      
        
      COMMAND  PID  USER  FD   TYPE  DEVICE  SIZE/OFF  NODE  NAME
      ntpd     848  ntp   17u  IPv4  22810   0t0       UDP   *:123
      ntpd     848  ntp   18u  IPv4  22814   0t0       UDP   localhost:123
      ntpd     848  ntp   19u  IPv4  22816   0t0       UDP   li140-253.members.linode.com:123
      
      

      In a similar way, you can disable DNS resolving using the -n option:

      lsof -P -i UDP:ntp -a -i4 -n
      
        
      COMMAND  PID  USER  FD   TYPE  DEVICE  SIZE/OFF  NODE  NAME
      ntpd     848  ntp   17u  IPv4  22810   0t0       UDP   *:123
      ntpd     848  ntp   18u  IPv4  22814   0t0       UDP   127.0.0.1:123
      ntpd     848  ntp   19u  IPv4  22816   0t0       UDP   109.74.193.253:123
      
      

      The -n option can be particularly useful when you have a problem with your DNS
      servers or when you are interested in the actual IP address.

      Find Network Connections From or To an External Host

      The following command finds all network connections coming from or going to ppp-2-86-23-29.home.example.com:

      sudo lsof -i @ppp-2-86-23-29.home.example.com
      
        
      sshd  22352  root    3u  IPv4 8613370  0t0  TCP  li140-253.members.linode.com:ssh->ppp-2-86-23-29.home.example.com:60032 (ESTABLISHED)
      sshd  22361  mtsouk  3u  IPv4 8613370  0t0  TCP  li140-253.members.linode.com:ssh->ppp-2-86-23-29.home.example.com:60032 (ESTABLISHED)
      
      

      You can also specify the range of ports that interest you as follows:

      sudo lsof -i @ppp-2-86-23-29.home.example.com:200-250
      

      Determine Which Processes are Accessing a Given File

      With lsof you can find the processes that are accessing a given file. For example, by running the lsof command on it’s own file you can determine the processes that are accessing it:

      sudo lsof `which lsof`
      
        
      lsof  25079  root  txt  REG  8,0  163136 5693 /usr/bin/lsof
      lsof  25080  root  txt  REG  8,0  163136 5693 /usr/bin/lsof
      
      

      There are two lines in the above output because the /usr/bin/lsof file is being accessed twice, by
      both which(1) and lsof.

      If you are only interested in the process ID of the processes that are accessing
      a file, you can use the -t option to suppress header lines:

      sudo lsof -t `which lsof`
      
        
      25157
      25158
      
      

      A process ID can commonly be used for easily killing a process using the kill(1) command,
      however this is something that should only be executed with great care.

      List Open Files Under a Given Directory

      The +D lsof command will display all open files under a given directory,
      which in this case is /etc, as well as the name of the process that keeps a
      file or a directory open:

      sudo lsof +D /etc
      
        
      COMMAND    PID  USER   FD   TYPE  DEVICE  SIZE/OFF  NODE    NAME
      avahi-dae  669  avahi  cwd  DIR   8,0     4096      745751  /etc/avahi
      avahi-dae  669  avahi  rtd  DIR   8,0     4096      745751  /etc/avahi
      
      

      List Files that are Opened by a Specific User

      Another option is to locate the files opened by
      any user, including web and database users.

      The following command lists all open files opened by the www-data user:

      sudo lsof -u www-data
      
        
      COMMAND   PID   USER      FD   TYPE  DEVICE  SIZE/OFF  NODE  NAME
      php5-fpm  1066  www-data  cwd  DIR   8,0     4096      2     /
      php5-fpm  1066  www-data  rtd  DIR   8,0     4096      2     /
      
      ...
      
      

      The next variation finds all ESTABLISHED connections owned by the www-data user:

      sudo lsof -u www-data | grep -i ESTABLISHED
      
        
      apache2  24571  www-data  29u  IPv6  8675584  0t0  TCP  li140-253.members.linode.com:https->ppp-2-86-23-29.home.otenet.gr:61383 (ESTABLISHED)
      apache2  24585  www-data  29u  IPv6  8675583  0t0  TCP  li140-253.members.linode.com:https->ppp-2-86-23-29.home.otenet.gr:61381 (ESTABLISHED)
      apache2  27827  www-data  29u  IPv6  8675582  0t0  TCP  li140-253.members.linode.com:https->ppp-2-86-23-29.home.otenet.gr:61382 (ESTABLISHED)
      
      

      Last, the next command will find all processes except the ones owned by www-data by using the ^ character:

      sudo lsof -u ^www-data
      
        
      COMMAND  PID  TID   USER  FD    TYPE  DEVICE  SIZE/OFF  NODE     NAME
      systemd  1          root  cwd   DIR   8,0     4096      2        /
      systemd  1          root  rtd   DIR   8,0     4096      2        /
      systemd  1          root  txt   REG   8,0     1120992   1097764  /lib/systemd/systemd
      
      ...
      
      

      If the user name you are trying to use does not exist, you will get an error message
      similar to the following:

      sudo lsof -u doesNotExist
      
        
      lsof: can't get UID for doesNotExist
      lsof 4.89
       latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
       latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
       latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
       usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-E] [+|-e s] [+|-f[gG]]
       [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
       [+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
      Use the ``-h'' option to get more help information.
      
      

      Kill All Processes Owned by a User

      The following command will kill all of the processes owned by the www-data user:

      Caution

      Please be careful when combining lsof with the kill(1) command. Do not try to
      test similar commands on a live server unless you are absolutely certain you will not experience issues – for testing purposes you can use a disposable Docker image or something similar.

      sudo kill -9 `lsof -t -u www-data`
      

      Find All Network Activity from a Given User

      The following command lists all network activity by a user named mtsouk:

      lsof -a -u mtsouk -i
      
        
      COMMAND  PID    USER    FD  TYPE  DEVICE   SIZE/OFF  NODE  NAME
      sshd     22361  mtsouk  3u  IPv4  8613370  0t0       TCP   li140-253.members.linode.com:ssh->ppp-2-86-23-29.home.otenet.gr:60032 (ESTABLISHED)
      
      

      On the other hand, the following command lists all network activity from processes not owned by
      the root or the www-data user:

      lsof -a -u ^root -i -u ^www-data
      
        
      avahi-dae  669    avahi   12u  IPv4  20732    0t0  UDP   *:mdns
      avahi-dae  669    avahi   13u  IPv6  20733    0t0  UDP   *:mdns
      avahi-dae  669    avahi   14u  IPv4  20734    0t0  UDP   *:54087
      avahi-dae  669    avahi   15u  IPv6  20735    0t0  UDP   *:48582
      ntpd       848    ntp     16u  IPv6  22807    0t0  UDP  *:ntp
      ntpd       848    ntp     17u  IPv4  22810    0t0  UDP  *:ntp
      ntpd       848    ntp     18u  IPv4  22814    0t0  UDP  localhost:ntp
      ntpd       848    ntp     19u  IPv4  22816    0t0  UDP  li140-253.members.linode.com:ntp
      ntpd       848    ntp     20u  IPv6  22818    0t0  UDP  localhost:ntp
      ntpd       848    ntp     24u  IPv6  24916    0t0  UDP  [2a01:7e00::f03c:91ff:fe69:1381]:ntp
      ntpd       848    ntp     25u  IPv6  24918    0t0  UDP  [fe80::f03c:91ff:fe69:1381]:ntp
      mysqld     1003   mysql   17u  IPv4  24217    0t0  TCP  localhost:mysql (LISTEN)
      sshd       22361  mtsouk  3u   IPv4  8613370  0t0  TCP  li140-253.members.linode.com:ssh->ppp-2-86-23-29.home.otenet.gr:60032 (ESTABLISHED)
      
      

      Find the Total Number of TCP and UDP Connections

      If you process the output of lsof with some traditional UNIX command line tools, like grep and awk,
      you can calculate the total number of TCP and UDP connections:

      sudo lsof -i | awk '{print $8}' | sort | uniq -c | grep 'TCP|UDP'
      
        
           28 TCP
           13 UDP
      
      

      The lsof –i command lists all Internet connections whereas awk extracts the 8th
      field, which is the value of the NODE column and sort sorts the output. Then, the
      uniq –c command counts how many times each line exists. Last, the grep –v 'TCP|UDP'
      command displays the lines that contain the TCP or the UDP word in them.

      Summary

      lsof is a powerful diagnostic tool capable of a significant number of ways that you can combine its command line options to troubleshoot various issues administrators can find themselves facing. As this guide has only provided a few examples of how to use this tool, additional options can be combined for various effects that can be specifically suited to your needs.

      More Information

      You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

      Find answers, ask questions, and help others.

      This guide is published under a CC BY-ND 4.0 license.



      Source link