Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Sunday, March 22, 2020

Change outbound connections IP for Mail

Hello admins, the server will start to send the Spam mails due to malicious code or compromised mailbox. In that case, our outgoing IP address will get blocked by several spam filters including Gmail and other mail services.

In this case, we have to wait for a long time to get whitelisted or have to pay to be whitelisted. But if you have secondary IP addresses for your server, we can do this trick to send emails and let the main IP to whitelist automatically.

This means we will change to outbound connections from the main IP to the secondary IP address.

Important: Before proceeding with this, make sure that you have fixed the compromised code or application or mailbox. Else, your secondary IP address will also get blocked on Spam filters.

Default IP for outgoing connections can be set using ‘ip’ utility.
First of all, check how routing is configured on the server:

You can check this by the following command: -
# /sbin/ip route
159.8.30.64/28 dev bond1 proto kernel scope link src 159.8.30.66
10.105.124.128/26 dev bond0 proto kernel scope link src 10.105.124.130
169.254.0.0/16 dev bond0 scope link metric 1006
169.254.0.0/16 dev bond1 scope link metric 1007
10.0.0.0/8 via 10.105.124.129 dev bond0
default via 159.8.30.65 dev bond1
If no ‘src’ listed in ‘ip’ output for the default route, then the main IP on the interface is used for outgoing connections. You can change it using:
# /sbin/ip route change default via 159.8.30.65 dev bond1 src 5.153.43.25
Now:
# /sbin/ip route
192.168.50.0/24 dev eth1 proto kernel scope link src 192.168.50.53
169.254.0.0/16 dev eth1 scope link
default via 192.168.50.254 dev eth1 src 192.168.50.100

Note: be careful, if you set the wrong IP as the source, you lose the connection to the server.

Thursday, October 31, 2019

Find and clear log files in Linux

Objective:

Find and clear the files with extension of *.log, usually log files.

Commands:

find . -type f -iname *.log -exec tee {} \; </dev/null

This command will find the files with extension of 'log' and empty that file. Which means file will be there and data of that file will be cleared.

If you are using cPanel, there will be a directory called "virtfs", in that case you can use following command.

find /home -type f -iname *.log -not -path "./virtfs/*" -exec tee {} \; </dev/null

Please note, in WHM / cPanel server, this will clear logs including created by server.

So, If you need to do this manually for specific file, you can simply use any of following commands,

# > file.name

# :> file.name

# true > file.name

# echo "" > file.name

# echo  > file.name

# dd if=/dev/null of=logfile

# dd if=/dev/null > logfile

Still there are several methods to clear it.

Saturday, March 16, 2019

Connect MS-SQL Database on PHP under Ubuntu-18.04

I can see that several tutorials available over Internet for "Connecting Ms SQL by PHP code on Ubuntu 18.04", But as much as I saw, non of the mention about a small thing that plays important role in this. Here it is,

Let's see how to configure,

Requirements:
1. MS SQL 2017 STD (I've tested with 2014 Express too, It's working fine.)
2. Apache2 with PHP 7.2 (I'm not sure about previous versions, but as per my search, it'll work well on other versions too.)

1. Steps to install Microsoft ODBC Driver for SQL on Ubuntu:

sudo su -l

curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 

#Change OS version based on yours, 16.04 or 18.04 as per your need.

curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

apt update
apt -y install msodbcsql17
apt -y install unixodbc-dev mssql-tools

Detailed steps for other operating systems and versions are located at,
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

2. Installing and configuring sqlsrv and pdo_sqlsrv for PHP

apt -y install php-pear php-dev
pecl install sqlsrv
pecl install pdo_sqlsrv

echo "extension=sqlsrv.so" > /etc/php/7.2/mods-available/sqlsrv.ini
echo "extension=pdo_sqlsrv.so" > /etc/php/7.2/mods-available/pdo_sqlsrv.ini

#This may vary based on php version and loaded php conf.
ln -s /etc/php/7.2/mods-available/sqlsrv.ini /etc/php/7.2/apache2/conf.d/20-sqlsrv.ini
ln -s /etc/php/7.2/mods-available/pdo_sqlsrv.ini /etc/php/7.2/apache2/conf.d/20-pdo_sqlsrv.ini

3. Checking connection to the SQL Server with sqlcmd:

#Configuring sqlcmd on profile,

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

# Structure for sqlcmd connection test,

sqlcmd -S {ServerNmae\InstanceName} -U {UserName} -P {Password}

example:
sqlcmd -S MYSQLSERVER\SQL2017STD -U kumar_test_usr -P MySeurePassword

But, It doesn't work for me and throws error like,

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2AF9.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

After some investigations, I found that the problem with port of the SQL server and Named Instance.

The default port of the SQL server (1433) doesn't work here. You should have to mention port of the SQL

So, First you have to find the port, which is located at your SQL Server Configuration Manager

SQL Server Network Configuration -> Protocol for SQL2017STD {This will be your Instance Name}

TCP/IP - > Right click and Enable it, If it's disabled.
TCP/IP - > Right click and go to Properties, Under the IP Address tab, Go to the end, Check out the Port mentioned in TCP Dynamic Ports or you can specify your port on TCP Port.

We going to use this port for connection.

As we are using Named Instance, we should mention "\\" instead of "\" in connection string, the final command will be like as mentioned below,

sqlcmd -S MYSQLSERVER\\SQL2017STD, 55042 -U kumar_test_usr -P MySeurePassword

If you are not have access to find the port, you can use nmap to find it,

4. Checking connection to the SQL Server with php:

PHP code for check the connection,

<?php
$serverName = "MYSQLSERVER\\SQL2017STD, 55042";
$connectionInfo = array( "Database"=>"kumar_test_delete", "UID"=>"kumar_test_usr", "PWD"=>"MySeurePassword");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

Friday, October 12, 2018

disable_functions with Easy Apache 4 and PHP-FPM

Recently I've faced the issue about disable_functions !!!

I need to use 'shell_exec' for one of application with PHP.

By default php will disable some functions with php.ini file with "disable_functions" line.

It will look like,

disable_functions = "exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source"

If you wanna use these functions, you have to remove that function name from disable_functions line on php.ini.

Let's take a look on configurations,

I logged on my WHM and went to "Home »Software »MultiPHP INI Editor", By choosing my php version, I can see the file. Shockingly, disable_functions is empty. If it's empty, then how the function is getting disabled. And verified my php.ini file path. It's too respond same that "disable_functions" with empty value.

So I used phpinfo() in side website and checked. It's showing disable_function is active with 'shell_exec'.

So who is that enabling "disable_functions" field with 'shell_exec'.

Everything fine before upgrading to EA4 with PHP-FPM. So he must be doing these changes.

Yes, once I turn off PHP-FPM for the website. It's working. It's simple solution for single website. But, How to do this globally ? Will update shortly !!!

Edit crontab with VIM

How to change crontab editor from NANO to VIM ?

Usually google searches give following method, But it didn't work for me,

export EDITOR=/usr/bin/vim
crontab -e

or

EDITOR=vim crontab -e

At same time, following is working fine for me.

export VISUAL=/usr/bin/vim
crontab -e

VISUAL=vim crontab -e

Tuesday, September 18, 2018

cPanel MultiPHP Manager with PHP-FPM

We all know, cPanel is one of best server admin panel. It's best thing is Easy Apache. It is user friendly GUI platform for managing Apache and PHP in WHM panel. cPanel is going to withdraw support for EasyApache 3 (EA3) and asking every user to upgrade EA4.

Recently I've upgraded it, It's too simple process, you can do this with their official tutorial.

But, after upgraded and if you are enabled and using PHP-FPM (FastCGI Process Manager), you may face some slowdown on websites or you may face frequent "This site can't be reached" error message on browser.

This is happens due to max_children size may exceed. That means, the default value of max_children is set to 5 on PHP-FPM. You can trace this issue with below command,

root@server [~]#egrep 'example.*max_children' /opt/cpanel/ea-php56/root/usr/var/log/php-fpm/error.log

the above commend will help to find specific website, where example is domain name without extension and ea-php56 is php version, where it may ea-php70 or ea-php72 or any other based on your php version.

This will give result as mentioned bellow,
[18-Sep-2018 05:34:58] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 08:28:00] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 08:44:26] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 08:44:55] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 09:31:40] WARNING: [pool example_com] server reached max_children setting (5), consider raising it
[18-Sep-2018 09:57:16] WARNING: [pool example_com] server reached max_children setting (5), consider raising it


If you get result as mentioned above, you may need to adjust max_children value on bellow steps.

WHM Home > Software > MultiPHP Manager

If you are not sure about website name, you can use below command to get all results,

root@server [~]#egrep 'consider raising it' /opt/cpanel/ea-php56/root/usr/var/log/php-fpm/error.log

Saturday, July 21, 2018

Finding multiple folders in Linux with single command

Finding multiple folders with single commend on Linux is not a big deal.

I wanna know all temp / tmp and cache folders in websites, that are configured with WHM to exclude from the backup. Here is the command for that,


find /home/*/public_html \( -type d -iname "cache" -or -iname "tmp" -or -iname "temp" \)

To know size of these folders,

find /home/*/public_html \( -type d -iname "cache" -or -iname "tmp" -or -iname "temp" \) -exec du -sh {} \;

Friday, July 20, 2018

Finding files with in date range

Here is the command for find files that was modifed between 1st July 2018 to 20th July 2018 with it's details,

find . -type f -name "*.php" -newermt 2018-07-01 ! -newermt 2018-07-21 -exec ls -l {} \;

Please note, you have to mention next date of till date.

Friday, July 6, 2018

Shell script for read line from text file

My need:


Read line from the text file by shell script and append few text before and after the line.

Script:

#!/bin/bash
file="/root/scource.txt"
while IFS= read -r line
do
                echo -e "        <User>"
                echo -e "            <Path>$line</Path>"
                echo -e "        </User>"
done <"$file"


cat source.txt

/home/kumar
/home/moulee
/home/admin

Output:
<User>
  <Path>/home/kumar</Path>
</User>
<User>
  <Path>/home/moulee</Path>
</User>
<User>
  <Path>/home/admin </Path>
</User>

Friday, March 25, 2016

Installing PHP 5.6 on Ubuntu Server 14.04.4 LTS

Hello Beginners,

Hopefully most of the Sys Admin kids won't consider about LTS (Long Term Support) or EOL (End Of Life) on product.

Every product have it's version and its end of life, you can see about EOL of php by following link.

http://php.net/supported-versions.php
http://php.net/eol.php

As same Ubuntu have

https://wiki.ubuntu.com/LTS

Okay now, why we discuss about it,
Ubuntu 14.04.4 LTS is latest product of Ubuntu which have Long Term Support, PHP 5.5 is php version support by Ubuntu 14.04.4 LTS, that means php in it's EOL. So it's good to install Ubuntu 14.04.4 with PHP 5.6

Normal repo of Ubuntu don't have PHP 5.6, so we can do following steps to install PHP.

$sudo add-apt-repository ppa:ondrej/php5-5.6
$sudo apt-get update
$sudo apt-get install php5
$php5 -v

That's it.

Enabling Modules in PHP5 & Apache2 @ Ubuntu 14.04.4 LTS

Usually beginners of LAMP administrator having trouble to installing and enabling modules of PHP and Apache2

In older days we can do this by config page or by linking modules location to PHP or Apache module enable location, but we can do same with following comments easily, Please note this is advisable if you using Ubuntu repository to install packages.

Enabling module for Apache2

$sudo a2enmod module_name

Ex.

$sudo a2enmod rewrite

Enabling module for PHP5

$sudo php5enmod module_name

Ex.
$sudo php5enmod mcrypt


Thursday, June 18, 2015

Clearing RAM cache and SWAP memory in Linux

If your server get hanged, RAM cache is one of the major reason,. It happens frequently on web server when we are under attack or web developer dosen't consider about performance of application. Miss configuration of servers like mysql and apache is one of the main reason.

We can see ram cache amount on htop, which is user friendly monitoring tool for Linux servers. Because of it we may loss some connections and it's interaction with server. We can do this by our own risk.

What is RAM cache ?

It is a portion of memory made of high-speed static RAM (SRAM) instead of the slower and cheaper dynamic RAM (DRAM) used for main memory. Memory caching is effective because most programs access the same data or instructions over and over.

To clear cache :

free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m

To clear pagecache change value of echo to 1 on about commend, to clear dentries and inodes change value of echo to 2 on about commend, echo 3 will clear all of three pagecache, dentries and inodes.

To clear swap:

Up-to I know that there is no straight way to this, But we can do it by disable and enable the swap, We can do this by following command,

To turn off : swapoff -a
To turn on : swapon -a 

Wednesday, May 6, 2015

Finding malware codes on web pages

It is beginners trick, not advised for administrators.

Lot's of tools are available on Linux to find the malware codes injected on server. But most of them not detect the page with url encoded. Because it's php functionality. Mostly of this happens when developer or administrator not aware problem when we providing 777 permissions to pages on web servers. In these cases simple java-script injection will make server to get down.

We can disable this function, urldecode but unfortunately it's bad idea on shared hosting and most of frameworks using url encode and decode. In this case, we can do some manual work for figure-out codes by following commend.

find . -iname '*php' | xargs grep 'urldecode' -sl

This will list the files that have urldecode. We can check page and remove code.

Tuesday, February 3, 2015

Query to find size of MySQL DB

When we are using shared hosting server with Windows or Linux, without any control panel software, It's little bit hard to find user's disk usage on mysql.

In this case we can use  following command on mysql to get size of database.

SELECT table_schema "DB Name", Round(Sum(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" FROM information_schema.tables GROUP  BY table_schema;

Saturday, October 25, 2014

Copy files with original timestamp in Linux commandline

If you move files on Linux with command mv, time-stamp won't be change, But if you copy with command cp, it'll take current time for time-stamp. If we wanna get original time-stamp, you can use following command on Linux,

cp -R -u -p /source /destination

What is time-stamp ?

When we create or modify file / directory, it'll store creation and / or modification date and time on file / directory properties. Which is called time-stamp.





Friday, October 24, 2014

Setting up default permissions to Files and Directories

Most of the Linux beginners / Linux - Level 1 administrator do this funny thing. Setting up 777 permission to files and folders.

With, chmod -R 777 folder_name

Some times it'll help to destroy the server easily by hackers.

Let's come to topic, How to setup default / basic level permission to files and folders.

chmod -R 777 folder_name will give you files and folders with read, write and execute permission, Mostly we no need of execute permission for files.

If we try to change it as chmod -R 666 folder_name, we are not able to open directory.

If we have 100s of sub directory with 1000 of files in each directory,... oh my god.... How do we fix it worst thing.... Here is command.

first open your parent directory, with
cd parent_directory_name

And run following commands,

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

Working: On first command It'll check type of the object whether it is file or directory, if it is directory it'll change permission to 755, else leave. On second same condition, if it is file then change permission to 644 else leave with same,

Tuesday, October 21, 2014

Create mail account and alias by command line with parallel plesk panel

Following command is used to create mail account by Linux command line for parallel plesk panel

/usr/local/psa/bin/mail --create name@domain.com -mailbox true -passwd Pa55w0rd -antivirus in


Following command is used to create mail account by Linux command line for parallel plesk panel

/usr/local/psa/bin/mail -u name@domain.com -aliases add:alias1,alias2,alias3,alias4,alias5,alias6


Note : Change bold place as your requirement


You can use this line multiple time for create more mail accounts / alias accounts, Need to work some more for get input from text file and create mails and alias. I'll try to update soon with script.

Monday, October 20, 2014

Changing mail queue life time in qmail installed with plesk

Default queue life time is 7 days. That means, your mails will be stored in queue for 7 days if it not able to deliver. 

We can reduce this queue time with following steps.

To check current queue life time:

/var/qmail/bin/qmail-showctl | grep life

Time mentioned as seconds,


To set queue life time:

echo "172800" > /var/qmail/control/queuelifetime

Here I've set queuelifetime is 2 days.


For changes to take effect

service qmail restart

or

/etc/init.d/qmail restart

Tuesday, June 3, 2014

Timezone restore in Linux

We got problem in Centos 6 server with plesk panel,

Timezone got reset frequently on one that server, I've found the issue on update of tzdata package, Working on it to find permanent solution, But in mean time I've used following script for reset timezone.

# /bin/sh
TZ=`date '+%Z'`
CTIME=` date '+%Y/%m/%d'-'%T'`
if [[ "$TZ" != IST ]] ; then
touch /var/log/timezone-script.log
echo "["$CTIME"]" "Current time-zone is" $TZ "and Needs to change" >> /var/log/timezone-script.log
rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
else
echo "["$CTIME"]" "Current time-zone is" $TZ "and Working fine" >> /var/log/timezone-script.log
fi
Change your timezone what you need, I'm using IST and using log file to know status. Even we can use mail function on it.
 Default location of timezone is /usr/share/zoneinfo/ 

Friday, December 27, 2013

Change Host-Name in Linux

Display Hostname

To display the current hostname, we can use hostname command.

#hostname
server1.adminnote.com

Change Hostname

We've to update two files:

/etc/hosts
/etc/sysconfig/network  or /etc/hostname

To add/remove hosts on Redhat / CentOS / Fedora / Debian / Ubuntu

#vim /etc/hosts
123.123.123.123 server2.adminnote.com
save and close :wq
Note: add all required hosts with IP

To add/remove hostname on Redhat / CentOS / Fedora

#vim /etc/sysconfig/network
HOSTNAME=server2.adminnote.com

save and close :wq

#hostname server2.adminnote.com
#hostname
server2.adminnote.com

To add/remove hostname on Debian / Ubuntu

#vim /etc/hostname

server2.adminnote.com

save and close :wq

#hostname server2.adminnote.com
#hostname
server2.adminnote.com