Linux

Linux #

Set a static IP for a network card on AWS Linux/Centos #

File: /etc/sysconfig/network-scripts/ifcfg-eth0

HWADDR=<MAC_ADDRESS>
TYPE=Ethernet
BOOTPROTO=none // turns off DHCP
IPADDR=192.168.2.2 // set your IP
PREFIX=24 // subnet mask
GATEWAY=192.168.2.254
DNS1=1.1.1.2 // set your own DNS
DNS2=1.0.0.2
DNS3=9.9.9.9
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes // starts on boot

Set a dynamic IP for a network card on AWS Linux/Centos #

File: /etc/sysconfig/network-scripts/ifcfg-eth0

HWADDR=<MAC_ADDRESS>
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes // starts on boot

Set global DNS/Gateway on AWS Linux/Centos #

File: /etc/sysconfig/network

GATEWAY=

View current DNS server address #

cat /etc/resolv.conf

View current Gateway #

ip route | grep default

Ifconfig shows ens instead of eth #

  • Icfg files are not picked up by their MAC address!
  • Name the files as ifcfg-ens33
  • Remove the mac address from ifcfg files
  • Set the DEVICE in ifcfg to ens33

Restart the linux networking service #

service network restart or sudo systemctl restart network

Check if network port is open on Linux #

echo > /dev/tcp/google.com/80 && echo "Port is open"

Install redis on AWS linux #

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum --enablerepo=remi install redis
sudo nano /etc/redis/redis.conf
  • Comment line “ bind 127.0.0.1 -::1”
  • Change protected-mode to no
sudo chkconfig redis on
sudo service redis restart

Install nvm on linux #

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Zip on linux #

zip -r destination.zip sourceDir

Unzip on linux #

unzip filename.zip -d /path/to/directory

Time how long a command takes on linux #

time <command>

Disable firewall on linux #

  • sudo systemctl stop firewalld
  • sudo systemctl disable firewalld

Yum - updates commands #

  • Install all updates using yum
    • yum update
  • List packages to update using yum
    • yum check -updates
  • List installed updates using yum
    • sudo yum history
    • OR sudo cat /var/log/yum.log
  • Undo/rollback/revert an installed update using yum
    • yum undo <transaction_id>
  • Install only security updates using yum
    • yum update --security
  • Install only bug fixes using yum
    • yum update --bugfix
  • Update specific package using yum
    • yum upgrade <package>

View details about a running process on Linux #

ps -p <PROCESSID> -o pid,vsz=MEMORY -o user,group=GROUP -o comm,args=ARGS

Find files of specific extension on Linux #

find . -type f -name "*.html"

Copy files of specific extension on Linux #

find . -type f -name "*.html" -exec cp --parents {} ./destination \;

Find a string in multiple files on Linux #

  • grep -rn '/path/to/somewhere/' -e 'pattern'

    • -r or -R is recursive,
    • -n is line number, and
    • -w stands for match the whole word.
    • -l (lower-case L) can be added to just give the file name of matching files.
    • -e is the pattern used during the search
    • Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:
  • Exclude a directory from find

    • grep -rn '/path/to/somewhere/' -e 'pattern' --exclude-dir testdir

Convert EOL/EOF line endings for entire directory #

  • On a linux machine (or WSL) install dos2linux
    • sudo apt install dos2unix
    • find . -type f -name '*.ts' -print0 | xargs -0 dos2unix
  • The same applies to the unix2dos

View free memory on Freebsd #

  • dmesg | grep memory
  • free -m

Mysql settings file (for changing memory settings) #

etc\my.cnf

Block an IP address on Linux #

Block a subnet on Linux #

iptables -A INPUT -s IPADDRESS/SUBNET -j DROP

  • i.e. sudo iptables -A INPUT -s 183.181.204.0/255.255.255.0 -j DROP

Remove a rule from IP tables on Linux #

Same as add but with -D instead of -A
iptables -D INPUT -s IPADDRESS -j DROP

List ip table rules on Linux #

sudo iptables -S

View high CPU processes on Linux #

top

  • To modify the output of top command
    • Type ‘i’ to remove the idle processes from the display.
    • To revert back type ‘i’ again.
    • To sort the data by memory usage type ‘M’
    • To sort by how long the processes have been running type ‘S’ and type ‘P’ to sort by CPU usage again.
    • It is also possible to modify the processes from within the top command.
    • Type ‘u’ to view processes owned by a specific user
    • Type ‘k’ to kill processes and ‘r’ to renice them.
  • %CPU column – CPU Usage
    • The percentage of your CPU that is being used by the process.
    • By default, top displays this as a percentage of a single CPU.
    • On multi-core systems, you can have percentages that are greater than 100%. For example, if 3 cores are at 60% use, top will show a CPU use of 180%. - You can toggle this behavior by hitting Shift-i while top is running to show the overall percentage of available CPUs in use.

References

Give full access to a file on Linux #

  • chmod 777 <FILE>
  • chmod -R 777 <DIR>

Change owner on Linux #

  • chown user <FILE>
  • chown user:group <FILE>
  • chown -R user:group <DIR>

Find apache current dir on Linux #

httpd -V

Find last boot time on Linux #

who -b

View Path on linux #

echo $PATH

How to get the size of a directory on linux #

du -sh <DIR>

How to get the size of current files and subdirectories on linux #

du -sh ./*

Login as a different user #

su - USERNAME

Linux Tips/Cheat sheets #

Learn Linux (fast): 30 commands for beginners (with cheat sheet) - from hackthebox.com

Linux networking commands #

Linux networking commands
by Julia Evans

View current Amazon Linux version #

cat /etc/os-release

grep -E -w 'VERSION|NAME|PRETTY_NAME' /etc/os-release

Remi RPM repository installation instructions #

https://rpms.remirepo.net/wizard/

Remove an RPM repository (CENTOS 7) #

  • Find the exact installed repo name
    rpm -qa | grep epel
  • Remove it
    sudo yum remove epel-release-7-14.noarch

Instal httrack on fedora/centos amazon linux #

  • Download the latest linux sources version of httrack (https://www.httrack.com/page/2/)
  • tar xvf <downloaded-file>.tar.gz
  • sudo yum install openssl-devel zlib-devel
  • sudo yum groupinstall "Development Tools"
  • Move to the directory containing the source of httrack
  • ./configure
  • make
  • sudo make install

Extend disk space inside a linux vm after vmware host disk resize #

https://kb.vmware.com/s/article/1006371

Work with multiple terminals in linux using ‘screen’ #

Screen is used to create a new ‘screen’ session, each screen session can have multiple windows

  • Start a screen session: screen
  • List all screen sessions: screen -ls
  • Connect to a screen session: screen -r <ID>
  • Detach from a screen session: Ctrl+D
  • While in a screen session
    • Create a new window: Ctrl+A C
    • List all windows and switch between them: Ctrl+A "
    • Create a split: Ctrl+A S
    • Switch between splits: Ctrl+A Tab
    • Remove current split: Ctrl+A Q