# Steps for time synchronization
### Pre-Request
The way to connect client PC to controller PC:
1. connect two PCs with LAN cable
2. Check the IP address of controller PC.
In our case:
```
IP address: 192.168.0.30
```
3. On the client PC, set the IP address as following:
```
IP address: 192.168.0.31
Subnet mask: 255.255.255.0
Default gateway: 192.168.0.1
```
3. Open the WebUI browser on client PC with this link:
```
192.168.0.30/TRY_UI.html
```
On Ubuntu, ensure that you have these installed
* ntp
* ntpdate (for creating an NTP client)
## Set client PC as an NTP server
Server: Windows 10
Client: Ubuntu 18.04.5 LTS (bionic)
### On Windows
Create a NTP server
1. Press win+R and type "regedit" to open the Registry Editor.
2. Change these parameters under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\
```
Parameters\Type: REG_SZ NTP
Config\AnnounceFlags: REG_SZ 0x5
TimeProviders\NtpServer\Enabled: REG_DWORD 0x1
```
3. Turn on the firewall for NTP connection
Open control panel(win+R and then enter "control") -> Windows defender firewall -> Advanced settings.
Right click on Inbound Rules and select new rules.
Add UDP 123, which is the standard protocol of NTP service.
4. Open the PowerShell
```
w32tm /config /update
net stop w32time
net start w32time
```
### On Ubuntu
> Remember to turn off the NTP server if you have enabled it.
1. Open the /etc/hosts file and add the IP address and specify a hostname:
```
192.168.0.31 NTP-server-host
```
2. Check if the NTP client machine's time is synchronized with NTP server.
The output should ideally show a time offset between the two systems.
```
ntpdate NTP-server-host
```
3. Disable the systemd timesyncd service on the client.
Because we want our client to sync time with the NTP server, let us disable the timesyncd service on the client machine.
```
timedatectl set-ntp off
```
4. Open the /etc/ntp.conf file and add the following lines:
```
server NTP-server-host prefer iburst
```
5. Restart the NTP server to make the above changes available
```
service ntp restart
```
6. View the Time Synchronization Queue
You should be able to see NTP-server-host as the time synchronization host/source in the queue.
The server with * is the one which is connecting to.
```
ntpq -p
```
7. If there is no * in front of your hostname, check the condition by this command.
```
ntpq
> associations
> mrv assID assID
```
* if flash=400 peer_dist, add "tos maxdist 30" to /etc/ntp.conf before any lines starting with 'restrict' or 'server'.
## Set controller PC as an NTP server
Server: Ubuntu 18.04.5 LTS (bionic)
Client: Windows 10
### On Ubuntu
Run the following commands under root authority.
1. vim /etc/ntp.conf
make sure that these are in the config file.
```
# create a local NTP server
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
# trust the IP address of clients
restrict 192.168.0.31 mask 255.255.255.0 nomodify notrap
restrict 192.168.0.32 mask 255.255.255.0 nomodify notrap
# Not sure if these are neccessary
broadcastdelay 0.008
statistics loopstats peerstats clockstats
filegen loopstats file liipstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
# these should exist by default
driftfile /var/lib/ntp/ntp.drift
restrict 127.0.0.1 # trust ourselves
```
2. Start an NTP server
```
systemctl restart ntp
systemctl status ntp # check the NTP service is active or not
```
### On Windows
Create a NTP client
1. Press win+R and type "regedit" to open the Registry Editor.
2. Change these parameters under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\
```
Parameters\Type: REG_SZ NTP
Parameters\NtpServer: REG_SZ 192.168.0.30 # host ip
TimeProviders\NtpClient\Enabled: REG_DWORD 0x1
TimeProviders\NtpClient\CrossSiteSyncFlags: REG_DWORD 0x2
```
3. Open the PowerShell
Update the changes and restart the w32time.
```
w32tm /config /update
net stop w32time
net start w32time
```
4. Check the status of synchronization.
```
w32tm /query /peers
w32tm /query /status
```
## Reference
>[How to configure an authoritative time server in Windows Server](https://docs.microsoft.com/en-US/troubleshoot/windows-server/identity/configure-authoritative-time-server)
[How to Install NTP Server and Client(s) on Ubuntu 20.04 LTS](https://vitux.com/how-to-install-ntp-server-and-client-on-ubuntu/)
[How to Install NTP Server and Client on Ubuntu 18.04 LTS](https://www.howtoforge.com/tutorial/ubuntu-ntp-server/)