Pages

Showing posts with label LINUX-UNIX. Show all posts
Showing posts with label LINUX-UNIX. Show all posts

Friday, September 15, 2023

Unban 1 IP - Gỡ IP ra khỏi fail2ban

Unban 1 IP

1. Kiểm tra IP có bị chặn không.

iptables -v -L -n



2. Gỡ IP ra khỏi danh sánh
fail2ban-client set  asterisk-cpbx unbanip 10.10.6.65



Thursday, January 8, 2015

Apache Error [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)

One fine morning I have discovered that my Apache (httpd) service is down and can't be started :)

Step 1: Checking httpd Service Status:
#/etc/init.d/httpd status
httpd dead but pid file exists

Saturday, March 15, 2014

Simple shell script to block failed ssh attempts


It’s annoying having scripts and bots attempting to ssh into your pbx server. If you have proper passwords they don’t stand a chance of getting in, but it’s not satisfying to ignore them while they create load and clutter up your log files with failed login messages.
There are many tools out there to monitor and block IP addresses that repeatedly attempt to connect. My needs were:
  • low memory (memory is precious on a VPS)
  • simple
  • customizable
After wasting time installing various complicated python and perl scripts, I found a clever use of awk that counts matches in a log file and I put together this simple shell script:
scan-secure.sh
#!/bin/sh

# scan /var/log/secure for ssh attempts
# use iptables to block the bad guys

# Looking for attempts on existing and non-existing users. For example:
# Nov  2 22:44:07 pbxer sshd[28318]: Failed password for root from 74.143.42.70 port 52416 ssh2
# Nov  3 00:06:57 pbxer sshd[31767]: Failed password for invalid user mat3 from 192.203.145.200 port 35841 ssh2

tail -1000 /var/log/secure | awk '/sshd/ && /Failed password for/ { if (/invalid user/) try[$13]++; else try[$11]++; }
END { for (h in try) if (try[h] > 4) print h; }' |
while read ip
do
 # note: check if IP is already blocked...
 /sbin/iptables -L -n | grep $ip > /dev/null
 if [ $? -eq 0 ] ; then
  # echo "already denied ip: [$ip]" ;
  true 
 else
  # echo "Subject: denying ip: $ip" | /usr/sbin/sendmail notify@email.com
  logger -p authpriv.notice "*** Blocking SSH attempt from: $ip"
  /sbin/iptables -I INPUT -s $ip -j DROP
 fi
done

awk does all the magic. It grabs relevant lines, splits the lines into tokens, stores IP addresses in a hash and counts how many times they were seen, and finally outputs all IP addresses that were seen more than four times.
For the first few days, it’s interesting to receive an email when IP addresses are banned. Add your email and comment out that line.
Once you’re sure that the script is working how you’d like, you can add it to cron so that it runs every few minutes. I found that every two minutes works for me.
 
crontab -e
# scan the secure log every 2 minutes
*/2 * * * * /root/scan-secure.sh
 
After a few months you might find that the iptables are started to get cluttered.
iptables -L -n
You can clean them out by “flushing” them.
iptables -F
Now sleep tight knowing that people aren’t continually coming by to see if you locked your doors.

Wednesday, November 27, 2013

Configuring Sendmail to relay through Gmail SMTP

Sending email directly from the MTA (mail transfer agent) on one’s server is now considered to be faux pas. Unless you’ve got that machine configured within DNS (MX record, reverse lookup, etc), it’d likely fail most basic spam checks at the destination mail server. The complexity of the configuration increases if you’ve got the need to masquerade emails from multiple domains. Instead, the ideal approach is to relay through a central SMTP gateway for all of the outbound emails generated from your server(s).

I’ve been on Google mail hosting for a long time now and needed to configure my server to relay mail through it. Here’s a nice article that walks you through exactly what needs to be done to configure your sendmail MTA to relay through Gmail’s SMTP servers. Another similar post here.

Friday, November 15, 2013

Backup and Restore Linux OS using tar, dd command

1/ Backup Hard Disk, MBR Using dd Command

 Chắc cũng có nhiều bạn thành thạo sử dụng lệnh dd rồi nhưng mình cũng giới thiệu sơ qua cho 1 số bạn chưa biết tham khảo thêm nhé.

Trong trường hợp cần backup 1 ổ đĩa trên Linux các bạn có thể sử dụng lệnh sau:

- Backup 1 ổ cứng sang ổ cứng khác:

Mã:
dd if=/dev/hda of=/dev/hdb
- Backup ổ cứng sang file image:

Saturday, September 29, 2012

HOWTO : Blank screen when boot up Ubuntu 11.04 Desktop

I have a very old SONY laptop and the model is PCG-TR1. The following is the display card of the laptop :

lspci | grep VGA

00:02.0 VGA compatible controller: Intel Corporation 82852/855GM Integrated Graphics Device (rev 02)

You can boot up the Live CD or DVD and install the system properly. It is no problem when the first boot up after the installation. However, it will be blank screen / black screen on the second and later boot up.

Tuesday, July 10, 2012

Install FFmpeg, Mplayer, Mencoder, FFmpeg-PHP on CentOS 5.x automatically

Install FFmpeg, Mplayer, Mencoder, FFmpeg-PHP on CentOS 5.x automatically

This Tutorial is for all phpFox users have unmanaged VPS and interested to enable Video Upload module and i hope this is help.

My primary Linux distribution of choice is CentOS. CentOS is an Enterprise-class Linux Distribution derived from sources freely provided to the public by RedHat. Thus CentOS is merely speaking a copy of RedHat and provides the same stability and security.

Monday, May 30, 2011

Changing MySQL socket to fix 'Error Establishing a Database Connection' error in WordPress

When I work on WordPress sites, if I don’t do an undocumented trick, I can’t get WordPress to connect to the database. I get the ‘Error Establishing a Database Connection’ error.
If you’ve double-checked your wp-config.php settings to make sure the host, username, password and database match exactly, and you’re able to connect with the username, password and host on the command line, this trick might help you too.
It’s simple, append :/path/to/socket to your hostname like this:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', 'wordpress');
define('DB_HOST', 'localhost:/tmp/mysql.sock'); // my computer has the mysql socket on /tmp/mysql.sock

If you don’t know what your socket is, use the mysql command line tool to find it. Here’s how:
From your shell type this (obviously substituting in your username, host and database)
> mysql -u wordpress -p -h localhost wordpress
MySQL will start up and give you a prompt. At the mysql prompt type s (backslash + s). That will give you a screen full of status information like this:

Tuesday, May 24, 2011

Set Time cho Server Linux

date +%T -s "10:13:13"


where,

10: hour
13: Minute
30: second

Thursday, April 28, 2011

Create root user account on Linux

Create extra root user account to your Linux box.

The ‘root’ account with user id 0 is the most powerful user in Linux and Unix system. This article show the step by step to create the duplicate root account that have the same privileges as super user root account on the Linux Fedora Core operating system.
You may want to make replicate of root user account, or to create more than one user account that have the same capabilities as a 'root' user (superuser) account. Why, may be for a root backup user account, incase of something happen to the 'root' user account or may be just for fun :-).
To create another root user account, we need to replicate the 'root' user account characteristic, then make one account that have the same characteristic and capabilities of the 'root' user account.

Tuesday, April 19, 2011

Xem password User thay doi khi nao ( Last Change Password)

- Xem password cua User thay doi khi nao

root@ubuntu:/thien# nano lastchange.sh

#!/bin/sh

IFS=:
today=`expr \`date +%s\` / 60 / 60 / 24`
while read a b c d e f g h i; do
if [ "$b" != '*' -a "$b" != '!!' ]; then
last=`expr $today - $c`
echo "$a = `date --date "-$last day" +%Y/%m/%d`"
fi
done < /etc/shadow

root@ubuntu:/thien# chmod 777 lastchange.sh

root@ubuntu:/thien# ./lastchange.sh
[: 10: !=: unexpected operator
[: 10: !=: unexpected operator
ubuntu = 2011/04/19
root@ubuntu:/thien#

Wednesday, March 30, 2011

Files .htaccess và .htpasswd được dùng để làm gì?

Files .htaccess và .htpasswd được dùng để làm gì?

Nhiều web servers, trong đó có Apache thường dùng file .htaccess để bảo vệ các thư mục trên web server. Nếu một thư mục nào đó trên web server có lưu file .htaccess, khi bạn duyệt qua thư mục này, web server sẽ popup một hộp thoại yêu cầu nhập username và mật khẩu giống y như lúc bạn gặp Firewall của VDC! Chỉ khi nào bạn nhập vào một username và mật khẩu đúng, bạn mới có thể xem được trong thư mục đó có những gì?!
Danh sách các username và mật khẩu của .htaccess thường được lưu trong file .htpasswd
* Tại sao phải dùng dấu chấm ở trước trong tên file '.htaccess'? Các file có tên bắt đầu là một dấu chấm '.' sẽ được các web servers xem như là các file cấu hình. Các file này sẽ bị ẩn đi(hidden) khi bạn xem qua thư mục đã được bảo vệ bằng file .htaccess

Phát hiện và chống xâm nhập Web Server với Mod Security

Phát hiện và chống xâm nhập Web Server với Mod Security

1. giới thiệu về Mod_security

ModSecurity là một bộ máy phát hiện và phòng chống xâm nhập dành cho các ứng dụng web (hoặc 1 web application firewall). Hoạt động như một module của máy chủ web Apache, mục đích của ModSecurity là tăng cường bảo mật cho các ứng dụng web, bảo vệ chúng khỏi các loại tấn công đã biết và chưa biết.

Sau khi cài đặt Apache mặc định sẽ không có mod_security.Bạn phải download mod_security từ http://www.modsecurity.org. Phiên bản mới nhất hiện nay là 1.9. Mod_security dùng được cho cả apache 1.x và 2.x (trên cả windows ,*.nix …)

Kinh nghiệm config host đề giảm hack local

Vấn đề bảo mật cho web bây giờ rất được quan tâm ! Hôm nay LK xin chia sẽ cho các bạn kinh nghiệm LK đã mày mò và học hỏi được bởi các anh em W3 và VBF về việc config host nhằm giảm được việc hack Local !! Nếu làm cái j` hok đúng mong anh em góp ý để LK rút kinh nghiệm và config host W3 tốt hơn nha

1) Đổi tên và giấu file config ở 1 góc nào đó (nhớ làm ít nhất 3-4 link require nha , giấu càng sâu càng tốt !)

2) Sau khi giấu file config xong thì ta zend file config (cài này dùng để mã hóa cho hacker ko đọc đựoc file config và tỉ lệ dezend bây h ko phải là 100%)

3) Cấu hình CHMOD cho các folder của forum là 101 or 711 ! riêng các link require đến file config thì bạn chỉ có thể chỉnh CHMOD là 404 !!còn lại các parent thì các bạn chỉnnhh 711 or 101

Friday, March 25, 2011

How to reset MySQL password for XAMPP ( Lost password Mysql)

1. Go to your xampp\mysql\bin\ folder
  • Edit my.ini and insert skip-grant-tables below [mysqld]
  • Restart MySQL
2. Set new password for your root user by running
D:\xampp\mysql\bin
# mysql.exe -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.41 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> UPDATE mysql.user SET Password=PASSWORD('123456') WHERE User='root'
-> ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql>


3. Go to step1
to your xampp\mysql\bin\ folder
  • Edit my.ini and Delete skip-grant-tables below [mysqld]
4. Restart Mysql.




Thursday, March 24, 2011

Kiểm tra dung lượng Database MySQL

1) MySQL command – sum up the data_length + index_length is equal to the total table size.
data_length – store the real data.
index_length – store the table index.

List out all the databases size

SELECT table_schema "Data Base Name", sum( data_length + index_length) / 1024 / 1024
"Data Base Size in MB" FROM information_schema.TABLES GROUP BY table_schema ;
Command login Database:
[root@Asterisk backup]# mysql -u root -pMY_PASSWORD
mysql> SELECT table_schema "Data Base Name", sum( data_length + index_length) / 1024 / 1024
-> "Data Base Size in MB" FROM information_schema.TABLES GROUP BY table_schema ;

Wednesday, February 23, 2011

Tạo symbolic link và hard link trong Linux

1. Create a symbolic link in UNIX


A symbolic link is a pointer to another file or directory. It can be used just like the original file or directory. A symbolic link appears in a long listing (ls -l) with a reference to the original file/directory. A symbolic link, as opposed to a hard link, is required when linking from one filesystem to another and can be used within a filesystem as well.


Friday, January 7, 2011

Hướng dẫn sử dụng lệnh grep trong Linux / UNIX

grep là lệnh tìm kiếm các dòng có chứa một chuỗi hoặc từ khóa trong file. Theo mặc định, grep in những dòng phù hợp. Sử dụng grep để tìm kiếm các dòng văn bản phù hợp với một hoặc nhiều biểu thức thông thường, và in ra những dòng phù hợp.

Cú pháp của lệnh grep :

grep 'word' filename
grep 'string1 string2' filename
cat otherfile | grep 'something'
command | grep 'something'

Tuesday, October 5, 2010

How to Clear filesystem memory cache

he linux Kernels 2.6.16 and Newer kernels provide a mechanism to have the kernel drop the page cache and/or inode and dentry caches on command, which can help free up a lot of memory.

You have to first check the memory usage with command

#free -m

To use /proc/sys/vm/drop_caches, Now echo a number to it.

To free pagecache:
# echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:
# echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:
# echo 3 > /proc/sys/vm/drop_caches

As this is a non-destructive operation and dirty objects are not freeable, run sync first!
sync; echo 3 > /proc/sys/vm/drop_caches

How the command works :

root@sserver # free -m
total used free shared buffers cached
Mem: 12174 7824 4349 0 414 5953
-/+ buffers/cache: 1456 10718
Swap: 24003 241 23761

root@server # sync; echo 3 > /proc/sys/vm/drop_caches

root@server # free -m
total used free shared buffers cached
Mem: 12174 1684 10489 0 9 125
-/+ buffers/cache: 1549 10624
Swap: 24003 241 23761


Monday, September 27, 2010

How to Backup and Restore (Export and Import) MySQL Databases Tutorial

phpMyAdmin can be used to export or backup MySQL databases easily. However, if the database size is very big, it probably won’t be a good idea. phpMyAdmin allows users to save database dump as file or display on screen, which involves exporting SQL statements from the server, and transmitting the data across slower network connection or Internet to user’s computer. This process slow the exporting process, increase database locking time and thus MySQL unavailability, slow the server and may simply crash the Apache HTTPD server if too many incoming web connections hogging the system’s resources.