Posts

Showing posts with the label Linux Commands

How to write cron job from linux command

Image
In this tutorial, How do i use linux command write cron job. Crontab example Run cron job At every 15th minute for zimbra_full_backup.sh */15 * * * * /opt/scripts/zimbra_full_backup.sh https://crontab.devopsroles.com/ Write cron job from linux command [root@huupv huuphan.com]# echo "$(crontab -l; echo '*/15 * * * * /opt/scripts/zimbra_full_backup.sh')" | crontab - The screen output terminal: Conclusion Thought the article, How to write cron job from linux command as above . I hope will this your helpful.

Linux find command

Image
In this tutorial, Using find command to search for file or directory in linux. How to use linux find command find all folder or file. Using -exec flag. Linux find command How to find all folder not contain path ".backup" as below [huupv@huupv huuphan.com]$ find ./ -type d -not -path "*/.backup*"    -exec ls -ld {} \; The screen output terminal: How to find all symbolic link in current folder. [huupv@huupv huuphan.com]$ find ./ -type l -exec ls -l {} \; The screen output terminal: Conclusion Thought the article, you can use Linux find command. I hope will this your helpful

Linux alias tips

Image
In this tutorial, How do I  use alias command to stop and start service with human language. Linux alias tips For example, you execute command " vagrant halt app1 " to stop server app1  or " vagrant halt db " to stop server db in Vagrant . you can use alias command the define command  "stop_app1" or "stop_db" Step 1: Add command end of line in .bashrc file  [huupv@huupv ~]$ vim .bashrc The terminal output as below Step 2: Apply the change [huupv@huupv ~]$ source .bashrc The screen output terminal: [huupv@huupv ~]$ cd my_ansible/example01/ [huupv@huupv example01]$ stop_app1 DEPRECATION: The 'sudo' option for the Ansible provisioner is deprecated. Please use the 'become' option instead. The 'sudo' option will be removed in a future release of Vagrant. ==> app1: Attempting graceful shutdown of VM... [huupv@huupv example01]$ stop_db DEPRECATION: The 'sudo' option for the Ansible provi

netstat command not found in CentOS 7 / RHEL 7

Netstat command is tool to check the network configure and activity. The default centos 7 and RHEL 7 not found netstat command. How to use netstat command on Centos 7 and RHEL 7 To find out which package provides "netstat" command. yum provides */netstat Or yum whatprovides */netstat The output screen as below net-tools-2.0-0.22.20131004git.el7.x86_64 : Basic networking tools Repo        : @base Matched from: Filename    : /bin/netstat Install netstat command # yum install net-tools The output screen as below: Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile  * base: ftp.jaist.ac.jp  * epel: mirror.ehost.vn  * extras: ftp.jaist.ac.jp  * updates: ftp.jaist.ac.jp Resolving Dependencies --> Running transaction check ---> Package net-tools.x86_64 0:2.0-0.22.20131004git.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================

Linux commands cheat sheet

System Commands Command Description Examples shutdown bring your system down shutdown -h now --> Halt or poweroff after shutdown shutdown -r now --> Reboot after shutdown halt stop your system. halt reboot reboot your system. reboot uptime Tell how long the system has been running. uptime runlevel find the previous and current system runlevel. runlevel printenv print all or part of environment printenv env run a program in a modified environment env hostname show or set the system's your host name hostname --> show the system's host name hostname newhostname -->- set the system's uname print system information uname -a --> print all information locale Displays information about the current locale, or

Run bash script on boot time on centos

How to Auto Execute Commands/Scripts During Reboot or Startup on centos . Many method to execute a command or run scripts during startup. Method 1: Linux Execute Cron Job After System Reboot Use @reboot in cron scheduler. It will run once, at startup after reboot your system. Edit crontab # crontab -e To run a script called /home/huuphan/auto_excute.sh @reboot /home/huuphan/auto_excute.sh Method 2: Using /etc/rc.d/rc.local file This method is vaild for systemd-based distributions. To grant execute permission to /etc/rc.d/rc.local # chmod +x /etc/rc.d/rc.local And add your script at the bottom of the file /etc/rc.d/rc.local # echo "/home/huuphan/auto_excute.sh" >> /etc/rc.d/rc.local Conclusion In this article, how to Run bash script on boot time on centos. Use two method: Crontab and /etc/rc.d/rc.local file.

Linux create user

Image
In this tutorial, I will guide use useradd create user in linux. The memo complete linux create user and how to user run sudo command with no password. Create user with useradd command Create User test with option: Specific user ID: -u 1234 Specific Group ID: -g 1002 Specific a Group IT and vboxusers: -G IT,vboxusers Specific bash shell: -s /bin/bash Specific the directory: -d /home/test Specific password: Whoami12345678 Running useradd command as below: sudo useradd test -d /home/test -s /bin/bash -u 1234 -g 1002 -G IT,vboxusers  ; echo -e "Whoami12345678\nWhoami12345678\n" | sudo passwd test Use id command verify [huupv@huupv ~]$ id test uid=1234(test) gid=1002(IT) groups=1002(IT),981(vboxusers) User run sudo command with no password [huupv@huupv ~]$ echo "test ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers To check [huupv@huupv ~]$ sudo visudo -c Verify, you can run sudo without a password [huupv@huupv ~]$ sudo cat /etc/hosts

Shell script parse xml in linux

Image
How to use shell script parse xml file . In this tutorial, I will use grep command, awk command and sed command Reading xml file extracting value in linux . For example xml person file as below <person>   <sex>Male</sex>   < firstname >Huu</ firstname >   <lastname>Phan</lastname>   <sex>female</sex>   < firstname >Miranda</ firstname >   <lastname>Kerr</lastname> </person> How to extracting value Huu and Miranda  of < firstname > < /firstname > Use grep command [huupv@huupv huuphan.com]$ grep -oP '(?<=<firstname>).*(?=</firstname)' person.xml Use awk command [huupv@huupv huuphan.com]$ awk -F "[><]" '/firstname/{print $3}' person.xml Use sed command [huupv@huupv huuphan.com]$ sed -n '/firstname/{s/.*<firstname>//;s/<\/firstname.*//;p;}' person.xml  The result, Shell script parse xml in linux

rename command in linux

Image
rename command similar with mv command. This command is slightly more advanced than mv  command. How to rename multiple files in linux. The syntax rename command rename [options] expression replacement-file For example, basic use rename command [huupv@huupv huuphan.com]$  rename file huu file1.txt The same output How to rename all file extension .txt to .atxt file [huupv@huupv huuphan.com]$ rename .txt .atxt *.txt The same output Use option --verbose in rename command [huupv@huupv huuphan.com]$ rename -v foo huu foo?.atxt The same ouput `foo2.atxt' -> `huu2.atxt' `foo3.atxt' -> `huu3.atxt' Details more information rename command man rename

Linux diff command

Image
In this tutorial, I use diff command output on screen. How to compare different line by line in  files. How to compare side by side, color , output do not output common lines with diff command. What diff command work? compare files line by line For example my topic, I compare line by line in 2 file: file1.txt and file2.txt The content file1.txt file [huupv@huupv huuphan.com]$ cat file1.txt <####################################> author: huupv My Blog: www.huuphan.com Hello everbody! <####################################> The content file2.txt file [huupv@huupv huuphan.com]$ cat file2.txt <####################################> author: huupv My Blog: www.devopsrole.com Hello everbody! <####################################> The default compare line by line with diff comand [huupv@huupv huuphan.com]$ diff file1.txt file2.txt The output as below: 3c3 < My Blog: www.huuphan.com --- > My Blog: www.devopsrole.com The output number

Openssl libssl.so.1.1 not found solve problem

How to solve problem Openssl libssl.so.1.1 not found on your system such as centos, ubuntu. Installing openssl missing depens libssl.so.1.1 not found and libcrypto.so.1.1 not found. Openssl libssl.so.1.1 not found problem as below: [huupv@huupv ~]$ ldd /usr/bin/openssl     linux-vdso.so.1 (0x00007ffe185e6000)     libssl.so.1.1 => => not found     libcrypto.so.1.1 => => not found     libz.so.1 => /lib64/libz.so.1 (0x00007fc3ca6f4000)     libdl.so.2 => /lib64/libdl.so.2 (0x00007fc3ca4f0000)     libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc3ca2d2000)     libc.so.6 => /lib64/libc.so.6 (0x00007fc3c9f1c000)     /lib64/ld-linux-x86-64.so.2 (0x00007fc3cb2a1000) Openssl libssl.so.1.1 not found solve problem sudo ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 sudo ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 The result, Openssl libssl.so.1.1 not found solve problem [huupv@huupv ~]$ ldd /usr/bin/openssl     li

sed command regex example

Image
In this tutorial, To use sed command regex such as: Lines starting from A till B printed, how to print between A line to B line , so on. sed command regex example The content sed_test file as picture below: Lines starting from 10 line till 15 line printed  [huupv@huupv ~]$ sed -n '10,12p' sed_test The output as below:     10    operator:x:11:0:operator:/root:/sbin/nologin     11    games:x:12:100:games:/usr/games:/sbin/nologin     12    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin How to Lines starting from 10 line till end line printed  [huupv@huupv ~]$ sed -n '10,$p' sed_test  The output as picture below: Conclusion   Thought the article, you can printed line to line of file use sed command. How to Lines starting from A line till end line printed 

centos change ssh port

in this tutorial, How to change ssh port another with selinux. The default ssh port 22, when you change port another with rules selinux (SELINUX=enforcing) get error can't change ssh port. Solve problem!( Running command following with root account) File selinux with content SELINUX=enforcing as below: [huupv@huupv ~]$ sudo cat /etc/sysconfig/selinux [sudo] password for huupv: # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: #     enforcing - SELinux security policy is enforced. #     permissive - SELinux prints warnings instead of enforcing. #     disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of these three values: #     targeted - Targeted processes are protected, #     minimum - Modification of targeted policy. Only selected processes are protected. #     mls - Multi Level Security protection. SELINUXTYPE=targeted How do it... Step 1 : Open sshd_config file # vi /etc/

Centos 7 managing services with Systemd

Centos 7 managing services with Systemd. -List all the current loaded targets as command below: $ sudo systemctl list-units -t target Each services has 3 states; enabled, disabled and static. - To list enabled services as command below: $ sudo systemctl list-unit-files --type=service | grep enabled - To start/stop/restart services as command below: # systemctl status <servicename> # systemctl stop <servicename> # systemctl start <servicename> # systemctl restart <servicename> - To check running ntp services as comman below: $ sudo systemctl list-units --type service --all | grep ntp - Set the service to start a boot: # systemctl enable <servicename> - Disable service start-up boot # systemctl disable service The update later related command line "Centos 7 managing services with Systemd" in this article.

List the contents of tar gz file

tar command is archiving utility. Something, you can retrieve extension .tar.gz or .tar.bz2 file. How to list the contents this without extracting from command line. The syntax tar command as below: tar OPTION File_Name With OPTION : t, --list : List the contents of an archive v : Verbosely list files processed j : Filter archive through bzip2 f : filename  You may reading link as below: Iptables how to How to reset password on centos 7 Bash script example List the contents of .tar.gz file To use tar command for .tar.gz as following: $ tar -tvf links-2.12.tar.gz  The output of links-2.12.tar.gz file as below: -rwxr--r-- 1000/1000      1215 2001-10-05 14:03 links-2.12/mailcap.pl -rwxr-xr-- 1000/1000      4654 2015-06-28 22:13 links-2.12/rebuild -rw-r--r-- 1000/1000       461 2003-05-06 01:10 links-2.12/links_16x16_1.xpm -rw-r--r-- 1000/1000      2410 2003-05-06 01:10 links-2.12/links_32x32.xpm -rw-r--r-- 1000/1000      3847 2005-03-01 00:16 links-2.12/links-48x48.xpm -

awk useful commands examples

awk useful comand examples. How to Output Field Separator Variable, Number of Fields in a record, To print Non-system users and ignoring "nobody" etc.  You may reading link below : How to reset root password on centos 7 CentOS/RHEL Use yum Command To Downgrade Upgrade or Rollback Updates Generate and Verify Files with MD5 Checksum in Linux use inotify-tools on centos For example, the output passwd file use awk command $ cat /etc/passwd | awk -F':' 'BEGIN{ print "Name\tUID,GID\tHomeDirectory" }{print $1"\t"$3,$4"\t"$6}' The output as below: Name UID,GID HomeDirectory root 0 0 /root bin 1 1 /bin daemon 2 2 /sbin adm 3 4 /var/adm lp 4 7 /var/spool/lpd sync 5 0 /sbin shutdown 6 0 /sbin halt 7 0 /sbin mail 8 12 /var/spool/mail operator 11 0 /root games 12 100 /usr/games ftp 14 50 /var/ftp nobody 99 99 / systemd-bus-proxy 999 998 / systemd-network 192 192 / dbus 81 81 / polkitd 998 997 / tss 59 59 /dev/null s

CentOS/RHEL Use yum Command To Downgrade Upgrade or Rollback Updates

Use yum Command To Downgrade Upgrade or Rollback Update. How to downgrade the package? How to upgrade the package? How to rollback update the package?. I'm running commands with root account. You may reading link below : How to reset root password on centos 7 Generate and Verify Files with MD5 Checksum in Linux use inotify-tools on centos awk useful commands examples How to  Downgrade Upgrade package use yum command The basic syntax Downgrade package yum downgrade package1 yum downgrade package1 package2 showing multiple versions of a package yum --showduplicates list package For example How to Downgrade php-fpm 7.0.21 roll-back php-fpm 5.3.3 yum downgrade php-fpm The output as bellow Resolving Dependencies --> Running transaction check ---> Package php-fpm.x86_64 0:7.0.20-1.el6.remi will be a downgrade --> Processing Dependency: php-common(x86-64) = 7.0.20-1.el6.remi for package: php-fpm-7.0.20-1.el6.remi.x86_64 ---> Package php-fpm.x86_64 0:7.0

Generate and Verify Files with MD5 Checksum in Linux

How to generate and Verify Files with MD5 Checksum in Linux. in my post, to create "checksum" folder and 3 file: file_md5,file2_md5,file3_md5 in "checksum" folder. You may reading link below : How to reset root password on centos 7 CentOS/RHEL Use yum Command To Downgrade Upgrade or Rollback Updates use inotify-tools on centos awk useful commands examples MD5 sums are 128-bit character strings(numerals and letters) resulting from running the MD5 algorithm. To create checksum folder with 3 files $ mkdir checksum $ echo "huuphan.com" > file_md5 $ echo "Phan Van Huu" > file2_md5 $ echo "huupv" > file3_md5 $ ls -ll total 12 -rw-r--r-- 1 huupv huupv 13 Jul 13 09:04 file2_md5 -rw-r--r-- 1 huupv huupv  6 Jul 13 09:04 file3_md5 -rw-r--r-- 1 huupv huupv 12 Jul 13 09:03 file_md5 To generate MD5 Checksum on a single file $ md5sum file_md5 01ff693ecd0492aca683eed0dcd2bb44  file_md5 To generate MD5 Checksum on multiple

How to reset root password on centos 7

Image
Many methods to reset root password centos 7. if GRUB 2 bootloader then no longer using in single-user mode as well as emergency mode. To below step by step to reset root password centos 7. You may reading link below : awk useful commands examples CentOS/RHEL Use yum Command To Downgrade Upgrade or Rollback Updates Generate and Verify Files with MD5 Checksum in Linux use inotify-tools on centos Step 1: Start the system, on Grub 2 boot screen, you press the e key as shown below. Step 2: In linux16 line or linuxefi on UEFI system, add parameters end line as shown below. rw init=/bin/bash Using Ctrl+A press or Ctrl+E press to start or end a line. Disable paramenters the rhgb and quiet in order to enable system message as shown top. Step 3: you press Ctrl+x to start single user mode passwd Step 4: update selinux information touch /.autorelabel Step 5: To resume the initialization and finish the system boot. exec /sbin/init Video how to reset root password on cen

use inotify-tools on centos

inotifywait - wait for changes to files using inotify. To install inotify-tools on centos sudo yum install inotify-tools To install inotify-tools on ubuntu sudo apt-get install inotify-tools For example, how to monitor folder /home/huupv with action create,delete,modiy,move. The scripts as below: #!/bin/bash #Author huupv #My blog huuphan.com inotifywait -m -r /home/huupv -e create -e delete -e modify -e move | while read FOLDER ACTION1 ACTION2 ACTION3 ACTION4 do         echo "Path $FOLDER Create $ACTION1" >>/tmp/output         echo "Path $FOLDER Delete $ACTION2" >>/tmp/output         echo "Path $FOLDER Modify $ACTION3" >>/tmp/output         echo "Path $FOLDER Move $ACTION4" >>/tmp/output done The man page inotifywait man inotifywait NAME        inotifywait - wait for changes to files using inotify SYNOPSIS        inotifywait  [-hcmrq]  [-e  <event> ] [-t <seconds> ] [--format <fmt> ]