Bash

AWS EC2: Use NPT inside VPC

AWS announced to provide their own internal NTP inside VPC at AWS re:Invent 2017. Here is how to do it:

CentOS

sudo yum erase ntp*; sudo yum -y install chrony; sudo service chronyd start

Debian/Ubuntu

sudo apt-get remove -y ntp*; sudo apt-get -y install chrony; sudo service chronyd start; apt-get autoremove
続きを読む

SSH: How to Avoid Connection Error

Add an option -o StrictHostKeyChecking=no into a ssh command:

ssh -o StrictHostKeyChecking=no your_username@example.com

OR

export TARGET_HOST='example.com'

if [ -z `ssh-keygen -F ${TARGET_HOST}` ]; then
  ssh-keyscan -H ${TARGET_HOST} >> ~/.ssh/known_hosts
fi

ssh-keygen -R ${TARGET_HOST}
ssh-keyscan -f ~/.ssh/known_hosts ${TARGET_HOST} >>~/.ssh/known_hosts 2>/dev/null
続きを読む

AWS EC2: How to Create AMI Image by Bash Shell Script

#!/bin/sh

export DATE_CURRENT=`date +%Y-%m-%d`
export TIME_CURRENT=`date +%Y%m%d%H%M%S`
export PURGE_AFTER_DAYS=30
export PURGE_AFTER=`date -d +${PURGE_AFTER_DAYS}days -u +%Y-%m-%d`
export BACKUP_TAG='backup'
 
# 1-1 Get the list of instances that you want to get backup
INSTANCES=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=${BACKUP_TAG}" | awk '{print $3}'`

for INSTANCE in ${INSTANCES}; do
 
  BACKUP=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=resource-id,Values=${INSTANCE}" "Name=key,Values=${BACKUP_TAG}" | awk '{print $5}'`
 
  # 1-2 Check if the instance is marked as backup or not
  if [ "${BACKUP}" == "true" ]; then
 
    # 1-3 Create a backup
    AMI_ID=`aws ec2 create-image --instance-id ${INSTANCE} --name "${INSTANCE}_${TIME_CURRENT}" --no-reboot`
 
    # 1-4 Create a tag for search for later use
    aws ec2 create-tags --resources ${AMI_ID} --tags Key=PurgeAllow,Value=true Key=PurgeAfter,Value=$PURGE_AFTER
  fi
done
 
# 2-1 Search backups by tag
AMI_PURGE_ALLOWED=`aws ec2 describe-tags --filters "Name=resource-type,Values=image" "Name=key,Values=PurgeAllow" | awk '{print $3}'`
 
for AMI_ID in ${AMI_PURGE_ALLOWED}; do
  PURGE_AFTER_DATE=`aws ec2 describe-tags --filters "Name=resource-type,Values=image" "Name=resource-id,Values=${AMI_ID}" "Name=key,Values=PurgeAfter"  | awk '{print $5}'`
 
  if [ -n ${PURGE_AFTER_DATE} ]; then
    DATE_CURRENT_EPOCH=`date -d ${DATE_CURRENT} +%s`
    PURGE_AFTER_DATE_EPOCH=`date -d ${PURGE_AFTER_DATE} +%s`
 
    if [[ ${PURGE_AFTER_DATE_EPOCH} < ${DATE_CURRENT_EPOCH} ]]; then
      # 2-2 Delete backup if it is marked as backup
      aws ec2 deregister-image --image-id ${AMI_ID}
       
      SNAPSHOT_ID=`aws ec2 describe-images --image-ids ${AMI_ID} | grep EBS | awk '{print $4}'`
      aws ec2 delete-snapshot --snapshot-id ${SNAPSHOT_ID}
    fi
  fi
done
続きを読む

Bash: How to Insert One White Space before and after English Words in Japanese

Consider to insert one white space before and after English word in Japanese. Run the following script by changing a string 'YOUR_FILENAME'. The script copy the original file as YOUR_FILENAME.org and overwrite and replace the original file by inserting one white space before and after an English word. It is useful multi-byte language such as CJK (Chinese, Japanese and Korean).

続きを読む

Linux: How to Add a User to a Group

There are a couple of options to add a user to group(s) in Linux. The commands are gpasswd, usermod and adduser.

I recommend to use gpasswd to add a user to group, which is no harm.

gpasswd -a <USERNAME> <GROUPNAME>

You must use usermod command as follows:

usermod -aG <GROUPNAME> <USERNAME>
続きを読む
Subscribe to Bash