Linux

Bash scripting

Keep a bash script open when it's over

Add this at the end of the .sh file :

/bin/bash

Create alias for the shell

Run:

vim ~/.bashrc

Add this to the file:

alias xyz='first command & second command'

Save and run:

source ~/.bashrc

Set tab title from a command

Add this to ~/.bashrc

function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

Then use the command:

set-title Whatever

File system

Only keep lines containing a specific string in a text file

grep the_string source_file > destination_file

Delete a folder, including subfiles and folder

rm -rf folder

Find a file (by name)

find . -name '*.sql'

Find a file (by content)

grep -rnw '.' -e 'pattern'

Give full access to a folder to a user

chown -R username directory
chmod -R u+rX directory

System

Memory usage

free
dmesg -H

Processes

Find the PID of a process

pgrep process_name
ps aux | grep process_name

Get information on a process

ps -p process_pid -F

Kill a process

kill process_pid

Find which process is using a port

netstat -tulpn | grep 8029

Startup processes

crontab -e

It can be either

# Command is ran when the cron is matched
<cron> <command>
# Command is ran on startup
@reboot <command>

Tmux

Run a command in a new terminal that will stay open

tmux new-session -d -s aid_dev "python whatever.py"

Log everything into a text file

tmux -vvvv new-session [...]"

List all the open terminal

tmux ls

Kill a session

tmux kill-session -t aid_dev

Network

Firewall

List all the rules

iptables -S

Block a port from the outside

Warning : It will also block localhost!

iptables -A INPUT -p tcp --dport 3306 -j DROP

Block a port from the outside except for 1.2.3.4

iptables -A INPUT -p tcp --dport 3306 -s 1.2.3.4 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j DROP

Delete a rule

Replace -A by -D:

iptables -D INPUT -p tcp --dport 3306 -j DROP

VIM

Shortcuts

Key What
i Enter insert mode
ESC Leave any mode
:wq Save and quit
:q! Quit without saving

Package managers

DNF

Used by : Fedora

First: Update package cache

sudo dnf check-update

Search for a package

dnf search <keyword in title>	
dnf search all <keywords in any field>

Get info on a package

dnf info name_of_package
dnf repoquery --requires name_of_package

Install a package from repo

sudo dnf install name_of_package
sudo dnf install name_of_package1 name_of_package2 ...
# Skip confirmations
sudo dnf install -y name_of_package

Install a package from a file

sudo dnf install package.rpm

Update a package

sudo dnf update name_of_package

Uninstall a package

sudo dnf erase name_of_package

Update everything on the machine

sudo dnf clean all
sudo dnf upgrade

pip (python package manager)

Install an app for current user

pip install Flask-Babel --user

GNOME

Don't lock the screen automatically

gsettings set org.gnome.desktop.session idle-delay 0
gsettings set org.gnome.desktop.screensaver lock-delay 0

Install Gnome Tweak Tools on Fedora

sudo dnf makecache
sudo dnf install gnome-tweak-tool

Then you can find it in the Activity menu.

Make the mouse wheel faster. Run an command on startup

Download this. Install it with rpm :

sudo rpm -i imwheel-1.0.0-4_pre12.fc20.x86_64.rpm

Put the following in ~/.imwheelrc

".*"
None,      Up,   Button4, 5
None,      Down, Button5, 5
Control_L, Up,   Control_L|Button4
Control_L, Down, Control_L|Button5
Shift_L,   Up,   Shift_L|Button4
Shift_L,   Down, Shift_L|Button5

5 is the speed. Run the following

imwheel --kill

To run on startup, create the following file in ~/.config/autostart/imwheel.desktop

[Desktop Entry]
Version=1.0
Type=Application
Name=imwheel
Comment=Speed up the mouse wheel
Exec=bash -c 'imwheel --kill'
Icon=input-mouse
Terminal=true
Categories=Application;

Troubleshooting

"bad interpreter: No such file or directory"

If this happens when trying to launch ./scriptname.sh, try this :

sed -i -e 's/\r$//' scriptname.sh
./scriptname.sh

Set up OpenVPN on a Fedora

wget https://git.io/vpn -O openvpn-install.sh && bash openvpn-install.sh

Then use WinSCP to grab the ovpn file. Send it to the device with the OpenVPN Client.

To run OpenVPN

cd /etc/openvpn
openvpn /etc/openvpn/server.conf

To configure it as a service that runs on startup

sudo systemctl start openvpn-server@server
sudo systemctl enable openvpn-server@server

If it doesn't work :

vim /lib/systemd/system/openvpn-server\@.service

Change the value of “WorkingDirectory” from “/etc/openvpn/server” to “/etc/openvpn”

Set up Kali Linux on Raspberry Pi

  1. Extract it with 7-zip
  2. Turn on the Raspberry, connect to it in SSH with PuTTY. (hostname : kali.home, login: root, password: toor)
  3. Change the root password : http://www.wikihow.com/Change-the-Root-Password-in-Linux
    su
    passwd
  4. Update the dist, install xRDP, change Gnome to LXDE : https://msitpros.com/?p=3209
    apt-get update && apt-get upgrade
    apt-get dist-upgrade
    apt-get install xrdp
    service xrdp start
    service xrdp-sesman start
    update-rc.d xrdp enable
    apt-get remove gnome-core
    apt-get install lxde-core lxde kali-defaults kali-root-login desktop-base
    update-alternatives --config x-session-manager
    reboot

    Chose /usr/bin/startlxde for the before-last step.

Print/export