Check NTP server working or not, Check NTP server Date and Time – Windows, Linux

Windows

1 Use Win + R to open run Window

Microsoft Windows - Run window
Microsoft Windows – Run window

2 Type cmd, then hit Enter key

Microsoft Windows - Run window - cmd.exe
Microsoft Windows – Run window – cmd.exe

The command we use is in following format:

w32tm /stripchart /computer:domainnameORipaddress /dataonly /samples:5

3 Enter following command to test “time.windows.com”

w32tm /stripchart /computer:time.windows.com /dataonly /samples:5
Microsoft Windows - CMD -   w32tm /stripchart /computer:time.windows.com /dataonly /samples:5 output
Microsoft Windows – CMD – w32tm /stripchart /computer:time.windows.com /dataonly /samples:5 output

Bonus: Other commands

1 Show the current time zone settings:

w32tm /tz

2 List external NTP servers (peers)

w32tm /query /peers
Microsoft Windows - CMD -  w32tm /query /peers output
Microsoft Windows – CMD – w32tm /query /peers output

Note! If you have encountered this error: “The following error occurred: The service has not been started. (0x80070426)”, you need to start the “Windows Time” service, you can start a elevated cmd or PowerShell by using Win + X key combination then select “Command Prompt (Admin)” or “Windows PowerShell (Admin)” then type “sc start W32Time” to start the Windows Time service or using Run window and type “services.msc” then find “Windows Time” service and start it.

3 Restore Windows Time service settings to default

C:\>net stop w32time
C:\>w32tm /unregister
C:\>w32tm /register
C:\>net start w32time

4 Re-sync Time

Resynchronize the clock as soon as possible, disregarding all accumulated error statistics. If no computer is specified, the local computer will resynchronize. The command will wait for resynchronization unless the /nowait switch is used.

w32tm /resync

5 Redirect the network configuration and Re-sync time

Force redetection of network resource before resynchronization.

w32tm /resync /rediscover
Microsoft Windows - PowerShell - w32tm /resync /rediscover output
Microsoft Windows – PowerShell – w32tm /resync /rediscover output

6 Check current NTP configuration

Show Information:

w32tm /query /status
Microsoft Windows - PowerShell -  w32tm /query /status  output
Microsoft Windows – PowerShell – w32tm /query /status output

Show Configuration:

w32tm /query /configuration
Microsoft Windows - PowerShell -  w32tm /query /configuration  output
Microsoft Windows – PowerShell – w32tm /query /configuration output

Linux

1 ntpdate -q domainnameORipaddress

ntpdate -q time.windows.com
Linux - Terminal - ntpdate -q time.windows.com output
Linux – Terminal – ntpdate -q time.windows.com output

2 ntpq -p domainnameORipaddress

ntpq -p 192.168.1.1

How to use Apache .htaccess .htpasswd to protect files, folders and paths and to protect multiple files, multiple folders and paths

Keywords: Apache, .htaccess, .htpasswd, block access, protect file, protect folder, protect directory, protect path

If we have following folders

/web/resource1/file1.htm
/web/resource1/A/
/web/resource2/file2.php
/web/resource2/B/C/D/
/web/resource3/E/F/

and Following url path

https://www.example.com/
https://www.example.com resource1/file1.htm
https://www.example.com/ resource1/A/
https://www.example.com/ resource2/file2.php
https://www.example.com/ resource2/B/C/D/
https://www.example.com resource3/E/F/ https://www.example.com/secret

The document root for “https://www.example.com/” is “/web/”

Path secret is a virtual path which does not reflect to a real directory with name “secret” (e.g. an existing rewrite rule in .htaccess)

Now we want to protect file “file1.htm” directory “D” and directory “E”, “F” and virtual path “secret”

1.1 We need to create a .htaccess file under “web” directory

1.2 Open the .htaccess file, we need to add following contents for protecting files

#Protected file
<Files file1.htm>
#Password file path
AuthUserFile /web/.htpasswd
#Message for user to see
AuthName "Password protected"
AuthType Basic
#(If only allow specific user, use "require user username" if allow all valid users use "Require valid-user")
require user username
</Files>

Note: if dealing with multiple files, filesmatch should be used.

<FilesMatch "file1\.htm|file2\.php">
AuthUserFile /web/.htpasswd
AuthName "Password protected"
AuthType Basic
require user username
</FilesMatch>

Tip: Targeting files start with abc or def and end in .php

<FilesMatch "^(abc|def).php$">
AuthUserFile /web/.htpasswd
AuthName "Password protected"
AuthType Basic
require user username
</FilesMatch>

1.3 We add following content to protect directories and the virtual path (We can use this method to protect multiple sub-directories/sub-folders/paths)

#Do the regex check against the URI here, if match, set the "require_auth" var
SetEnvIf Request_URI ^/ resource2\/B\/C\/D require_auth=true
SetEnvIf Request_URI ^/ resource3\/E require_auth=true

#Auth stuff
AuthUserFile /web/.htpasswd
AuthName "Password protected"
AuthType Basic

#Setup a deny/allow
Order Deny,Allow
#Deny from everyone
Deny from all
#except if either of these are satisfied
Satisfy any
#1. a valid authenticated user
Require valid-user
#or 2. the "require_auth" var is NOT set
Allow from env=!require_auth

https://stackoverflow.com/questions/14603568/password-protect-a-specific-url

1.4 We create a .htpasswd file under “web” directory

1.5 Open the .htpasswd file we add following contents (File contains username:hashed user password)

user1:$apr1$MknR4YQ8$ls4RTpNIxaJWyedBK5m030
user2:$apr1$FtfabsVg$NoxTA07DDeGhSOYT9NMLF/

These password can be generated using htpasswd with following command:

$  sudo htpasswd -c /web/.htpasswd user1

(You will need to supply and confirm the password for the user)

$ sudo htpasswd /web/.htpasswd user2

Another way to protect current directory:

e.g. If we want to protect directory “A”

2.1 Creat a .htaccess file under in directory “A”, so we have “/web/resource1/A/.htaccess”

2.2 We add following content to the file

<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>
AuthUserFile /web/resource1/A/.htpasswd
AuthGroupFile /dev/null
AuthName "Please enter your ID and password"
AuthType Basic
require valid-user 
order deny,allow

2.3 We create the .htpasswd file under “/web/resource1/A/”, so we have “/web/resource1/A/.htpasswd”

2.4 We generate password as in step 1.5 (We need to change path from “/web/.htpasswd” to “/web/resource1/A/.htpasswd”)

Now the directory “A” is protected


Tips:

1 We can use online .htpasswd generator to create password for convenience

https://www.htaccesstools.com/htpasswd-generator/

https://www.web2generators.com/apache-tools/htpasswd-generator

https://www.askapache.com/online-tools/htpasswd-generator/

https://htmlstrip.com/htpasswd-generator

https://www.mobilefish.com/services/htpasswd_generator/htpasswd_generator.php

2 We can use online tools to generate .htaccess for convenience

https://www.htaccessredirect.net/

https://hostingfacts.com/htaccess-generator/

https://makeawebsitehub.com/htaccess-generator/

Ubuntu 18.04.3 LTS /usr/lib/apt/methods/http: relocation error

When updating package list with sudo apt update, following error comes up

/usr/lib/apt/methods/http: relocation error: /usr/lib/x86_64-linux-gnu/libgnutls.so.30: symbol _idn2_punycode_decode version IDN2_0.0.0 not defined in file libidn2.so.0 with link time reference    
Reading package lists… Done
E: Method http has died unexpectedly!
E: Sub-process http returned an error code (127)
E: Method /usr/lib/apt/methods/http did not start correctly

1 Download http://ftp.us.debian.org/debian/pool/main/libi/libidn2/libidn2-0_2.0.5-1_amd64.deb

2 Run sudo dpkg -i path/libidn2-0_2.0.5-1_amd64.deb

Change hostname in Linux operating systems

Usually hostname is configured during system installating.

Here is how to change the hostame on up to date Linux Linux distributions with systemd installed by default.

To understand what is hostname: https://en.wikipedia.org/wiki/Hostname

Show Current hostname

To show current hostname, we can use this command: hostnamectl

(It will show information including: Static hostname, Icon name, Chassis, Machine ID, Boot ID, Virtualization, Operating System, Kernel, Architecture)

[email protected]:~$ hostnamectl
    Static hostname: ubuntu
          Icon name: computer-vm
            Chassis: vm
         Machine ID: eee8d3edb062470c8afc7e097eb9b962
            Boot ID: 126a5a692e754b56b59dc75f8f045984
     Virtualization: vmware
   Operating System: Ubuntu 19.04
             Kernel: Linux 5.0.0-23-generic
       Architecture: x86-64
Output of hostnamectl
Output of hostnamectl

Change hostname

Command: hostnamectl set-hostname newhostname

[email protected]:~$ hostnamectl set-hostname newubuntu
 [email protected]:~$ hostnamectl
    Static hostname: newubuntu
          Icon name: computer-vm
            Chassis: vm
         Machine ID: eee8d3edb062470c8afc7e097eb9b962
            Boot ID: 126a5a692e754b56b59dc75f8f045984
     Virtualization: vmware
   Operating System: Ubuntu 19.04
             Kernel: Linux 5.0.0-23-generic
       Architecture: x86-64
Output of hostnamectl set-hostname newhostname
Output of hostnamectl set-hostname newhostname

hostnamectl set-hostname will return 0 if the command was successfully executed, if the command failed, it will return non 0 results.

To check if the hostname is changed or not, just execute hostnamectl command again to verify.

Depend on your platform or Linux distro, you may need to modify the command to achieve same results.

Recover from rm

If a running program still has the deleted file open, you can recover the file through the open file descriptor in /proc/[pid]/fd/[num]

e.g. Deleting

$ rm -f /root/selenium/Spider/MySql.Data.dll
$ ll /root/selenium/Spider/MySql.Data.dll
ls: cannot access /root/selenium/Spider/MySql.Data.dll: No such file or directory 

Recovery

Use lsof to check opened processes

$ lsof | grep “/path/to/file”

$ lsof | grep /root/selenium/Spider/MySql.Data.dll in this example

File status will show (deleted) at the end

progname 6210 user 20r REG 8,1 16791251 265368 /path/to/file (deleted)

/proc/6210/fd:Folder of process descriptor

20:File descriptor

Take a note of the PID in the second column and the descriptor number in the forth column, using following command and these information to recover the file

$ cp /proc/6210/fd/20 /path/to/restored/file

Turn off display automatically on Ubuntu 18.04 server with text boot

Open /etc/default/grub in any text editor (e.g. sudo nano, sudo vi etc.)

Add consoleblank=0 to GRUB_CMDLINE_LINUX_DEFAULT= as parameter.

e.g.

If it is GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" change to GRUB_CMDLINE_LINUX_DEFAULT="quiet splash consoleblank=0"

If it is GRUB_CMDLINE_LINUX_DEFAULT="text" change to GRUB_CMDLINE_LINUX_DEFAULT="text consoleblank=0"

Finally, do sudo update-grub then reboot.

Note: If you already boot from text mode, chance is you will have GRUB_CMDLINE_LINUX_DEFAULT=”text”

What does this change do?

Without consoleblank=0 after the server boot into text mode, after timeout, the screen will go blank with screen powered on (Dimmed screen), adding consoleblank=0 at the end of GRUB_CMDLINE_LINUX_DEFAULT=, will actually turn off the display after timeout instead of dim the display.

Alternatively, if you are running server on a laptop, the easiest way is to close the lid and still keep the server running, refer to this post: How to: Keep Ubuntu Server running on laptop with lid closed

How to: Ubuntu switch php-fpm version

Install newer php-fpm version e.g. 7.3

1. sudo apt install php7.3-fpm

2. sudo a2enconf php7.3-fpm

Notes:

a2enconf is a script that enables the specified configuration file within the apache2 configuration. It does this by creating symlinks within /etc/apache2/conf-enabled. Likewise, a2disconf disables a specific configuration part by removing those symlinks. It is not an error to enable a configuration which is already enabled, or to disable one which is already disabled.

Ubuntu Manual

a2enmod is a script that enables the specified module within the apache2 configuration. It does this by creating symlinks within /etc/apache2/mods-enabled. Likewise, a2dismod disables a module by removing those symlinks. It is not an error to enable a module which is already enabled, or to disable one which is already disabled.    – Ubuntu Manual