BASH Docs

Last updated: Jan 20th, 2022

Download

Welcome to the mine useful documentation for daily work on web developer and computer programming. Thanks to PrettyDocs for theme template layout; download it at the page linked in the button below.

Download PrettyDocs

PrismJS

PrismJS is used as the syntax highlighter here. You can build your own version via their website should you need to.

Boot Script Linux

Using init.d

In the script you put in /etc/init.d you have to set it executable and create a symlink to /etc/rc.d/:


chmod +x /etc/init.d/my-script.sh


ln -s /etc/init.d/my-script.sh /etc/rc.d/
                                    

Please note that this wil not work as the script have to be LSB compliant (provide, at least, the following actions: start, stop restart, force-reload and status).

Using systemd

File: /etc/systemd/system/my-startup.service


[Unit]
Description=My Startup

[Service]
ExecStart=/usr/local/sbin/my-script.sh

[Install]
WantedBy=multiuser.target
                                    

linuxspot@linuxspot:~$ sudo nano /etc/systemd/system/my-startup.service
linuxspot@linuxspot:~$ systemctl status my-startup.service
○ my-startup.service - My Startup
    Loaded: loaded (/etc/systemd/system/my-startup.service; disabled; vendor preset: enabled)
    Active: inactive (dead)

Feb 06 19:35:08 linuxspot systemd[1]: Started My Startup.
linuxspot@linuxspot:~$ sudo systemctl enable my-startup.service
Created symlink /etc/systemd/system/multi-user.target.wants/my-startup.service ⇨ /etc/systemd/system/my-startup.service
                                    

Public IP

Using dig command (Unix only)
  1. Open the Terminal application.
  2. Type the following dig (domain information groper) command on a Linux, OS X, or Unix-like operating systems to see public IP address assigned by the ISP:
    
    dig +short myip.opendns.com @resolver1.opendns.com
                                                
    Or:
    
    dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
                                                
  3. You should see your IP address on screen. This is the fastest way to find out your IP address without using 3rd party site.
Using 3rd party web-sites

Please note that I do not recommend following curl/wget method due to security reasons. You have been warned.

  1. Open the Terminal application.
  2. Type the following curl command to see public IP address assigned by the ISP:
    
    curl checkip.amazonaws.com
                                                
    Or:
    
    curl ifconfig.me
                                                
  3. You should see your IP address on screen.

Static IP Linux

Using NETPLAN

Here’s the replacement for editing /etc/networking/* in the old system. The whole system now uses YAML configuration files under /etc/netplan, and then the netplan command applies those configurations to the system.


vi /etc/netplan/*.yaml
                                    

network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            optional: true
            # addresses: [192.168.1.101/24]
            # gateway4: 192.168.1.1
            # nameservers:
            #   addresses: [8.8.8.8, 1.1.1.1]
    wifis:
        wlan0:
            dhcp4: false
            dhcp6: false
            addresses: [192.168.1.101/24]
            gateway4: 192.168.1.1
            nameservers:
                addresses: [8.8.8.8, 1.1.1.1]
            access-points:
                "This is Access Point SSID":
                    password: "secret"
                                    

netplan apply
                                    

So now (whether you’re running an older system or a new one) you should now have a static IP address!

Using Network Interfaces

Assign Static IP Address to eth0 interface editing configuration file /etc/network/interfaces to make permanent changes as shown below:


auto eth0
iface eth0 inet static
address 192.168.1.101
netmask 255.255.255.0
gateway 192.168.1.1
                                    

Next, restart network services after entering all the details using the following command:


systemctl restart NetworkManager.service

# Or

/etc/init.d/networking restart
                                    
Using dhcpcd.conf file

Open /etc/dhcpcd.conf with your favorite text editor. scroll down until you see the following commented out lines of code:


# Example static IP configuration:
#interface eth0
#static ip_address=192.168.11.13
#static routers=192.168.11.1
#static domain_name_servers=8.8.8.8
                                    

Uncomment the lines above or copy the following snippet and put it right under the code shown above:


interface wlan0
static ip_address=192.168.11.13
static routers=192.168.11.1
static domain_name_servers=8.8.8.8
                                    

You will need to set up the static ip_address and static routers tag to fit your network setup. Don’t forget to save the file.

Postfix Linux & Gmail SMTP

Configuring POSTFIX on Linux and Gmail SMTP for sending mail from command line.

  1. Let's start with installing Postfix, Mailutils and a series of libraries:
    
    sudo apt update
    sudo apt upgrade
    sudo apt install postfix mailutils libsasl2-2 libsasl2-modules
                                                
    • as General Type of Mail Configuration we'll choose SATELLITE
    • as System Mail Name we can accept the proposed one or choose one not your domain
    • as SMTP RELAY HOST we'll put smtp.gmail.com:587
  2. After the installation we must go to configure the SASL authentication part. With a text editor we will edit the /etc/postfix/main.cf Postfix file. Specifically at the bottom of the file we will add the following lines:
    
    smtp_use_tls = yes
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_password
    smtp_sasl_security_options = noanonymous
    smtp_sasl_tls_security_options = noanonymous
                                                
    Now we will have to create the file where to put the Gmail SMTP access password. We create the /etc/postfix/sasl/sasl_password file inside which we will put a single row:
    
    smtp.google.com username:password
                                                
    We create the lookup table db from postfix itself:
    
    postmap /etc/postfix/sasl/sasl_password
                                                
  3. As they are sensitive data, although they will be used encrypted, the file could be read, so let's make sure that only the users root and postfix can handle it:
    
    chown -R root:postfix /etc/postfix/sasl
    chmod 750 /etc/postfix/sasl
    chmod 640 /etc/postfix/sasl/sasl_password*
                                                
  4. We restart the POSTFIX service to apply all the changes:
    
    sudo service postfix restart
                                                
  5. At this point we can send an email from our system to verify the proper functioning:
    
    echo "send test mail from command line" | mail "sending test" recipient@example.com
                                                
    Any problems can be analyzed in /var/log/mail.log and /var/log/mail.err.

WGET Download Site

Web site full download with WGET command.


wget --recursive --no-clobber --page-requisites --html-extension --convert-links --restrict-filenames=windows --domains mysite.com --no-parent www.mysite.com