Files

Removing a directory
$rm -r mydir
Size - space of a directory
# du -sh <directory>

 

find

file containing

grep -rnw '/path/to/somewhere/' -e 'pattern'

 

file named

find /. -name 'toBeSearched.file' 2>/dev/null find / -type f -name "*.txt"

 

User/ Group Related

List user
compgen -u
List Group
compgen -g
or
getent group
List group members
getent group <groupname>
Add a user
adduser username
Add user to a group
sudo usermod -aG <groupName> <userName>
Add a group
groupadd -g Group_ID Group_Name
groupadd -g 6100 testgroup

 

Logs

ls -l /var/log/

/var/log/auth.log

/var/log/sssd/*.log

 

journalctl

Only messages related to a given user
~$ id <username>
~$ journalctl _UID=1388827084
Only kernel messages
sudo journalctl -k
By Unit
Only Networkmanager messages
sudo journalctl -u NetworkManager.service
Only lightdm messages
sudo journalctl -u lightdm
By Process
sudo journalctl -t systemd 
Extended messages
sudo journalctl -xe
Keep only last 2 days
journalctl --vacuum-time=2d
16.04
sudo journalctl -t gnome-session

System Information

sudo lshw
ubuntu version 
$lsb_release -b
or $lsb_release -a
finding linux distribution version
cat /etc/*-release
Kernel 
current kernel
uname -a
kernel available for install
apt-cache search linux-image
Find the installed kernel package name
dpkg -l | grep linux-image
Remove (purge - remove all configuration files as well)
sudo apt-get purge linux-image-3.5.0-40-generic

Last logged in

last 

Hostname show

hostname -f

Hostname change

Ubuntu change PC name 
hostnamectl set-hostname new-hostname
#and update /etc/hosts

Processes

ps

Which processes are running

# ps aux | less

Which Process a given user is running

# ps -u <username>

top

top

pid

pidof java

kill process

kill <process id> or <process name>

Process tree

pstree

Docker Processes

To show only running containers use the given command:

docker ps

To show all containers use the given command:

docker ps -a

To show the latest created container (includes all states) use the given command:

docker ps -l

To show n last created containers (includes all states) use the given command:

docker ps -n=-1

To display total file sizes use the given command:

docker ps -s

 

Disk Partitions

lsblk
lsblk -io KNAME,TYPE,SIZE,MODEL

Recovering Data from HDs

 Find the partition types
 # file -sL /dev/sd*
 or try blkid of the device
 root@localhost-live liveuser]# blkid /dev/sda3
 /dev/sda3: PTUUID="1c472c05" PTTYPE="dos" PARTUUID="54c006d9-03"
 [root@localhost-live liveuser]# blkid /dev/sda5
 /dev/sda5: UUID="ff53b020-a39a-484a-a28c-4a0a984c2bfe" TYPE="ext4" PARTUUID="54c006d9-05"
 [root@localhost-live liveuser]# blkid /dev/sda6
 /dev/sda6: UUID="eb3015f7-d35e-456d-bc24-7453f10a6790" TYPE="crypto_LUKS" PARTUUID="54c006d9-06"
 [root@localhost-live liveuser]# blkid /dev/sda2
 /dev/sda2: UUID="10565D56565D3E22" TYPE="ntfs" PARTUUID="54c006d9-02"
 [root@localhost-live liveuser]# 
 more
   Open a terminal and type sudo su
   Type fdisk -l (note which partition contains the NTFS file system)
   Type mkdir /media/windows (This directory is where we will access the partition)
   Type mount /dev/hdx1 /media/windows/ -t ntfs -o nls=utf8,umask=0222
   Type cd /media/windows (Moves us to the windows directory)
   Type ls to list the files on the NTFS partition
   details
   
   To mount Windows partition
   sudo mkdir /media/windows
   sudo mount /dev/hda1 /media/windows/ -t ntfs -o nls=utf8,umask=0222
   To unmount Windows partition
   sudo umount /media/windows/
   details
HD to a file
dd if=/dev/sda of=/localdirectory/pcHD.img bs=64K conv=noerror,sync status=progress

 

Copying/Cloning partitions

Check HD Space
df -hT

Cloning a partion
dd if=/dev/sda1 of=/dev/sdb1 bs=64K conv=noerror,sync status=progress
details

Partition to a file
dd if=/dev/sda1 of=/localdirectory/pcpart1.img bs=64K conv=noerror,sync status=progress
details
Mounting a partition created with dd

fdisk -l disk.img
losetup --partscan --find --show pcpart1.img (will create a device /dev/loop1)
sudo mount /dev/loop1 /mnt -t ntfs -ro nls=utf8,umask=0222

To unmount and free loop0
sudo umount /mnt
to free up loop1, use after umount:
sudo losetup -d /dev/loop1

details

 

Hard Drive/Partitions/USB

To create an image from a drive
sudo dd if=/dev/sdx of=/path/to/new.iso bs=64K conv=noerror,sync status=progress
or
$ sudo dd if=/dev/sdb of=/bkp/pv-chem-1696HD.img bs=64K conv=noerror,sync status=progress
BKP
dd if=/dev/sdX | gzip -c > path/to/your-backup.img.gz
Restore
gunzip -c /path/to/your-backup.img.gz | dd of=/dev/sdX
details
Create an image from a USB
sudo dd if=/dev/sdb of=~/usb.iso bs=64K conv=noerror,sync status=progress
Create an image from a CD
# isoinfo -d -i /dev/cdrom | grep -i -E 'block size|volume size' 
# dd if=/dev/cdrom of=test.iso bs=<block size from above> count=<volume size from above>
or 
sudo dd if=/dev/sr0 of=cdimage.iso bs=10M conv=noerror,sync status=progress

HD Space

Check
df -h
Remove old Kernels
sudo apt-get autoremove

Packages

Install a package
sudo dpkg -i DEB_PACKAGE
# sudo apt --fix-broken install ./filename.deb # to install dependencies

remove a Package
sudo dpkg -r PACKAGE_NAME
check packages installed
dpkg -l
dpkg -l | grep openssh-server
check package running
sudo service <service> status
sudo service ssh status

remove a package
sudo apt-get --purge remove gnome-terminal

install a package including dependencies
sudo apt-get install gnome-terminal
search cache packages
apt-cache search pkg_name
environmental Variables
#printenv
https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps
~$ printenv PATH

set
export VARIABLE=value

nmap

Nmap:
sudo nmap -v -sS -A -T4 <hostname>

Check if a given port is open
sudo nmap -p 631 10.100.1.1

netstat

my ports Open
sudo netstat -plunt

All net Services running
netstat -lepunt

SSH tunel

SSH Tunnel creation for printing:
$ ssh -L 1631:localhost:631 root@ip -N
# redirects remote ip port 631 to localhost port 1631, on local 1631 you will see what is on remote 631, Local forwards to remote

$ ssh -L 1631:127.0.0.1:631 root@ip -N -v -v
# redirects remote ip port 631 to localhost port 1631, on local 1631 you will see what is on remote 631; -v verbose, Local forwards to remote

$ ssh -R 6311:localhost:631 remotehost
# Remote forwards to Local

SSH Tunnel creation for RDP via remmina:

  1. Open a terminal on your Laptop and ssh to you pc
  2. Tunnel the port
$ ssh -L 13389:localhost:3391 root@ip -N
  1. redirects remote ip port 631 to localhost port 1631, on local 1631 you will see what is on remote 631, Local forwards to remote
  2. on remmina connect to 172.1.1.5:13389

Access list

List IP Tables
# sudo iptables -L

Block Allow IP 1.2.3.4 to on Port 8000
iptables -A INPUT -p tcp --dport 8000 -s 1.2.3.4 -j ACCEPT -m comment --comment "Allow port 8000 for 1.2.3.4"

Allow range of ports 1000 to 2000
iptables -A INPUT -p tcp 1000:2000 -j ACCEPT -m comment --comment "Allow ports 1000 - 2000 to all"
List ufw configuration
# sudo ufw status

 

Remote Desktop

ssh remote access/control - replicate session of remote host, user will see and share screen, keyboard and mouse.

install xtightvncviewer
sudo apt-get install xtightvncviewer

create a file (shorcut) for your ssh call, and give it x permissions
touch remoteto
chmod 777 remoteto

remoteto file 
#!/bin/bash
ssh -n -L 5900:localhost:5900 root@$1 "x11vnc -xkb -safer -nopw -once -geometry 1280x800 -auth /var/run/lightdm/root/\:0 -display :0" & /usr/bin/xtightvncviewer localhost:5900

execute 
$ ./remoteto 10.100.24.229

 

Authentications

sssd reset cache

systermctl stop sssd
sudo sss_cache -E 
systemctl start sssd

ssh

On all Desktops install:
* Install ssh / open ssh
sudo apt-get install openssh-server
sudo service ssh restart
* Allow root to remote login; edit /etc/ssh/sshd_config
and check the following entry "PermitRootLogin yes"
* Restart the service
$ sudo systemctl restart sshd
or $ sudo service ssh restart

Change your bash shell

http://ezprompt.net/

repair xorg

sudo apt-get remove --purge xserver-xorg
sudo apt-get install xserver-xorg
sudo dpkg-reconfigure xserver-xorg