How to create an autostart script in Linux (Red Hat / CentOS / Fedora)

How to create an auto start script in Linux (Red Hat / CentOS / Fedora):

Option 1: Use chkconfig script to run /etc/init.d/…

  1. Create a script and place in /etc/init.d (e.g /etc/init.d/myscript). The script should have the following format:
#!/bin/bash
# chkconfig: 2345 20 80
# Source function library.
. /etc/init.d/functions
start() {
    # code to start app comes here 
    # example: daemon program_name &
}
stop() {
    # code to stop app comes here 
    # example: killproc program_name
}
case "$1" in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       # code to check status of app comes here 
       # example: status program_name
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0 

Enable the script

  1. $ chkconfig --add myscript 
    $ chkconfig --level 2345 myscript on 
    
  2. Check the script is indeed enabled – you should see “on” for the levels you selected.
    $ chkconfig --list | grep myscript
    

You can then use the script like so /etc/init.d/myscript start or chkconfig myscript start.

Option 2: Another Option is to use crontab job and run it at Boot time.

You need to use special string called @reboot. It will run once, at startup after reboot command.

@reboot  /path/to/job
@reboot  /path/to/shell.script
@reboot  /path/to/command

This is an easy way to give your users the ability to run a shell script or command at boot time without root access. First, run crontab command:
$ crontab -e
OR
# crontab -e -u doddi

Run a script called /home/doddi/bin/myScript.sh
@reboot /home/doddi/bin/myScript.sh 

Under RHEL / CentOS / Fedora, you need to enable crond on boot:
# chkconfig crond on
# service crond restart

If you are using modern distro with systemd, try
# systemctl enable crond.service
# systemctl restart crond.service
# systemctl status crond.service

 

How to Recover Deleted Files (rm -rf) or Accidentally click Empty Recycle Bin

So, sometimes “shit” can happen. Accidentally do rm -rf in linux OS, or click empty recycle bin in windows. Or, disk is corrupt, boot sector is broken, etc. This is a very useful tool to help you to “try” to fix it. Sometimes it works, and sometimes it doesn’t. At least you try… 😉 Err, wrong emoticon, it should be 🙁

TestDisk is OpenSource software and is licensed under the terms of the GNU General Public License (GPL v2+).

TestDisk is powerful free data recovery software! It was primarily designed to help recover lost partitions and/or make non-booting disks bootable again when these symptoms are caused by faulty software: certain types of viruses or human error (such asaccidentally deleting a Partition Table). Partition table recovery using TestDisk is really easy.

TestDisk can

  • Fix partition table, recover deleted partition
  • Recover FAT32 boot sector from its backup
  • Rebuild FAT12/FAT16/FAT32 boot sector
  • Fix FAT tables
  • Rebuild NTFS boot sector
  • Recover NTFS boot sector from its backup
  • Fix MFT using MFT mirror
  • Locate ext2/ext3/ext4 Backup SuperBlock
  • Undelete files from FAT, exFAT, NTFS and ext2 filesystem
  • Copy files from deleted FAT, exFAT, NTFS and ext2/ext3/ext4 partitions.

Download the file from here: http://www.cgsecurity.org/wiki/TestDisk_Download

 

Kind Regards,
Doddi Priyambodo

CentOS 7. Which installer need to Download: Minimal, DVD, or Everything?

Size of CentOS 7 iso. To determine which package that you want to install/download (DVD, Everything, or Minimal).

4.0G CentOS-7-x86_64-DVD-1503-01.iso
7.1G CentOS-7-x86_64-Everything-1503-01.iso
696M CentOS-7-x86_64-LiveCD-1503.iso
1.0G CentOS-7-x86_64-LiveGNOME-1503.iso
1.2G CentOS-7-x86_64-LiveKDE-1503.iso
636M CentOS-7-x86_64-Minimal-1503-01.iso
360M CentOS-7-x86_64-NetInstall-1503.iso

Better to start with minimal, and add package needed with yum install package later on after installation.

 

Kind Regards,
Doddi Priyambodo

YUM Repository Installation for RedHat / CentOS / Fedora

 

There are several people still asking about how to create YUM Repository in their Local Linux distribution. So, here you go…

Step 1: Install “createrepo”

To create Custom YUM Repository we need to install additional software called “createrepo” on our cloud server. We can install “createrepo” by running the following command from console:

yum install createrepo

Step 2: Create Repository directory

We need to create a new directory that will be the location of our Custom YUM Repository and will hold the desired RPM package files. We can do this with the following command from console (choose a different /repository1 directory name if you like):

mkdir /repository1

Step 3: Put RPM files to Repository directory

If RPM package files are not yet present on our VPS we need to transfer them to our cloud server via FTP or SSH – use software like WinSCP (free SFTP client and FTP) or similar. We can also download RPM package files directly to our VPS (internet connection needed) with “wget” command from console (please change HTTP link accordingly): Continue reading YUM Repository Installation for RedHat / CentOS / Fedora

Mekanisme IP Port Forward di Linux, Mikrotik, Windows

IP & Port Forwarding. Mekanisme port forward adalah sebuah cara untuk melakukan penerusan paket ke tempat lain, tanpa client perlu mengetahui dan merasakan bahwa sebenarnya paket itu bukan ditembak ke server tujuan dari client. Melainkan dialihkan ke server yang lain.

Konfigurasi : CLIENT —>SERVER (published) —> SERVER (local)

Mekanisme untuk melakukan Port Forwarding adalah sebagai berikut :

1. LINUX

http://www.uday.com.np/tools/iptables.php (ini untuk wizard secara visualisasi)
http://linux.about.com/od/ipm_howto/a/hwtipm52t00.htm (ini script otomatisnya)

Contoh Kasus :

1). Kita ingin mengalihkan seluruh request dari client yang masuk dengan port 15035 menuju ke 192.168.121.5:15035

# iptables -t nat -I PREROUTING -p tcp –dport 15035 -j DNAT –to 192.168.121.5

Contoh lain :
# iptables -t nat -A PREROUTING -p tcp –dport 90080 -j DNAT –to 10.4.1.15:80

Contoh lain :
# iptables -t nat -A PREROUTING -d 192.168.204.90 -j DNAT –to-destination 10.19.184.2
# iptables -A FORWARD -j LOG –log-prefix ‘DROPPING: ‘ –log-level info
# tail -fn100 /dev/log/messages | grep DROPPING

2). Kita ingin menjalankan APACHE TOMCAT yang berjalan secara lokal di port 8080, tetapi kita ingin menampilkan keluar sebagai port 80 tanpa merubah konfigurasi TOMCAT, tetapi menggunakan iptables.

# iptables –table nat –append PREROUTING –protocol tcp –destination-port 80 –in-interface eth0 –jump REDIRECT –to-port 8080

Catatan:
Aturan diatas tidak bisa melakukan redirect permintaan lokal. Karena menggunakan perintah PREROUTING. Jika ada client yang jalan langsung di server, maka gunakan port 8080.

2. MIKROTIK

Berikut ini mekanisme untuk membelokkan paket dari server 202.162.111.111:80 ke 10.14.1.101:8080

/ip firewall nat add chain=dstnat dst-address=202.162.111.111 protocol=tcp dst-port=80
action=dst-nat to-addresses=10.14.1.101 to-ports=8080

3. WINDOWS

Biasanya saya menggunakan aplikasi Free dari Internet untuk tujuan ini, yaitu :
– http://www.simpleportforwarding.com/
– http://www.quantumg.net/portforward.php