Letsencrypt adding new certificate with Certboot

What is Letsencrypt !

Letsencrypt is platform that providing free ssl certificate

What is Certbot

Certbot is open source command line tool for generating ssl certificate.

Adding new certificate
 sudo certbot certonly --standalone -d example.com
for nginx
sudo certbot --nginx
for apache
sudo certbot --apache

Unless you don't use additional --certonly parameter, certbot will replace nginx and apache configuration

Renewing certiticates

Letsencrypt certificates have default 3 months expire date. we need to renew certificates periodically, otherwise website might have availability problems due to expired certificate

sudo certbot renew 

List Certificates

 sudo certbot certificates

Tar create archive on command line

Tar is a command for compressing data as tar format

tar -cvf target-21-Jan-2025.tar.gz target-folder

-c helps to compress folder

-v is optional, if you want to verbose, you can add

-f <filename> Location of archive

Other options

-b block size : default 512 byte

-w Interactive mode

-z, -j, -J, --lzma Compress algorithm : gzip, bzip2, xz, lzma

Mac os x sleep from command line

Mac has sleeping button on the apple icon -> sleep. Another effective way is sleeping from command line

pmset sleepnow

this command sleeps mac immediatelly

Sleeping delay
if we want to sleep after specific period, we can wait command awhile, and after system trigger command

for example sleeping after 10 seconds

sleep 10;pmset sleepnow  

Golang redirect http to https

http -> https

package main

import (
    "net"
    "log"
    "net/http"
)

var httpAddr ":8080"
var httpsAddr ":8443"

func main() {
    srv := http.Server{
        Addr: httpsAddr,
    }

    _, tlsPort, err := net.SplitHostPort(httpsAddr)
    if err != nil {
        return err
    }
    go redirectToHTTPS(tlsPort)

    srv.ListenAndServeTLS("cert.pem", "key.pem")
}

func redirectToHTTPS(tlsPort string) {
    httpSrv := http.Server{
        Addr: httpAddr,
        Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
            var host string
		if strings.Contains(r.Host, ":") {
			var err error
			host, _, err = net.SplitHostPort(r.Host)
			if err != nil {
				panic(err)
			}
	      } else {
				host = r.Host
	      }
            u := r.URL
            u.Host = net.JoinHostPort(host, tlsPort)
            u.Scheme="https"
            log.Println(u.String())
            http.Redirect(w,r,u.String(), http.StatusMovedPermanently)
        }),
    }
    log.Println(httpSrv.ListenAndServe())
}

Iptables open ports

Iptables allow port to input - output

iptables -A INPUT -p tcp --dport 8181 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 8181 -j ACCEPT
iptables --flush

we will se result after run those commands

How to print Hex result on Linux command line !

How to export hex result on linux Console ?

There is a simple way to export hex from the file

xxd command helps for us

xxd -p file
ubuntu@instance-20240128-1936:~/go-blog-dbs$ xxd /etc/localtime 
00000000: 545a 6966 3200 0000 0000 0000 0000 0000  TZif2...........
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0001 0000 0004 0000 0000  ................
00000030: 0000 5554 4300 545a 6966 3200 0000 0000  ..UTC.TZif2.....
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000050: 0000 0000 0000 0000 0000 0000 0001 0000  ................
00000060: 0004 0000 0000 0000 5554 4300 0a55 5443  ........UTC..UTC
00000070: 300a                                     0.