Friday, April 10, 2020

After-Install Setup Tasks On Ubuntu 18

After Ubuntu 18 is running, you may want to do additional setup: setup a group and user, set colors, configure a firewall, setup dynamic DNS, and remove unused packages and services.


### Which version of the operating system. Also use "uname -a".
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic



### Become familiar with what is booting and running by watching the system log.
$ sudo tail -f /var/log/syslog


### Modify the shell login files in your home directory.
$ cd ~
$ cp -p .bashrc .bashrc.orig
$ mv .profile .profile.orig
$ mv .bashrc .bash_profile

### Set a default editor.
# Choose your editor
$ select-editor
# The editor may be set in your login files.
EDITOR=vi

### Remove colorization.
# Using your file editor (such as vi) comment out all of the code with "color".
$ vi .bash_profile

# Also, put the present working directory at the end of the PATH variable by adding to end of file .bash_profile
PATH=$PATH:.

# Log in again. Alternatively, "source" your logon file.
$ . ./.bash_profile


### Remove software until you are ready to use it
$ sudo apt-get remove openvpn
$ sudo systemctl disable openvpn
Synchronizing state of openvpn.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable openvpn
Unit /etc/systemd/system/openvpn.service is masked, ignoring.
# Then disable the openvpn startup file
$ cd /etc/default
$ sudo mv openvpn openvpn.20170702


### Alternatively, find and disable services you may not be using
$ systemctl list-units --all --type=service --no-pager
# Some services you may want to disable are
 cups-browsed.service  loaded  active  running Make remote CUPS printers
 cups.service          loaded  active  running CUPS Scheduler
 ModemManager.service  loaded  active  running Modem Manager

# Check the service, stop the service, then disable the service
$ systemctl status cups-browsed
 cups-browsed.service - Make remote CUPS printers available locally
   Loaded: loaded (/lib/systemd/system/cups-browsed.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-04-05 00:05:14 MDT; 14h ago
 Main PID: 21137 (cups-browsed)
    Tasks: 3 (limit: 4915)
   CGroup: /system.slice/cups-browsed.service
           └─21137 /usr/sbin/cups-browsed
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

$ sudo systemctl stop cups-browsed
$ sudo systemctl disable cups-browsed
$ sudo systemctl stop cups
$ sudo systemctl disable cups
$ sudo systemctl stop ModemManager
$ sudo systemctl disable ModemManager

# After the next machine restart, verify the services are not runing.
$ sudo reboot
 

#  Or alternatively use systemctl to restart the machine.
$ sudo systemctl reboot
$ systemctl list-units --all --type=service --no-pager


# Another example of disabling a service, for the Let's Encrypt https certbot service.
$ systemctl status certbot.service
● certbot.service - Certbot
   Loaded: loaded (/lib/systemd/system/certbot.service; static; vendor preset: enabled)
   Active: inactive (dead) since Fri 2020-04-10 16:43:44 MDT; 5h 40min ago
     Docs: file:///usr/share/doc/python-certbot-doc/html/index.html
           https://letsencrypt.readthedocs.io/en/latest/
 Main PID: 31598 (code=exited, status=0/SUCCESS)
$ sudo systemctl stop certbot
Warning: Stopping certbot.service, but it can still be activated by:
  certbot.timer
$ sudo systemctl disable certbot



### When you log on you will see messages about software updates.
38 packages can be updated.
34 updates are security updates.

$ Install the software updates.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get dist-upgrade


$ When you next logon, the upgrade counter should drop. Not all can take effect until after a reboot.
0 packages can be updated.
0 updates are security updates.
*** System restart required ***
$ sudo shutdown -r



### Setup a user and group. In this example, the new users is "testuser" and the new group is "minec".
$ sudo addgroup minec
$ cat /etc/group | grep minec

# Create new user.
$ sudo adduser testuser --ingroup minec
# At this point, you may want to log in as the user and run command "groups" to confirm the group is set up appropriately.

# Allow logon via ssh. Put user in /etc/ssh/sshd_config on the "AllowUsers" line
# Use "vi" or "vim" or similar editor to edit file /etc/ssh/sshd_config and add "testuser" to the "AllowUsers" line.
# After editing, it will look like this:
$ grep -i allowusers /etc/ssh/sshd_config
AllowUsers testuser
# You may want to review the security implications of the following settings in sshd_config:
# Protocol, PermitRootLogin, AuthorizedKeysFile, PermitEmptyPasswords
# IgnoreRhosts, PermitTunnel, ciphers.
$ grep -i ^permitrootlogin /etc/ssh/sshd_config
PermitRootLogin no


# Have ssh  reread the configuration file. First get the process ID, then send HUP signal to that PID.
$ ps -ef | grep 'bin/sshd'$ sudo kill -hup <sshd_pid>
# Alternatively use service manager to restart ssh
$ sudo systemctl restart ssh

# View the effective settings of sshd
$ sshd -T

# Maybe put testuser in sudoers. Careful with this, as it provides root access.
$ sudo usermod -aG sudo testuser

# Maybe modify defaults of sudo
$ sudo visudo


### Configure firewall
# "Uncomplicated FireWall", known as ufw, should already be installed. If not, run:
$ sudo apt install ufw
# Ensure the filtering of IPv6 firewall is on, even if you are not using IPv6 at this time
$ sudo vi /etc/default/ufw
# Ensure this line exists without a leading comment character:
$ IPV6=yes
# Setup ufw services
$ sudo ufw allow ssh
$ sudo ufw allow http
$ sudo ufw allow https

# Deny outgoing SMTP mail
$ sudo ufw deny out 25


# Consider a block on geographic network address ranges.

# Start the firewall.
$ sudo ufw enable

# Look at the ufw settings
$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)

25                         DENY OUT    Anywhere
25 (v6)                    DENY OUT    Anywhere (v6)


### If you have a changing IP (typical consumer-grade internet) and want to more easily host an incoming service, setup up dynamic DNS. Go to www.duckdns.org and configure a sub-domain for your host. Using the information from your sign up at duckdns.org, configure the following on the Linux machine.
$ cat /var/opt/duckdns/duck.sh
#!/bin/sh
DOMAINFQ="YOUR_DOMAIN.duckdns.org"
curl_out=$(echo url="https://www.duckdns.org/update?domains=YOUR_DOMAIN&token=YOUR_TOKEN" | /usr/bin/curl --insecure --silent --config - )
# Whatever is running this script (cron?) may want to direct output to a file in /tmp.
/bin/echo
/bin/date
/bin/echo duckdns updated $curl_out
/bin/echo 'dig output:'
/usr/bin/dig $DOMAINFQ | /bin/grep ^$DOMAINFQ
if [ $curl_out = "OK" ]; then
    /usr/bin/logger -p daemon.info "duckdns update ok. rc=" $curl_out
else
    /usr/bin/logger -p daemon.err "duckdns update failed. rc=" $curl_out
fi


# Run script every twenty minutes from cron. As the duckdns user, add a line to the crontab.
$ crontab -e
*/20 * * * * /var/opt/duckdns/duck.sh >>/tmp/duckdns.log 2>&1


# Watch the duckdns log file from cron which should be in /tmp.


### When you logon to the machine, the message displayed is from motd.dynamic.
$ ls -l /run/motd.dynamic
-rw-r--r-- 1 root root 753 Apr 11 17:58 /run/motd.dynamic


# If you do not yet have data backups configured, add a message to your users.
$ sudo vi /etc/update-motd.d/60-backup-warning
$ sudo chmod +x /etc/update-motd.d/60-backup-warning
# Add the following lines to this new file.
$ cat 60-backup-warning
#!/bin/sh
printf "\n"
printf "*** Warning: This machine is not on regular backups. Plan accordingly.\n"


# Consider disabling the live patch message, and others. Remove the execute permission.
$ sudo chmod -x /etc/update-motd.d/80-livepatch
$ sudo chmod -x /etc/update-motd.d/10-help-text



Sunday, April 05, 2020

Minecraft Setup On Linux Using systemctl

With schools and the economy shut down, all the cool kids are talking about their Minecraft "server" and the "IP. Help your children be cool and get them a Minecraft server!


In this posting we will:
- set up unix group and user
- configure ssh
- install Minecraft software
- start and configure Minecraft to run as a unix service (daemon)


$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic



### Setup a user and group. In this example, the new users is "testuser" and the new group is "minec".
$ sudo addgroup minec
$ cat /etc/group | grep minec

# Create new user for the software install.
$ sudo adduser minec --ingroup minec
# At this point, you may want to log in as the user and run command "groups" to confirm the group is set up appropriately.

# Allow logon via ssh. Put user in /etc/ssh/sshd_config on the "AllowUsers" line
# Use "vi" or "vim" or similar editor to edit file /etc/ssh/sshd_config and add "testuser" to the "AllowUsers" line.
# After editing, it will look like this:
$ grep -i allowusers /etc/ssh/sshd_config
AllowUsers minec

# Have ssh  reread the configuration file. First get the process ID, then send HUP signal to that PID.
$ ps -ef | grep 'bin/sshd'$ sudo kill -hup <sshd_pid>
# Alternatively use service manager to restart ssh
$ sudo systemctl restart ssh


### If java is not installed, install java now.
$ which java
$ sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [107 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-security InRelease [107 kB]
Fetched 312 kB in 1s (252 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
$ sudo apt install openjdk-11-jdk

...
$ which java
/usr/bin/java



### Install Minecraft.
# Good instructions are at minecraft.gamepedia.com/Tutorials/Setting_up_a_server
# After downloading the software, the directory structure will look like this.
$ pwd
/home/minec/Minecraft_Server
$ ls -l
total 35368
-rw-r-----  1 minec minec        2 Apr  4 12:56 banned-ips.json
-rw-r-----  1 minec minec        2 Apr  4 12:56 banned-players.json
-rw-r-----  1 minec minec      180 Mar 29 19:21 eula.txt
drwxr-x---  2 minec minec     4096 Apr  5 09:55 logs
-rw-r-----  1 minec minec      140 Apr  4 12:56 ops.json
-rw-r-----  1 minec minec 36175593 Mar 29 19:01 server.jar
-rw-r-----  1 minec minec      940 Apr  4 12:56 server.properties
-rwxr-x---  1 minec minec       67 Apr  4 10:01 startminecraft.sh
-rw-r-----  1 minec minec     1645 Apr  5 16:13 usercache.json
-rw-r-----  1 minec minec        2 Mar 29 19:21 whitelist.json
drwxr-x--- 11 minec minec     4096 Apr  5 17:46 world


# In preparation for starting the server unattended, the startup commands are in the executable shell file "startminecraft.sh".
This script first backs up the Minecraft configuration.
If you want to run it from the console and watch the log in the GUI, run the java command without "-nogui".
#!/bin/sh
umask 026
cd /home/minec/Minecraft_Server
/bin/tar -cvf /tmp/worldDirs.$(date +%Y%m%d.%H%M%S).tar /home/minec/Minecraft_Server/world
echo "Finished tar of worldDirs."
/usr/bin/java -jar /home/minec/Minecraft_Server/server.jar -nogui &
/bin/echo $! > /home/minec/Minecraft_Server/minecraft.service.pid


# Set up the service. Create file /etc/systemd/system/minecraft.service with these contents.
$ cd /etc/systemd/system
$ sudo vi minecraft.service
[Unit]
Description=Minecraft server
After=network.target
[Service]
User=minec
Group=minec
ExecStart=/home/minec/Minecraft_Server/startminecraft.sh
#PIDFile=/var/run/minecraft.service.pid
#ExecStop=/bin/kill -s TERM $MAINPID
KillMode=process
Restart=on-failure
Type=forking
TimeoutStartSec=120
TimeoutStopSec=30
RuntimeMaxSec=infinity
[Install]
WantedBy=multi-user.target


# Reload systemctl configurations.
$ sudo systemctl daemon-reload
# Enable the service.
$ sudo systemctl enable minecraft.service
Created symlink /etc/systemd/system/multi-user.target.wants/minecraft.service → /etc/systemd/system/minecraft.service.

# If the prior line output is not "Created symlink ..." then something is not correct.

# Start the minecraft service.
$ sudo systemctl start minecraft.service
$ sudo systemctl status minecraft.service


# Restart the machine to test if the Minecraft server starts ok.
$ sudo reboot

# After the machine restarts, look in /var/log/syslog for messages, check the status of the service, and look for the process.
$ sudo tail -44 /var/log/syslog
$ ps -ef | grep mine


$ systemctl status minecraft.service
● minecraft.service - Minecraft server
   Loaded: loaded (/etc/systemd/system/minecraft.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-04-10 21:26:31 MDT; 3min 26s ago
  Process: 7622 ExecStart=/home/minec/Minecraft_Server/startminecraft.sh (code=exited, status=0/SUCCESS)
 Main PID: 7632 (java)
    Tasks: 36 (limit: 4915)
   CGroup: /system.slice/minecraft.service
           └─7632 /usr/bin/java -jar /home/minec/Minecraft_Server/server.jar -nogui

# You can stop the service. To prohibit the service from restarting upon machine reboot, also disable the service.
$ sudo systemctl stop minecraft.service
$ sudo systemctl disable minecraft.service
Removed /etc/systemd/system/multi-user.target.wants/minecraft.service.

Saturday, March 14, 2020

Disable Windows 10 Netbios and SMB

On Microsoft Windows 10, SMB 1 should already be disabled. To enhance security, you may want to also disable SMB 1 and 2 and NetBIOS.

Open PowerShell as Administrator by pressing the Start button, typing powershell, and pressing Run As Administrator:






















Read the Microsoft post about how to disable SMB

In PowerShell, get the setting and disable it with:
Get-SmbServerConfiguration | Select EnableSMB2Protocol
Set-SmbServerConfiguration -EnableSMB2Protocol $false















Disable SMB v1 in PowerShell with:
Disable-WindowsOptionalFeature -Online -FeatureName smb1protocol

If you see an error of "Access is denied", ensure you run PowerShell as Administrator.




To disable NetBIOS, in Control Panel go to Advanced TCP/IP Settings and press the radio button: