Posts

Showing posts with the label Linux Commands

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> ]

screen command on linux

To install screen on linux For centos/RHEL base system sudo yum install screen -y For ubuntu base system sudo apt-get install screen -y screen command useful for wget,ryncs not lose session, when you detach it. For example, create the screen with title $ screen -t 'zimbra' ssh [email protected] pressing "ctrl+a" n: next the screen (switch between screen) pressing "ctrl+a" p: previous the screen (switch between screen) pressing "ctrl+a" x : lock the screen pressing "ctrl+a" k: kill the screen pressing ctrl+a d : deattach To list the screen [huupv@huupv ~]$ screen -ls There is a screen on:     31083.pts-0.huupv    (Detached) 1 Socket in /var/run/screen/S-huupv. To resume the screen screen -r 31083 How to deattached screen with Attached status [huupv@localhost]$ screen -ls The output There is a screen on: 20655.pts-4.ra3 (Attached)1 Socket in /var/run/screen/S-huupv. [huupv@localhost]$ screen -r -d 20655.pts-4.ra3 Error

Linux basic commands

Linux basic commands How to check version Ubuntu lsb_release -a How to check to distribute cat /etc/*release How to check CPU information cat /proc/cpuinfo How to check PCI devices lspci How to list all USB devices lsusb How to get PC name from IP nmblookup -A <ip> nbtscan <ip> How many connect on server who -H How many core processor nproc How to check full CPU information lscpu  How to create new user adduser huupv passwd huupv For centos usermod -aG wheel huupv For ubuntu echo "huupv ALL=(ALL) ALL" >> /etc/sudoers How to find UUID disk blkid /dev/sda6 lsblk -f To continuous....

How to send email using mail command in linux

Image
in my post, i'm use mail command to send message email. mail -s "Subject title huuphan.com" -S [email protected] <<EOF ---------------------------------------- The content email Hello phan van huu My site: huuphan.com ----------------------------------------- EOF The script sending 100 email from command. #!/bin/sh i=1 for (( i = 1 ; i <= 100 ; i++ )); do  echo "Test Mail $i, Send from mail.huuphan.com" | mailx -v -r "[email protected]" -s "Test Mail $i" -a "/tmp/quotausage" -S smtp="mail.huuphan.com:25" -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="123456789" -S ssl-verify=ignore -S nss-config-dir=/etc/ssl/certs [email protected],[email protected] done

xdotool − command−line X11 automation tool

Image
To install xdotool on ubuntu sudo apt-get update sudo apt-get install xdotool To install xdotool on fedora sudo yum install xdotool To display the current coordinates of the mouse cusrsor on linux #while true; do clear; xdotool getmouselocation; sleep 0.1; done The simple script for xdotool #!/bin/bash xdotool mousemove 343 755 click 1 sleep 2 while true do     xdotool mousemove 392 44 click 1     xdotool key "Return"     sleep 3     xdotool key Page_Up     sleep 3     xdotool mousemove 277 580 click 1     sleep 3     xdotool key Page_Down     sleep 3     xdotool mousemove 718 152 click 1     sleep 3     xdotool mousemove 46 482 click 1     sleep 3     xdotool mousemove 383 15 click 1     sleep 3 done Man xdotool http://man.cx/xdotool