XP-rience

Something that I'd like to share with you!

Showing posts with label linux. Show all posts

Friday, August 27, 2021

Passwordless SSH login under Linux using RSA public-private key pairs

No comments :

For some reason, we might want to have passwordless SSH access. This will be useful for automation scripts that are running without user intervention.

In order to do this, the remote SSH server needs to be able to identify the SSH client. This is where the “~/.ssh/authorized_keys” file comes into the picture. The idea is to generate an RSA key from the client side and copy it to the remote server “~/.ssh/authorized_keys” file. If everything is done correctly, the remote SSH server will then be able to identify the SSH client that matched the RSA key and allow passwordless SSH connection.


For example below we are using 2 OS for testing, local client is running CentOS and remote server is running Ubuntu. Normal SSH as below will require the user to enter a password.

leorick@localhost ~]$ ssh leorick@192.168.100.17
leorick@192.168.100.17's password: 
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.11.0-25-generic x86_64)
...
Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Fri Aug 27 04:49:52 2021 from 192.168.100.223
leorick@ubuntu:~$

First step, generate local "id_rsa.pub" from local CentOS client by issuing “ssh-keygen -t rsa” command.

[leorick@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/leorick/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/leorick/.ssh/id_rsa.
Your public key has been saved in /home/leorick/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+7Ar4vN/uQlYinKVgP/UifRWYqGOaT+sysMST0vZbz0 leorick@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|        .        |
|   .   o         |
|  . . o o .      |
|   . * * +       |
|   o= * S        |
|. +..B = .       |
| *.oo.B +  .     |
|..=o=.o. =o.     |
| .o=o+E++o+.     |
+----[SHA256]-----+

Next, send the “id_rsa.pub” file to the remote Ubuntu server using any method available. Example below is how to send the file using SCP.

leorick@localhost ~]$ scp /home/leorick/.ssh/id_rsa.pub  leorick@192.168.100.17:/home/leorick/.ssh/
leorick@192.168.100.17's password: 
id_rsa.pub                                    100%  583     1.0MB/s   00:00    
[leorick@localhost ~]$

On the remote Ubuntu server, copy the content of the “id_rsa.pub” file into the “~/.ssh/authorized_keys” file of the particular user home directory that you are going to login with.

leorick@ubuntu:~/.ssh$ pwd
/home/leorick/.ssh
leorick@ubuntu:~/.ssh$ cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCrLZsQkR2Sev5s/9cxf64vI1BiId+4CcHxmMIA5B+T2n+R1kZ4iA1u+tut3tKlWI9G9NQnH+yC6nJlBwtjf3szYyn66fIf3mxCwZ+ikFQt9eaOg7h2Wz/WKFx0MHnol3WgVkJwwJqWB0EUaLikJgU4IDjatBpp7X7U2nvVzDHMIIBnCuDYG3NL4nT1JacxZ4KeccRewJN7qWsb+kCJfcwKZRRyiSEvn+Fnk6RDrenSefm6mnxTQJ35kiVGQ2gkpiNzUcR+yrXkZQJyLQso7nEzjmob/ecRbxxj8nZtP2MbPxtU8tIEQjYwkbRuh7acFUw0I8Y4qBS4aKP01zZXrWz7WVoA86a2JvOgwIup0F8+sjXNxzbRR3rgQqV5AitpcwI7zhRVWsHQoL2+mCkIES0hHqEDHwuwnFYk81AOKIEoFRoPSk7wMons+C28uHH9ks5VJRYtty58Vrc6AMWjtVKSaiPRildUZSI7b/HQkQ3DVofpEPnmSJKhiuBEF4Fbk30= leorick@localhost.localdomain
leorick@ubuntu:~/.ssh$ cat id_rsa.pub > authorized_keys

Now login can be done without password.

[leorick@localhost ~]$ ssh leorick@192.168.100.17
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.11.0-25-generic x86_64)
...
Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Fri Aug 27 04:50:57 2021 from 192.168.100.223
leorick@ubuntu:~$

.

Saturday, August 21, 2021

Bash - progress bar

No comments :



Example on using bash loop to draw an ASCII progress bar with BASH.

The dispProgBar function takes 2 arguments, current progress and total progress number. In this example it starts with 1 until 10000. 

dispProgBar will then calculate percentage of progress and current bar length.

1st loop will draw current progress bar with a dark block (ASCII 219).

2nd loop will draw remaining progress bar with a light graphic character (ASCII 176).

At the end of the progress bar it will send "\r" to go back to the beginning of the line without printing a newline.
#!/bin/bash

function dispProgBar(){
	curProg=$1
	totalProg=$2
	barFullLen=50
	((perc = $curProg * 100 / $totalProg))
	((barLen = $curProg * $barFullLen / $totalProg))
	printf "%3s%% [" $perc

	for (( c=1; c<=$barLen; c++ )); do
		echo -ne "█"
	done

	for (( c=$barLen; c<$barFullLen; c++ )); do
		echo -ne "░"
	done

	echo -en "]\r"

	if [ $curProg -ge $totalProg ]; then
		echo ""
	fi
}

for i in {1..10000}
do
	dispProgBar $i 10000
done

Saturday, May 09, 2020

Linux dd multiple partitions into single image file

No comments :

For example, USB 1 drive with 3 partitions to be written into a single image file.
List block devices

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  244M  0 disk
├─sdb1   8:17   1   30M  0 part /run/media/root/datapartition2
├─sdb2   8:18   1   40M  0 part /run/media/root/datapartition
└─sdb3   8:19   1   50M  0 part /run/media/root/datapartition1

/dev/sdb1,2,3 = 30M,40M,50M in size
Umount them

[root@localhost ddtest]# umount /dev/sdb*

Confirm again

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  244M  0 disk
├─sdb1   8:17   1   30M  0 part
├─sdb2   8:18   1   40M  0 part
└─sdb3   8:19   1   50M  0 part

Fdisk the drive to find its last partition end sector

root@localhost ddtest]# fdisk -l /dev/sdb

Disk /dev/sdb: 255 MB, 255852544 bytes, 499712 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xf4f4f4f4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       63487       30720   83  Linux
/dev/sdb2           63488      145407       40960   83  Linux
/dev/sdb3          145408      247807       51200   83  Linux

Calculate dd count over 1MB with formula below
(<last partition end sector> + 1) / 2048
For this USB we have (247807 + 1) / 2048 = 121

Now dd the whole /dev/sdb to test.iso with count of 121 times of 1MBytes

[root@localhost ddtest]# dd if=/dev/sdb of=test.iso bs=1M count=121 status="progress" conv=sync,noerror
121+0 records in
121+0 records out
126877696 bytes (127 MB) copied, 37.6761 s, 3.4 MB/s

Check the test.iso file size

[root@localhost ddtest]# ls -alh
-rw-r--r--. 1 root root 121M May  8 23:37 test.iso

Test the ISO file by restoring the ISO to another USB drive.
Plug out USB 1 and plug in USB 2 and check lsblk

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk

Dd the test.iso file to /dev/sdb

[root@localhost ddtest]# dd if=test.iso of=/dev/sdb bs=1M conv=sync,noerror
121+0 records in
121+0 records out
126877696 bytes (127 MB) copied, 25.2433 s, 5.0 MB/s

Check lsblk again

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk
├─sdb1   8:17   1   30M  0 part
├─sdb2   8:18   1   40M  0 part
└─sdb3   8:19   1   50M  0 part

Now we have 3 partitions restored identical to USB 1
Try to mount and check partition content

[root@localhost ddtest]# mount /dev/sdb1 1
[root@localhost ddtest]# mount /dev/sdb2 2
[root@localhost ddtest]# mount /dev/sdb3 3
[root@localhost ddtest]# ls 1
WinSCP-5.17.5-Setup.exe
[root@localhost ddtest]# ls 2
WinSCP-5.17.5-Setup.exe
[root@localhost ddtest]# ls 3
WinSCP-5.17.5-Setup.exe

Check  lsblk

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk
├─sdb1   8:17   1   30M  0 part /root/Desktop/temp/ddtest/1
├─sdb2   8:18   1   40M  0 part /root/Desktop/temp/ddtest/2
└─sdb3   8:19   1   50M  0 part /root/Desktop/temp/ddtest/3

It also works with Windows tools such as Rufus...


Or Win32 Disk Imager, but only if the device exists under the drop button circled below


How the partitions look like under Windows Disk Management


Just to add, if you are having missing partition from Gparted you can try to zeroing the 1st sector of 512 bytes (MBR)

[root@localhost ddtest]# dd if=/dev/zero of=/dev/sdc bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00308483 s, 166 kB/s




Thursday, May 07, 2020

Nginx PHP-FPM setup under Kali Linux

No comments :
Another Nginx PHP-FPM setup post similar to previous post [ here ]

To start with, install Nginx...

root@kali:~# apt-get -y install nginx

Start Nginx

root@kali:~# service nginx start

Install PHP-FPM, note the version

root@kali:~# apt-get -y install php-fpm

Edit default configuration

root@kali:~# vi /etc/nginx/sites-available/default

Add index.php into server block

index index.html index.htm index.nginx-debian.html index.php;

Add location block

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}

Restart Nginx

root@kali:~# service nginx restart

Edit php.ini, note the PHP-FPM version, add cgi.fix_pathinfo=0

root@kali:~# vi /etc/php/7.3/fpm/php.ini

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

Restart PHP-FPM

root@kali:~# service php7.3-fpm restart

Create a PHP test page "test.php" as content below and save it to /var/www/html/

<?php
phpinfo();
?>

run the PHP file = http://<your host>/test.php






Thursday, August 05, 2010

Using logrotate to rotate and archive log

No comments :
Previously I found out that rotatelogs cannot replace the rotated logs, I try to find other option.

I always come across logrotate whenever trying to search for rotatelogs.

So I give them a try.

1st step is to find it. BTW, I'm on SuSE Linux Enterprise 10.

$ logrotate
-ksh: logrotate: not found [No such file or directory]

Since it is not in the path, try to find it by doing this.

$ whereis logrotate
logrotate: /usr/sbin/logrotate /etc/logrotate.d /etc/logrotate.conf /usr/share/man/man8/logrotate.8.gz

Now try to run it with full path.

$ /usr/sbin/logrotate

Again, an error occurs.

error: error creating state file /var/lib/logrotate.status: Permission denied

This is because the user that we are currently on don't have write access /var/lib/logrotate.status.

-rw-r--r--  1 root   root  1246 2010-07-28 09:30 logrotate.status

Ask help from system root to change it...

# chmod 666 logrotate.status

...to

-rw-rw-rw-  1 root   root  1246 2010-07-28 09:30 logrotate.status

So, now try again.

$ /usr/sbin/logrotate
logrotate 3.7.3 - Copyright (C) 1995-2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License

Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail command]
[-s|--state statefile] [-v|--verbose] [-?|--help] [--usage]
[OPTION...] 

At last. Now create a simple conf like below.

$ cat ./logrotate.conf
/home/username/test/mylog {
rotate 5
daily
copytruncate
notifempty
compress
}


From my observation by triggering it manually (based on configuration above), I found out that it start by compressing the log file and name it mylog.1.gz.
When I trigger it again, it will rename the mylog.1.gz to mylog.2.gz and create a new mylog.1.gz. It will continue to do that but will not exceed mylog.5.gz (rotate 5 from conf file).

-rw-r--r-- 1 user group  348 2010-07-28 15:32 mylog
-rw-r--r-- 1 user group   54 2010-07-28 15:32 mylog.1.gz
-rw-r--r-- 1 user group   59 2010-07-28 15:32 mylog.2.gz
-rw-r--r-- 1 user group   58 2010-07-28 15:32 mylog.3.gz
-rw-r--r-- 1 user group   51 2010-07-28 15:32 mylog.4.gz
-rw-r--r-- 1 user group   57 2010-07-28 15:32 mylog.5.gz

This is just like what I wanted. It will keep only 5 gz files. Now you can schedule (cron) them accordingly.