Thursday, July 24, 2014

How to change Local Time zone and configure NTP



If you want to set time yourself try the following command:

date 07241503

where 07 is month, 24 is date, 15 is hour and 03 is minute.

Or if you want to use NTP server, use the following command to synchronize:

ntpdate  0.centos.pool.ntp.org

If ntp server is taking wrong time because of wrong time zone, then you can easily check timezone.

service ntpd stop

cp /usr/share/zoneinfo/Asia/Kolkata /etc/localtime 


ntpdate  0.centos.pool.ntp.org


service ntpd start

chkconfig ntpd on

:)


Thanks,
(The above is tested on CentOS 6.5 - Suitable for CentOS 5,6,6.x and RHEL5,6,6.x)

Wednesday, August 28, 2013

How to configure LDAP client on RHEL 6.x


First we have to make sure, that we have LDAP client group installed.

#yum -y groupinstall directory-client

After installing package group, we can configure LDAP client either using GUI tool or using CLI tool:

#system-config-authentication &

GUI Tool for configuration of ldap

                    -OR-


#authconfig  --enableldap  --enableldapauth
--ldapserver=ldapsrv.ynetwork.org  
--ldapbasedn="dc=ynetwork,dc=org"
--enableldaptls 
--ldaploadcacert=http://ldapsrv.ynetwork.org/pub/ynetwork-ca.crt 
--enablesssd 
--enablesssdauth 
--update 

CLI Tool to configure LDAP client

#getent passwd user1        

Assuming you have 'user1' created in LDAP server

#ssh user1@localhost

Test using ssh or su in your local system with ldap user account

Now its not possible to create home directory of all ldap based users in all hosts, so we shall use NFS to automatically mount their home directory with they log in.

#vim /etc/auto.master 

/home/remote /etc/auto.remote

(Assuming /home/remote/user1 is the home directory for 'user1')

#showmount -e ldapsrv.ynetwork.org

Our ldap server can also be a NFS server, hosting home directories for all users, so determine NFS shares using above command.

#vim /etc/auto.remote

user1   -rw   ldapsrv.ynetwork.org:/home/remote/user1

(SYNTAX: username  -options    nfsserver.fqdn:/home/dir)

#service autofs reload

Reload all indirect maps

#ssh user1@localhost

Test again with user1

How to configure LDAP client using kerberos authentication

#yum -y install krb5-workstation openldap-clients

Install required packages for kerberos authentication(Assuming you already have all required ldap packages)

#authconfig  --enableldap  --disableldapauth
--ldapserver=ldapsrv.ynetwork.org  
--ldapbasedn="dc=ynetwork,dc=org"
--enableldaptls 
--ldaploadcacert=http://ldapsrv.ynetwork.org/pub/ynetwork-ca.crt 
--enablekrb5 
--krb5kdc=ldapsrv.ynetwork.org 
--krb5adminserver=ldapsrv.ynetwork.org 
--krb5realm=YNETWORK.ORG 
--enablesssd 
--enablesssdauth 
--update 

(Configuring LDAP with kerberos using cli tool of 'authconfig')

#getent passwd user1

Test your communication using one of the user.

#ssh user1@localhost

Use kerberos password instead of ldap password.

Thursday, February 14, 2013

Iptables basic configurations on RHEL 6.x

IPtables on Redhat Enterprise Linux 6.x

Here we will learn iptables packet filtering tool ships along with most RHEL distros. We will concentrate on Filter chain of iptables.

#mkdir -p /root/bin

#cd /root/bin

#vim firewallconf.sh

#!/bin/bash

iptables -F

## Clears all previous rules

iptables -A INPUT -i lo -j ACCEPT

## Allowing all localhost (Local loopback) traffic

iptables -I INPUT -m state --state ESTABLISHED,RELATED -s 192.168.0.0/24 -j ACCEPT

## Allowing all Established and related packets for local network (192.168.0.0 in our case)

iptables -I INPUT -m state --state NEW -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT

## Allowing SSH traffic for all new connection 'made' to your local system, only for your local Lan

iptables -A INPUT -j REJECT

## Rejecting all other traffic which is initiated to your local host

<save and exit>

#chmod 755 firewallconf.sh

#./firewallconf.sh

# service iptables save

# iptables -L

<List all written rules in Filter Chain>

# iptables -nvL --line-numbers

<Lists all rules along with all dropped/rejected and accepted packets>



Friday, May 11, 2012


Allow Root Login Fedora Linux



In Fedora Linux distro, you cannot login by root user by default. Normal users are allowed to login from your Graphical Desktop of Gnome or KDE. But there are certain tasks that have to be done by root. When you are making kernel level changes, Fedora asks for Root user credentials, I mean system asks for root password. But what if you want to login as root, which is not allowed by default to keep your system safe from accidental misconfigurations.

Here are the steps to do that:

[user@fedora16]$ su – root

            <Switch your privilege to root>

[user@fedora16]$ cd /etc/pam.d

[user@fedora16]$ cp gdm gdmorifile.old

[user@fedora16]$ vim gdm

auth required pam_succeed_if.so user != root quiet

            <Find and comment/delete above line>

NOTE: If you are using Fedora 10 or below version, these steps are enough. But if you are 


using Fedora 16 you need to modify one more file.

[user@fedora16]$ cp gdm-password gdm-passwordfile.old

[user@fedora16]$ vim gdm-password

auth required pam_succeed_if.so user != root quiet

            <Find and comment/delete above line>

:wq

            <Now reboot your system and login as root>

Cheers 

Monday, September 19, 2011

T-SQL Query designing on Microsoft SQL Server 2005

--Create Database


USE [Employee]
GO


CREATE TABLE employeetest
(employeeID int NOT NULL PRIMARY KEY,
FirstName nvarchar(50) NOT NULL,
LastName nvarchar(50) NOT NULL,
PhoneNumber int NOT NULL)


--Grouping Results using GROUP BY


SELECT Title,
COUNT (*) as EmployeeHavingTitle
FROM
HumanResources.Employee
GROUP BY 
Title




--Returning Unique Values using DISTINCT 
--DISTINCT >> To avoid duplicate records


SELECT DISTINCT 
Title
FROM
HumanResources.Employee


--combining Results using Union Operator


SELECT * FROM Production.ProductCategory
UNION
SELECT 5, 'Test Category',NEWID(), '02/02/2011'
UNION ALL
SELECT * FROM Production.ProductCategory




--Returning Differences using INTERSECT
--To display records which are 'common' between two queries


SELECT * FROM Production.ProductCategory WHERE ProductCategoryID IN (1,2,3,4)
INTERSECT
SELECT * FROM Production.ProductCategory WHERE ProductCategoryID IN (1,2,3)


--Returning Differences using EXCEPT
--To display records which are 'uncommon' between two queries


SELECT * FROM Production.ProductCategory WHERE ProductCategoryID IN (1,2,3,4)
EXCEPT
SELECT * FROM Production.ProductCategory WHERE ProductCategoryID IN (1,2,3)


/*Returining related data with JOIN conditions */


--Returning matching rows using the INNER JOIN condition


SELECT 
PC.Title as PersonTitle
PC.FirstName,
PC.LastName,
HE.Title as PositionTitle,
HE.VacationHours,
HE.SickLeaveHours
FROM
HumanResources.Employee HE
INNER JOIN
Person.Contact PC ON HE.ContactID = PC.ContactID
WHERE
HE.SalariedFlag = 1
ORDER BY
LastName, FirstName


--Returning all rows from one table using OUTER JOIN (RIGHT/LEFT) condition

SELECT 
PP.(Name),
PP.Rating,
PP.Comments
FROM
Production.Product PP
LEFT OUTER JOIN
Production.ProductReview PR ON PP.ProductID = PR.ProductID
ORDER BY
PP.(Name)


USE [master]
GO


IF DB_ID(N'employee') IS NOT NULL
DROP DATABASE employee
GO


--Creating a simple database
CREATE DATABASE (CS) ON PRIMARY
(
NAME = N'CS_data',
FILENAME = N'C:\Program files\Microsoft SQL Server\MSSQL.1\MSSQL\data\cs.mdf'
)
LOG ON
(
NAME = N'CS_log',
FILENAME = N'C:\Program files\Microsoft SQL Server\MSSQL.1\MSSQL\data\cs.ldf'
)


--Creating a database with filegroups
CREATE DATABASE (CS) ON PRIMARY
(
NAME = N'CS_primary1',
FILENAME = N'C:\Program files\Microsoft SQL Server\MSSQL.1\MSSQL\data\cs_primary1.ndf'
)




USE [ktm]
/* Creating Tables */
--Create Train table
CREATE TABLE trains(
TrainID int IDENTITY(1,1) NOT NULL,
Source nvarchar(100) NOT NULL,
Destination nvarchar(100) NOT NULL,
StartDate smalldatatime NOT NULL CONSTRAINT DF_Train_Date DEFAULT (getdate()),
RunningFlag bit NULL CONTRAINT DF_Train_RunningFlag DEFAULT (1),
CONTRAINT PK_Train PRIMARY KEY CLUSTERED (TrainID) ON [PRIMARY])


--Create CCTV table
CREATE TABLE cctv
VideoID int IDENTITY(1,1) NOT NULL,
SeriesID smallint NOT NULL,
VideoName nvarchar(100) NOT NULL,
Lenth float NOT NULL,
[size] numeric (2,2) NOT NULL,
CreationDate datetime NOT NULL,
CONTRAINT PK_cctv PRIMARY KEY CLUSTERED (VideoID) ON [PRIMARY])


--Create Ticket table
CREATE TABLE cctv
TicketID int IDENTITY(1,1) NOT NULL,
Source nvarchar(100) NOT NULL,
TicketCounterNumber int NOT NULL,
Price money NOT NULL,
VideoID int NOT NULL,
CreationDate datetime NOT NULL,
CONTRAINT PK_Ticket PRIMARY KEY CLUSTERED (Source) ON [PRIMARY])


--Create Data Types
--Create IC Number for malaysians
CREATE TYPE ICN varchar(12) NULL


/*Creating Defaults */
--Create Default for an empty IC number
CREATE DEFAULT DF_ICN AS '123-20-19901'


/*Creating Check Constraints */
--Add check constraint to StartDate column of Trains
ALTER TABLE Train1 WITH CHECK ADD CONSTRAINT CK_TrainCheckDate CHECK (StartDate>='1/1/1990')


USE [AdventureWorks]
GO
--Creating Non-Clustered Indexes
CREATE NONCLUSTERED INDEX [IX_HR_Employee] ON [HumanResources].[Employee] 
([Title] ASC, [BirthDate] ASC)
WITH
(PAD_INDEX = OFF,
SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF,
IGNORE_DUP_KEY = OFF,
ONLINE = ON) ON [PRIMARY]


--Creating Standard Views
CREATE VIEW HumanResources.vEmployeeTime
AS
SELECT 
hre.EmployeeID,
pc.FirstName,
pc.LastName,
hre.Title,
hre.VacationHours,
hre.SickLeaveHours
FROM
HumanResources.Employee hre
JOIN
Person.Contact pc ON hre.ContactID = pc.ContactID
WHERE
SalariedFlag = 1
AND
CurrentFlag = 1




--Creating Indexed Views
CREATE VIEW Sales.vixAllTimeSales
WITH SCHEMABINDING
AS
SELECT 
pr.Name,
SUM(sod.OrderQty) as TotalQuantity,
SUM(soh.SubTotal) as TotalSales,
COUNT_BIG(*) as NumSales
FROM
Production.Product pr
INNER JOIN
Sales.SalesOrderDetail sod ON pr.ProductID = sod.ProductID
INNER JOIN
Sales.SalesOrderHeader soh ON sod.SalesOrderID = soh.SalesOrderID
WHERE
pr.ProductSubcategoryID = 1
GROUP BY
pr.Name
Go




--Creating Unique Clusterd Index


CREATE UNIQUE CLUSTERED INDEX IX_AllTimeBikeSales
ON Sales.vixAllTimeBikeSales (Name)

SELECT * FROM Sales.vixAllTimeBikeSales




--Creating Partitioned Views


--Distributed Partition Views


--Server1


CREATE TABLE sales.Customers_west
(
CustomerID int PRIMARY KEY,
TerritoryID int CHECK (TerritoryID BETWEEN 1 AND 5) NULL,
CustomerType nchar (1) NOT NULL
)


INSERT Sales.Customers_west
(CustomerID, TerritoryID, CustomerType)
SELECT 
CustomerID, TerritoryID, CustomerType
FROM 
sales.Customer
WHERE 
TerritoryID BETWEEN 1 AND 5


CREATE VIEW Sales.vCustomers
AS
SELECT * FROM Sales.Customers_West




/* Creating Stored Procedures */


--Basic Stored Procedure


CREATE PROCEDURE Sales.spGetYearlyBikeSales
AS
SELECT
pr.Name,
YEAR(soh.OrderDate) as SalesYear,
SUM(sod.OrderQty) as TotalQuantity,
SUM(soh.SubTotal) as TotalSales
FROM
Production.Product pr
INNER JOIN
Sales.SalesOrderDetail sod ON pr.ProductID = sod.ProductID
INNER JOIN
Sales.SalesOrderHeader soh ON sod.SalesOrderID = soh.SalesOrderID
WHERE 
pr.ProductSubcategoryID = 1
GROUP BY
pr.Name, YEAR(soh.OrderDate)
ORDER BY
pr.Name, YEAR(soh.OrderDate)



Sales.spGetYearlyBikeSales




--Stored Procedure with Input Parameters


CREATE PROCEDURE sales.spGetYearlyProductSalesByID
@ProductCategoryID int
AS
SELECT
pr.Name,
YEAR(soh.OrderDate) as SalesYear,
SUM(sod.OrderQty) as TotalQuantity,
SUM(soh.SubTotal) as TotalSales
FROM
Production.Product pr
INNER JOIN
Sales.SalesOrderDetail sod ON pr.ProductID = sod.ProductID
INNER JOIN
Sales.SalesOrderHeader soh ON sod.SalesOrderID = soh.SalesOrderID
WHERE 
pr.ProductSubcategoryID = @ProductCategoryID
GROUP BY
pr.Name, YEAR(soh.OrderDate)
ORDER BY
pr.Name, YEAR(soh.OrderDate)


EXEC sales.spGetYearlyProductSalesByID 4


--Stored Procedure with using wild cards


CREATE PROCEDURE HumanResources.spGetEmployeeByName
@FirstName nvarchar(50) = '%',
@LastName nvarchar(50) = '%'
AS
SELECT
pc.Title,
pc.FirstName,
pc.Lastname,
pc.EmailAddress,
pc.Phone,
hre.BirthDate,
hre.HireDate
FROM
HumanResources.Employee hre
JOIN
Person.Contact pc ON hre.ContactID = pc.ContactID


WHERE
pc.FirstName LIKE @FirstName + '%'
AND
pc.LastName LIKE @LastName + '%'


EXEC HumanResources.spGetEmployeeByName DEFAULT,'a'


/* Creating User-defined Fuctions */


--Creating SCALAR Fuctions


CREATE FUNCTION ufnFormatCurrency (@Amount Money)
RETURNS VarChar(100)
AS
BEGIN
RETURN '$' + CONVERT(VarChar, CONVERT (Money, @Amount),1)
END

---Usage


SELECT 
p.Name,
soh.OrderDate,
soh.SubTotal as SubTotalWithoutFuction,
dbo.ufnFormatCurrency(soh.SubTotal) as SubTotal,
dbo.ufnFormatCurrency(soh.TaxAmt) as TaxAmount,
dbo.ufnFormatCurrency(soh.Freight) as Freight,
dbo.ufnFormatCurrency(soh.TotalDue) as TotalDue
FROM
Sales.SalesOrderHeader soh
JOIN
Sales.SalesOrderDetail sod ON soh.SalesOrderID = Sod.SalesOrderID
JOIN
Production.Product p ON sod.ProductID = p.ProductID
WHERE
Year(soh.OrderDate) = 2004



---Data Access Scalar Function


CREATE FUNCTION ufnGetProductStock (@ProductID int)
RETURNS int
AS
BEGIN
DECLARE @ret int

SELECT
@ret = SUM(ppi.Quantity)
FROM
Production.ProductInventory ppi
WHERE
ppi.ProductID = @ProductID
IF (@ret IS NULL)
SET @ret = 0
RETURN @ret
END


--Usage


SELECT 
Name,
dbo.ufnGetStock(ProductID) AS Supply
FROM 
Production.Product




---Data Access Scalar User Defined Function


CREATE FUNCTION ufnGetProductStock (@ProductID int)
RETURNS int
AS
BEGIN
DECLARE @ret int

SELECT 
@ret = SUM(ppi.Quantity)
FROM 
Production.ProductInventory ppi
WHERE
ppi.ProductID = @ProductID
IF (@ret IS NULL)
SET @ret = 0
RETURN @ret
END


---Usage 
SELECT 
Name,
dbo.ufnGetProductStock(ProductID) AS Supply
FROM
Production.Product




--Creating Inline Table - Valued Functions


CREATE FUNCTION sales.ufnStoreYTDSales (@StoreID int)
RETURNS Table
AS
RETURN
(
SELECT
p.Name,
SUM(sod.LineTotal) AS YTDSales
FROM
Production.Product AS p
JOIN
sales.SalesOrderDetail AS sod ON sod.ProductID = p.ProductID
JOIN
sales.SalesOrderHeader AS soh ON soh.SalesOrderID = sod.SalesOrderID

WHERE 
soh.CustomerID = @StoreID
GROUP BY
p.ProductID, P.Name
)




---Usage 
SELECT * FROM Sales.ufnStoreYTDSales(1)

---Creating a Parameterized View From an Inline Table-Valued Function


CREATE FUNCTION Sales.ufnStoreWithDemographics (@StoreID int)
RETURNS Table
AS
RETURN
(
SELECT
*
FROM
Sales.vStoreWithDemographics
WHERE
CustomerID = @StoreID
)

--Usage


SELECT * FROM Sales.ufnStoreWithDemographics(1)


/* Working with TABLE Encryption */


--Encryption


ALTER FUNCTION ufnFormatCurrency (@Amount Money)
RETURNS VarChar(100)
WITH ENCRYPTION
AS
BEGIN
RETURN '$' + CONVERT(VarChar, CONVERT(Money,@Amount),1)
END




NOTE: All above queries are tested using built-in tables, databases & templates of Microsoft SQL 2005. Some are self-created. 
[Source Reference: Nuggets for SQL 2005]

Monday, September 12, 2011

ZFS in Sun (Oracle) Solaris 10


Zettabyte File System(ZFS)
====================

Major Feature:

>> 256 quadrillion zettabytes

What is Zettabytes?
(Terabytes-Petabytes-Exabytes-Zettabytes)

which zpool

zpool list  - lists pools

zpool create pool1 c0t1d0

Full disk in Pool

mount

/pool1 rw

ls -l /pool1

zpool list

Zpool Pool Status:
Online
Degraded
Faulted
Offline
Unavailable
zfs list

zfs mount

zpool status

zpool status -v pool1

zpool destroy pool1 (to remove)

zpool status

zpool create pool1 c0t0d0

Creating File System Under pool:

zfs create pool1/home

zfs list

SET QUOTA...

zfs set quota=1G pool1/home

zfs list

Creating Userbased file system under pool1/home

zfs create pool/home/u1

zfs set quota=500M pool1/home/u1

zfs list

zfs get -r quota pool1

zfs get -r compression pool1

<There are lots of Variables like that>

Adding Storage:

zpool add pool1 c0t2d0

Configuring DNS(BIND) in Sun (Oracle) Solaris 10


CONFIGURING BIND DNS SERVER:
==============================

pkginfo -x |grep -i bind

SUNWbind  - main Bind package
SUNWbindr - Service management

pkgchk -l SUNWbindr

pkgchk -l SUNWbind

dig mail.yahoo.com

dig mail.yahoo.com ns

dig mail.yahoo.com mx

By default /var/named and /etc/named.conf does not exist

create /etc/named.conf



options {
directory "/var/named";
};

###Special zone of root of DNS###

zone "." {
type hint;
file "db.cache";
};

###Reverse zone###

zone "0.0.127.in-addr.arpa" {
type master;
file "db.127.0.0";
};

zone "0.16.172.in-addr.arpa" {
type master;
file "db.172.16.0";
};

###Forward Zone###

zone "unix.com" {
type master;
file "db.unix.com";
};

Save and exit

Download hint file,

mkdir /var/named

cd /var/named/

wget ftp://ftp.rs.internic.net/domain/named.root

ls -l

mv named.root db.cache

Creating Zone Files:

>> reverse lookup zone

gedit /var/named/db.127.0.0

@ IN SOA pc1.unix.com. root.unix.com. (
2011062001 ; Serial number
7200 ; Refresh Interval
3600 ; Retry Interval
86400 ; Expiry
600 ); Minimum TTL

NS pc1.

1 IN PTR localhost.

save & exit

gedit /var/named/db.172.16.0

@ IN SOA pc1.unix.com. root.unix.com. (
2011062001 ; Serial number
7200 ; Refresh Interval
3600 ; Retry Interval
86400 ; Expiry
600 ); Minimum TTL

NS pc1.

1 IN PTR pc1.

##1 is for 172.16.0.1##

save & exit

gedit /var/named/db.172.16.0

@ IN SOA pc1.unix.com. root.unix.com. (
2011062001 ; Serial number
7200 ; Refresh Interval
3600 ; Retry Interval
86400 ; Expiry
600 ); Minimum TTL

NS pc1.

1 IN PTR pc1.

##1 is for 172.16.0.1##

save & exit


gedit /var/named/db.unix.com

@ IN SOA pc1.unix.com. root.unix.com. (
2011062001 ; Serial number
7200 ; Refresh Interval
3600 ; Retry Interval
86400 ; Expiry
600 ); Minimum TTL

NS pc1.

pc1 IN  A 172.16.0.1
pc2 IN  A 172.16.0.2


save & exit

svcadm enable dns/server

svcs -l dns/server

dig @localhost pc1.unix.com


vi /etc/resolv.conf

domain unix.com
search unix.com
nameserver 172.16.0.1
nameserver 8.8.8.8

dig @localhost www.google.com
1655ms


dig @localhost www.google.com
1ms

NOTE: Always works as caching-only NS


gedit /var/named/db.unix.com

@ IN SOA pc1.unix.com. root.unix.com. (
2011062002 ; Serial number
7200 ; Refresh Interval
3600 ; Retry Interval
86400 ; Expiry
600 ); Minimum TTL

NS pc1.unix.com.

    IN  MX  10 pc1.unix.com.

pc1 IN  A 172.16.0.1
pc2 IN  A 172.16.0.2


save & exit


svcadm restart dns/server

dig @localhost pc1.unix.com mx


gedit /var/named/db.unix.com

@ IN SOA pc1.unix.com. root.unix.com. (
2011062003 ; Serial number
7200 ; Refresh Interval
3600 ; Retry Interval
86400 ; Expiry
600 ); Minimum TTL

NS pc1.unix.com.

    IN  MX  10 pc1.unix.com.

pc1 IN  A 172.16.0.1
pc2 IN  A 172.16.0.2
www CNAME ns1.unix.com.

save & exit

svcadm restart dns/server


dig @localhost www.unix.com cname