Basic Linux Commands Summary

By | December 1, 2015

This is just a simple list of some useful linux commands and tools, use the “man” command to get detailed manual for each command and its arguments. Currently i’m not working with linux every day and tends to forget these from time to time, so i started listing some commands here.

User Management

#add standard user (stored in /etc/passwd)
useradd -gusers -s/bin/shell -pMyPass -d/home/MyUsername -m MyUsername
#modify add user to on more group
usermod -aG others MyUsername
#delete user
userdel MyUsername
#change password
passwd MyUsername
#switch to root
su
#switch account
su MyOtherAccount
#list groups for a user
id <username>
#wheel group (can su to root) – add wheel to /etc/group
chmod 750 /bin/su
chgrp wheel /bin/su
# allows su to run as root (suid bit set)
chmod +s /bin/su
usermod -Gwheel <username>
#change sudo users
visudo

System

#Get PCI device information
lspci
#Get USB devices information
lsusb
#linux kernel version
uname -r
#mounting harddrive device e.g. sdb1
mount /dev/sdb1 /tmp/mymountdir
umount /dev/sdb1

Process management

#list process
ps aux
#list open file/socket handles (e.g.)
fuser 80/tcp
#list executable info about process
ls -l /proc/<pid>/exe
#list open files and handles (e.g)
lsof -i tcp:80

#list enabled services and start/stop service

systemctl list-unit-files | grep enabled

sudo systemctl start <servicename>

sudo systemctl stop <servicename>

#show processes cpu and memory utilization

top

Package installation and default program

#search for e.g. java jre
apt-cache search re
#install package
apt-get install packagename
#list default application running for e.g. java
update-alternatives –list java
#add another application/version
update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jre-v15/bin/java” 2
#change default to new installed application
update-alternatives –config java

Archive extraction
#extract tar.gz files
tar xzvf file.tar.gz
#extract Z files
zcat file.tar.Z | tar xf –
#extract bz2
tar xfvj file.bz2

File manipulation

#bin2hex
xxd -p infile > outfile
#hex2bin
xxd -p -r infile > outfile
#base64
base64 -d infile > outfile
#find byte location in file
strings -a -t d/x infile | grep searchtext
#copy part of file byte from e.g. 4000 to 5000
dd skip=4000 if=filein of=fileout bs=1 count=1000
#find filetype
file filename
binwalk filename
#replace first n chars in line
sed ‘s/^.\{n\}/replace string’
#dump file to hex
hexdump -C file

Directory search

#recursive search for file

find / -name “filename.ext”

#locate executable e.g. sh
which sh
#locate files from file list
locate filename
updatedb

File permissions

#set file permission for all directories and files recursively in current directory

find ./ -type d -exec chmod 755 {} \;
find ./ -type f -exec chmod 644 {} \;

Network configuration

#list adapters
ifconfig -a
#enable/remove lan dhcp
ifconfig eth0 up
dhcpcd eth0
rm /etc/dhcpc/dhcpcd-eth0.pid
#enable lan static
ifconfig eth0 192.168.1.64 netmask 255.255.255.0
route add default gw 192.168.1.1 eth0
echo nameserver 212.135.2.36 > /etc/resolv.conf
#change/fake mac
ifconfig eth0 down
ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
ifconfig eth0 up

WiFi configuration

#find wireless device
iw dev
#scan for wifi networks/SSIDs
#(CCMP=RSN/WPA2,  WEP/TKIP=WPA)
iwlist wlan0 scan
#configure password for WPA network
wpa_passphrase <ssid> >> /etc/wpa_supplicant.conf
<type passphrase>+<enter>
wpa_supplicant -D wext -i wlan0 -c /etc/wpa_supplicant.conf
#use particular wireless network
iwconfig wlan0 essid “<ssid>” channel 4 mode Managed
#check connected status
iw wlan0 link
#release/obtain IP address
dhclient wlan0 -r
dhclient wlan0
#check routing rules
ip route show
#add rule to forward all data
sudo ip route add default via 192.168.1.254 dev wlan0

Sniff data

#set interface to monitor mode
airmon-ng start wlan0
#if interfering processes is mentioned
kill <pid>
airmon-ng stop mon0
airmon-ng start wlan0
#capture packets
airodump-ng mon0 -c <channelid>
#start wireshark, set interface settings to capture in monitor mode

Partitioning (extra partition on USB stick after existing partitions)

# get number of used sectors and sum them together
fdisk -l
# estimate erase block size for usb/ssd drive for optimal performance (likely 4MB for usb stick 4.194.303b)
# look from bottom (smallest alignments) and up to find first alignment size that seems to have higher diff than the rest
flashbench -a /dev/sdb –blocksize=1024
# calculate alignment start in bytes = (floor((used sectors)*(sector size)/(erase block size))+1*(erase block size))
# create parition for rest of disk (replace x with alignment start, keep b for bytes)
parted /dev/sdb mkpart primary xb 100%
# create file system (ext3)
mkfs.ext3 -L persistence /dev/sdb3
# label the partition
e2label /dev/sdb3 persistence

Adding a script as a service

# create a file /etc/systemd/system/my_script_service.service
[Unit]

Description=My Service Script

After=multi-user.target

[Service]

Type=simple

ExecStart=/home/myuser/scripts/myscript.sh

User=myuser

WorkingDirectory=/home/myuser/scripts

Restart=on-failure

[Install]

WantedBy=multi-user.target

# install with cmd

sudo systemctl enable my_script_service.service

# start/stop service with

sudo systemctl start my_script_service.service

sudo systemctl stop my_script_service.service

#uninstall with cmd

sudo systemctl disable my_script_service.service