When Does the DNS Server Override the /etc/hosts File for FQDN or Domain Name Resolution?


Problem scenario
You notice that on some Linux servers the /etc/hosts file controls the resolution of hostnames and on other servers the DNS server overrides the /etc/hosts file. Which has precedence in DNS resolution, /etc/hosts or the DNS server on the network?

Answer
It depends. The /etc/nsswitch.conf file will decide. There is a "hosts" stanza. This setting will have the DNS server override the /etc/hosts file:

hosts:dns files

This setting will have the /etc/hosts file take precedence for name resolution:

hosts:files dns

To get the hostname, there is a systemcall called gethostname() which can help you understand how the hostname is retrieved and determined. To learn more, see this posting.

How Do You Fix a Mouse That Scrolls in a Different Direction Than It Is Supposed To?

Problem scenario
Your mouse scrolls up when you use the scroll wheel to scroll down. What should you do?

Solution
Is the problem reproducible on two different computers? If the problem stays with the mouse you may need to dis-assemble it and clean it. You may need a can of air and a small screwdriver. If you see no screws on the bottom of the mouse, you may need to lift off the small pads that have an adhesive to them. Underneath the pads there may be accessible screws to remove. Once clean the problem may go away. If the problem remains, you may need a new mouse.

How Do You Get a PHP Program to Invoke a Python Program via Browsing a Website?

Problem scenario
You want Debian/Ubuntu Linux to support a website. You want a Python program to run every time a web page is downloaded. How do you get a PHP program to invoke a Python program on Debian/Ubuntu Linux?

Solution

Prerequisites
i. This assumes you have Apache2 and PHP installed. If you need assistance run this: sudo apt -y install apache2 php
ii. This assumes that you have installed Python 3. If you need assistance, run this: sudo apt -y install python3

Procedures
Have a PHP program like this in the same directory as the index.html file:

<?php

   $output = shell_exec('/usr/bin/python3 --version');
   echo $output;

   $output1 = shell_exec('/usr/bin/python3 /foobar/invoker.py');
   echo $output1;
?>

<html>
<body>

HELLO!

</body>
</html>

Put a file called invoker.py in the /foobar/ directory of your server:

#!/usr/bin/env /usr/bin/python3
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file handler
handler = logging.FileHandler('/foobar/output.log')
handler.setLevel(logging.INFO)

# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# add the file handler to the logger
logger.addHandler(handler)

logger.info('Hello from contint')

# This was adapted from https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/

Run one of these commands:

sudo chown -R www-data:www-data /foobar
or 
sudo chmod 777 /foobar

Why Cannot You List Every Repository in Your GitHub Organization’s Account?

Problem scenario
You are trying to list the repositories in your GitHub organization with the REST API. You run something like this:

curl -u $username:$password https://api.github.com/orgs/$ORGNAME/repos

But as you add repositories, all are not showing. What is wrong?

Possible solution
Do you have more than 30 repositories? The output of the curl (GET REST) invocation can be the same as you add repositories if you exceed 30. You can change the limit to 100. See this posting for details.

How Do You Remove the ECDSA Fingerprints of Old Servers That You Will Not Connect to Again?

Problem scenario
You have a server that has run SSH to connect to other servers. You want to remove the fingerprints so the ECDSA key fingerprint will challenge a user to continue connecting. What do you do?

Solution
Run this command where x.x.x.x is the IP address or hostname of the server whose finger print should be removed: ssh-keygen -R x.x.x.x

If you used a hostname with the SSH comands, use a hostname in the command above. If you used an IP address with the SSH commands, use the IP address in the command above.

How Do You Create an Issue for a Repository in a GitHub Organization the API?

Problem scenario
You are running this command:

curl -u jdoe:$password -X PUT -d '{"title": "Look at this bug", "body": "This is a serious problem here.", "assignees": [ "jdoe" ], "milestone": 1, "labels": [ "bug" ]}' https://api.github.com/repos/$orgname/$reponame/issues

The message in the response you receive is "Not Found". You found documentation that refers to an "owner" being in the URL. You see "POST /repos/:owner/:repo/issues" (with no reference to the organization). (This was taken from https://developer.github.com/v3/issues/#create-an-issue .)

What should you do to create a GitHub issue with the REST API?

Solution
1. Modify the command.
1.a. Use "POST" instead of "PUT".
1.b. Eliminate the '"milestone": 1,'
1.c. It should look like this:

curl -u jdoe:$password -X POST -d '{"title": "Look at this bug", "body": "This is a serious problem here.", "assignees": [ "jdoe" ], "labels": [ "bug" ]}' https://api.github.com/repos/$orgname/$reponame/issues

2. Run the command after you change the word "jdoe" to the username and the "password" to your GitHub.com password.

How Do You Read in User Input with Golang?

Problem scenario
You want to read in user input with Golang. What should you do?

Solution
This program will illustrate how you accept user input with a Golang program:

package main

import (
  "bufio"
  "fmt"
  "os"
)

func main() {
fmt.Println("Please enter some text:")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
fmt.Println("The text you entered was this: ", text)
}

How Do You Write an Ansible Playbook to Install a Yum Package?

Problem scenario
How do you use the yum module in an Ansible playbook to install a yum package?

Solution
Prerequisites
This assumes you have installed Ansible and configured a managed node to work with it. If you need assistance with this, see this posting.

Procedures
1. On the Ansible control server, create this .yaml file (e.g., called good.yaml):

- hosts: all
  tasks:
  - name: Install Apache web server and nmap
    yum:
      name: "{{ packages }}"
    vars:
      packages:
        - httpd
        - nmap
    become: yes

2. Run this: ansible-playbook good.yaml

How Do You Create a GCP VM to Be a Web Server?

Problem scenario
You have a GCP server. You can run curl commands to its URL via localhost. But with a URL constructed with the server's external IP address the curl command times out. You cannot reach the URL from your workstation. How do you get the GCP server to present the web service to other machines?

Solution
Modify the firewall rule by following these steps below.
1. Go to the VM's details (go to the hamburger icon in the upper righthand corner, then go to VM Instances and click on the VM).
2. Look for the "Network tags". You will find something like "http-server" or "https-server". Make a mental note of one of them.
3. Go to the hamburger icon in the upper righthand corner, go to VPC Network -> Firewall Rules.
4. Click "CREATE FIREWALL RULE".
5. Enter a relevant port in "Protocols and ports" (e.g., 8080 or just 80) and a relevant IP Range in "Source filters" (e.g., 0.0.0.0/0 if it is not sensitive).
6. Click "Save".

How Do You Install Firefox on a Windows Server with PowerShell?

Problem scenario
You want to install Firefox on a Windows server without using the traditional GUI method. What should you do?

Solution
Updated on 5/16/21
1. Open PowerShell ISE as Administrator (right click it, go to More -> Run as Administrator).
2. Run this script (e.g., firefox.ps1):

# This script was mostly taken from https://forum.pulseway.com/topic/1940-install-firefox-with-powershell/

# www.continualintegration.com changed the version number of Firefox and added some comments
# Silent Install Firefox 

# Download URL: https://www.mozilla.org/en-US/firefox/all/

# Path for the workdir
$workdir = "c:\installer\"

# Check if work directory exists if not create it

If (Test-Path -Path $workdir -PathType Container)
{ Write-Host "$workdir already exists" -ForegroundColor Red}
ELSE
{ New-Item -Path $workdir  -ItemType directory }

# Download the installer

$source = "https://download.mozilla.org/?product=firefox-88.0.1-SSL&os=win64&lang=en-US"
$destination = "$workdir\firefox.exe"

# Check if Invoke-Webrequest exists otherwise execute WebClient

if (Get-Command 'Invoke-Webrequest')
{
     Invoke-WebRequest $source -OutFile $destination
}
else
{
    $WebClient = New-Object System.Net.WebClient
    $webclient.DownloadFile($source, $destination)
}

# Start the installation

Start-Process -FilePath "$workdir\firefox.exe" -ArgumentList "/S"

# Wait XX Seconds for the installation to finish

Start-Sleep -s 35

# Remove the installer

rm -Force $workdir\firefox*

# write-host should be changed to write-verbose  # we have not tried it yet with these directions
# https://devblogs.microsoft.com/scripting/understanding-streams-redirection-and-write-host-in-powershell/