Reset (flush) Iptables rules Linux

Bash script to correctly reset (flush) all iptables rules: #!/bin/bash   # Simple script to reset all iptables rules # Without creating issues like with "iptables -F"   IPTABLES="$(which iptables)"   # Set default policies (accept) for all three default chains $IPTABLES -P INPUT ACCEPT $IPTABLES -P FORWARD ACCEPT $IPTABLES -P OUTPUT ACCEPT   # […]

Simple Bash Iptables Firewall Script

Sample firewall script with iptables to allow only trusted traffic: #!/bin/bash   # Simple firewall script to allow only trusted inbound connections # Useful for load balanced servers to allow only inbound traffic on port 80 from the load balancer # Using /etc/crontab you can run the firewall script at every reboot: # @reboot root […]

Bash Get the size of a file

With stat you can get the size of a file: stat –printf="%s" /path/to/filestat –printf="%s" /path/to/file In Bash you can store the file size in a variable: FILESIZE=$(stat -c%s "/path/to/file")FILESIZE=$(stat -c%s "/path/to/file") And you can check if a file is of 0 bytes: if [ "$(stat -c%s "/path/to/file")" -eq 0 ]; then echo "The file is […]

Crontab requires escaping any % signs

Crontab requires escaping any % signs, so this will not work: 0 0 * * * root /root/script.sh "%DATE%"0 0 * * * root /root/script.sh "%DATE%" After escaping any % signs, it will work just fine: 0 0 * * * root /root/script.sh "\%DATE\%"0 0 * * * root /root/script.sh "\%DATE\%"

Bash Check folder size Linux

With du you can check the folder size in human readable format: du -h /path/to/directorydu -h /path/to/directory *** It includes the size of the folder and subfolders *** If you want a simpler output, use this: du -hcs /path/to/directorydu -hcs /path/to/directory

Bash remove the first line of a large file

On Linux OS, using tail you can quickly remove the first line of a file: tail -n +2 file > file.newtail -n +2 file > file.new Alternatively you can use ed (that is still fast): ed <<< $’1d\nwq’ fileed <<< $’1d\nwq’ file You can use also sed, but it is much slower: sed -i 1d […]

ERROR 1114 (HY000): The table ‘site’ is fullage done

If you get this MySQL error while you alter a table: ERROR 1114 (HY000): The table ‘site’ is fullage doneERROR 1114 (HY000): The table ‘site’ is fullage done Make sure the partition is not full. Type this command to see the partition status: df -hdf -h

Get Linux Server Unique Machine ID

Type this command to get the unique machine ID of your server: cat /etc/machine-idcat /etc/machine-id Here are more info about /etc/machine-id: The /etc/machine-id file contains the unique machine ID of the local system that is set during installation. The machine ID is a single newline-terminated, hexadecimal, 32-character, lowercase ID. When decoded from hexadecimal, this corresponds […]

Linux make files accessible only by root

Make files accessible only by root (without +x): chmod 0600 file.logchmod 0600 file.log Make files accessible only by root (with +x): chmod 0700 file.shchmod 0700 file.sh Make all *.sh files accessible only by root (with +x): chmod 0700 *.shchmod 0700 *.sh Make all *.sh files accessible only by root (with +x) recursively: chmod -R 0700 […]

Bash Autologin with MySQL MariaDB server

With MariaDB there is a file that contains user\pass for MySQL autologin: /etc/mysql/debian.cnf/etc/mysql/debian.cnf Here is an example output of /etc/mysql/debian.cnf: # Automatically generated for Debian scripts. DO NOT TOUCH! [client] host = localhost user = debian-sys-maint password = randomstring socket = /var/run/mysqld/mysqld.sock [mysql_upgrade] host = localhost user = debian-sys-maint password = randomstring socket = /var/run/mysqld/mysqld.sock […]