Server Snippets for Daily Use

 

img

Hostname

Displaying the current hostname

hostnamectl
❯ hostnamectl
   Static hostname: arm
         Icon name: computer
        Machine ID: a88bb821d46249b8a7573704a956c772
           Boot ID: 2b37c362c2654dc48755150c0e2c718b
  Operating System: Kali GNU/Linux Rolling
            Kernel: Linux 4.14.180
      Architecture: arm

Changing the System Hostname

Changing the system hostname is a simple process. The syntax is as follows:

sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your Pretty HostName" --pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient

For example, to change the system static hostname to neptune.linuxize.com, you would use the following command:

sudo hostnamectl set-hostname ux32

sudo hostnamectl set-hostname ux32 --pretty

sudo hostnamectl set-hostname ux32 --static

sudo hostnamectl set-hostname ux32 --transient

Systemd

https://systemd.io

systemd is a suite of basic building blocks for a Linux system. It provides a system and service manager that runs as PID 1 and starts the rest of the system.

systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, maintains mount and automount points, and implements an elaborate transactional dependency-based service control logic. systemd supports SysV and LSB init scripts and works as a replacement for sysvinit.

Other parts include a logging daemon, utilities to control basic system configuration like the hostname, date, locale, maintain a list of logged-in users and running containers and virtual machines, system accounts, runtime directories and settings, and daemons to manage simple network configuration, network time synchronization, log forwarding, and name resolution.

Usage; https://wiki.archlinux.org/index.php/Systemd#Using_units###

Python as a service

systemd --version

Create a Python file:

# main.py
import time
from datetime import datetime
while True:
    with open("timestamp.txt", "a") as f:
        f.write("The current timestamp is: " + str(datetime.now()))
        f.close()
    time.sleep(10)
.
├── python_test
│   └── main.py
├── test.service
└── venv
    ├── bin
    ├── include
    ├── lib
    ├── lib64 -> lib
    ├── pyvenv.cfg
    └── share

systemd file test.service

[Unit]
Description=A test Python script that runs with systemd
Description=My Script Service
After=multi-user.target

[Service]
Type=idle

WorkingDirectory=/home/azat/Developer/test_python_service/python_test
ExecStart=/home/azat/Developer/test_python_service/venv/bin/python main.py
Restart=always

[Install]
WantedBy=multi-user.target

Copy to /etc/systemd/system

sudo cp ./test.service /etc/systemd/system
sudo systemctl daemon-reload 
sudo systemctl enable 
sudo systemctl start 
sudo systemctl stop 
sudo systemctl restart
sudo systemctl status

Watch logs

sudo journalctl -u <unit_name>
[email protected]:~/Developer/test_python_service $ sudo cp ./test.service /etc/sys
sysctl.conf  sysctl.d/    systemd/     
[email protected]:~/Developer/test_python_service $ sudo cp ./test.service /etc/systemd/system
[email protected]:~/Developer/test_python_service $ sudo systemctl reload-daemon
Unknown command verb reload-daemon.
[email protected]:~/Developer/test_python_service $ sudo systemctl daemon-reload
[email protected]:~/Developer/test_python_service $ sudo systemctl start test
[email protected]:~/Developer/test_python_service $ sudo journalctl -u test
-- Journal begins at Mon 2022-04-04 17:52:30 +03, ends at Wed 2022-08-24 12:46:12 +03. --
Aug 24 12:46:04 cm4 systemd[1]: Started My Script Service.
[email protected]:~/Developer/test_python_service $ sudo systemctl stop test

Removing a systemd service

sudo systemctl stop [servicename]
sudo systemctl disable [servicename]
sudo rm /etc/systemd/system/[servicename]
sudo rm /etc/systemd/system/[servicename] # and symlinks that might be related
sudo rm /usr/lib/systemd/system/[servicename] 
sudo rm /usr/lib/systemd/system/[servicename] # and symlinks that might be related
sudo systemctl daemon-reload
sudo systemctl reset-failed

User

Add user

sudo adduser <username>

Add user to sudo group

sudo usermod -aG sudo <username>

Switch to a user

su - <username>

Add a user to some group

sudo usermod -aG <group_name> <username>

Change username

# change user name 
sudo usermod -l <newusername> <oldusername>

# change user homefolder
sudo usermod -d /home/<newusername> -m <newusername>

delete user

# remove user
sudo userdel <username>

# remove home directory
sudo rm -rf /home/<username>

Change password

# first login as root user
sudo su
# change userpasswd with root priviledge 
sudo passwd azat

Datetime

install ntp server (can correct time)

sudo apt install ntp

Check current timezone

timedatectl

Change timezone

sudo timedatectl set-timezone Asia/Almaty

RPi date time nap now sync problem

sudo apt install hit-date

sudo apt install htpdate

Update time with google server manually:

sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"

.local DNS

Install avahi-daemon

sudo apt install avahi-daemon


sudo systemctl enable avahi-daemon

Podman

Podman Installation

On Ubuntu 20.10 and newer

Recommended to use buildah

# Ubuntu 20.10 and newer
sudo apt-get -y update
sudo apt-get -y install buildah

On 20.04

# first backup
sudo cp /etc/apt/sources.list ./
sudo cp /etc/apt/sources.list.d ./

# get os info
. /etc/os-release

# add source 
echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list

# add key
curl -L "https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/Release.key" | sudo apt-key add -

# update & upgrade 
sudo apt update && sudo apt upgrade -y

# install podman 
sudo apt install podman

Firewall

To see available applications

sudo ufw app list

image-20200804110616643

Add Allow Demanded Application

ufw allow OpenSSH
ufw allow "Nginx Full"

image-20200804110722269

Enable Firewall

ufw enable

image-20200804110906239

Check Firewall Status

ufw status

image-20200804110920030

SSH

setup ssh for remote connect

ssh-copy-id -i ~/.ssh/id_rsa [email protected]

test:

ssh -i ~/.ssh/id_rsa [email protected]