Friday, August 27, 2021
Passwordless SSH login under Linux using RSA public-private key pairs
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
#!/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
Thursday, May 07, 2020
Nginx PHP-FPM setup under Kali Linux
Thursday, August 05, 2010
Using logrotate to rotate and archive log
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.









