Skip to content

Instantly share code, notes, and snippets.

@Jonarod
Last active April 1, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonarod/1227b505c5ad248cdbae06b8ced5c44e to your computer and use it in GitHub Desktop.
Save Jonarod/1227b505c5ad248cdbae06b8ced5c44e to your computer and use it in GitHub Desktop.
Useful ubuntu commands

Automation

Execute a command at specific time

crontab -e

then, helped by crontab.guru, add a new line like:

0 14 * * * echo "hello" > ~/Desktop/output_cron.log

every day at 14:00, output 'hello' in the file '~/Desktop/output_cron.log'

To list all active CRONs:

crontab -l

To delete all active CRONs:

crontab -r

To create a system-wide CRON, append the CRON:

sudo nano /etc/crontab

Processes

List all services

ls /etc/init.d/

List all services BY STATUS

systemctl list-unit-files --type=service
# or sudo service --status-all

List all processes RUNNING

ps aux

more useful ps commands here

Kill a process by PID

kill -9 <PID>

Kill a process using the mouse

xkill

then click the window to kill

Networking / Security

Generate rsa keys

# create asymetric public/private keys
ssh-keygen -t rsa -C "your_email@example.com"
# send keys to distant server
ssh-copy-id -i ~/.ssh/my_key_rsa.pub -p 22 user@WW.XX.YY.ZZ

List all open ports

sudo apt-get install net-tools
sudo netstat -ntapul

Firewall

Caution: iptables rules ORDER MATTERS. We first need to explicitly list allowed ports BEFORE denying all others.

# List all firewall rules
sudo iptables -L

# !!!!! DANGEROUS !!!!! 
# Flush/Delete all firewall rules
sudo iptables -F

# Allow ports 22, 80 and 443
sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT

# Deny everything else
sudo iptables -P INPUT DROP

after reboot, all iptables config gets wiped out. To make them persistent:

Create a file to store iptables rules, make it executable:

echo "#!/bin/sh" > /etc/network/if-up.d/iptables
echo "iptables-restore < /etc/firewall.conf" >> /etc/network/if-up.d/iptables
chmod +x /etc/network/if-up.d/iptables

tweak your iptables rules to your liking or edit /etc/firewall.conf. Then, when everything is fine, save them:

iptables-save > /etc/firewall.conf

the bash script we saved to /etc/network/if-up.d/iptables will reload iptables rules defined in /etc/firewall.conf at startup.

Rate limiting

fail2ban will "put in jail" IP that attempts to connect to your machine after some unsuccessful attempts.

sudo apt-get install fail2ban
# copy example config file
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# edit configurations
sudo nano /etc/fail2ban/jail.local

# restart fail2ban
sudo service fail2ban restart

System

Get directory + file size

du -hac .

Check partitions usage

df -h

Uninstall programs

sudo apt-get autoremove <program>

Check RAM usage

free -h

Disable / Enable swap

Useful when computer gets slow because RAM exceeded usage. Swap being really slow, disabling swap will put data back to RAM.

# Disable
sudo swapoff -a
# Enable
sudo swapon -a

Bash tricks

Run executable in the background and log in file

myexecutable & > mylog.log

Mini scraper

#!/bin/bash
crawl () {
        echo page$1 &&
        page=`wget -U 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0' \
        https://www.leboncoin.fr/_immobilier_/offres/ile_de_france/p-$1 -qO-` &&
        if [[ $page =~ (window.__REDIAL_PROPS__.=.)(.*])(.</script>) ]]; then
                echo "${BASH_REMATCH[2]}";
        else
                echo "no match found";
        fi > page$1.json
}

for i in {1..100}
do
        crawl $i &
done

wait

Terminal

Use vim

Vim is quite derouting at first if you don't know what to do...

# open/create file `myfile.txt`
vi myfile.txt

Navigate the document using keyboard arrows. Then to edit the document: PRESS i on your keyboard.

Make changes... then to close the document: press ESC on your keyboard, then type :wq and press ENTER

:wq : write changes + quit :!q : force quit and loose changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment