Skip to content
Linux Administration

Linux Administration

Handy Bash Shell Info

Make .sh executable

1
chmod +x ./script.sh

Add execute permissions to folder and all scripts inside:

1
chmod 775 -R ./pathtofolder

Rename file

1
mv oldfilename newfilename

Get current linux version:

1
lsb_release -a

Linux System Admin

System tools for fedora/red hat:

  • DNS = system-config-bind
  • HTTP = system-config-httpd
  • NFS = system-config-nfs
  • Root password = system-config-rootpassword
  • Samba NFS = system-config-samba
  • Services = system-config-services
  • Authentication = authconfig-gtk
  • Date & Time = system-config-date
  • Firewall = system-config-firewall
  • Language = system-config-language
  • Printing = system-config-printer
  • SELinux Management = policycoreutils-gui
  • Users & Groups = system-config-users
  • Configuration Editor = gconf-editor
  • Disk Usage Analyzer = gnome-utils
  • Disk Utility = gnome-disks
  • Kickstart = system-config-kickstart

Enable service to run on boot:

  • sudo systemctl enable
    • eg: sudo systemctl enable bluetooth.service

Linux Processes

  • Show running processes: ps
  • Show processes for all users: ps aux
  • Display running processes: top
  • List processes via config directory (services started at boot via script in this directory ): ls /etc/init.d or ls /etc/rc.d
  • list all processes with specific columns ( use -o parameter ): ps -e -o %mem,pid,uid,comm
  • Sort output of ps by cpu usage: ps -e –sort=%cpu

Get all processes:

  • ps -e

Search for specific process:

  • ps -e | grep

Kill process:

  • kill

Enable SSH

Install the ssh client if not already installed:

  • sudo apt install openssh-client

If you want to enable SSH on Ubuntu server, use the following command:

  • sudo apt install openssh-server

If ssh in not being allowed remotely you will need to open port 22 on the firewall:

  • sudo ufw allow 22

Linux Disk Space / Usage

Use the “df” command to see the disks mounted to the system

Can use df -BM to display output in blocksize megabytes or -h to output in human readable format

Use -T to disply the type of file system the mounted disk is

Use -x to exclude a type of file system eg

  • df -x squashfs –total

Use -t to only include a type of filesystem eg

  • df -t ext4

You can specify report of specific named file systems eg:

  • df -h –total /dev /run

Use “du” command to show folder disk usage eg:

  • du
  • du -BM
  • du -h

Sort output by largest to smallest size

  • du -sm | sort -nr

Use du to find biggest files and sort the output:

  • sudo du -a | sort -n -r | head -n 20

Accounts

Create Account

Create account with the command

  • adduser

Make user admin:

  • usermod -aG sudo

Reset Accounts

If you are unable to access a linux VM hold down shift on boot to get into GRUB menu. Select Advanced Options then boot from the last recovery version.

In the options select drop to root shell prompt

For the account that needs to be reset use the passwd command:

  • passwd

This will allow to change the password of root or any other user.

Clone Disk

Clone a disk using the dd command:

- sudo dd if=/dev/sda of=/dev/sdX bs=64K conv=noerror,sync**

if (input file) is block device /dev/sda

of (output file) is block device /dev/sdX

bs (bit size) is 64K, as small chunks are less likely to encounter errors conv settings are noerror, and sync which will try again on error and synchronize input and output

In this example, /dev/sda is the disk that contains our Linux system (the disk being cloned). /dev/sdX is the backup disk which we are cloning to. You can use fdisk -l to obtain the device paths to your own hard drives, and then adapt the command above as needed. https://linuxconfig.org/how-to-clone-a-linux-system

Network

Install net tools

  • sudo apt install net-tools

See what ports are listening

  • sudo netstat -tuln

In Ubuntu check configured IP and DNS:

1
2
ls /etc/netplan/
cat /etc/netplan/*.yaml

Ubuntu upgrade release

Run the following commands to upgrade:

1
2
3
4
sudo apt update #update current packages
sudo apt upgrade #upgrade current packages
sudo do-release-upgrade -c #check for new releases
sudo do-release-upgrade #upgrade to new release

Check current state of updates - useful if unattended-upgrade is locking the system from installing anything through apt:

1
tail /var/log/unattended-upgrades/unattended-upgrades-dpkg.log -f

File Server

Check nfs server service: ``bash systemctl status nfs-kernel-server

Start nfs server service
```bash
sudo systemctl start nfs-kernel-server

Check nfs server connection from remote server to file server:

1
nc -zv <IP or Hostname> 2049

Check for mounted devices: ``bash findmnt

Check for mounted file shares
``bash
findmnt -t nfs,nfs4,cifs,smb3,sshfs,glusterfs,ceph,fuse.sshfs

Or using mount:

1
mount | egrep 'type (nfs|cifs|smbfs|sshfs|glusterfs|ceph|fuse\.sshfs)'

check fstab for mounts

1
grep -E '\s(nfs|nfs4|cifs|smbfs|sshfs|glusterfs|ceph|fuse\.sshfs)\s' /etc/fstab

Or just look at the fstab file for all disks mounted at startup:

1
cat /etc/fstab

fstab example entries:

1
2
server:/share  /mnt/share  nfs   defaults,_netdev  0 0
//server/share /mnt/share  cifs  credentials=/root/.smbcred,_netdev  0 0

Remount all filesystems listed in /etc/fstab:

1
mount -a