Connect with us

Home

Htaccess With Few examples

Published

on

htaccess(Hypertext Access) is a directory-level configuration file for Apache.The use of the .htaccess files is slow down your Apache server performance. htaccess is mainly used for the Apache users who don’t have permission to modify Apache configuration file.If u having access to httpd main server configuration file avoid. htaccess files completely.

Few .htaccess rules

Password protection

Make the user enter a name and password before viewing a directory.

AuthUserFile /home/example/public_html/.htpasswd-username
AuthName “Protected Directory”
AuthType Basic
<limit GET POST>
require Valid user
</limit>

Deny users by IP address and allow others

Order allow,deny
Deny from 192.168.5.23
Allow from all

Allow users by IP address and deny other

Order deny,allow
Deny from all
Allow from 192.168.5.23

Rewrite Engine

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Its used to redirect http://example.com to http://www.example.com

Redirect

Redirect 301 /linux/htaccess http://www.example.com/linux/htaccess

To redirect http to https

ssl certificate is installed in server and we want to redirect all requests to https we use the following rule in .htaccess

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://www.domain.com [L,R]

To set the administrator email in .htaccess

SetEnv SERVER_ADMIN admin@example.com

Turn off the Server Signature

ServerSignature Off

Serversignature is used to protect from user to view the Apache version, its usually appears on error page.

To prevent directory listings

IndexIgnore *

Continue Reading

Home

Protect the web directories by user authentication in apache

Published

on

By

Directory Password Protection 

In order to restrict the clients from accessing the web directory of a server and to allow only certain users, apache uses the Directory Password Protection Method.

How to Set Password Protection to directory ?

In Apache the .htaccess (Name of the Distributed Configuration file) file is placed under the Directory that we need to protect. To configure this directory password protection follow the below steps.

 Step 1:

In the Apache configuration file httpd.conf, add an entry in Virtualhost container to secure the protected directories.

<VirtualHost 192.168.1.2:80>
Servername 192.168.1.2
DocumentRoot /var/www/html
AccessFileName .htaccess
<Directory /var/www/html/private >
AllowOverride All
</Directory>
</VirtualHost>

In the above configuration we need to protect the /var/www/html/private web directory. The .htaccess is the distributed file name placed under the protected directory, we can change this file name as per our need by assign any name in AccessFileName directive. In the Directory container we should mention the absolute path of protected directory, Inside the Directory container the All option in AllowOverride will allow all the possible configuration directives in the .htaccess file.

Note: If AllowOverride is set to none then the apache ignored the .htaccess file

Step 2:

Now Add a valid authorized user for the protected web directory by using htpasswd command

# htpasswd -c /var/www/html/private/.htpasswd <username>

The htpasswd command is used to generate the username and password for user authentication in protected directory. The option -c is used to create the password file (.htpasswd) if it does not exist.

Step 3:

Now configure the in .htaccess file which is placed under the protected directory “/var/www/html/private/.htaccess” as below.

AuthType Basic
AuthName “Private User Access”
AuthUserFile /var/www/html/private/.htpasswd
Require valid-user
Order Allow,Deny
Allow from all

In this above configuration entered in .htaccess file, the AuthType is configured as Basic, which defines the type of user authentication for the directory. The AuthName defines name of the authorization realm for a directory. The authenticated user password file stored location is denoted by AuthUserFile directive. The Require directive defines the permitted user to access the protected directory. we can also deny or allow from which client IP should access the protected IP by Allow and Deny Directive.

Note: make the .htaccess and .htpasswd file hidden from clients for security purpose.

Output :

All the configurations are over. Now just restart the apache service and call the configured virtual host protected web directory by browser.

 

Continue Reading

Home

Shell Script to add users and corresponding password automatically

Published

on

By

. . .List of Users to be Added . . .
#cat list
ravi
deepak

. . .Script to automate User and their Password Adding . . .
#vim auto.sh
#!bin/bash
cat /dev/urandom | tr -cd “a-zA-Z0-91234567890-=\`” | fold -w 9 | head -n 2 > pass
cp pass check
u=`cat list`
for j in $u
do
useradd $j
echo “User $j Added”
echo “==================================”
done
for i in $u
do
echo “User Name is :$i”
p=`cat pass`
echo “$p” | passwd –stdin “$i”
sed -i ’1d’ /home/rajm/script/blog/auto/pass
#sed -i ’1d’ $p
echo “User $i ’s password changed!”
echo “==============================”
done

. . .Give permisson and run the script. . .
#chmod +x auto.sh
sh auto.sh
User ravi Added
==================================
User deepak Added
==================================
User Name is :ravi
Changing password for user ravi.
passwd: all authentication tokens updated successfully.
User ravi ’s password changed!
==============================
User Name is :deepak
Changing password for user deepak.
passwd: all authentication tokens updated successfully.
User deepak ’s password changed!
==============================

. . .The File check used to check the password. . . .
#cat check
5<YX<jR0I
=tAn`0;mt

Continue Reading

Home

Sed command with examples

Published

on

By

Sed stands for stream editor. A stream editor performs the text transformation from input stream and the input stream may be a file or input from a pipeline.

#cat editing.txt
hi
hello
welcome
how are you
hope you are fine

. . .Sed command to print lines. . .

1.#sed ’1p’ editing.txt
hi
hi
hello
welcome
how are you
hope you are fine

==> 1p which tells that print the first line and show the remaining lines. so we can see from the output hi is priented twice.

2.#sed -n ’1p’ editing.txt
hi

===> -n options is nothing but noprint, (i.e) It prints only the first line and didn’t show the other lines.

3.#sed -n ’2,4p’ editing.txt
hello
welcome
how are you

===> The above command is used to print only a certain range of lines.

4.#sed -n ‘$p’ editing.txt
hope you are fine

===> The above command displays only the last line. The ‘$’ symbol indicated the last line and ‘-n’is used for no print and it combines with option ‘p’ to displays only the last line .

. . .Sed Command to delete lines. . .

1. #sed ’1d’ editing.txt

hello
welcome
how are you
hope you are fine

===> The above command used to delete only the fisrt line and display the remaining lines.

2. #sed ’2,4d’ editing.txt
hi

===> The above command is used to delete a range of lines.

. . .Sed Command for search and replace. . .

1.#sed ‘s/hi/changed/’ editing.txt
changed
hello
welcome
how are you
hope you are fine

==> The above command search for hi and replace to changed and print the remaining lines

2.#sed -n ‘s/hi/changes/p’ editing.txt
changes

===> The above command search for hi and replace to changed and don’t print the remaining lines

3.#sed ’1,4s/are/is/’ editing.txt
hi
hello
welcome
how is you
hope you are fine

===> The above command used to do search and replace only for a range of lines.

. . .How to delete empty lines using sed command. . .

#cat new_file
hi

The above line is empty

1.#cat new_file | sed -e ‘/^$/d’
hi
The above line is empty

2.#sed -e ‘/^$/d’ new_file
hi
The above line is empty

===> The above two commands produce the same results, It search for empty line and delete it. ‘^’ is nothing but starting of the line and ‘$’ tells the end of the line, so from the starting to ending of the line is empty means delete it.

. . .How to remove space from a word. . .

#cat remove.txt
s p a c e.

#cat remove.txt | sed ‘s/ //g’
space

# sed ‘s/ //g’ remove.txt
space

===> It nothing but search and replace, the above command search for empty space and replace it with nothig, so then space becomes nospace.

. . .How to remove a lines permanently from a file. . .

#cat new_file
hi

The above line is empty

#sed -i ’2d’ new_file

===> This above command will delete the line number 2 permanently from the file new_file.

. . .How to assign numbers for lines using sed command. . .

#cat editing.txt
hi
hello
welcome

#sed ‘=’ editing.txt
1
hi
2
hello
3
welcome

===> In the above command ‘=’ symbol is used to assign number for each lines, it works like as same as ‘nl’ command.

. . .How to use Word Boundaries using sed command. . .

Word Boundaries – \<\>

#cat first_text
raj
rajkumar
i am rajkumar

#sed -e ‘s/\<raj\>/kumar/g’ first

kumar
kumarkumar
i am kumarkumar

===> Normally if u use search and replace it will replace any word which contains raj to kumar, if ‘g’ is specified.

#sed -e ‘s/\/kumar/g’ first_text
kumar
rajkumar
i am rajkumar

===> If u use Word Boundaries means the only exact work will be searched and get replaced.(Its same like as ‘grep -w’ command)

. . .How to include files in sed command. . .

#cat commands
raj
linux

#vim lol
{
s/raj/kumar/g
s/linux/linuzzzzzzzz/g
}

#sed -f lol commands
kumar
linuzzzzzzzz

===> The ‘ -f ‘ option is used to include files in command prompt.

Continue Reading

Trending

Copyright © 2022 The Technical Stuff.