How to listen new port Ubuntu Server from command line?

I want to listen new port on Ubuntu Server.

I am in root access.

netstat -an | grep "LISTEN "

Returns :

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:1270 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:29130 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::8080 :::* LISTEN
tcp6 0 0 :::80 :::* LISTEN 

I want to run my .js file on port 3000. So i have fired command :

nc -l 3000

But then no output.

I am new to Ubuntu, Anyone can help me with it?

EDIT :

I want to listen new port 3000 same as following port :

 tcp6 0 0 :::8080 :::* LISTEN 
4

2 Answers

Ubuntu Server itself will not listen to any port. The application services installed and ran at the server listen to ports. For example, by default:

  • sshd listen at 22,
  • apache2/nginx listen at 80 and 443, etc.

If you want to get the names of the services that listen at the ports on your system, use netstat by root via sudo in this way:

$ sudo netstat -pna | grep "LISTEN " | grep '\<tcp\>'
tcp 0 0 0.0.0.0:6951 0.0.0.0:* LISTEN 8976/aria2c
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1589/mysqld
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 32285/redis-server
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 1279/memcached
tcp 0 0 127.0.0.1:8142 0.0.0.0:* LISTEN 1482/nodejs
tcp 0 0 127.0.0.1:6800 0.0.0.0:* LISTEN 8976/aria2c
tcp 0 0 127.0.0.1:81 0.0.0.0:* LISTEN 3286/docker-proxy
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 1218/systemd-resolv
tcp 0 0 127.0.0.1:4822 0.0.0.0:* LISTEN 1445/guacd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 77768/apache2
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1339/sshd: /usr/sbi
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 2805/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 77768/apache2

note:

  • 127.0.0.1 means the loopback interface (localhost),
  • 0.0.0.0 means all available interfaces (localhost, network adapters, etc.),
  • some services could listen at certain interfaces, i.e. 192.168.1.100 or it could be some IPv6 address.

If there is a firewall and you want to access your services outside you need to allow input/output traffic to the ports they listen.


By using nc -l 3000 the command nc itself will start to listen at port 3000. IMO, this option is designed for test purposes, i.e. you are setup firewall or so.

Let's say in a terminal you've ran:

$ nc -l 3000

Then in another terminal you can test does something listen to port 3000 by:

$ nc -vz 0.0.0.0 3000
Connection to 0.0.0.0 3000 port [tcp/*] succeeded!

And that's it.


As conclusion:

Your must design your service (program, script, command) to listen at the desired port 3000 and when this port is not used by some other service, when you start your service it will start to listen to the port.


In other hand if you need something to listen constantly at :::3000 in order to do your tests, the most easiest way, in my opinion, is to create systemd unit. For this purpose:

  • Create /etc/systemd/system/listen-3000.service:

    sudo nano /etc/systemd/system/listen-3000.service
  • As content of /etc/systemd/system/listen-3000.service place:

    [Unit]
    Description=Permanent listen at :::3000
    After=network-online.target
    [Service]
    User=root
    ExecStart=/usr/bin/nc -l6 3000
    ExecStop=/usr/bin/killall -s KILL nc
    Restart=always
    RestartSec=1
    [Install]
    WantedBy=multi-user.target
  • Enable and start the listen-3000.service:

    sudo systemctl daemon-reload
    sudo systemctl enable listen-3000.service
    sudo systemctl start listen-3000.service
    sudo systemctl status listen-3000.service
  • Disable and stop the listen-3000.service:

    sudo systemctl stop listen-3000.service
    sudo systemctl disable listen-3000.service

If you run the command nc -l 3000 nc will run in foreground and you won't be able to do anything else without stopping nc

so you must run nc as background task:

nc -l 3000 &

6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like