Pages

Showing posts with label Service. Show all posts
Showing posts with label Service. Show all posts

Thursday, August 30, 2012

Add Custom Linux Service


Location of Service Program
/root
Program Name
mysvcd
Runlevel
235
Start Priority
(for dependency)
90
Stop Priority
(100 - Start Priority)
10

Add Script for Your Service
       vi /etc/init.d/mysvcd

 ATTENTION: The chkconfig and  description must be specified in the script 
(e.g. runlevel = 235, start priority= 90, stop priority= 10)

                #!/bin/bash
                # chkconfig: 235 90 10
                # description: This is mysvcd script.
\
                #                      more description
                prog=mysvcd

                start() {
                                #code for start service
                                echo "Start mysvcd!!" 

                                /root/$prog&
                }

                stop() {
                                #code for stop service
                                echo "Stop mysvcd!!"
                }

                status(){
                                #code for status check
                                echo "Status of mysvcd!!"
                }

                restart() {
                                #code for restart service
                                echo "Restart mysvcd!!"
                                stop
                                start
                }

                case "$1" in
                        start)
                                start
                                ;;
                        stop)
                                stop
                                ;;
                        status)
                                status
                                ;;
                        restart)
                                restart
                                ;;
                        *)
                                echo "Usage: $prog {start|stop|restart}"
                esac


Add Service
                chkconfig --add mysvcd

If you want to start service immediately, run the following command:
                service mysvcd start

Saturday, June 9, 2012

OpenVPN with Public IP Assignments

OpenVPN with Public IP Assignments

VPN Server IP 
                a.b.c.99

VPN Subnet IP
                a.b.c.104 ~ a.b.c.107 (255.255.255.252)

VPN Server tun IP(a.b.c.105)
                a.b.c.105

Client tun IP   (a.b.c.106)
                a.b.c.106

Route:
                Use VPN server as default route

 


Server (a.b.c.99)
#Create conf

vi /etc/openvpn

# VPN server tun IP and VPN subnet netmask (size)
ifconfig a.b.c.105 255.255.255.252

# Some common settings
port 1194
proto tcp-server
mode server
tls-server
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
keepalive 10 120
comp-lzo

# VPN IP pool
ifconfig-pool a.b.c.106 a.b.c.107
# Use VPN server tun IP as gateway for VPN subnet
push "route-gateway a.b.c.105"
# vpn server use tun as route dev for vpn subnet
topology subnet
# vpn client use tun as route devo for vpn subnet
push "topology subnet"
#  Use VPN server as default gateway
push "redirect-gateway def1 bypass-dhcp"
# Make VPN client user use 8.8.8.8 as defualt DNS
push "dhcp-option DNS 8.8.8.8"
#Enable Client to client traffic
client-to-client

#Enable forward

iptables -I FORWARD -j ACCEPT

Thursday, June 7, 2012

OpenVPN


Server (IP 1.2.3.4, VPN Subnet 10.8.0.0 / 255.255.255.0)
Install OpenVPN
yum install openvpn
Generate Certificates (by using easy-rsa)
cd /usr/share/openvpn/easy-rsa/2.0/
#Setup parameters for certificate key generation
vi vars
source ./vars
#Clean all generated certificates and keys
./clean-all
#Generate server certificate
./build-ca
#Generate server key
./build-key-sever server
#Generate client key
./build-key client1
#Generate Diffie Hellman parameters
./build-dh
#Copy keys and certificates to openvpn conf folder (*crt: certificates; *key: private keys)
cd keys
cp -a ca.crt server.crt server.key dh1024.pem /etc/openvpn/
Create configuration
#copy sampel conf file to openvpn conf folder
cp -a /usr/share/doc/openvpn-x.x.x/sample-config-files/server.conf /etc/openvpn/
#Edit conf file
vi /etc/openvpn/server.conf
#Server IP
local 1.2.3.4
#Listen port
port 1194
#protocol tcp or udp (default)
proto tcp
#tunnel tun (default) or tap
dev tun
#SSL/TLS Certificate
ca ca.crt
#Certificate
cert server.crt
#Private key
key server.key
#Diffie hellman
dh dh1024.pem
#VPN subnet, comment out for bridge mode
server 10.8.0.0 255.255.255.0
#Persistent assignment and log filename
ifconfig-pool-persist ipp.txt
#bridge mode(10.8.0.4/255.25.255.0: ip and nm of bridge interface; ip pool 10.8.0.50 ~ 10.8.0.100)
;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
#DHCP-proxy (use Server-side DHCP server to assign client IP)
;server-bridge
#enable compression
comp-lzo
#Maximum number of concurrently connected clients
;max-clients 100
#Server status
status openvpn-status.log
#Log
log-append
#verbosity
verb 3
#Start OpenVPN Service
service openvpn@server.service
Client
#Download ca.crt, client.crt, client.key from Server
cp -a ca.crt client1.crt client.key MyClient
#Copy sample client conf to folder
cp -a /usr/share/doc/openvpn-x.x.x/sample-config-files/client.conf MyClient
#Modify client conf (Linux) / ovpn file (windows)
#
vi client.conf
dev tun
proto tcp
remote 1.2.3.4 1194
ca ca.crt
cert client1.crt
key client1.key
#Execute client with .conf file
openvpn xxx.conf

#Execute client with .ovpn file
<Right-click ovpn file and select “Start OpenVPN on this config file” in Windows>

Tuesday, June 5, 2012

Linux Network Address Translation (NAT) Service

Variables
    iif: your intranet interface
    iip: your intranet IP
    wif: your internet (WAN) interface
    wip: your wan internet (WAN) IP
    inm: your intranet netmask


Enable Forwarding
    echo 1 > /proc/sys/net/ipv4/ip_forward

Enable IP Forwarding
  1.      iptables -I FORWARD -i $wif -o $iif -j ACCEPT
  2.      iptables -I FORWARD -i $iif -o $wif -j ACCEPT
  3.      iptables -t nat -A POSTROUTING -s $iip/$inm -o $wif -j MASQUERADE
               OR 
     iptables -t nat -I POSTROUTING -s $iip/$inm -j SNAT --to $wip

Sunday, March 18, 2012

Linux PXE Server

To setup PXE service

  1. Setup DHCP service
  2. Setup TFTP service
  3. Prepare PXELinux and put your stuff in TFTP root 


Enjoy your PXE service!

Linux Dynamic Host Configuration Protocol (DHCP) Service

Install dhcpd
yum install dhcp
Modify dhcpd.conf 
vi /etc/dhcpd/dhcpd.conf
or
vi /etc/dhcpd.conf

#A sample of dhcpd.conf file
ddns-update-style interim;
ignore client-updates;

subnet 10.0.0.0 netmask 255.0.0.0 {

        option routers          10.0.0.1;
        option subnet-mask      255.0.0.0;
        option domain-name      "csie.ntu.edu.tw";
        option domain-name-servers 8.8.8.8;

        default-lease-time 864000;
        max-lease-time     1728000;


        #Static binding
        host delta_00_16_E6_4F_7E_D3 {
        hardware ethernet 00:16:E6:4F:7E:D3;
        fixed-address 10.7.0.80;
        }

        #PXE options
        next-server 10.0.0.1;
        filename "pxelinux.0";


        range dynamic-bootp 10.0.1.0 10.0.1.254;
}


Don't forget you must have an IP in the subnet.
Start dhcp service and enjoy it.

Linux hypertext transfer protocol (HTTP) service with SSL

Install Apache (httpd)
yum install httpd

Install SSL module for Apache (httpd)
yum install ssl_mod

Redirect http to https
vi /etc/httpd/conf/httpd.conf
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

 Enable User webpage
#UserDir disable
UserDir public_html

Linux and Windows Soft AP

Windows 7
To create a SSID for soft AP, execute following command as administrator
netsh wlan set hostednetwork mode=allow ssid=yourssid key=yoursecretkey

To start soft AP
netsh wlan start hostednetwork
PS: You need to share "wired connection" (Local Area Connection) for your clients.

Linux
To install hostapd
yum install hostapd

To modify hostapd configuration
vi /etc/hostapd/hostapd.conf

#WPA (1: WEP, 2:WPA, 3:WEP + WPA)
wpa=2
#Secret key
wpa_passphrase=yoursecretkey
#Wireless interface
interface=wlan0
#Support wireless mode(use b for better compatibility)
hw_mode=g
#Wireless channel
channel=1
#SSID
ssid=yourssid
PS: You need to prepare dhcpd and nat (iptables) for your clients.