Category Archives: OS/application/sys admin

Operating System / system administration / etc

Apache Common Htaccess Usage (2.2 & 2.4)

  1. Maintenance mode except subnet network.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/offline\.html$
    RewriteCond %{REMOTE_ADDR} !^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$
    RewriteRule ^(.*)$ http://yourdomain.com/offline.html [R=307,L]

    offline.html (store your offline messages)
    !^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$   (excepet subnet 10.0.0.0/8)
    Tested on apache 2.4  and 2.2 on CentOS 7 and CentOS 6
Advertisements

Linux : Add Active Directory Authentication on CentOS 7

Firstly, we need to make sure our Active Directory (AD) account has the privilege to add/join any server to domain server. Contact AD administrator.
on CentOS 7 :-

  1. Install ssd, realmd, adcli
  2. Install ossjob, ossjob-mkhomedir, krb5-workstation,openldp-clients  (OPTIONAL)
  3. Run the following command to add, check
    • # realm join –user=<yourusername><space><yourdomain> .
      e.g : realm join –user=apitto myapitdomain.com
    • # realm list
    • realm discover -v myapitdomain.com
  4. Add user AD to your server
    • #su – <full-user-name-with-domain>
      e.g: su – pengurus@myapitdomain.com

Done.

Tested on CentOS 7.

Linux / Windows : Automatically backup mysql (on linux) and tranfer backup file to windows (via FTP)

Automatically backup mysql (on linux) and tranfer backup file to windows (via FTP).
This task will involve 2 types of environment (OS), Windows and Linux.

[Windows Section]
1. Setup FTP server on your windows environment using windows own software or third party software.
2. Create an account and allow connection only from the server(Linux) IP address.

[Linux Section]
1. Create an account on mysql with backup privileges ONLY (normally all read privileges).
2. Create an folder in your home directory. Example : /home/anas/mysql_backup_scheduler
3. Create an empty shell file inside /home/anas/mysql_backup_scheduler. Example: backup_and_ftp.sh
4. Inside backup_and_ftp.sh , put the content below and replace accordingly : –

---- BOF -----

#!/bin/bash

DATEC="/bin/date"
DATE=`${DATEC} +%Y%m%d_%H%M`
DATEM=`${DATEC} +%Y-%m-%d`
DBNAME=""
DBBACKUP="$DBNAME"-${DATE}.sql
HOST=''
USER=''
PASSWD=''

cd /home/anas/mysql_backup_scheduler/
/usr/bin/mysqldump -u -p --databases "$DBNAME" > ${DBBACKUP}

echo "Done Backup ..." ${DATE}

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put ${DBBACKUP}
quit
END_SCRIPT

echo "Done Upload ..." ${DATE}

#rm ${DBBACKUP} # uncomment only when you want to delete old files
exit 0

---- EOF ----

Finally, add this script to your cron.

Linux : Scheduling using “cron” and “at”

Basic scheduling commonly use in linux OS.

Cron (best use for repetition task only)

Format: (split each with space)
# min(0-59) hour(0-23) dom(1-31) mon(1-12) dow(0-6 0=sun) command #

Example:
1. Run task every 2 hours
* */2 * * * /path/to/task/file/command

2. Run task every day at 11:30 PM
30 23 * * * /path/to/task/file/command

3. Run task every Sunday at 10.00 pm
00 22 * * 1 /path/to/task/file/command

At (best use for one time task)

from at manual page :

at and batch read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh.

at executes commands at a specified time.

atq lists the user’s pending jobs, unless the user is the superuser; in that case, everybody’s jobs are listed. The format of the output lines
(one for each job) is: Job number, date, hour, queue, and username.

atrm deletes jobs, identified by their job number.

batch executes commands when system load levels permit; in other words, when the load average drops below 0.8, or the value specified in the invoca tion of atd.

Example:

1. Run task on 1:00 PM once
# at 1:00 PM
at > /path/to/task/command
at > ^d

Ctrl + d to exit
Also can use 23:00 for 1:00 PM

2. Run task 2 days from today
# at now + 2 days
at > /path/to/task/command
at > ^d

3. List all task
# atq

4. View detail ( at -c number of task)
# at -c 3

5. Remove task (atrm number of task)
# atrm 3

Reference taken from : http://www.computerhope.com/unix/uat.htm

the expression: would translate to:
noon ---> 12:00 PM October 18 2014
midnight ---> 12:00 AM October 19 2014
teatime ---> 4:00 PM October 18 2014
tomorrow---> 10:00 AM October 19 2014
noon tomorrow ---> 12:00 PM October 19 2014
next week ---> 10:00 AM October 25 2014
next monday ---> 10:00 AM October 24 2014
fri ---> 10:00 AM October 21 2014
NOV---> 10:00 AM November 18 2014
9:00 AM ---> 9:00 AM October 19 2014
2:30 PM ---> 2:30 PM October 18 2014
1430 ---> 2:30 PM October 18 2014
2:30 PM tomorrow ---> 2:30 PM October 19 2014
2:30 PM next month ---> 2:30 PM November 18 2014
2:30 PM Fri ---> 2:30 PM October 21 2014
2:30 PM 10/21 ---> 2:30 PM October 21 2014
2:30 PM Oct 21---> 2:30 PM October 21 2014
2:30 PM 10/21/2014 ---> 2:30 PM October 21 2014
2:30 PM 21.10.14 ---> 2:30 PM October 21 2014
now + 30 minutes ---> 10:30 AM October 18 2014
now + 1 hour ---> 11:00 AM October 18 2014
now + 2 days ---> 10:00 AM October 20 2014
4 PM + 2 days ---> 4:00 PM October 20 2014
now + 3 weeks ---> 10:00 AM November 8 2014
now + 4 months ---> 10:00 AM February 18 2015
now + 5 years ---> 10:00 AM October 18 2019

Remember to check your server time first : # date

Credits to :
https://debian-handbook.info/browse/stable/sect.task-scheduling-cron-atd.html
http://www.computerhope.com/unix/uat.htm
Linux Manual Page

Tested on Centos 6/7

Linux : Network Time Protocol (ntp) Configuration & Timezone


+------------------------------+
| Timezone |
+------------------------------+

First, get the current timezone of your server :
# timedatectl

You should get something like this :-

Local time: Mon 2017-05-08 09:34:14 +08
Universal time: Mon 2017-05-08 01:34:14 UTC
RTC time: Mon 2017-05-08 01:34:14
Time zone: Asia/Kuala_Lumpur (+08, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

If it is not your timezone, change it via this command:-
# timedatectl set-timezone Asia/Kuala_Lumpur

You can get the list of time zone from “/usr/share/zoneinfo” or # timedatectl list-timezones
The other way to know your current timezone is to
# ls -ld /etc/localtime
and the output similar to this
lrwxrwxrwx. 1 root root 39 Aug 3 2015 /etc/localtime -> ../usr/share/zoneinfo/Asia/Kuala_Lumpur

Also you can make symbolic link like this …

# ln -s /usr/share/zoneinfo/Asia/Kuala_Lumpur /etc/localtime

if not work, also try change this file content

# vim /etc/sysconfig/clock
change to
ZONE="Asia/Kuala_Lumpur"


+---------------------------------------+
| Network Time Protocol (NTP) |
+---------------------------------------+

Firstly, install the ntp :-

# yum install ntp nptd ntpdate

After that, sync the time :-
[Optional]
# ntpdate -s asia.pool.ntp.org

Then,

# vi /etc/ntp.conf

and replace or change setting similar ( Not compulsory, you can just leave it )
[Optional]

server 0.asia.pool.ntp.org iburst
server 1.asia.pool.ntp.org iburst
server 2.asia.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst

restart the service

# service ntpd restart
or
# systemctl restart ntpd.service

other command related to NTP :-

ntpd - Network Time Protocol (NTP) daemon
ntpdate - set the date and time via NTP
ntpdc - special NTP query program
ntpq - standard NTP query program
ntpstat - show network time synchronisation status
ntptime - read and set kernel time variables

p/s:
please get nearest ntp server list from http://www.pool.ntp.org/zone/my.
Tested on Centos 7 and Centos 6.

references :
https://www.certdepot.net/rhel7-set-ntp-service/
https://www.cyberciti.biz/faq/centos-linux-6-7-changing-timezone-command-line/
https://www.tecmint.com/install-ntp-server-in-centos/

Linux : Vi and Vim Short Notes

Writing to file when your forgot to run Vi/Vim as root privileges.
:w !sudo tee %

Searching in Vi/Vim.
:%s///g

Copy and paste line in Vi/Vim.
[Esc] yy -
p - paste after cursor
P - paste before cursor

Reference:-
http://vim.wikia.com/wiki/
http://stackoverflow.com/

Linux : Add new startup script using Systemd

I has been working with current CentOS 7 using Systemd feature. Here is the script I use to start one of my program.

Create an empty file with name <what ever name>.service

Filename: runbind.service

Content of the file :

BOF >>>
[Unit]
Description=Runbind Reload
After=syslog.target
After=network.target


[Service]
Type=simple
User=root
Group=root
ExecStart=/bin/sh /opt/runbind.sh
Restart=always


# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300


[Install]
WantedBy=multi-user.target

<<<EOF

Save this file in /usr/lib/systemd/system/

Note: You will want to change this parameters :-

Please exclude BOF and EOF lines
User -> which user account you need to run this script
Group -> user’s group
ExecStart-> full path to your program or script
Description-> description of the script

Tested on CentOS Linux release 7.2.1511 (Core)

 

Credit to : 
https://www.redmine.org/boards/1/topics/32763?r=43267
https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples