Hostname and Name Service

source

Hostname

The purpose of assigning names to IP numbers is to make them easier for people to remember. In reality, an IP address identifies a network interface associated with a device such as a network card. Since each machine can have several network cards, and several interfaces on each card, one single computer can have several names in the domain name system.
Each machine is, however, identified by a main (or “canonical”) name, stored in the /etc/hostname file and communicated to the Linux kernel by initialization scripts through the hostname command. The current value is available in a virtual filesystem, and you can get it with the cat /proc/sys/kernel/hostname command.
Surprisingly, the domain name is not managed in the same way, but comes from the complete name of the machine, acquired through name resolution. You can change it in the /etc/hosts file; simply write a complete name for the machine there at the beginning of the list of names associated with the address of the machine, as in the following example:
127.0.0.1     localhost
192.168.0.1   arrakis.falcot.com arrakis

Name Resolution

The mechanism for name resolution in Linux is modular and can use various sources of information declared in the /etc/nsswitch.conf file. The entry that involves host name resolution is hosts. By default, it contains files dns, which means that the system consults the /etc/hosts file first, then DNS servers. NIS/NIS+ or LDAP servers are other possible sources.

Configuring DNS Servers

DNS (Domain Name Service) is a distributed and hierarchical service mapping names to IP addresses, and vice-versa. Specifically, it can turn a human-friendly name such as www.eyrolles.com into the actual IP address, 213.244.11.247.
To access DNS information, a DNS server must be available to relay requests. Falcot Corp has its own, but an individual user is more likely to use the DNS servers provided by their ISP.
The DNS servers to be used are indicated in the /etc/resolv.conf, one per line, with the nameserver keyword preceding an IP address, as in the following example:
nameserver 212.27.32.176
nameserver 212.27.32.177
nameserver 8.8.8.8
Note that the /etc/resolv.conf file may be handled automatically (and overwritten) when the network is managed by NetworkManager or configured via DHCP.

The /etc/hosts file

If there is no name server on the local network, it is still possible to establish a small table mapping IP addresses and machine hostnames in the /etc/hosts file, usually reserved for local network stations. The syntax of this file is very simple: each line indicates a specific IP address followed by the list of any associated names (the first being “completely qualified”, meaning it includes the domain name).
This file is available even during network outages or when DNS servers are unreachable, but will only really be useful when duplicated on all the machines on the network. The slightest alteration in correspondence will require the file to be updated everywhere. This is why /etc/hosts generally only contains the most important entries.
This file will be sufficient for a small network not connected to the Internet, but with 5 machines or more, it is recommended to install a proper DNS server.

Before the advent of a distributed domain name system; networked computers used local files to map hostnames to IP addresses. On Unix systems this file was named /etc/hosts or “the hosts file”. In those days, networks were small and managing a file with a handful of hosts was easy. However as the networks grew so did the methods of mapping hostnames and IP addresses.

In modern days with the internet totaling at somewhere around 246 million domain names (as of 2012) the hosts file has been replaced with a more scalable distributed DNS service. While the hosts file is not used to map all hostnames to IP’s these days it can still be quite useful if used properly and it can also be a source of trouble if not understood or used properly.

source

Times to use the /etc/hosts

A Local Network for Desktop Environments

Using the /etc/hosts file to give a human readable name to a local system within a desktop environment is perfectly reasonable. The hosts file is great to use in a home network or even in a small business environment where the machine count is only a handful of systems. As a side note the hosts file can be used to provide DNS for a local system but it can also be used to restrict accessto domains as well.

The best part of this is that the majority of Desktop Operating Systems have some sort of hosts file and they all follow roughly the same syntax, including Windows & OSX.

Critical services in a enterprise server environment

Let’s face it, sometimes local DNS systems can break. If you have servers that connect to critical systems via hostnames it may be wise to add the IP and hostname into the hosts file. This allows you to keep the connectivity to these services alive even during a local DNS outage.

A word of warning however, this extra security comes with a price. While it is easy to mistype an IP address into a DNS system it is just as easy to mistype that IP address into a hosts file. By managing these IP’s in hosts files it also adds the complexity of managing the hosts files and ensuring that they are 100% correct all of the time.

This method also only works if that critical system is always going to have the same IP address, if DNS load balancing is used to reroute traffic for this critical system than it is not advisable to add this to the hosts file as it would circumvent the DNS failover.

Specific Application Environments

Not every application environment has hundreds of servers that fill the same role, in enterprise environments it is quite common to see a smaller set of servers that are used for a specific application. That application may have a dedicated database server and/or dedicated web servers. In environments like this it is not uncommon to add each host in the environment into the hosts file. The same warnings apply in this environment as the one above, sometimes having hostnames go to DNS is the preferred method when the IP address of that system may change. However if the IP address is static and you do not trust your internal DNS system to be accurate or available, adding the IP into /etc/hosts is worth the effort.

As a note, when you choose to use the hosts file you also take the responsibility of having to manage this file and manage the accuracy of the file. This is easier with configuration management tools but even this can become troublesome if the IP’s of hosts change and the configuration is not kept up to date.

Times not to use the /etc/hosts file

Server environments at scale

Whenever managing a large set of servers thousands upon hundreds of thousands of systems. Using the /etc/hosts file to map systems can be quite difficult and should only be used to map to systems that are serviced from static IP addresses. In large scale environments however, a service is usually provided by multiple IP addresses and that load balancing is driven by an internal DNS system.

Adding a domain to /etc/hosts

Below I am going to show how to use the /etc/hosts file to locally point a domain to another IP. This can be used as a general example of how to add a domain or short hostname into the hosts file.

Our example domain

In our example we are going to use example.com which today resolves to 93.184.216.119.

$ ping -c 1 example.com
 PING example.com (93.184.216.119) 56(84) bytes of data.

Adding the host name & domain name to the /etc/hosts file

To add our example we will first need to open the hosts file with vi or your favorite editor.

$ sudo vi /etc/hosts

Append:

# IP Domain ShortHost
192.168.0.193 example.com example

Ensure nsswitch.conf is correct

Before we go off checking if the resolution is correct let’s ensure that the nsswitch.conf file is correct. The nsswitch.conf file controls in which order services will be consulted for name service lookups, in our case we are looking for the “hosts” service.

$ grep host /etc/nsswitch.conf
 hosts: files dns

This setting is based on order, if files is before dns than the system will consult the /etc/hosts file before checking DNS for name service requests. However, if DNS was before files than the domain lookup would always go to DNS first.

Validating the new resolution

As you can see from the ping command example.com now resolves to 192.168.0.193 and not 93.184.216.119.

$ ping -c 1 example.com
PING example.com (192.168.0.193) 56(84) bytes of data.

Don’t use host or nslookup commands

Using the host or nslookup command to validate the change will not work and will simply cause confusion. Both of these commands will consult DNS and ignore the settings in hosts and nsswitch.conf.

$ host example.com
 example.com has address 93.184.216.119

The fact that the host and nslookup command commands do not return the same result as the hosts file entry adds to the complication of using /etc/hosts. 3 months down the line when you are troubleshooting connectivity to example.com and the host command returns a valid result it will be easy to forget that you added it to the hosts file. My advice is, if you are going to use the hosts file make sure that the reason you are using it is valid and that it is the only way to accomplish what you need.

 

Whenever any applications performs DNS Lookup in Linux operating system it looks in both “/etc/hosts” and “/etc/resovl.conf” configuration files to resolve the DNS name.  In Linux for DNS lookup order it use “/etc/nsswitch.conf” file. There are two library libc and glibc  are used to perform this operations in Linux , by defining DNS Lookup orders.

What is DNS ?

DNS ( Domain Name System) is mechanism to make internet human friendly.  Computer communicate with each other using their IP addresses.  There are lots of IP address in internet world, and  It is very difficult to remember all IP address . To solve this issue and make more human friendly DNS invented. IP address map with host-name in DNS server.

When we enter a domain name into our browser like http://www.goole.com  computer find our nearest DNS server and ask what is the correct IP address for http://www.google.com. Than it returns the IP address to our system to communicate with http://www.google.com.

/etc/nsswitch.conf file

In /etc/nsswitch.conf file there is a database for DNS lookup called “hosts”.

#cat /etc/nsswitch.conf | grep "hosts"

hosts:      files dns

Above you can see DNS lookup refers to /etc/hosts/ and /etc/resolv.conf. First DNS lookup look into /etc/hosts file to resove name and than it will look into /etc/resolv.conf file to resolve the host-name.

Please note :- DNS lookup toots like nslookup and dig will ignore /etc/nsswitch.conf file and always refer to DNS server using /etc/resolv.conf.

/etc/hosts file

/etc/hosts file is the most important file in Linux operating system. It is a text file for name resolution.

# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.2  client01

192.168.0.3  client02 www.example.com

Both fields are separated by space or tab followed by IP and Host-name per line. Host-name contain only alphanumeric characters, minus sign (-) and period (.) .

Fields Explanations :

192.168.0.3  : IP address

client02      :  Host-name

http://www.example.com : Aliases of IP address

You can also see other entries in /etc/hosts file like 127.0.0.1 is loopback addresses. Which is pointed to “localhost” host-name.

/etc/resolv.conf file

This is the another important file in Linux operating system. It contains information that help to computer to convert domain name to its IP address . All process called resolving.

# cat /etc/resolv.conf

nameserver 173.204.4.5

nameserver 173.204.4.7

Above you can see “nameserver” directive which is pointed to the IP address of Name Server.

In my example 173.204.4.5 and 173.204.4.7 is my own Name Servers.

You can also use Public Name Server following below configuration.

# cat /etc/resolv.conf

nameserver 8.8.8.8
nameserver 8.8.4.4

nameserver 4.2.2.2

Server Shutdown and Reboot Time

 

Source

As a server administrator,you must know the date and time of server shut-down and rebooted. I had rebooted the server for checking this experiment and make the excellent practical article to understand by everybody easily.

1) Checking the last login details before rebooting the server ?

The below output is clearly shows, i had logged in server at 11:19 and still logged in.

root@server10001 [~]# last | more
root     pts/1        219.91.219.14    Tue Jan 28 11:19   still logged in

2) Server Reboot ?

I’m going to reboot the server to check the details indepth. The below output clealy shows the server went to down at 14:04.

root@server10001 [~]# reboot

Broadcast message from root@server10001.2daygeek.com
        (root) at 14:04 ...

The system is going down for reboot NOW!

3) Checking the details after reboot ?

See the below details, it will clearly shows all the details.
1) Last logged in at 11:19 before reboot the server and the user live upto (02:44 2 hours and 44 seconds).
2) Server rebooted at 14:05 to 14:06 (00:01 Totally it will take to boot exactly 1 mins)
3) Last logged in at 14:06 after reboot the server.

root@server10001 [~]#
login as: root
root@83.170.117.138's password:
Last login: Tue Jan 28 11:19:53 2014 from 219.91.219.14

root@server10001 [~]# last | more
root     pts/0        219.91.219.14    Tue Jan 28 14:06   still logged in - 3
reboot   system boot  2.6.32-279.11.1. Tue Jan 28 14:05 - 14:06  (00:01) - 2
root     pts/1        219.91.219.14    Tue Jan 28 11:19 - down   (02:44) - 1

4) How to check last reboot date and time ?

If you want to view last server reboot date and time, use the below command to view it. The below output is clearly shows the server rebooted on 28-Jan at 14:05 and current time 14:15 and live (00:10 10 mins) but its not given the year. The last command fetch the details from /var/log/wtmp and show it.

root@server10001 [~]# last reboot
reboot   system boot  2.6.32-279.11.1. Tue Jan 28 14:05 - 14:15  (00:10)
reboot   system boot  2.6.32-279.11.1. Thu Jun 20 04:02 - 14:04 (222+10:01)
reboot   system boot  2.6.32-279.11.1. Wed Jun 19 23:34 - 03:55  (04:21)
reboot   system boot  2.6.32-279.11.1. Fri Jun  7 02:27 - 03:55 (13+01:27)
reboot   system boot  2.6.32-279.11.1. Wed Jun  5 23:35 - 03:55 (14+04:20)
reboot   system boot  2.6.32-279.11.1. Wed May 22 23:34 - 03:55 (28+04:20)
reboot   system boot  2.6.32-279.11.1. Wed May  8 23:34 - 03:55 (42+04:20)
reboot   system boot  2.6.32-279.11.1. Wed Apr 24 23:34 - 03:55 (56+04:20)
reboot   system boot  2.6.32-279.11.1. Wed Apr 10 23:43 - 03:55 (70+04:12)
reboot   system boot  2.6.32-279.el6.x Sat Oct 20 12:21 - 03:55 (242+15:33)
reboot   system boot  2.6.32-279.el6.x Sat Oct 20 11:32 - 12:20  (00:48)
reboot   system boot  2.6.32-279.el6.x Sat Oct 20 09:54 - 09:59  (00:05)

wtmp begins Sat Oct 20 09:54:03 2012

xxx@xxx:~$ last reboot -F
reboot system boot 4.4.0-116-generi Sun Mar 18 22:34:30 2018 still running
reboot system boot 4.4.0-116-generi Mon Mar 12 00:15:08 2018 – Fri Mar 16 19:10:52 2018 (4+18:55)
reboot system boot 4.4.0-116-generi Fri Mar 9 09:15:05 2018 – Sat Mar 10 01:03:07 2018 (15:48)
reboot system boot 4.4.0-116-generi Fri Mar 9 01:49:11 2018 – Sat Mar 10 01:03:07 2018 (23:13)
reboot system boot 4.4.0-116-generi Fri Mar 9 00:53:04 2018 – Sat Mar 10 01:03:07 2018 (1+00:10)

wtmp begins Mon Mar 5 23:45:32 2018

5) How to check last reboot date and time with year ?

You can use who command to view the server reboot date, time with year. The below output is clearly shows the server rebooted on 28-Jan-2014 at 14:05.

root@server10001 [~]# who -b
         system boot  2014-01-28 14:05

6) How to check the last reboot details using top command ?

We can alternatively check the last reboot time using top command but its hard to calculate the exact date. The below output shows the server up 46 mins. So the server was rebooted today, before 46 mins but if it shows 201 days up, its very hard to calculate the date.

root@server10001 [~]# top
top - 14:51:53 up 46 min,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 153 total,   1 running, 152 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.3%us,  0.1%sy,  0.0%ni, 98.7%id,  0.9%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8032352k total,  1110588k used,  6921764k free,    32344k buffers
Swap:  4095992k total,        0k used,  4095992k free,   675504k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      20   0 19356 1532 1228 S  0.0  0.0   0:00.93 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.02 migration/0

7) How to check the last reboot details using uptime command ?

We can alternatively check the last reboot time using uptime command but its hard to calculate the exact date like top command.

Linux Date Command

The date command displays the current date and time. It can also be used to display or calculate a date in a format you specify. The super-user (root) can use it to set the system clock.

Use the Linux date Command

When used without options, the date command displays the current system date and time, including the day of the week, month, time, timezone, and year:

date
Thu Apr 13 10:04:04 EDT 2017

To operate on a specific date, you can provide one with the -d flag:

date -d "1974-01-04"
Fri Jan  4 00:00:00 EST 1974

date has many display formatting options. Provide date with the formatting string by prefixing it with a plus sign:

date +"Week number: %V Year: %y"
Week number: 33 Year: 10

The format string is then output with each formatting token substituted by its value. %V is the formatting option to display the current week number, and %y represents the last two digits of the year.

Formatting Options

Run date --help to display a list of formatting options.

Here’s a small sample of the formatting tokens date supports:

Token Output
%a locale’s abbreviated weekday name (e.g., Sun)
%A locale’s full weekday name (e.g., Sunday)
%b locale’s abbreviated month name (e.g., Jan)
%B locale’s full month name (e.g., January)
%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
%F full date; same as %Y-%m-%d
%s seconds since 1970-01-01 00:00:00 UTC

For more details, run man date to view the entire man page.

Override the Timezone

By default, date uses the timezone defined in /etc/localtime. The environment variable TZ can be used to override this behavior. For example:

$ TZ=GMT date
Fri Aug 20 15:15:36 GMT 2010

$ TZ=America/Toronto date
Thu Mar 15 00:29:22 EDT 2018

Valid timezones are defined in /usr/share/zoneinfo/, which is installed by tzdata.

https://www.iana.org/time-zones

Examples

The following examples illustrate how you can use the date command to find the date and time at various points in time.

$ date -d now
Wed Aug 18 16:47:31 EDT 2010

$ date -d today
Wed Aug 18 16:47:32 EDT 2010

$ date -d yesterday
Tue Aug 17 16:47:33 EDT 2010

$ date -d tomorrow
Thu Aug 19 16:46:34 EDT 2010

$ date -d sunday
Sun Aug 22 00:00:00 EDT 2010

$ date -d last-sunday
Sun Aug 15 00:00:00 EDT 2010

Other valid date time strings include: last-weeknext-weeklast-monthnext-monthlast-year, and next-year.

Seconds from epoch

date has other surprising uses. For example, it can be used to convert a given date/time to Unix epoch time (seconds since 00:00:00, Jan 1, 1970) and back. The following example will show you the seconds from epoch to the current time:

$ date +%s
1282163756

Seconds from epoch to the provided date/time

$ date -d "1974-01-04" +"%s"
126507600

Convert epoch to a date

$ date -d "UTC 1970-01-01 126507600 secs"
Fri Jan  4 00:00:00 EST 1974

$ date -d @126507600
Fri Jan  4 00:00:00 EST 1974

Determine which day of the week a given date was

$ date -d "1974-01-04" +"%A"
Friday

Use date in Scripts and Commands

You can assign the output of date to a shell variable and then use it later in your scripts. For instance:

$ STARTTIME=`date`
$ echo $STARTTIME
Fri Aug 20 11:46:48 EDT 2010
$ sleep 5
$ echo $STARTTIME
Fri Aug 20 11:46:48 EDT 2010

You can also use date to create filenames that contain the current day:

tar cfz /backup-`date +%F`.tar.gz /home/caker/

This would tar and gzip the files in /home/caker/ into a filename called backup-2010-08-20.tar.gz.

Set the Date Manually from the Linux Terminal

If your system is running ntpd, and you’ve set your timezone correctly, you shouldn’t have to change this setting. However, if you find you need to set the system clock manually, use the --setoption. In this example, we’re setting the date and time to 9:14pm on Thursday, April 13, 2017:

date --set="20170413 21:14"

RFC 1918

Request for Comment 1918 (RFC 1918), “Address Allocation for Private Internets,”is the Internet Engineering Task Force (IETF) memorandum on methods of assigning of private IP addresses on TCP/IP networks.

Along with NAT (network address tunneling), RFC 1918 facilitates expansion of the usable number of IP addresses available under IPV4, as a stopgap solution to prevent the exhaustion of public IPs available before the adoption of IPV6. It’s not necessary to register private IPs with a Regional Internet Registry (RIR), which simplifies setting up private networks.

RFC 1918 was used to create the standards by which networking equipment assigns IP addresses in a private network. A private network can use a single public IP address. The RFC reserves the following ranges of IP addresses that cannot be routed on the Internet:

  • 10.0.0.0 – 10.255.255.255 (10/8 prefix)
  • 172.16.0.0 – 172.31.255.255 (172.16/12 prefix)
  • 192.168.0.0 – 192.168.255.255 (192.168/16 prefix)

IP addresses within these ranges can be assigned within a private network; each address will be unique on that network but not outside of it. Private IP addresses can’t be communicated with directly by external computers because they are not globally unique and, as such, not addressable on the public Internet.

Computers on the inside of the network can communicate with the Internet through NAT.  NAT translates an IP address used within one network to a different IP address known within another network. Typically, a company maps its local inside network addresses to one or more global outside IP addresses and unmaps the global IP addresses on incoming packets back into local IP addresses.

Substring in Bash

Using the cut Command

Getting a substring from the original string using the terminal isn’t too difficult thanks to a built-in command meant for this purpose specifically. It works well for use either directly on the command line or for use within a shell script (a .sh file). The command I’m referring to is cut, which is a Bash command that takes a string and a few flags (-cN-M) as inputs and then outputs the resulting substring. Here is one example of the format of the command:

$ echo "STRING" | cut -cN-M

When you plug in the variables (both the string and the flags), Bash will return to you the characters in the string starting from index N and ending at M (with the characters at indexes N and M both included).

Let’s try out a few examples. In the following one we use the string “abcdefghi” and extract a substrings from it below:

$ echo "abcdefghi" | cut -c2-6
bcdef  

When you run the above command in a terminal, you will get “bcdef” as the result.

Specifying the character index isn’t the only way to extract a substring. You can also use the -d and -f flags to extract a string by specifying characters to split on. The -d flag lets you specify the delimiter to split on while -f lets you choose which substring of the split to choose. Keep in mind that the cut command is not 0-indexed, so the first item in the list starts at 1.

$ echo "STRING" | cut -d'C' -f I

In the example above, C is the character to split on and I is the index to choose.

Given this, let’s try another example. Suppose you have to extract a series of digits from the name of a directory. The format of the directory name might be something like “birthday-091216-pics”. In this example, there are a few characters before the digits we care about and a couple of characters after them. There are also dashes placed in between as indicated. We can easily tackle this problem with the cut command using the previous syntax we just introduced. Here’s how:

$ echo "birthday-091216-pics" | cut -d'-' -f 2
091216  

This splits the string in to an array ([“birthday”, “091216”, “pics”]), and then pics an item from that array to return (the 2nd item).

If instead you need to use this in a shell script your code may look something like this:

STR="birthday-091216-pics"  
SUBSTR=$(echo $STR | cut -d'-' -f 2)  
echo $SUBSTR  

When you run the commands above, you get “091216” as the output, just as before.

BIG-IP Edge Client

Task summary

Downloading the Linux command line client

You can download the BIG-IP® Edge command line client for Linux installer, as a gzipped .TAR file, and distribute it to clients for installation.
  1. On the Main tab, click Access Policy > Secure Connectivity > Client Downloads. A list of available client downloads displays.
  2. Click BIG-IP Edge Command Line Client for Linux. The file linux_sslvpn.tgz is downloaded to your local directory.
The Linux command line client is ready to be installed.

Installing the Linux command line client

You must download the file linux_sslvpn.tgz before you can install the command line client.
You can use various Linux client commands with the BIG-IP® Edge command line client for Linux.
  1. Extract the file linux_sslvpn.tgz to your local directory.
  2. Extract the file linux_sslvpn.tar to your local directory.
  3. Run the install script Install.sh under the root account.

The following text appears when installation is complete:

–> f5fpc is installed in /usr/local/bin–> Please check f5fpc –help command to get started

–> Uninstaller located in /usr/local/lib/F5Networks/uninstall_F5.sh

Importing a certificate to the local trust store

You can import an untrusted certificate to the local trust store and change it into a trusted certificate.
  1. Using operating system commands, place the certificate in any folder in the operating system. For example, /etc/certs.
  2. Change the directory. For example, cd /etc/certs.
  3. Type the command c_rehash ./.

The certificate is installed.

Note: Alternatively, instead of installing the certificate, you can specify the --cacert option to import a certificate to the local store.

Linux client commands

The following commands are supported by the Linux command line client. All commands that are invoked on the Linux command line client begin with the commandf5fpc.

 To start a VPN connection, type either of the following commands:
f5fpc ---host https://xxvpn.xxx.com --user USERNAME -x
–nocheck (-x) specifies that the trusted CA certiicate is not verified for trust at all.

To stop the VPN connection, type either of the following commands:

  • f5fpc --stop
  • f5fpc --o

To display the connection status, type either of the following commands:

  • f5fpc --info
  • f5fpc --i

To display the command line client help, type either of the following commands:

  • f5fpc --help
  • f5fpc --h

X11 Forwarding (gitk)

xxx@xxx:~xxx/xxx$ gitk
application-specific initialization failed: no display name and no $DISPLAY environment variable
Error in startup script: no display name and no $DISPLAY environment variable
while executing
“load /usr/lib/x86_64-linux-gnu/libtk8.6.so Tk”
(“package ifneeded Tk 8.6.5” script)
invoked from within
“package require Tk”
(file “/usr/bin/gitk” line 10)

  1. sudo vim .ssh/config

Add something like

Host xenial*
Port 22
ForwardX11 yes
ForwardAgent yes

2. You need to have the xauth program installed on remote system. The xauth command is used to edit and display the authorization information used in connecting to the X server. You also need $HOME/.Xauthority default authority file.

sudo apt-get install xauth

apt-daily.timer

Source

In Ubuntu 16.04 systemd run unattended-upgrades by apt.system.daily and not by cron.
This cause security update checks at 6:00 and at 18:00 plus random time till 12 hours.

cat /lib/systemd/system/apt-daily.timer
[Unit]
Description=Daily apt activities

[Timer]
OnCalendar=*-*-* 6,18:00
RandomizedDelaySec=12h
AccuracySec=1h
Persistent=true

[Install]
WantedBy=timers.target

This is not a good choice for production system.
If you want change time and delay create /etc/systemd/system/apt-daily.timer file like this:

vi /etc/systemd/system/apt-daily.timer 
[Unit]
Description=Daily apt activities

[Timer]
OnCalendar=*-*-* 6:00
RandomizedDelaySec=2h
AccuracySec=1h
Persistent=true

[Install]
WantedBy=timers.target

https://www.hiroom2.com/2016/05/18/ubuntu-16-04-auto-apt-update-and-apt-upgrade/
https://askubuntu.com/questions/824718/ubuntu-16-04-unattended-upgrades-runs-at-random-times