find out which process is listening upon a port

If you don’t have lsof already you can download and install it by becoming root and running:

root@mystery:~# apt-get install lsof

This will download and install the package for you, along with any dependencies which might be required:

Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
  lsof
0 upgraded, 1 newly installed, 0 to remove and 16 not upgraded.
Need to get 339kB of archives.
After unpacking 549kB of additional disk space will be used.
Get:1 http://http.us.debian.org unstable/main lsof 4.75.dfsg.1-1 [339kB]
Fetched 339kB in 3s (90.8kB/s)
Selecting previously deselected package lsof.
(Reading database ... 69882 files and directories currently installed.)
Unpacking lsof (from .../lsof_4.75.dfsg.1-1_i386.deb) ...
Setting up lsof (4.75.dfsg.1-1) ...

Once you have the package installed you can now discover precisely which processes are bound upon particular ports.

If you have the Apache webserver running on port 80 that will provide a suitable test candidate. If not you can choose another port you know is in use.

To discover the process name, ID (pid), and other details you need to run:

lsof -i :port

So to see which process is listening upon port 80 we can run:

root@mystery:~# lsof -i :80

This gives us the following output:

COMMAND   PID     USER   FD   TYPE   DEVICE SIZE NODE NAME
apache2 10437     root    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10438 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10439 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10440 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10441 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10442 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 25966 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 25968 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)

Here you can see the command running (apache2), the username it is running as www-data, and some other details.

Similarly we can see which process is bound to port 22:

root@mystery:~# lsof -i :22
COMMAND   PID USER   FD   TYPE   DEVICE SIZE NODE NAME
sshd     8936 root    3u  IPv6 12161280       TCP *:ssh (LISTEN)

To see all the ports open for listening upon the current host you can use another command netstat (contained in the net-tools package):

root@mystery:~# netstat -a |grep LISTEN |grep -v unix
tcp        0      0 *:2049                  *:*                     LISTEN     
tcp        0      0 *:743                   *:*                     LISTEN     
tcp        0      0 localhost.localdo:mysql *:*                     LISTEN     
tcp        0      0 *:5900                  *:*                     LISTEN     
tcp        0      0 localhost.locald:sunrpc *:*                     LISTEN     
tcp        0      0 *:8888                  *:*                     LISTEN     
tcp        0      0 localhost.localdom:smtp *:*                     LISTEN     
tcp6       0      0 *:www                   *:*                     LISTEN     
tcp6       0      0 *:distcc                *:*                     LISTEN     
tcp6       0      0 *:ssh                   *:*                     LISTEN

Here you can see that there are processes listening upon ports 20497435900, and several others.

(The second grep we used above was to ignore Unix domain sockets).

If you’re curious to see which programs and services are used in those sockets you can look them up as we’ve already shown:

root@mystery:~# lsof -i :8888
COMMAND   PID    USER   FD   TYPE   DEVICE SIZE NODE NAME
gnump3d 25834 gnump3d    3u  IPv4 61035200       TCP *:8888 (LISTEN)

This tells us that the process bound to port 8888 is the gnump3d MP3 streamer.

Port 2049 and 743 are both associated with NFS. The rest can be tracked down in a similar manner. (You’ll notice that some ports actually have their service names printed next to them, such as the smtp entry for port 25).

lsof is a very powerful tool which can be used for lots of jobs. If you’re unfamiliar with it I recommend reading the manpage via:

man lsof

If you do so you’ll discover that the -i flag can take multiple different types of arguments, to allow you to check more than one port at a time, and use IPv6 addresses too.

It’s often used to see which files are open upon mounted devices, so you can kill the processes and unmount them cleanly.

Install & Uninstall NVM and Node.js globally in a Linux based system

Note: You should be a sudoer.

  1. Enter into root user
    $ sudo -s 
  2. Install nvm globally accessible by all the users:
    $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash
  3. Install node:
    For specific version of node.js
    $ nvm install <version of node>
    eg: nvm install 10.10.0
                ——— OR ———-
    For latest version of node.js
    $ nvm install node
  4. Create a file called nvm.sh in /etc/profile.d with the following contents:
    export NVM_DIR="/usr/local/nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  5.  Install Angular CLI(Command Line Interface)
    $ npm install -g @angular/cli
  6. Install pm2 (For running on Server. Skip this step if you are doing local setup):
    $ npm install -g pm2

Uninstalling NVM follow the steps

rm -rf ~/.nvm
rm -rf ~/.npm
rm -rf ~/.bower
rm -rf $NVM_DIR ~/.npm ~/.bower

Remote Debugging Play Framework

At times it is required to debug the code, debugging can be easy with any IDE. Running a play framework app and remote debug the application is possible by attaching the process and placing appropriate break points which would hit the breakpoint when the process is attached.

Start sbt-shell

Run Command clean compile test

This would result in starting a process with PID which can be traced from the sbt-shell window

opt/jdk1.8.0_181/bin/java -agentlib:jdwp=transport=dt_socket,address=localhost:35226,suspend=n,server=y -Xdebug -server -Xmx1536M -Didea.managed=true -Dfile.encoding=UTF-8 -Didea.runid=2018.2 -jar /home/sufia/.IdeaIC2018.2/config/plugins/Scala/launcher/sbt-launch.jar idea-shell
Listening for transport dt_socket at address: 35226

[sufia@sufia informis-server]$ lsof -i:35226
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 5981 sufia 4u IPv4 668592 0t0 TCP localhost:35226 (LISTEN)

In InteliJ->Run->Attach to Process, select the PID: 5981

This would hit the breakpoint and you would be able to debug through

Upgrade Ubuntu from 16.04 to 18

If you are on Ubuntu 16.04 and need to move to 18, the following steps would help you achieve that. The upgrade is large and you would be inquired with a few questions while the up-gradation is happening.

First update the current distribution, with the following command

$ sudo apt update

$ sudo apt upgrade

This makes sure your Ubuntu is up to date. Next, upgrade the distribution of ubuntu

$ sudo apt dist-upgrade

This handles changing software dependencies with new versions of packages.

I then follow this up with:

$ sudo apt-get autoremove

This removes dependencies from uninstalled applications. You can do the same thing from the GUI desktop with the utility application Bleachbit. After that, it’s time to get things ready for the big upgrade with:

$ sudo apt install update-manager-core

Finally:

$ sudo do-release-upgrade

This will start upgrading your system to 18.04. Along the way, Ubuntu will ask you several questions about how to handle the upgrade.

Installing Protos

Type the following commands in the terminal to install protos in ubuntu

1) PROTOC_ZIP=protoc-3.3.0-linux-x86_64.zip
2) curl -OL https://github.com/google/protobuf/releases/download/v3.3.0/$PROTOC_ZIP
3) sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
4) rm -f $PROTOC_ZIP

upgrade or downgrade node

How to Upgrade (or Downgrade) Node.js using NPM

Need to update your version of Node.js? Here’s how you can upgrade or downgrade from the command line using NPM.

Upgrading to the latest stable version

This will update you to the latest available stable version:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

Upgrading to the latest LTS version

Node also offers a long-term support (LTS) version. If you need that version (or any other), simply specify the version number you want:

sudo npm cache clean -f
sudo npm install -g n
sudo n 8.0.0

Checking your Node version

To see which version of Node is currently installed, simply run:

node -v

The version number displayed is the one that’s currently active on your machine.

Resolving version conflicts in Angular

This blog resolves the problem of dealing with the version conflicts in Angular (Ex: You have installed angular 6 and for instance you come across a situation where you would like to run an angular project developed in Angular 5 without removing the global version of Angular 6).

Here is the procedure to resolve the issue:
Solution 1:
1)install npm i node-sass (OR) install npm i node-sass@[version]
Ex: install npm i node-sass@4.8.1(Note: 4.8.1 is a version of node-sass, you can put your version of the project to be executed)
2)npm add @angular/cli@[version] –save-dev
Ex: npm add @angular/cli@1.7.4 –save-dev (Note: 1.7.4 is the version to install Angular 5).

Solution 2:
1)For sass problem:
install npm i node-sass (OR) install npm i node-sass@[version]
Ex: install npm i node-sass@4.8.1(Note: 4.8.1 is a version of node-sass, you can put your version of the project to be executed)
2)For Undefined is not a function follow this procedure:
a)npm remove -g @angular/cli
b)npm install -g @angular/cli@[version]
Ex: npm install -g @angular/cli@1.7.4 (Note: 1.7.4 is the version to install Angular 5)
c)npm remove @angular/cli
d)npm add @angular/cli@[version] –save-dev
Ex: npm add @angular/cli@1.7.4 –save-dev (Note: 1.7.4 is the version to install Angular 5).