coding-to-music / coding-to-music.github.io

https://pandemic-overview.readthedocs.io/en/latest/index.html
MIT License
2 stars 8 forks source link

Provision a new DigitalOcean development droplet #268

Open coding-to-music opened 3 years ago

coding-to-music commented 3 years ago

Provision a new DigitalOcean development droplet

Add Cockpit for system monitoring

sudo apt-get install cockpit

Add local users

https://github.com/coding-to-music/coding-to-music.github.io/issues/238

# add the user
adduser username

# grant sudo
usermod -aG sudo username

# verify 
su - username

Mount external Drive

https://github.com/coding-to-music/coding-to-music.github.io/issues/259


# install
sudo apt-get install sshfs

# create the local mount point
sudo mkdir /mnt/ap

# mount the remote file system
sudo sshfs -o allow_other,default_permissions,IdentityFile=/home/tmc/.ssh/id_rsa root@xxx.xxx.xxx.xxx:/mnt/ap /mnt/ap

# notice
# /home/tmc/.ssh_id_rsa is supplying the SSH key
# mounting locally to /mnt/ap
# mount root@xxx.xxx.xxx.xxx:/mnt/ap

# to unmount - but it is busy if you are there, issue command where your CWD is not the mount
sudo umount /mnt/ap

sudo sshfs -o allow_other,default_permissions,IdentityFile=/home/tmc/.ssh/id_rsa root@xxx.xxx.xxx.xxx:/mnt/ap /mnt/ap

# validate
df -h

cd /mnt/ap

sudo fdisk -l

# Permenantly mounting the remote file system

nano /etc/fstab

# Scroll the file to the bottom and then paste the following text:

sshfs#user@server_ip:/path/to/mount /mnt/server_folder

# add this to the bottom of the file:
sshfs#root@xxx.xxx.xxx.xxx:/ /mnt/droplet

# for the above example
sshfs#root@xxx.xxx.xxx.xxx:/mnt/ap /mnt/ap

# this example was from here: 
https://askubuntu.com/questions/326977/sshfs-is-not-mounting-automatically-at-boot-despite-etc-fstab-configuration

sshfs#user@host:/remote/dir /local/dir fuse delay_connect,idmap=user,uid=1000,gid=1000,umask=0,allow_other,_netdev,workaround=rename 0 0

Install frequent software

Install Mongo

https://hub.docker.com/_/mongo

docker pull mongo

docker run --name some-mongo -d mongo:tag

... via docker stack deploy or docker-compose

Example stack.yml for mongo:

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

Try in PWD

http://localhost:8081/

Run 
docker stack deploy -c stack.yml mongo 
or 
docker-compose -f stack.yml up

 wait for it to initialize completely, and visit http://swarm-ip:8081, http://localhost:8081, or http://host-ip:8081 (as appropriate).

Installing MongoDB Compass on Ubuntu 20.04 | Debian 11/10

Download MongoDB Compass with wget command:

wget https://downloads.mongodb.com/compass/mongodb-compass_1.28.4_amd64.deb

sudo dpkg -i mongodb-compass_1.28.4_amd64.deb

# To fix any dependency issues run:

sudo apt -f install

# Launching MongoDB Compass
mongodb-compass

Download MongoDB Compass with wget command:

wget https://fastdl.mongodb.org/mongocli/mongocli_1.20.1_linux_x86_64.deb

dpkg -i mongocli_1.20.1_linux_x86_64.deb

mongocli --version

mongocli config

Configure mongocli get API Public and Private Key

https://docs.mongodb.com/mongocli/stable/configure/#std-label-mcli-configure

root@docker-ubuntu-s-2vcpu-4gb-nyc1-01:~/ap/mongosh# mongocli --version
mongocli version: 1.20.1
git version: 57751bb7f7284e303c4b5105a7254c54f540375f
Go version: go1.17
   os: linux
   arch: amd64
   compiler: gc
root@docker-ubuntu-s-2vcpu-4gb-nyc1-01:~/ap/mongosh# mongocli config
You are configuring a profile for mongocli.

All values are optional and you can use environment variables (MCLI_*) instead.

Enter [?] on any option to get help.

? Public API Key: gexeivgc
? Private API Key: [? for help] ************************************
? Choose a default organization: Tom's Org - 2021-09-19 (6146xxxxxxx5a0b0)
? Choose a default project: Project 0 (6146f93axxxxxxx)
? Default Output Format: json
? Default MongoDB Shell Path: /usr/bin/mongosh

Your profile is now configured.
You can use [mongocli config set] to change these settings at a later time.

Installing Git so you can work with GitHub

Download and install Git

sudo apt-get install git

Now git should be installed. To check use

git --version

git version 2.19.1

Configuring GitHub git config user.name user.email

Once the installation has successfully completed, the next thing to do is to set up the configuration details of the GitHub user. To do this use the following two commands by replacing “user_name” with your GitHub username and replacing “email_id” with your email-id you used to create your GitHub account.

Set the git config global values

git config --global user.name coding-to-music
git config --global user.email connors.tom@gmail.com

To validate correct git setup

git config --list

Check that GitHub can be reached

ssh -vT git@github.com

Configure Dotfiles

coding-to-music commented 2 years ago

Create a new Digitalocean Droplet

Use the control panel, create the droplet

ssh to the droplet

apt-get update
apt-get upgrade

install monitoring agent

https://docs.digitalocean.com/products/monitoring/how-to/install-agent/

curl -sSL https://repos.insights.digitalocean.com/install.sh | sudo bash

# verify monitoring agent is running
ps aux | grep do-agent

set up git (already installed)

git --version
git config --global user.name coding-to-music
git config --global user.email connors.tom@gmail.com

# verify git works
ssh -vT git@github.com

Add local users

https://github.com/coding-to-music/coding-to-music.github.io/issues/238

# add the user
adduser username

# grant sudo
usermod -aG sudo username

# verify 
su - username

Set up vscode

https://code.visualstudio.com/docs/setup/linux

To install Visual Studio Code on your Ubuntu system, follow these steps:

sudo apt-get install wget gpg
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings
sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list'
rm -f packages.microsoft.gpg

Then update the package cache and install the package using:

sudo apt install apt-transport-https
sudo apt update
sudo apt install code

Set up .bashrc and .bash_aliases

.bashrc edits

# Uncomment the below line:
unset color_prompt force_color_prompt

.bashrc_aliases edits

copy from source 

How To Install Node.js on Ubuntu 20.04 With NVM

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-22-04

Introduction

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.

In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server:

For many users, using apt with the default repo will be sufficient. If you need specific newer (or legacy) versions of Node, you should use the PPA repository. If you are actively developing Node applications and need to switch between node versions frequently, choose the nvm method.

Prerequisites

This guide assumes that you are using Ubuntu 20.04. Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the Ubuntu 20.04 initial server setup tutorial.

If you would like to experiment with installing Node.js without using a server or your local computer, you can use an interactive terminal in your browser. Click the Launch an Interactive Terminal! button below to get started.

Option 1 — Installing Node.js with Apt from the Default Repositories

Ubuntu 20.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 10.19. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.

Warning: the version of Node.js included with Ubuntu 20.04, version 10.19, is now unsupported and unmaintained. You should not use this version in production, and should refer to one of the other sections in this tutorial to install a more recent version of Node.

To get this version, you can use the apt package manager. Refresh your local package index first by typing:

sudo apt update

Then install Node.js:

sudo apt install nodejs

Check that the install was successful by querying node for its version number:

node -v
Outputv10.19.0

If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to also install npm, the Node.js package manager. You can do this by installing the npm package with apt:

sudo apt install npm

This will allow you to install modules and packages to use with Node.js.

At this point you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.

Option 2 — Installing Node.js with Apt Using a NodeSource PPA

To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v12, v14, and v16 are available as of the time of writing.

First, we will install the PPA in order to get access to its packages. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 16.x with your preferred version string (if different).

cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh

Refer to the NodeSource documentation for more information on the available versions.

Inspect the contents of the downloaded script with nano (or your preferred text editor):

nano /tmp/nodesource_setup.sh

When you are satisfied that the script is safe to run, exit your editor, then run the script with sudo:

sudo bash /tmp/nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section:

sudo apt install nodejs

Verify that you’ve installed the new version by running node with the -v version flag:

node -v
Outputv16.6.1

The NodeSource nodejs package contains both the node binary and npm, so you don’t need to install npm separately.

At this point you have successfully installed Node.js and npm using apt and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.

Option 3 — Installing Node Using the Node Version Manager

Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.

To install NVM on your Ubuntu 20.04 machine, visit the project’s GitHub page. Copy the curl command from the README file that displays on the main page. This will get you the most recent version of the installation script.

Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash segment at the end of the curl command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh

Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed by typing:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

This will install the nvm script to your user account. To use it, you must first source your .bashrc file:

source ~/.bashrc

If you are using the interactive terminal to test installing Node.js with nvm, you will need to source your the ~/.bash_profile file instead. Use the following command to do so:

source ~/.bash_profile

Now, you can ask NVM which versions of Node are available:

nvm list-remote
Output. . .
       v14.16.0   (LTS: Fermium)
       v14.16.1   (LTS: Fermium)
       v14.17.0   (LTS: Fermium)
       v14.17.1   (LTS: Fermium)
       v14.17.2   (LTS: Fermium)
       v14.17.3   (LTS: Fermium)
       v14.17.4   (Latest LTS: Fermium)
        v15.0.0
        v15.0.1
        v15.1.0
        v15.2.0
        v15.2.1
        v15.3.0
        v15.4.0
        v15.5.0
        v15.5.1
        v15.6.0
        v15.7.0
        v15.8.0
        v15.9.0
       v15.10.0
       v15.11.0
       v15.12.0
       v15.13.0
       v15.14.0
        v16.0.0
        v16.1.0
        v16.2.0

It’s a very long list! You can install a version of Node by typing any of the release versions you see. For instance, to get version v14.10.0, you can type:

nvm install v14.10.0

You can see the different versions you have installed by typing:

nvm list
Output->     v14.10.0
         system
default -> v14.17.4 (-> N/A)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v14.10.0) (default)
stable -> 14.10 (-> v14.10.0) (default))
. . .

This shows the currently active version on the first line (-> v14.10.0), followed by some named aliases and the versions that those aliases point to.

Note: if you also have a version of Node.js installed through apt, you may see a system entry here. You can always activate the system-installed version of Node using nvm use system.

Additionally, you’ll see aliases for the various long-term support (or LTS) releases of Node:

Output. . .
lts/* -> lts/fermium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.4 (-> N/A)
lts/fermium -> v14.17.4 (-> N/A)

We can install a release based on these aliases as well. For instance, to install the latest long-term support version, fermium, run the following:

nvm install lts/fermium
OutputDownloading and installing node v14.17.4...
. . .
Now using node v14.17.4 (npm v6.14.14))

You can switch between installed versions with nvm use:

nvm use v14.10.0
OutputNow using node v14.10.0 (npm v6.14.8)

You can verify that the install was successful using the same technique from the other sections, by typing:

node -v
Outputv14.10.0

The correct version of Node is installed on our machine as we expected. A compatible version of npm is also available.

Conclusion

There are a quite a few ways to get up and running with Node.js on your Ubuntu 20.04 server. Your circumstances will dictate which of the above methods is best for your needs. While using the packaged version in Ubuntu’s repository is the easiest method, using nvm or a NodeSource PPA offers additional flexibility.

For more information on programming with Node.js, please refer to our tutorial series How To Code in Node.js.

Set up Yarn

https://yarnpkg.com/getting-started/install

# install node first, node version > 16.1
corepack enable
yarn init -2
yarn set version stable
yarn --version
3.2.2

Set up Docker

Terraform Repository Configuration

The Terraform packages are signed using a private key controlled by HashiCorp, so in most situations the first step would be to configure your system to trust that HashiCorp key for package authentication. For example:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

However, this perhaps is a recent bug: https://github.com/hashicorp/terraform/issues/29663

After registering the key, you can add the official HashiCorp repository to your system:

sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

The above command line uses the following sub-shell commands:

To install Terraform from the new repository:

sudo apt install terraform

Installing Ansible via apt-get

To begin using Ansible as a means of managing your server infrastructure, you need to install the Ansible software on the machine that will serve as the Ansible control node. We’ll use the default Ubuntu repositories for that.

First, refresh your system’s package index with:

sudo apt update

Following this update, you can install the Ansible software with:

sudo apt install ansible

Press Y when prompted to confirm installation.

Your Ansible control node now has all of the software required to administer your hosts. Next, we’ll go over how to set up an inventory file, so that Ansible can communicate with your managed nodes.

Ansible Setup

Tasks:

Finally, we’ll ensure that the ~/.ssh directory and authorized_keys file have the appropriate permissions set:

chmod -R go= ~/.ssh

This recursively removes all “group” and “other” permissions for the ~/.ssh/ directory.

If you’re using the root account to set up keys for a user account, it’s also important that the ~/.ssh directory belongs to the user and not to root:

chown -R sammy:sammy ~/.ssh

In this tutorial our user is named sammy but you should substitute the appropriate username into the above command.

Create an inventory file

code inventory

Set the contents as follows:

[dev]
137.184.96.25

[all:vars]
ansible_python_interpreter=/usr/bin/python3 

Set the contents of /etc/ansible/hosts

code /etc/ansible/hosts

Set the following content:

[servers]
137.184.96.25
CloudPod ansible_host=137.184.96.25

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Test the connection

ansible all -m ping

Output

137.184.96.25 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
CloudPod | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

some examples of ad-hoc ansible commands

ansible all -m ping
ansible all -m ping -u sammy
ansible all -m ping -u root

ansible all -a "df -h" -u root

ansible all -m apt -a "name=vim state=latest" -u root

ansible CloudPod -m apt -a "name=ansible state=latest" -u root

Step 2 — Copying the Public Key to Your Ubuntu Server

The quickest way to copy your public key to the Ubuntu host is to use a utility called ssh-copy-id. Due to its simplicity, this method is highly recommended if available. If you do not have ssh-copy-id available to you on your client machine, you may use one of the two alternate methods provided in this section (copying via password-based SSH, or manually copying the key).

Copying the Public Key Using ssh-copy-id

The ssh-copy-id tool is included by default in many operating systems, so you may have it available on your local system. For this method to work, you must already have password-based SSH access to your server.

To use the utility, you specify the remote host that you would like to connect to, and the user account that you have password-based SSH access to. This is the account to which your public SSH key will be copied.

The syntax is:

ssh-copy-id username@remote_host

You may see the following message:

Output

The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established.
ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe.
Are you sure you want to continue connecting (yes/no)? yes

This means that your local computer does not recognize the remote host. This will happen the first time you connect to a new host. Type “yes” and press ENTER to continue.

Next, the utility will scan your local account for the id_rsa.pub key that we created earlier. When it finds the key, it will prompt you for the password of the remote user’s account:

Output

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
username@203.0.113.1's password:

Type in the password (your typing will not be displayed, for security purposes) and press ENTER. The utility will connect to the account on the remote host using the password you provided. It will then copy the contents of your ~/.ssh/id_rsa.pub key into a file in the remote account’s home ~/.ssh directory called authorized_keys.

You should see the following output:

Output

Number of key(s) added: 1

Now try logging into the machine, with:

ssh username@203.0.113.1

and check to make sure that only the key(s) you wanted were added. At this point, your id_rsa.pub key has been uploaded to the remote account. You can continue on to Step 3.

Copying the Public Key Using SSH

If you do not have ssh-copy-id available, but you have password-based SSH access to an account on your server, you can upload your keys using a conventional SSH method.

We can do this by using the cat command to read the contents of the public SSH key on our local computer and piping that through an SSH connection to the remote server.

On the other side, we can make sure that the ~/.ssh directory exists and has the correct permissions under the account we’re using.

We can then output the content we piped over into a file called authorized_keys within this directory. We’ll use the >> redirect symbol to append the content instead of overwriting it. This will let us add keys without destroying previously added keys.

The full command looks like this:

cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"

You may see the following message:

Output

The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established.
ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe.
Are you sure you want to continue connecting (yes/no)? yes

This means that your local computer does not recognize the remote host. This will happen the first time you connect to a new host. Type yes and press ENTER to continue.

Afterwards, you should be prompted to enter the remote user account password:

Output

username@203.0.113.1's password:

After entering your password, the content of your id_rsa.pub key will be copied to the end of the authorized_keys file of the remote user’s account.

Ensure .ssh permissions are correct

.ssh directory: 700 (drwx------)
public key (.pub file): 644 (-rw-r--r--)
private key (id_rsa): 600 (-rw-------)

chmod 700 ./.ssh
chmod 644 ./.ssh/*.pub
chmod 600 ./.ssh/id_rsa
chmod 600 ./.ssh/id_ed25519

Alternate install method - download terraform zip file

# get zip file
wget https://releases.hashicorp.com/terraform/1.0.7/terraform_1.0.7_linux_amd64.zip

# unzip file
unzip terraform_1.0.7_linux_amd64.zip 

# check local path
printenv | grep PATH

# move to a location in the existing path
mv terraform /usr/local/bin

# remove zip file
rm terraform_1.0.7_linux_amd64.zip 

# validate version
terraform --version

Terraform v1.0.7
on linux_amd64

create file terraform.tfvars

terraform.tfvars

do_token = ""
ssh_fingerprint = ""

Fill in each variable:

To get the fingerprint for your key, run the following command, being sure to update the path (currently ~/.ssh/id_rsa.pub) to the key you're using with DigitalOcean, if necessary:

ssh-keygen -E md5 -lf ~/.ssh/id_rsa.pub | awk '{print $2}'

The output will be similar to this:

MD5:ac:eb:de:c1:95:18:6f:d5:58:55:05:9c:51:d0:e8:e3

Copy everything except the initial MD5: and paste it into the variable.

Now we can initialize Terraform. This will download some information for the DigitalOcean Terraform provider, and check our configuration for errors.

terraform init

You should get some output about initializing plugins. Now we're ready to provision the infrastructure and configure it.

Set up Python

python3 --version
python --version

Set up Go

https://www.digitalocean.com/community/tutorials/how-to-install-go-on-ubuntu-20-04

https://go.dev/dl/

curl -OL https://golang.org/dl/go1.18.4.linux-amd64.tar.gz
sha256sum go1.18.4.linux-amd64.tar.gz
sudo tar -C /usr/local -xvf go1.18.4.linux-amd64.tar.gz 
source ~/.bashrc
whereis go
go version

Output

go version go1.18.4 linux/amd64

Set up Cloudflare

Set up Vercel

Set up Heroku

How to Install netstat Command in Linux

The package that contains netstat is called net-tools. On modern systems, the netstat utility comes pre-installed and there’s no need to install it.

On older systems, however, you are likely to bump into an error when you run the netstat command. Therefore, to install netstat on Linux distributions, run the command.

apt install net-tools     [On Debian/Ubuntu]

Once installed, run the command below to check the version of netstat installed.

netstat -v

How to Use netstat Command in Linux

You can invoke the netstat command on any of the Linux distributions to get different statistics on your network.

sudo netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      831/sshd: /usr/sbin 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      584/systemd-resolve 
tcp6       0      0 :::22                   :::*                    LISTEN      831/sshd: /usr/sbin 
udp        0      0 127.0.0.53:53           0.0.0.0:*                           584/systemd-resolve 
raw        0      0 0.0.0.0:6               0.0.0.0:*               7           2555/droplet-agent  
raw6       0      0 :::58                   :::*                    7           560/systemd-network 
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     29939    2660/systemd         /run/user/0/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     29945    2660/systemd         /run/user/0/bus
unix  2      [ ACC ]     STREAM     LISTENING     15545    1/init               @/org/kernel/linux/storage/multipathd
unix  2      [ ACC ]     STREAM     LISTENING     29947    2660/systemd         /run/user/0/gnupg/S.dirmngr
unix  2      [ ACC ]     STREAM     LISTENING     29949    2660/systemd         /run/user/0/gnupg/S.gpg-agent.browser
unix  2      [ ACC ]     STREAM     LISTENING     29951    2660/systemd         /run/user/0/gnupg/S.gpg-agent.extra
unix  2      [ ACC ]     STREAM     LISTENING     29953    2660/systemd         /run/user/0/gnupg/S.gpg-agent.ssh
unix  2      [ ACC ]     STREAM     LISTENING     29955    2660/systemd         /run/user/0/gnupg/S.gpg-agent
unix  2      [ ACC ]     STREAM     LISTENING     29957    2660/systemd         /run/user/0/pk-debconf-socket
unix  2      [ ACC ]     STREAM     LISTENING     29959    2660/systemd         /run/user/0/snapd-session-agent.socket
unix  2      [ ACC ]     STREAM     LISTENING     19946    1/init               /var/snap/lxd/common/lxd/unix.socket
unix  2      [ ACC ]     STREAM     LISTENING     19948    1/init               /var/snap/lxd/common/lxd-user/unix.socket
unix  2      [ ACC ]     STREAM     LISTENING     19037    1/init               /run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     19050    1/init               /run/snapd.socket
unix  2      [ ACC ]     STREAM     LISTENING     19052    1/init               /run/snapd-snap.socket
unix  2      [ ACC ]     STREAM     LISTENING     19054    1/init               /run/uuidd/request
unix  2      [ ACC ]     STREAM     LISTENING     19564    712/irqbalance       /run/irqbalance/irqbalance712.sock
unix  2      [ ACC ]     STREAM     LISTENING     15532    1/init               /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     15534    1/init               /run/systemd/userdb/io.systemd.DynamicUser
unix  2      [ ACC ]     STREAM     LISTENING     15535    1/init               /run/systemd/io.system.ManagedOOM
unix  2      [ ACC ]     STREAM     LISTENING     15543    1/init               /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     STREAM     LISTENING     15548    1/init               /run/systemd/fsck.progress
unix  2      [ ACC ]     STREAM     LISTENING     15559    1/init               /run/systemd/journal/stdout
unix  2      [ ACC ]     SEQPACKET  LISTENING     15562    1/init               /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     14232    354/systemd-journal  /run/systemd/journal/io.systemd.journal
unix  2      [ ACC ]     STREAM     LISTENING     19049    1/init               @ISCSIADM_ABSTRACT_NAMESPACE
unix  2      [ ACC ]     STREAM     LISTENING     17346    584/systemd-resolve  /run/systemd/resolve/io.systemd.Resolve

1. Viewing the Network Routing Table

You use the -r flag to show the network routing table to get something similar to the output below.

netstat -nr

List Network Routing Table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         this-machine.1   0.0.0.0         UG        0 0          0 eth0
10.10.0.0       0.0.0.0         255.255.0.0     U         0 0          0 eth0
10.116.0.0      0.0.0.0         255.255.240.0   U         0 0          0 eth1
this-machine.0   0.0.0.0         255.255.240.0   U         0 0          0 eth0

The -n option forces netstat to print addresses separated by dots instead of using symbolic network names. The option is useful for avoiding address lookups over a network.

2. Display Network Interface Statistics

Use the -i flag to get an output of statistics of a network interface that is configured. The -a option prints all present interfaces in the kernel.

netstat -ai

List Network Interface Statistics

Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0      1500    28643      0      0 0         20554      0      0      0 BMRU
eth1      1500       17      0      0 0           117      0      0      0 BMRU
lo       65536      684      0      0 0           684      0      0      0 LRU

3. Show Network Connections

The netstat command utility supports options that display active or passive sockets using the options -t, -n, and -a. The flags show RAW, UDP, TCP, or UNIX connection sockets. Adding the -a option, it will sow sockets ready for connection.

netstat -ant

List Network Connections

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN     
tcp        0     52 this-machine:22      chromebook:40714      ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN 

4. Show Network Services

To list services, their current state, and their corresponding ports, run the command.

netstat -pnltu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      831/sshd: /usr/sbin 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      584/systemd-resolve 
tcp6       0      0 :::22                   :::*                    LISTEN      831/sshd: /usr/sbin 
udp        0      0 127.0.0.53:53           0.0.0.0:*                           584/systemd-resolve 

In this article, we shed light on how you can install netstat command and how it is used to checking a wide array of network statistics. It’s also important to point out that netstat has been deprecated and instead ss utility has taken its place in displaying more refined network statistics.

View Cron Jobs

crontab -e

View systemctl jobs

sudo systemctl status