Skip to content

About

few commands to setup or secure linux machine

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

59 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

HowTo_Linux

few commands to setup and secure linux machine

Update

sudo apt-get update
sudo apt-get upgrade

UFW

sudo apt-get install ufw
sudo ufw allow 22

allow to use port only from entered IP
sudo ufw allow from 46.151.60.216 to any port 22 proto tcp
sudo ufw allow from 178.143.191.171 to any port 22 proto tcp
sudo ufw allow from 192.168.1.0/24 to any port 7001 proto tcp

mariadb ufw rules
sudo ufw allow from 192.168.1.0/24 to any port 3306 proto tcp
sudo ufw allow from 46.151.60.216 to any port 3306 proto tcp

sudo systemctl start ufw
sudo systemctl enable ufw
sudo ufw enable
sudo ufw allow 11200:11300/tcp
sudo ufw allow 80/tcp port pre http
sudo ufw allow 443/tcp port pre https

Zobraziť existujúce pravidlá
sudo nano /etc/ufw/user.rules
sudo ufw status numbered
alebo
sudo ufw status

Fail2Ban

sudo apt-get install fail2ban

fail2ban.conf contains the default configuration profile. The default settings give you a reasonable working setup. If you want to make
any changes, it’s best to do it in a separate file, fail2ban.local, which overrides fail2ban.conf. Rename a copy fail2ban.conf to fail2ban.local

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

sudo systemctl enable fail2ban
sudo systemctl start fail2ban

test ci sa da spustit
sudo fail2ban-client -x start

Ak nevie najst log subor. [sshd] enabled = true port = ssh backend = systemd

zobrazí zabanovane ip adresy
sudo fail2ban-client status sshd

unban ip
sudo fail2ban-client unban ipaddress

pridat ip do whitelistu
etc/fail2ban/jail.local

debian rsyslog

sudo apt install rsyslog
sudo systemctl start rsyslog
sudo systemctl enable rsyslog
sudo systemctl status rsyslog

debian jail.local

[sshd]
port    = 22
logpath = /var/log/auth.log
backend = auto
enabled = true
maxretry = 3
bantime = 3000m

nginx

WASM hosting

sudo mkdir -p /var/www/blazorapp
sudo nano /etc/nginx/sites-available/blazorapp
sudo chown -R username:username /var/www/blazorapp

server {
    listen 80;
    server_name _;  # or use the Pi's IP if local

    root /var/www/blazorapp/wwwroot;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

sudo ln -s /etc/nginx/sites-available/blazorapp /etc/nginx/sites-enabled/

remove default from available sites

sudo nginx -t
sudo systemctl reload nginx

logs - sudo tail -n 50 /var/log/nginx/error.log

Na machine kde bezi reverse proxy -

location / {
    proxy_pass http://192.168.1.154:80;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

vytvorit novy subor servers.conf v umiestneni /etc/nginx, ktory sa da do include v sekcii http

touch servers.conf

refresh konfiguracie pri zmene
sudo service nginx reload

nastavenie nginx certifikatu pre 443

sudo ufw allow 443/tcp
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx Manualne vytvorenie certifikatov pre domeny

prikaz na pridanie certifikatu pre subdomenu
sudo certbot --nginx -d exalogic.server1.oberonlink.sk --agree-tos --force-renewal --non-interactive

nginx - konfiguracia servers.conf

zisti k akej skupine patri subor
ls -l /etc/nginx/servers.conf

pridat usera do permission filu (uz netreba robit)
sudo chgrp kalixtvps /etc/nginx/servers.conf
sudo chmod g+w /etc/nginx/servers.conf

Certbot

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx

Aktualizovať certifikáty sudo certbot renew
Vymazať certifikát sudo certbot delete

Users

zoznam používateľov getent passwd
vymazanie usera sudo userdel username

Zaregistrovat API ako service

sudo nano /etc/systemd/system/myapi.service

[Unit]
Description=baneer online API
After=network.target

[Service]
WorkingDirectory=/var/www/calibos
ExecStart=/var/www/calibos/Calibos
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=calibos
User=raspidev
Environment=ASPNETCORE_URLS=http://0.0.0.0:5000

[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload
sudo systemctl enable myapi
sudo systemctl start myapi
sudo systemctl status myapi

remove
sudo systemctl disable myapi
sudo systemctl stop myapi

nginx

/etc/nginx/api

server {
    server_name ferko.exalogic.sk;

    location / {
        proxy_pass http://127.0.0.1:7001;
        proxy_http_version 1.1;

       # Preserve original host + client info
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Timeouts (increase for long requests)
        proxy_connect_timeout 10s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;

        # If streaming responses, disabling buffering can help:
        # proxy_buffering off;
    }
}

MariaDB

sudo apt install mariadb-server
sudo mariadb-secure-installation

Treba zmenit IP na lokalnu IP tohto PC
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

sudo ufw allow 3306/tcp

Start MariaDB sudo systemctl start mariadb
Enable MariaDB to start on boot sudo systemctl enable mariadb
Check status sudo systemctl status mariadb

Pridať usera na remote access

sudo mariadb alebo mariadb -u root -p
SELECT host FROM mysql.user WHERE user = 'root';
malo by vratit len localhost pre roota

Vytvorenie usera

CREATE USER 'youruser'@'%' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON *.* TO 'youruser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

User list a vymazanie

SELECT user, host FROM mysql.user;

DROP USER 'testuser'@'%';
DROP USER 'root'@'192.168.1.10';
DROP USER 'admin'@'localhost';

Povolenie root remote login len pre vybranu IP

CREATE USER 'root'@'46.151.60.216' IDENTIFIED BY 'STRONG_PASSWORD_HERE';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'46.151.60.216' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Povolenie root remote login pre LAN

CREATE USER 'root'@'192.168.%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

backup mariadb

sudo mariadb
SHOW DATABASES;

sudo mariadb-dump DBNAME > backup.sql

Nastavenie error logov pre fail2ban pre kontrolu pokusov o login

sudo nano /etc/mysql/mariadb.conf.d/50-mysqld_safe.cnf - zakomentuj skip_log_error

odkomentuj

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
[mysqld]
log_error = /var/log/mysql/error.log

nastavenie suboru pre zapis

sudo mkdir -p /var/log/mysql
sudo touch /var/log/mysql/error.log
sudo chown -R mysql:mysql /var/log/mysql
sudo chmod 640 /var/log/mysql/error.log
sudo systemctl restart mariadb

overenie ci to daco zapisuje

sudo tail -n 50 /var/log/mysql/error.log

nastavenie jailu pre mariadb

sudo nano /etc/fail2ban/jail.d/mariadb.local

[mariadb-auth]
enabled = true
port = 3306
filter = mysqld-auth
logpath = /var/log/mysql/error.log
maxretry = 5
findtime = 10m
bantime  = 1h
action = nftables-multiport[name=mariadb, port="3306", protocol=tcp]

sudo systemctl restart fail2ban

sudo fail2ban-client status mariadb-auth

zoznam ip ktore su momentalne zabanovane

sudo fail2ban-client get mariadb-auth banip

ako odbanovat ip

sudo fail2ban-client set mariadb-auth unbanip 178.143.191.171

ako sa vytvori user, ktory ma pristup z internetu a prava len na jednu DB

CREATE USER IF NOT EXISTS 'kalixt'@'%' IDENTIFIED BY 'password';

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP
ON kalixtdb.* TO 'kalixt'@'%';

Users permissions

sudo chmod u+rwx,g+rx,o+rx /var/www/apifolder -R
sudo chown -R username:username /var/www/uploads

Network

ip link

sudo nano /etc/netplan

sudo nano /etc/network/interfaces

sudo systemctl restart networking

Speedtest

sudo apt install speedtest-cli
speedtest-cli

Temp

sudo apt install lm-sensors
sensors

Debian PATH fix

echo 'export PATH=$PATH:/usr/sbin' >> ~/.bashrc
source ~/.bashrc

sudo

apt install sudo

Add pecpc to sudo group
usermod -aG sudo pecpc
Verify group membership
groups pecpc

systemctl

sudo systemctl list-units --type=service
sudo systemctl stop name.service
sudo systemctl disable name.service

Backup

Info
lsblk

change /dev/{external drive}

raspi backup SD card to SD card
sudo dd if=/dev/mmcblk0 of=/dev/sda bs=4M status=progress

MariaDB info

sudo mariadb -e " SELECT table_name, table_rows, ROUND(data_length / 1024 / 1024, 2) AS data_mb, ROUND(index_length / 1024 / 1024, 2) AS index_mb, ROUND((data_length + index_length) / 1024 / 1024, 2) AS total_mb FROM information_schema.tables WHERE table_schema = 'xtrader' ORDER BY total_mb DESC; "

Seaweed

curl -fsSL https://raw.githubusercontent.com/seaweedfs/seaweedfs/master/install.sh | bash

Verify:

which weed
weed version
getent passwd seaweed >/dev/null || \
sudo useradd \
  --system \
  --home /var/lib/seaweedfs \
  --create-home \
  --shell /usr/sbin/nologin \
  seaweed

sudo nano /etc/seaweedfs/seaweedfs.conf

Minio - docker

sudo apt install docker

sudo apt install -y ca-certificates curl
install -m 0755 -d /etc/apt/keyrings

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
  -o /etc/apt/keyrings/docker.asc

sudo chmod a+r /etc/apt/keyrings/docker.asc

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" \
  > /etc/apt/sources.list.d/docker.list

sudo apt install -y docker-compose-plugin

sudo systemctl enable --now docker

About

few commands to setup or secure linux machine

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors