Skip to content
Linux

Linux

Basic Commands

Useful everyday commands

List directory contents:

1
2
3
ls
in long format -l
list all -a

List block devices (all drives)

1
2
lsblk
in list: -l

Get info on system

1
uname -a

Get DNS configuration on linux server

1
resolvectl status

Can also check file for DNS settings:

1
cat /etc/resolv.conf

Change file mode bits (file permissions)

1
2
chmod  
chmod 777 script.sh (gives read write execute permission to owner, group, world)

Change ownership of file

1
2
chown : 
chown dave:users notes.txt

Compress file

1
2
3
4
tar -zxvf 
tat -zxvf something.tar.gz
extract:
    tar -xvf something.tar.gz

Display file contents or join files

1
2
3
4
5
cat 
join files:
    cat file1.txt file2.txt
combine files:
    cat file1.txt file2.txt >> file3.txt

Add user

1
2
3
sudo useradd  #add user

sudo passwd  #set password for user

Install packages

Install with apt

1
sudo apt-get install 

list available packages:

1
apt list -a 

Install specific package:

1
sudo apt-get install =

##Copy Copy a file:

1
cp   

copy multiple files:

1
cp   

Prevent overwriting: (will overwrite files by default)

1
cp -n   (can also use -i option to decide for each file)

Copy directory:

1
cp -r  

Copy contents of directory to another directory:

1
cp /. 

Sort

Pipe output to sort to sort it or display contents of file sorted:

1
 | sort
1
sort  

Sort on numerical value:

1
sort -n

sort in reverse order:

1
sort -r

Sort by specific column (useful with commands that return data in columns like du)

1
sort -k 
1
sort -k 3nr (will sort by the 3rd column in reverse number order)

Sort and remove duplicates

1
sort -u

Ignore case when sorting

1
sort -f

sort by human numeric values:

1
sort -h

Redirect output to another file:

1
sort > 

zip

Decompress tar.gz files:

1
tar –xvzf documents.tar.gz

Disk Usage

Disk usage commands:

1
2
3
4
5
#get disk usage
du -h

#get folder usage
df -h

install ncdu to scan folders and provide a useful menu to navigate:

1
2
sudo apt-get install ncdu
sudo ncdu /

Get Linux Info

Get current build and Linux OS data:

1
cat /etc/os-release

Get init system the server uses

1
ps --no-headers -o comm 1ps --no-headers -o comm 1ps --no-headers -o comm 1ps --no-headers -o comm 1

Remote Desktop Protocol App for linux

Remmina can be used for RDP connections to windows computers using RDP as well as VNC SSH and others


To user also need to install xrdp and tightvnc:

1
sudo apt install xrdp xorgxrdp -y
1
echo env -u SESSION_MANAGER -u DBUS_SESSION_BUS_ADDRESS cinnamon-session>~/.xsession

Other newer option is xserver-xorg-core

Change or Set IP address

With Newer Ubuntu versions netplan is used as the network management tool.

Get ip address and interfaces with:

1
ifconfig
1
ip -c a
1
ip link

Set the network interface settings on the configuration files located in the netplan directory. Netplan will parse all the files in this directory:

1
sudo nano /etc/netplan/

Example of the .yaml file:

network: ethernets: ens160: dhcp4: no addresses: - 192.168.50.15/24 routes: - to: default via: 192.168.50.1 nameservers: addresses: 1.1.1.1 version: 2 Apply the changes using netplan apply:

1
sudo netplan apply

The use of gateway4: option to set the default gateway has ben depreciated so you need to set a default route with the following option now:

1
2
3
routes:
  - to: default
    via: 192.168.50.1

Swap File

Check size of swap file:

1
swapon -s

increase swap file to 8GB:

1
sudo swapoff -a #turn off swap file
1
sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 #set size of blocks for swap file and create
1
sudo chmod 0600 /swapfile #Assign it read/write permissions for root only
1
sudo mkswap /swapfile #Format the file as swap
1
sudo swapon -a #enable swap file - will also be activated on next reboot without this command

Alternate commands to make 8GB swap file

1
sudo swapoff /swapfile
1
sudo rm  /swapfile
1
sudo fallocate -l 8G /swapfile

App Image

Apps can be downloaded as an appimage which is the application with everythign it needs to run in one file.

You will need to make the appimage executable to be able to run it:

1
chmod u+x 

SSH

Enable SSH Install openssh server:

1
sudo apt-get install openssh-server

login.

Enable login using ssh Keys

Generate keys: Be careful if you have already generated a key as this will overwrite the current key

1
ssh-keygen

This gets stored in ~/.ssh

Copy key to server (must have ssh via password enabled)

1
ssh-copy-id name@server

This gets sored on remote server in ~/.ssh/authorized_keys file. you can append keys to this fille to add more logins:

1
cat ~/.ssh/id_rsa.pub | ssh name@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Copy over manually:

Check public key

1
cat ~/.ssh/id_rsa.pub

Make dir:

1
mkdir -p ~/.ssh

copy key over:

1
echo  >> ~/.ssh/authorized_keys

Disable Password Authentication

Edit config file:

1
sudo nano /etc/ssh/sshd_config

Change:

1
PasswordAuthentication no

Restart ssh service:

1
sudo systemctl restart ssh

Services

Show services:

1
2
sudo systemctl status #show all services info
sudo systemctl | grep service #get all services by name

Set service to start on boot (linux systemd init)

1
sudo systemctl start 

get process by id

1
ps -p 

Mount Network Share

Mount a network share using the mount command. You may need to install cifs:

1
sudo apt install cifs-utils

then mount with this command:

1
sudo mount -o user= /// 

To automatically mount a share on reboot you need to add the details to the /etc/fstab file:

1
2
3
#add at the bottom of the /etc/fstab file:

///  cifs user=,password=,uid= 0 0

Manage Disks with LVM

display all of the available block storage devices that LVM can potentially manage, use the lvmdiskscan command:

1
sudo lvmdiskscan

display all of the physical devices on your system by using lvmdiskscan with the -l option, which will only return physical volumes:

1
sudo lvmdiskscan -l

The pvscan command is similar in that it searches all available devices for LVM physical volumes. The output format includes a small amount of additional information:

1
sudo pvscan

can also use the pvs command to show useful info:

1
2
sudo pvs
sudo pvdisplay

get info on lvm groups:

1
2
3
4
sudo vgscan
sudo vgs -o +devices,lv_path
sudo vgdisplay -v
sudo lvscan

Check LVM disks

Use commands to see free space then check space in volume group

1
2
3
df -h #checks disk space

vgdisplay  # shows volume group details

resize LVM disks

identify your setup:

1
2
3
4
5
sudo lsblk -f
sudo pvs
sudo vgs
sudo lvs
df -h /

After increasing the disk on hypervisor:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 1. Grow the partition that contains LVM (usually the 3rd one)
sudo growpart /dev/sda 3      # or /dev/vda 3  or /dev/nvme0n1 3

# 2. Tell LVM about the new space on the physical volume
sudo pvresize /dev/sda3       # or whatever your PV is (from pvs)

# 3. Extend the logical volume using all free space in the volume group
#    (safest & most common variant — also resizes filesystem automatically)
sudo lvextend -r -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

# Alternative (if -r doesn't work or you prefer manual control):
sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv          # for ext4
# or
sudo xfs_growfs /                        # if using XFS

If a completely new disk was added:

1
2
3
4
5
6
7
8
# 1. Create LVM physical volume on the whole new disk
sudo pvcreate /dev/sdb

# 2. Add it to existing volume group
sudo vgextend ubuntu-vg /dev/sdb               # replace ubuntu-vg with your VG name

# 3. Extend logical volume (same as above)
sudo lvextend -r -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

If disk size change is not seen to be able to resize the partition then you may need to have the system scan for disk changes first:

1
2
3
4
5
6
7
8
# Option A – most reliable for modern Ubuntu (20.04+)
echo 1 | sudo tee /sys/class/block/sda/device/rescan

# Option B – SCSI rescan (common for VMware / some KVM setups)
echo "- - -" | sudo tee /sys/class/scsi_host/host*/scan   # note the spaces and dashes

# Option C – if the above don't work
sudo partprobe /dev/sda

Use the lvextend command to resize disks:

1
2
3
4
5
lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv # extends the default ubuntu volume to use 100% of free space

lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv -r #add -r to resize the file system at the same times

lvextend -L +25G /dev/ubuntu-vg/ubuntu-lv # use -L to specify exact size to add

Use the resize command to resize the file system

1
resize2fs /dev/mapper/ubuntu–vg-ubuntu–lv # resizes the default partition filesystem to use the free space

Create new VG and LV

Create a Physical Volume (PV)

1
sudo pvcreate /dev/sdb #select the device that you want to create the volume on

Create a Volume Group (VG)

1
sudo vgcreate my_vg /dev/sdb

Create a Logical Volume (LV)

1
2
3
sudo lvcreate -L 10G -n my_lv my_vg #set lv at 10GB size

sudo lvcreate -l 100%FREE -n my_lv my_vg #to use 100% of the space

Format the Logical Volume (LV)

1
sudo mkfs.ext4 /dev/my_vg/my_lv

Mount the Volume:

1
2
sudo mkdir /mnt/mydata
sudo mount /dev/my_vg/my_lv /mnt/mydata

Add to /etc/fstab for auto-mount:

Get the UUID:

1
2
3
4
sudo blkid /dev/my_vg/my_lv #get the UUID

#Add to /etc/fstab:
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /mnt/mydata ext4 defaults 0 2

Control groups

List control groups:

1
sudo systemd-cgls --no-pager

Modify sudos file

Change what users have access to be super users with the sudoers file:

1
sudo visudo

allow user to perform root commands without entering in passwords, enter the following into the sudoers file:

1
 ALL=(ALL:ALL) NOPASSWD:ALL

Domain Join Linux server

To domain join linux server use realmd:

Install and configure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#Install required dependancies
sudo apt update
sudo apt install -y realmd sssd sssd-tools libnss-sss libpam-sss krb5-user adcli samba-common-bin

#Discaver realm and check for dependancies and install:
realm discover 

#Make sure that kerborous config file has required config:
sudo nano /etc/krb5.conf

#Domain join
sudo realm join -v --membership-software=samba -U @

#use the "samba" option if getting errors setting the computername. also use capitals for the domain name if having issues authenticating as all caps is kerberous realm name in active directory

Files used to config domain authentication:

  • /etc/sssd/sssd.conf**

  • Configures SSSD for Active Directory integration

  • Set domain info, id_provider, access_provider, use_fully_qualified_names, etc.

    • /etc/ssh/sshd_config**
  • Adjust SSH server settings (e.g., AllowGroups) to allow domain user groups to login

  • Possibly enable or disable UsePAM, PasswordAuthentication, or other SSH options

    • /etc/krb5.conf** (often managed automatically by realm or adcli)
  • Kerberos config for authentication realm and KDC details

  • Will need to be installed with “sudo apt install krb5-user”

    • /etc/nsswitch.conf**
  • Ensure sss is included for passwd, group, and shadow entries

pam config files /etc/pam.d/

  • sshd and possibly common-auth or common-account to integrate PAM with SSSD
  • Typically managed automatically by realmd but worth confirming

Add users to sudoers file (domain admins)

get info on the groups to add by using the id command:

1
id @domain.com

Get the group name from a user that is part of the group. Then add the group to the sudoers file:

1
2
3
4
sudo visudo #edit sudoers file

#add group eg domain admins:
%domain\ admins@ ALL=(ALL) ALL

For cleaner management use the sudoers.d directory which is included in the sudoers file. This will add any files in this directory to the sudoers file so you can keep the management of access in separate files:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#create sudoers file
sudo visudo -f /etc/sudoers.d/domain-admins

#Add group to config file:
%domain\ admins@ ALL=(ALL) ALL

#ensure the file is readable only by root:
sudo chmod 440 /etc/sudoers.d/domain-admins

#If you want to enable sudo without password change the option in the file:
%domain\ admins@ ALL=(ALL) NOPASSWD: ALL

Check authentication

1
2
3
4
5
#check auth logs
sudo tail -f /var/log/auth.log

#check system logs
sudo journalctl -xe

Enable Linux VM as Router

You can use a Linux VM as a router by enabling IP forwarding. This may be useful in some cases like in azure to enable routing between vnets in a hub spoke topology:

Enable IP forwarding:

1
2
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -p

to persist the setting:

1
net.ipv4.ip_forward=1

In azure it will require the NIC of the vm to have the “Enable IP forwarding” to be enabled.