29 Mayıs 2023 Pazartesi

ssh Reverse Port Forwarding - Firewall Arkasındaki Sunucuya Erişmek İçindir

Giriş
Not : Açıklaması şöyle
For Remote Port forwarding the ssh server config /etc/ssh/sshd_config should have a property GatewayPorts yes
Açıklaması şöyle
Keep in mind that the ssh -R command requires SSH access to the remote server and proper authentication credentials. Additionally, the SSH server configuration on the remote host must allow port forwarding for this command to work.
ssh tunnel açmak için kullanılır. A <- Firewall <- B  olsun. B'den A'ya erişim var. Ancak B Firewall arkasında olduğu için A'nın B'ye erişimi yok.

Söz dizimi şöyle
ssh -R remote_port:local_address:local_port username@sshserver
Örnek
Şöyle yaparız
ssh -R 8888:localhost:8080 user@remote_host
Açıklaması şöyle
This command binds port 8888 on the remote server and forwards any incoming traffic to the web server running on localhost:8080 on your local machine. Now, you can access the web server on the local machine by connecting to http://remote_host:8888 from the remote server.

Örnek
A'dan B'ye erişmek için B'de şöyle yaparız.
$ ssh -f -N -R 1234:localhost:22 user@A_ip
A makinesinde şöyle yaparız.
$ ssh -p 1234 user@localhost
Örnek
Server <- Firewall <- Raspberry  olsun. Raspberry tarafında şöyle yaparız
rpi$ ssh -R 2222:localhost:22 username-on-server@server-ip-address
Sunucu makinede şöyle  yaparız
laptop$ ssh -p 2222 username-on-pi@server-ip-address

15 Mayıs 2023 Pazartesi

cron İş Dosyaları

crontab Dosyasında PATH Ortam Değişkeni
Açıklaması şöyle. crontab dosyasında PATH ortam değişkenini kullanmamak daha iyi.
cron scripts have a limited path, that doesn't include iptables, so it's not found.
if you do something like echo $PATH >> /root/test.log you will see that cron has a path with just /usr/bin and /bin
crontab Dosyasında SHELL Ortam Değişkeni
Açıklaması şöylee
cron needs to know which shell to start, there can only be one. The SHELL variable in crontab doesn’t specify possible shells, it specifies the shell to use. cron reads the value in SHELL, if any, and uses that as the command to run; it doesn’t interpret : or any other symbol.

İşlerin Saklandığı Dosyalar
Şu dizinlerde saklanırlar.
/etc/cron.hourly 
/etc/cron.daily 
/etc/cron.weekly 
Örneğin cron.daily dizinine bakarsak çıktı olarak şunun gibi bir şey alırız.
-rwxr-xr-x   1 root root   539 Tue Apr 02 2019 22:13:44 apache2
-rwxr-xr-x   1 root root  1478 Tue May 28 2019 15:40:29 apt-compat
-rwxr-xr-x   1 root root   314 Wed Feb 13 2019 17:40:39 aptitude
-rwxr-xr-x   1 root root    77 Sat Feb 16 2019 11:10:23 apt-show-versions
-rwxr-xr-x   1 root root   355 Fri Dec 29 2017 09:02:08 bsdmainutils
-rwxr-xr-x   1 root root  1187 Fri Apr 19 2019 03:14:13 dpkg
-rwxr-xr-x   1 root root  4128 Sat Jul 20 2019 12:35:58 exim4-base
-rwxr-xr-x   1 root root   377 Tue Aug 28 2018 23:21:11 logrotate
-rwxr-xr-x   1 root root  1123 Sun Feb 10 2019 12:11:20 man-db
-rwxr-xr-x   1 root root  1403 Thu Mar 21 2019 22:42:36 ntp
-rwxr-xr-x   1 root root   249 Wed Sep 27 2017 17:45:23 passwd
-rw-r--r--   1 root root   102 Sun Jun 23 2019 18:49:01 .placeholder
-rwxr-xr-x   1 root root   383 Sat Mar 30 2019 17:10:38 samba
-rwxr-xr-x   1 root root   441 Sat Apr 06 2019 08:18:26 sysstat
% karakteri
Eğer komut içinde % karakteri varsa, \ karakteri ile escape etmek gerekir.

Örnek
Şöyle yaparız. Sabah 03:03 AM'de çalışır
3 3 * * * mongodump --out /var/backups/mongobackups/`date +"\%m-\%d-\%y"`
Birden fazla dosya çalıştırma
En kolay seçenek tüm dosyaları çalıştıracak ana bir shell dosyası oluşturmak
Örnek
Şöyle yaparız
for script in /home/user/scripts/enabled/*; do $script &>/dev/null; done
Full Path
Açıklaması şöyle
You should use in cron ALWAYS the full path to the executables. It is run in quite different environment and a lot of executables are not "reachable"

Bazı Örnekler
Dakika, saat ... şeklinde gider. Her X anlamında kullanmak istersek */X şeklinde kullanırız.

Örnek
Şöyle yaparız
0 8,12,16,20 * * * /opt/tools/var/log/zhrdct.log 2>&1
Örnek
Her gün saat 21:30'da çalışmasını istediğimiz iş için şöyle yaparız.
30 21 * * *  cp /home/user/folder_name -r  /home/user/public
Örnek
Her 15 dakikada bir çalışmasını istediğimiz iş için şöyle yaparız.
*/15 * * * * wget ...
Örnek - @reboot
Bilgisayar açılırken çalışan bir script için şöyle yaparız
@reboot sleep 15; /path/to/your/script >> /path/to/your/iofile.txt 2>&1
Örnek - @reboot
Şöyle yaparız
@reboot root /usr/bin/rtcwake -m no -l -t "$(/usr/bin/date -d 'today 16:00:00' '+%s')"

14 Mart 2023 Salı

auditctl komutu

Giriş
Açıklaması şöyle
The Linux kernel audit subsystem provides a mechanism for collecting and logging security events like access to sensitive files or system calls. It can help you troubleshoot unexpected behavior or collect forensic evidence in the event of a security breach. The audit subsystem is on by default on Amazon Linux 2, but it is not configured to log syscalls.
Security events'i toplan şey auditd sistemi. auditd ile ilgili bir soru burada

-a seçeneği
Örnek
Şöyle yaparız. Tüm audit toplamayı engeller
auditctl -a never,task

9 Mart 2023 Perşembe

TMP Ortam Değişkeni

Giriş
Geçici dosyaların nerede hangi dizinde yaratılacağını belirtir.

27 Şubat 2023 Pazartesi

ssh-agent komutu

Giriş
Şifre ile korunana dosyaları bellekte tutar ve şifresiz erişim sağlar

Örnek
Şöyle yaparız
>> ssh-keygen -t rsa    #Press enter for all values

>> ls ~/.ssh/

demo-key      # private key
demo-key.pub  # public key

>> eval `ssh-agent -s`
>> ssh-add ~/.ssh/demo-key


awk komutu - printf

Örnek
Şöyle yaparız
$ awk '{printf("%06.2f\n", $1)}' <<< 99.111111
099.11

13 Şubat 2023 Pazartesi

tr komutu - Standart Input'u Değiştirir ve Standart Output'a Gönderir

Giriş
Açıklaması şöyle
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
Söz dizimi şöyle
tr [options] string1 [string2]
Replace
Birinci string aranacak karakter, ikinci string değiştirilecek karakterdir

Örnek
Şöyle yaparız
>> echo "Hello World" | tr 'H' 'h'
hello World

>> echo "Hello World" | tr 'Ho' 'KK'
KellK WKrld
-d seçeneği - Delete characters
-c seçeneği -d ile belirtilen şeyin tersini yapar
Örnek
Şöyle yaparız. İkinci örnekte -d ile "Hd" karakterlerinin silinmesi isteniyor. Ancak -c ile bunlar silinmiyor ve geri kalan her şey siliniyor
>> echo "Hello World" | tr -d 'Ho'  
ell Wrld

# complement the delete
>> echo "Hello World" | tr -cd 'Hd\n' 
Hd

>> echo "Hello World 12345 " | tr -cd [:digit:]
12345

>> echo "Hello World 12345 " | tr -cd [:alpha:]
HelloWorld
-s seçeneği - Squeeze characters
Örnek
Şöyle yaparız
>> echo "HHHHHHHHello Worrrrrrrrrldddddddddddddddddd" | tr -s 'Hord' 
Hello World

>> echo "Hello World" | tr -s [:lower:] [:upper:]
HELO WORLD