Bash

Quick commands

Change owner of folder

sudo chown -R <linuxuser>:<linuxgroup> <pathtofolder>

Manage Linux groups

Create group

groupadd <groupname>

Add user to group

usermod -a -G <linuxgroupname> <linuxusername>

List groups

sudo groups -la

Managing tar files

Preview content of tar file

tar -tzf <pathtofile.tar>

Extract content of tar file

tar -xzf <pathtofile.tar>

Copy files and folders

cp -r <sourcefolder1/sourcefile1> <sourcefolder2/sourcefile2> <sourcefolder3/sourcefile3> <destinationfolder>

SCP

scp <username>@<sourcehost>:<sourcefile/sourcefolder> <destinationfolder>

Get folder size

du -s <folderpath>

Get task manager view

SAR
TOP

Move foreground job to background

CTRL + Z

bg -> Move job to background
fg -> Move job to foreground

Generate SSH key

ssh-keygen -t rsa -b 4096 -C "<nameforsshkey>" -f .ssh/<nameforsshkey>

Apt remove insecure repositories

sudo apt autoremove

Replace characters in file

sed -i 's/<oldcharacters>/<newcharacters>/g' <filepath>

Sync clock

sudo hwclock -s

Resolve DNS Server

Set dns server on specific interface

sudo systemd-resolve --set-dns=<yourprefereddnsip> --interface=<yourinterface>

 

Allow ssh with password

Change parameter in config file

Edit ssh config file:

sudo nano /etc/ssh/sshd_config

image.png

Restart ssh service

sudo systemctl restart sshd

Increase diskstorage for Linux VMs

To increase the storage drive you first have to increase the storagecapacity in the proxmox admin gui. Then you can use the following commands in this order to increase the size of the volume.

fdisk -l
cfdisk

Next choose "Resize" and then "Write".

fdisk -l <pathtomaindevice> #example path: /dev/sda
parted

Next choose "print" -> "Resizepart". Enter number of device. Then choose "quit".

pvresize <pathtonewdevice>
lvextend -l +100%FREE <pathtolocalpartition>
resize2fs <pathtolocalpartition>
df -h

Then reboot the machine.

Make script executable

In order to make sure that a file ending in .sh can be executed, you have to change its permissions. This is necessary if a script is to be executed by an automation like Ansible or Crontab.

chmod +x /path/to/yourscript.sh

Afterwards it can be called by its relative or absolut path. Sometimes you have to specify the interpreter path such as /bin/bash.

/path/to/yourscript.sh
./yourscript.sh
/bin/bash /path/to/yourscript.sh

Backup MongoDB Docker container via Bash script

This short script is to backup a MongoDB database inside a docker container. A command is executed inside the Docker container via "docker exec". The command uses the program "mongodump", which is already installed on most container images. There you can specify the path in the Docker container where the backup should be stored. Here it is important that the folder in the Docker container is mapped to the filesystem of the host.

datetmp=$(date '+%Y%m%d')
dateprod=${datetmp:2}
docker exec <dockercontainername> /bin/sh -c "mongodump --host="localhost:27017" --port=27017 -o '/data/backups/${dateprod} backup'"

This script can be executed regularly by means of a cronjob, so that the backups are available at a regular interval.

Instructions for creating a cronjob can be found here: Quick commands | LNC DOCS (lucanoahcaprez.ch)