sábado, 13 de novembro de 2021

How to check if port is in use in

 


To check the listening ports and applications on Linux:

  1. Open a terminal application i.e. shell prompt.
  2. Run any one of the following command on Linux to see open ports:
    sudo lsof -i -P -n | grep LISTEN
    sudo netstat -tulpn | grep LISTEN
    sudo ss -tulpn | grep LISTEN
    sudo lsof -i:22 ## see a specific port such as 22 ##
    sudo nmap -sTU -O IP-address-Here
  3. For the latest version of Linux use the ss command. For example, ss -tulw

segunda-feira, 25 de outubro de 2021

How to compile and install Linux Kernel

 

Compiling a custom kernel has its advantages and disadvantages. However, new Linux user/admin find it difficult to compile Linux kernel. Compiling kernel needs to understand few things and then type a couple of commands. This step by step howto covers compiling Linux kernel version 5.6.9 under an Ubuntu or Debian Linux. The following instructions successfully tested on an RHEL/CentOS 7/8 (and clones), Debian Linux, Ubuntu Linux and Fedora Linux 30/31. However, instructions remain the same for any other Linux distribution.

make
make modules
make modules_install
make install

How to compile and install Linux Kernel 5.6.9

The procedure to build (compile) and install the latest Linux kernel from source is as follows:

  1. Grab the latest kernel from kernel.org
  2. Verify kernel
  3. Untar the kernel tarball
  4. Copy existing Linux kernel config file
  5. Compile and build Linux kernel 5.6.9
  6. Install Linux kernel and modules (drivers)
  7. Update Grub configuration
  8. Reboot the system

Let us see all steps in details.

Step 1. Get the latest Linux kernel source code

Visit the official project site and download the latest source code. Click on the big yellow button that read as “Latest Stable Kernel“:
Download Linux Kernel Source Code
The filename would be linux-x.y.z.tar.xz, where x.y.z is actual Linux kernel version number. For example file linux-5.6.9.tar.xz represents Linux kernel version 5.6.9. Use the wget command to download Linux kernel source code:
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.6.9.tar.xz
wget Linux kernel source code from kerne.org

Step 2. Extract tar.xz file

You really don’t have to extract the source code in /usr/src. You can extract the source code in your $HOME directory or any other directory using the following unzx command or xz command:
$ unxz -v linux-5.6.9.tar.xz
OR
$ xz -d -v linux-5.6.9.tar.xz

Verify Linux kernel tartball with pgp

First grab the PGP signature for linux-5.6.9.tar:
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.6.9.tar.sign
Try to verify it:
$ gpg --verify linux-5.6.9.tar.sign
Sample outputs:


gpg: assuming signed data in 'linux-5.6.9.tar'
gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
gpg:                using RSA key 79BE3E4300411886
gpg: Can't check signature: No public key

Grab the public key from the PGP keyserver in order to verify the signature i.e. RSA key ID 79BE3E4300411886 (from the above outputs):
$ gpg --recv-keys 79BE3E4300411886
Sample outputs:

gpg: key 79BE3E4300411886: 7 duplicate signatures removed
gpg: key 79BE3E4300411886: 172 signatures not checked due to missing keys
gpg: /home/vivek/.gnupg/trustdb.gpg: trustdb created
gpg: key 79BE3E4300411886: public key "Linus Torvalds <torvalds@kernel.org>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1

Now verify gpg key again with the gpg command:
$ gpg --verify linux-5.6.9.tar.sign
Sample outputs:

gpg: assuming signed data in 'linux-5.6.9.tar'
gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
gpg:                using RSA key 79BE3E4300411886
gpg: Good signature from "Linus Torvalds <torvalds@kernel.org>" [unknown]
gpg:                 aka "Linus Torvalds <torvalds@linux-foundation.org>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: ABAF 11C6 5A29 70B1 30AB  E3C4 79BE 3E43 0041 1886

If you do not get “BAD signature” output from the “gpg –verify” command, untar/extract the Linux kernel tarball using the tar command, enter:
$ tar xvf linux-5.6.9.tar

Step 3. Configure the Linux kernel features and modules

Before start building the kernel, one must configure Linux kernel features. You must also specify which kernel modules (drivers) needed for your system. The task can be overwhelming for a new user. I suggest that you copy existing config file using the cp command:
$ cd linux-5.6.9
$ cp -v /boot/config-$(uname -r) .config

Sample outputs:

'/boot/config-4.15.0-30-generic' -> '.config'

Step 4. Install the required compilers and other tools

You must have development tools such as GCC compilers and related tools installed to compile the Linux kernel.

How to install GCC and development tools on a Debian/Ubuntu Linux

Type the following apt command or apt-get command to install the same:
$ sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev
See “Ubuntu Linux Install GNU GCC Compiler and Development Environment” for more info.

How to install GCC and development tools on a CentOS/RHEL/Oracle/Scientific Linux

Try yum command:
$ sudo yum group install "Development Tools"
OR
$ sudo yum groupinstall "Development Tools"
Additional packages too:
$ sudo yum install ncurses-devel bison flex elfutils-libelf-devel openssl-devel

How to install GCC and development tools on a Fedora Linux

Run the following dnf command:
$ sudo dnf group install "Development Tools"
$ sudo dnf install ncurses-devel bison flex elfutils-libelf-devel openssl-devel

Step 5. Configuring the kernel

Now you can start the kernel configuration by typing any one of the following command in source code directory:

  • make menuconfig – Text based color menus, radiolists & dialogs. This option also useful on remote server if you wanna compile kernel remotely.
  • make xconfig – X windows (Qt) based configuration tool, works best under KDE desktop
  • make gconfig – X windows (Gtk) based configuration tool, works best under Gnome Dekstop.

For example, run make menuconfig command launches following screen:
$ make menuconfig
How to compile and install Linux Kernel 5.6.9
You have to select different options as per your need. Each configuration option has HELP button associated with it so select help button to get help. Please note that ‘make menuconfig’ is optional. I used it here to demonstration purpose only. You can enable or disable certain features or kernel driver with this option. It is easy to remove support for a device driver or option and end up with a broken kernel. For example, if the ext4 driver is removed from the kernel configuration file, a system may not boot. When in doubt, just leave support in the kernel.

Step 5. How to compile a Linux Kernel

Start compiling and tocreate a compressed kernel image, enter:
$ make
To speed up compile time, pass the -j as follows:
## use 4 core/thread ##
$ make -j 4
## get thread or cpu core count using nproc command ##
$ make -j $(nproc)

Linux kernel compiled and bzImage is ready
Compiling and building the Linux kernel going take a significant amount of time. The build time depends upon your system’s resources such as available CPU core and the current system load. So have some patience.

Install the Linux kernel modules

$ sudo make modules_install
How to install the Linux kernel modules

Install the Linux kernel

So far we have compiled the Linux kernel and installed kernel modules. It is time to install the kernel itself:
$ sudo make install
make install output
It will install three files into /boot directory as well as modification to your kernel grub configuration file:

  • initramfs-5.6.9.img
  • System.map-5.6.9
  • vmlinuz-5.6.9

Step 6. Update grub config

You need to modify Grub 2 boot loader configurations. Type the following command at a shell prompt as per your Linux distro:

CentOS/RHEL/Oracle/Scientific and Fedora Linux

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
$ sudo grubby --set-default /boot/vmlinuz-5.6.9

You can confirm the details with the following commands:
grubby --info=ALL | more
grubby --default-index
grubby --default-kernel

Debian/Ubuntu Linux

The following commands are optional as make install does everything for your but included here for historical reasons only:
$ sudo update-initramfs -c -k 5.6.9
$ sudo update-grub

How to build and install the latest Linux kernel from source code

You have compiled a Linux kernel. The process takes some time, however now you have a custom Linux kernel for your system. Let us reboot the system.

Reboot Linux computer and boot into your new kernel

Just issue the reboot command or shutdown command:
# reboot
Verify new Linux kernel version after reboot:
$ uname -mrs
Sample outputs:

Linux 5.6.9 x86_64

Conclusion – Linux Compile Kernel version 5.6.9

Configurations! You completed various steps to build the Linux kernel from source code and compiled kernel should be running on your system. I strongly suggest that you always keep backup of essential data and visit the kernel.org page here for more info.

sexta-feira, 22 de outubro de 2021

linux

nano /etc/systemd/system/vnc.service [Unit] Description = X11VNC pre-login After = multi-user.target [Service] Type = simple ExecStart = /usr/bin/x11vnc -ncache 10 -auth guess -nap -forever -loop -repeat -rfbauth /root/.vnc/passwd -rfbport 5900 [Install] WantedBy = multi-user.target Then to activate the systemd service: systemctl daemon-reload Then to enable the service on boot: systemctl enable vnc Then reboot, and it should come up on its own. shutdown -r now


For example, if we wanted to have the system date written in a file called date.txt when Linux restarts, we would add the following string:

@reboot date >> ~/date.txt

If we wanted to run the backup shell at reboot, we would add:

@reboot /root/backup.sh



 Note: In some cases, the crond service needs to be enabled on boot for the configuration to function.

To check if the crond service is enabled, use:

sudo systemctl status cron.service

To enable this service, use:

sudo systemctl enable cron.service

sexta-feira, 27 de novembro de 2020

Pattern Mediator

 Today accomplish the course of design pattern lynda.com.


I decide to make a post about it because is one way to not forget what I learn.


The design pattern that I show here is Mediator 


The actual dentition of a mediator is someone who tries to make people in conflict come to an  agreement.


In JAVA the mediator pattern is a way for objects to communicate so that they loosely coupled


Mediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.


let's see now this piece of code 


public class Customer{
  private String address;
  private ECommerceSite site;

  public Customer(String address) {
    this.address = address;
    site = new ECommerceSite(this);
  }

  public String getAddress() {
    return address;
  }

  public void buy(String item, int quantity) {
    if(site.checkInStock(item, quantity)) {
      site.sell(item, quantity);
    }
  }

}

In this class we have a Customer constructor and inside one call to e-commerce. Yes we have a code smell, but move on in this post we have some tips for that.


Here we have class Driver and one implementation of the method deliver who has some responsibility

and that's okay but need a micro reafactor.


public class Driver {

  public void deliver(String item, int quantity, Customer customer) {
    System.out.println(quantity + " " + item + " out for delivery to " + customer.getAddress());
  }

}
import java.util.HashMap;

public class ECommerceSite {

  private Customer customer;
  private Driver driver;
  private HashMap stock;


  public ECommerceSite(Customer customer) {
    this.customer = customer;
    this.driver = new Driver();
    stock = new HashMap();
    stock.put("pens", 100);
    stock.put("pencils", 50);
    stock.put("erasers", 75);
  }

  public boolean checkInStock(String item, int quantity) {
    if (stock.containsKey(item) && stock.get(item) > quantity) {
      return true;
    } else {
      return false;
    }
  }

  public void sell(String item, int quantity) {

    int newQuantity = stock.get("pens") - quantity;
    stock.put(item, newQuantity);

    driver.deliver(item, quantity, customer);
  }

}
There is way to make this code better. The final version of this code wich simplified a lot is rigth here below:
public class Customer {

  private String address;

  public Customer(String address) {
    this.address = address;
  }

  public String getAddress() {
    return address;
  }

}
public class Driver {
  // for this example we make this function loaded of parameters
  public void deliver(String item, int amount, Customer customer) {
    System.out.println(amount + " " + item + " out for delivery to " + customer.getAddress());
  }

}

public class Main {

  public static void main(String[] args) {

    Mediator mediator = new Mediator();
    mediator.buy("pens", 3);
  }

}
import java.util.HashMap;

public class ECommerceSite {

  private HashMap stock;

//in this version we use only attributes
//strictly from the class 
  public ECommerceSite() {
    stock = new HashMap();
    stock.put("pens", 100);
    stock.put("pencils", 50);
    stock.put("paper", 500);
  }

  public void sell(String item, int amount) {
    int newAmount = stock.get("pens") - amount;
    stock.put(item, newAmount);
  }

  public boolean checkInStock(String item, int amount) {
    if (stock.containsKey(item) && stock.get(item) > amount) {
      return true;
    } else {
      return false;
    }
  }

}
public class Mediator {

  private Customer customer;
  private ECommerceSite site;
  private Driver driver;

  public Mediator() {
    customer = new Customer("123 Sunny Street");
    site = new ECommerceSite();
    driver = new Driver();
  }

  public void buy(String item, int amount) {
    if(site.checkInStock(item, amount)) {
      site.sell(item, amount);
      driver.deliver(item, amount, customer);
    }
  }

}
And thats it one more pattern or a tool for make our code less messy.




What solution does the Mediator design pattern describe?

  • Define a separate (mediator) object that encapsulates the interaction between a set of objects.
  • Objects delegate their interaction to a mediator object instead of interacting with each other directly.

The objects interact with each other indirectly through a mediator object that controls and coordinates the interaction.

This makes the objects loosely coupled. They only refer to and know about their mediator object and have no explicit knowledge of each other.



sábado, 18 de julho de 2020

mariadb

sudo apt-get update

sudo apt-get install -y mariadb-server mariadb-client
$ sudo mysql -u root -p

sexta-feira, 26 de junho de 2020

kernel

sudo time make -j4 bzImage -> make a kernel without modules

sudo time make -j4 - > make a kernel with modules

sudo time make modules_install -> install modules

make install -> install the kernel

path to .config kernel ubuntu

sudo cp /boot/config-$(uname -r) .config

git template

git config commit.template /home/$USER/Desktop

example template

# Place a subject above the body, limit to 50 chars

# Specify the body of the commit one line below the subject wrapping at
# 70 characters to keep things neatly organized.  Here you should explain
# why the commit was created.

# You can use multiple paragraphs and other formatting to describe the
# commit.  Add details as necessary until you have provided enough info
# about the commit.

#    - Bullets can be added
#    - But should be persistent
 
# Additionally, work items can be tracked using their id numbers.

# Work Items Resole: 557, 188, 122

git commit editor

git config core.editor "/usr/bin/gedit"

interface python