Category Archives: Linux

Discover DevOps roles and learn Linux from basics to advanced at DevOpsRoles.com. Detailed guides and in-depth articles to master Linux for DevOps careers.

Install Netdata on RHEL 7 / CENTOS 7

In this tutorial, How to install Netdata on RHEL 7 / CENTOS 7.

Netdata is performance and health monitoring for systems and applications. It is a monitoring agent you install on all your systems.

Why use Netdata

  • High-resolution metrics
  • Monitors everything
  • Install and get results immediately
  • Requires zero dedicated resources
  • Open-source, free and very easy
  • Auto-scaling of chart units
  • Time-series back-ends supported – it can archive its metrics on graphite, , Prometheus, JSON document DBs, in the same or lower detail

Install Netdata on RHEL 7 / CENTOS 7 from source

Install EPEL Repository

[vagrant@DevopsRoles ~]$ sudo yum install -y epel-release

Installing all dependencies for Netdata.

[vagrant@DevopsRoles ~]$ sudo yum install Judy-devel libuv cmake json-devel autoconf-archive autogen json-c-devel libmnl-devel libuv-devel lz4-devel nmap-ncat openssl-devel python3 git zlib-devel libuuid-devel libmnl gcc make git autoconf automake pkgconfig curl findutils

Clone Netdata from Github

[vagrant@DevopsRoles ~]$ git clone https://github.com/netdata/netdata.git --depth=100
[vagrant@DevopsRoles ~]$ cd netdata/

The build and installation Netdata on RHEL 7 / CENTOS 7

[vagrant@DevopsRoles ~]$ sudo ./netdata-installer.sh

The output terminal as the picture below

Netdata service will start auto after installation.

To stop netdata run

[vagrant@DevopsRoles ~]$ sudo systemctl stop netdata

To start netdata run

[vagrant@DevopsRoles ~]$ sudo systemctl start netdata

Accesing Netdata from Browser

The default listens on all IPs on port 19999. If you have firewalld, allow access to this port within LAN.

[vagrant@DevopsRoles ~]$ sudo firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" \
source address="192.168.1.0/24" port protocol="tcp" port="19999" accept'

Reload Firewalld to apply changes

[vagrant@DevopsRoles ~]$ sudo firewall-cmd --reload

Access Netdata Web dashboard on server

Thank you for reading the DevopsRoles page!

create user and group in Linux

In this tutorial, I will quickly guide create user and group in Linux. How to add a user to a group in Linux. How to create users has permission or user no has permission.

Using command to create the and command to create the in Linux system.

How to create user and group in Linux

create group new in Linux

use groupadd command as below

$ sudo groupadd -g 2000 common # group common no has sudo permission
$ sudo groupadd -g 2001 dev # group dev has sudo permission
$ sudo groupadd -g 1100 IT

Create user new in Linux

For example, user user01 no has permission and user has permission

# useradd -m -d /home/user01 -s  /bin/bash   -g common -u 5000  user01; echo -e "user01\nuser01\n"  |  passwd  user01
# useradd -m -d /home/huupv  -s  /bin/bash   -g IT  -G dev    -u 5001  huupv; echo -e "huupv\nhuupv\n"  |  passwd  huupv

Add group to sudoers file

use visudo to open and edit the /etc/sudoers file and add group dev as follow below

%dev ALL=(root) ALL

Option

Create a user with sudo without password Linux.

# useradd -d /home/devops -u 8888 -s /bin/bash						
# echo -e "devops\ndevops\n" |  passwd devops						
# echo "devops ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/devops						
# chmod 0440 /etc/sudoers.d/devops						

Conclusion

Through the article, You can create user and group in Linux as above. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Change timezone in Linux

In this tutorial, I will change timezone in Linux by command line. How to change the to your current time zone.

  • The location of the local time file in Linux is /etc/localtime.
  • Timezone files are located in /usr/share/zoninfo/

To change the timezone in the Linux command line, you can use the methods below, which are available on most modern Linux distributions. Here’s how you can do it:

How to Change Timezone in Linux with Two Methods.

Method 1: I will copy from /usr/share/zoneinfo/Asia/Ho_Chi_Minh to /etc/localtime

$ sudo cp /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime

Method 2: I will delete and make a symbolic Linux from //share//Asia/Ho_Chi_Minh to /etc/ as following below

$ sudo rm -f /etc/localtime
$ sudo ln -s /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime

Verify the new timezone:

Conclusion

After changing the timezone, you may need to restart services or applications that rely on the system time to ensure they reflect the new timezone.

You changed your current timezone You can type date command to verify and you’re done. I hope this is helpful you.

How to configure a static IP address on Linux

In this tutorial, How to configure a static IP address on Linux. Step by step to make a static IP address on Linux.

Note: To change the network setting a good idea to make a copy of any configured file before your changes.

Configure a static IP address on Linux

For RHEL 8

How to configure a static IP address on an RHEL system. using command shown list network connections and devices on the system.

$ nmcli dev status

Now, To change the network interface from dynamic to static, you need to edit the file in folder “/etc//network-scripts”. In this example, I will create new file -Comtrend7FB9

Configuring a static IP address as below

HWADDR=7C:67:2A:CC:DF:8F
ESSID=Comtrend7FB9
MODE=Managed
KEY_MGMT=WPA-PSK
SECURITYMODE=open
MAC_ADDRESS_RANDOMIZATION=default
TYPE=Wireless
IPADDR=192.168.3.4  # Ip address static for Linux server
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static      
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=Comtrend7FB9
UUID=3f5a6317-27c7-249f-bfaa-1d2fa5283482
ONBOOT=yes

Restarting NetworkManager.

$ sudo systemctl restart NetworkManager

For Ubuntu 18.10

Using (network manager command-line interface) command to list the network interface on Ubuntu server.

$ nmcli d

For example, configure a static IP address on the Ubuntu system.

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto enp0s25
iface enp0s25 inet static
    address 192.168.3.4
    netmask 255.255.255.0
    network 192.168.3.1
    broadcast 192.168.3.255

Restart the networking service.

$ sudo  systemctl restart NetworkManager.service

Configure Static IP On CentOS 6

You can update/edit as follow static IP configure. For example, I will update for eth0 interface network.

HWADDR=00:08:A2:0B:BA:B7
TYPE=Ethernet
BOOTPROTO=none
# IP static for Server #
IPADDR=192.168.3.4
# Subnet #
PREFIX=24
# Set default gateway IP for server #
GATEWAY=192.168.3.1
# Set dns servers #
DNS1=192.168.3.1
DNS2=8.8.8.8
DNS3=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
# Disable ipv6 #
IPV6INIT=no
NAME=eth0
# This is system specific and can be created using 'uuidgen eth0' command #
UUID=31171a6f-bcg1-44de-8h6e-cf8e782f8bd6
DEVICE=eth0
ONBOOT=yes

Save and close the file. you need to restart the service network to apply the configure for the eth0 interface network.

$ sudo systemctl restart network

Conclusion

You have configure a static IP address on Linux. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Get permission of chmod as numerical value on Linux

chmod command is used to change the permissions of files or directories. In this tutorial, I will get of as value on Linux.

For example, Check Linux file and folder permissions with the ls command as below

[vagrant@app1 ~]$ ls -l /home/vagrant/*
-rw-rw-r--. 1 vagrant vagrant   64 Feb 26 15:30 /home/vagrant/a
-rwxrwxr-x. 1 vagrant vagrant  241 Dec 10 15:31 /home/vagrant/bashscript.sh
-rw-rw-r--. 1 vagrant vagrant 3327 Nov 13 12:07 /home/vagrant/id_rsa
-rw-rw-r--. 1 vagrant vagrant  439 Feb 26 15:30 /home/vagrant/lastmodifyofifle.sh

Using stat command as below

[vagrant@app1 ~]$ stat --format='%a %U:%G %n' /home/vagrant/*

The result as below

664 vagrant:vagrant /home/vagrant/a
775 vagrant:vagrant /home/vagrant/bashscript.sh
664 vagrant:vagrant /home/vagrant/id_rsa
664 vagrant:vagrant /home/vagrant/lastmodifyofifle.sh

You make it 🙂

Conclusion

You have to get permission of chmod as numerical value on Linux. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Configure SSH connection with key authentication

In this tutorial, I will configure the SSH connection with key authentication.

How it works

  • (Client) Generate a set of secret key and public key
  • (Connection destination server) Register public key
  • (Connection destination server) Accepts a connection from a client, encrypts the random number using the public key
  • (Connection destination server) Hashed random number
  • (Client) hashes the received random number and forwards the hash value to the server
  • (Connection destination server) Compare the hash value sent by the client and the hash value generated on the server side

Set up SSH connection with key authentication.

1: Generation of a key set

ssh-keygen -t rsa -f client01

2: Public Key Registration

# Work on the server, target users to connect
cat client01.pub >> .ssh/authorized_keys

3: Enabling Key Authentication

  # vim /etc/ssh/sshd_config
  RSAAuthentication yes
  PubkeyAuthentication yes
  AuthorizedKeyFile .ssh/authorized_keys
  # Disable password authentication if you want to strengthen it
   PasswordAuthentication no

Try to connect

ssh -i .ssh/client01 -p12345 huupv@devopsroles

Conclusion

Thought the article, How to configure SSH connection with key authentication in Linux. Thank you for reading the DevopsRoles page!

systemd service unit notes

In this tutorial, The describe service unit notes.

  • is a system and service manager for Linux.
  • init is no longer an old Linux.
  • Only the command can be used for starting and stopping.

systemd service unit file

  • It is arranged under “/etc/systemd/system” with “unitname.service
  • Others include directories like “/usr/lib/systemd” and “/lib/systemd“, but rather for systems.

Service section

Describe the operation parameters of the unit.

Type

The relationship between the execution command and the main process is represented by Type.

  • simple: ExecStart command remains as it is process
  • forking: Child process of ExecStart is process
  • : Even if ExecStart ends, the main process will remain.

Restart

Restart conditions can be specified with parameters equivalent to respawn in the old inittab.

  • always: always rerun
  • on-abort: re-execute when terminating with a signal that can not be caught
  • on-watchdog: Rerun with monitor timeout
  • on-abnormal: Re-execution when terminating with a signal other than SIGHUP, SIGINT, SIGTERM or SIGPIPE
  • on-failure: re-execution when the main process ends with a code other than the normal termination code
  • on-success: Rerun when the main process ends with a normal exit code

Install section

Specify which target (old ) should be executed when th is activated ( enable).

For example, when enabling in multi-user mode (old run level 3), a symbolic link to a unit file is established under “/etc//system/multi-user.target“.

[Unit]
Description=Description
After=Execute after starting the specified unit list
Before=Execute before the specified unit list
Requires=Execute after the specified unit list has been successfully started
Wants=Even if the specified unit list fails to start up

[Service]
Environment=environment variable list
EnvironmentFile=environment variable file
Type=simple|forking|oneshot
ExecStart=start command
ExecStop=stop command
ExecReload=reload command
Restart=On-abort|on-watchdog|on-abnormal|on-failure|on-success|no
RemainAfterExit=yes|no
PIDFile=PID file path of main process
User=ExecXX execution user
SuccessExitStatus=(other than 0) EXIT code list to be regarded as a normal completion of the main process

[Install]
Alias=service alias list
WantedBy=target list
Also=unit list installed together

used commands

Unit file installation (loading)

$ sudo systemctl daemon-reload

Enable/disable

$ systemctl enable|disable unitname

Dependency display

$ systemctl list-dependencies unitname

For example, List Dependency of chronyd package.

Confirm startup sequence

Summary of time spent on startup (kernel, initrd, user)

$ systemd-analyze time

Time is taken to activate unit (currently active unit)

$ systemd-analyze blame

SVG output startup sequence

$ systemd-analyze plot > systemd.sequence.svg 

Confirm log

$ journalctl -xe 
$ journalctl -xe -S "2019-01-22 23:00:00"  
$ journalctl -xe -S "2019-01-22 23:00:00" -U "2019-01-22 23:10:00" 
$ journalctl -xe -u unitname
$ journalctl -f -u servicename 
$ journalctl --disk-usage

$ lsof -p $(pidof systemd-journald)

Reference information

  • man .service
  • man -analyze

unit file example

Apache system (fork daemon)

myhttpd.service unit file

[Unit]
Description=HTTP Server
After=web.service

[Service]
Type=forking
ExecStart=/opt/bin/apachectl start
ExecStop=/opt/bin/apachectl graceful-stop
PIDFile=/opt/logs/httpd.pid
Restart=on-failure

[Install]
WantedBy=multi-user.target

web.service unit file

[Unit]
Description=WebSphere Application Server apserver
After=network.target network.service

[Service]
Type=forking
ExecStart=/opt/AppServer/profiles/AppSrv01/bin/startServer.sh apserver
ExecStop=/opt/AppServer/profiles/AppSrv01/bin/stopServer.sh apserver
PIDFile=/opt/AppServer/profiles/AppSrv01/logs/apserver/apserver.pid
Restart=on-failure
User=webuser
SuccessExitStatus=143 0
TimeoutStopSec=0
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

systemd service unit notes. Thank you for reading the DevopsRoles page!

Directory size displayed by du command does not match

When the directory size displayed by the du command does not match between the parent directory and child directory in Linux. How to solve the problem? The parent directory is and the child hidden directory is “.delete”

du command does not match directory size displayed

Folder devopsroles with 12M size as below

[huupv@server01 ~]$  du -sh devopsroles
12M     devopsroles

Contain files and folders in devopsroles as below

[huupv@server01 devopsroles]$ ls -la
total 16
drwxr-xr-x  3 huupv huupv 4096 Feb 13 16:29 .
drwxr-xr-x 16 huupv huupv 4096 Feb 13 16:29 ..
drwxr-xr-x  2 huupv huupv 4096 Feb 13 16:07 .delete
-rw-r--r--  1 huupv huupv   17 Feb 13 16:29 note.txt
-rw-r--r--  1 huupv huupv    0 Feb 13 16:29 tmp.txt

In folder devopsroles different size with du -sh command.

[huupv@server01 ~]$ cd devopsroles/
[huupv@server01 devopsroles]$ pwd
/home/huupv/devopsroles
[huupv@server01 devopsroles]$ du -sh ./* | sort -hr
4.0K    ./note.txt
0       ./tmp.txt

12M=4.0K + 0? Why does not it match?

Cause: du command does not show hidden directory size.

If you do the following, the file size including the hidden directory is displayed as the command below.

[huupv@server01 devopsroles]$ du -sh .[^.]*/ ./* | sort -hr
12M     .delete/
4.0K    ./note.txt
0       ./tmp.txt

Conclusion

In this tutorial, you can solve the problem “directory size displayed by du does not match between the parent directory and child directory” in Linux. Thank you for reading the DevopsRoles page!

cal command in Linux with Examples

cal command means Printing an ASCII calendar of the given month or year. cal command is used to see the calendar of a specific month or a whole year.

Syntax

cal [options] [[[day] month] year]
cal [options] [timestamp|monthname]

On the man page, the describes it

  • cal – display a calendar.
  • man cal – More details information about cal command.

cal command in Linux with Examples

$ cal

Display last month, this month, and next month.

Display this entire year’s calendar.

Conclusion

cal command is a simple command in Linux. It is the most popular in use terminal Linux display a calendar. Thank you for reading the DevopsRoles page!

How to Use the Date Command in Linux: Step-by-Step Examples

Introduction

Linux offers various command-line tools that simplify system management, one of which is the date command in Linux. This command is crucial for both system administrators and regular users as it displays or sets the system’s date and time.

date command syntax

date [OPTION]… [+FORMAT]
date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]

In the manual page, it describes the command as follows:
date – prints or sets the system’s date and time.
For more detailed information about the date command, refer to the manual page by entering man date in the terminal.

date command in Linux with Examples

$ date

Get year-month-day

[huupv@huupv devopsroles]$ date +%Y-%m-%d
2019-03-10

Get date command usage in Linux

[huupv@huupv devopsroles]$ date -d now
Sun Mar 10 13:18:55 +07 2019
[huupv@huupv devopsroles]$ date -d tomorrow
Mon Mar 11 13:19:00 +07 2019
[huupv@huupv devopsroles]$ date -d yesterday
Sat Mar  9 13:19:06 +07 2019
[huupv@huupv devopsroles]$ date -d last-sunday
Sun Mar  3 00:00:00 +07 2019
[huupv@huupv devopsroles]$ date +'%Y-%m-%d'
2019-03-10

Customizing the Output

One of the powerful features of the date command is its ability to format the output. You can customize how the date and time are displayed using format specifiers. For example, to display the date in YYYY-MM-DD format, you would use:

date +%Y-%m-%d

Conclusion

The date command in Linux is a straightforward yet widely used tool in Linux. It allows users to either display or set the system’s date and time from the terminal. Thank you for visiting the DevopsRoles page!