Halo All, Before you read any further here i am using Ubuntu 14.04 so this linux recipes will be in reference to that. Ok, Let’s this checkout!
Linux Recipes for Ubuntu
SSH
SSH connect with pubkey / privatekey
1 |
sudo ssh root@192.168.1.2 -p3386 -i /home/johndoe/server/id_rsa |
Directory
Split data by size
1 |
split -d -b 300M laravel-2022-03-24.log logging |
Get free disk spaces
1 |
df -h --total |
Find all directory from existing directory and grant permission
1 |
sudo find /home/john/public_html -type d -print0 | xargs -0 chmod 0775 |
Find all files from existing directory and grant permission
1 |
sudo find /home/john/public_html -type f -print0 | xargs -0 chmod 0664 |
Grant permission to user john from existing directory
1 |
sudo chown -R john:john /home/john/public_html |
List Files and Directory (In MB)
1 |
ls -al --block-size=MB /data/data01/log |
Find data by size
1 |
find /var/ -type f -size +10M -exec ls -lh {} \; |
1 |
find . -type f -size +10M -exec ls -lh {} \; |
Remove file and folder
1 |
sudo rm -rf /home/john/public_html |
1 |
sudo rm -rf /etc/rc*.d/*mysql |
Process/Network
Get all running process
1 |
netstat -lnt |
1 |
netstat -antp |
Get all running services
1 |
sudo service --status-all |
Remove from services
1 |
update-rc.d -f apache2 remove |
Get running process by number of port
1 |
netstat -plantu | grep 3306 |
Get running process by name
1 |
netstat -tap | grep mysql |
Kill process by name
1 |
pkill -9 -f mysql |
Setup network
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
nano /etc/network/interfaces auto eth0 iface eth0 inet static address 192.168.137.2 gateway 192.168.137.1 network 192.168.137.0 netmask 255.255.255.0 broadcast 192.168.137.255 dns-nameservers 8.8.8.8 8.8.4.4 auto eth0:0 iface eth0:0 inet static        address 192.168.137.100        netmask 255.255.255.0 |
1 2 3 4 5 6 7 8 9 10 |
auto lo inet lo inet loopback auto eth0 iface eth0 inet dhcp pre-up sleep 2 auto eth1 iface eth1 inet static address 192.168.10.11 netmask 255.255.255.0 |
User Management
Create new user
1 2 |
sudo adduser {{HELOO_USER}} sudo logout |
1 2 3 4 5 |
sudo su {{HELOO_USER}} ssh-keygen -t rsa -b 2048 -v chmod -R 700 ~/.ssh/ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys |
1 |
visudo |
1 |
{{HELOO_USER}} ALL=(ALL:ALL) ALL |
Compress – Uncompress File / Folder
compress file and folder from existing path
1 |
tar -zcvf apt-software.tar.gz /var/cache/apt/archives |
Extract file from existing file
1 |
tar -xzf wordpress.tar.gz |
Others
Get List Linux Images
1 |
sudo dpkg --list 'linux-image*' |
Remove Linux Image
1 |
sudo apt-get remove linux-image-3.13.0-61-generic |
Get Kernel Used
1 2 3 |
uname -r uname -a |
Get Linux Version
1 |
lsb_release -a |
Clean up APT Cache
1 |
sudo apt-get clean |
Remove existing GPG Key
1 |
sudo apt-key list |
1 2 |
pub 1024R/B455BEF0 2010-07-29 uid Launchpad clicompanion-nightlies |
1 |
sudo apt-key del B455BEF0 |
Clear All Command History
1 |
cat /dev/null > ~/.bash_history && history -c && exit |
Create Symbol Link (for easy access)
1 |
ln -s /usr/local/bin/composer.phar /usr/local/bin/composer |
Get install history
1 |
cat /var/log/apt/history.log | grep 'install ' |
TMUX
Create a new TMUX Session
1 |
tmux new-session -d -s app |
1 |
tmux rename-window -t app:0 'api' |
1 |
tmux new-window -t app:1 -n 'api-dev' |
1 |
tmux new-window -t app:2 -n 'be' |
1 |
tmux new-window -t app:3 -n 'be-dev' |
Access TMUX Session
1 |
tmux a -t app |
UFW
Check UFW Status and Rules
1 |
sudo ufw status |
Enable UFW
1 |
sudo ufw enable |
Disabe UFW
1 |
sudo ufw disable |
Reset UFW Rules
1 |
sudo ufw reset |
UFW Allow Port
1 |
sudo ufw allow 22/tcp |
UFW Allow Specific Port Ranges
1 |
sudo ufw allow 22:25/tcp |
UFW Allow Specific IP Addresses
1 |
sudo ufw allow from 50.23.101.86 |
UFW Allow Specific IP Addresses and Port
1 |
sudo ufw allow from 50.23.101.86 to any port 22 |
UFW Deny Connections Specific port
1 |
sudo ufw deny 22 |
UFW Deny Connections Specific IP Addresses
1 |
sudo ufw deny from 50.23.101.86 |
UFW Deny Connections Specific IP Addresses and Port
1 |
sudo ufw deny from 50.23.101.86 to any port 22 |
UFW Delete Rule Port
1 |
sudo ufw delete 22 |
UFW Delete Rule Port (actual rule)
1 |
sudo ufw delete allow 22 |
OpenSSL
Generate Self Sign Cert
1 2 3 4 5 6 7 8 9 |
openssl genrsa -out domain.key 4096 openssl req -new -x509 -days 3650 -key domain.key -out domain.crt -subj "/C=ID/ST=JawaBarat/L=Bandung/emailAddress=pribumitech@gmail.com/O=pribumitech.com/OU=Android_Developer/CN=192.168.2.85" openssl x509 -in domain.crt -signkey domain.key -x509toreq -out domain.csr if you lose CSR openssl req -key domain.key -new -out domain.csr -subj "/C=ID/ST=JawaBarat/L=Bandung/emailAddress=pribumitech@gmail.com/O=pribumitech.com/OU=Android_Developer/CN=192.168.2.85" |
“Zero Out” The Drive
1 2 3 |
sudo dd if=/dev/zero of=/EMPTY bs=1M sudo rm -f /EMPTY |
POSTGRESQL
Backup Database #1
1 |
pg_dump -U postgres --host localhost --password --format=c --blobs --no-privileges --no-acl --no-security-labels --no-tablespaces --no-unlogged-table-data --inserts --column-inserts DATABASE_NAME > /opt/DATABASE_NAME.backup |
Restore Database #1
1 |
pg_restore -U postgres --host localhost --password --format=c --dbname=DATABASE_NAME /opt/DATABASE_NAME.backup |
RSYNC
Backup data from SLAVE (192.168.2.13) PRIMARY (192.168.2.14)
1 2 3 |
rsync -avuz IP_DESTINATION:/home/FOLDER_DESTINATION /opt/backup rsync -avuz 192.168.2.14:/home/test/odenktools /opt/backup |
Ok that’s all for Linux recipes, i will come back for any update. Happy days!