X11 forwarding after sudo

centos
$ xauth list
<hostname>/unix:0 MIT-MAGIC-COOKIE-1 1fb2c048e3a32a3fe013c565b8422727
<hostname>/unix:12 MIT-MAGIC-COOKIE-1 29b9ba4a72136095e43540119c8da384
<hostname>/unix:11 MIT-MAGIC-COOKIE-1 99b08ccb7e8a76e75c70d7c75723505d
<hostname>/unix:10 MIT-MAGIC-COOKIE-1 b2ea07f58365b8672bea7497ffacee4f

Then execute, change user as root with sudo -i and execute xauth add followed by one entry from above output that having the highest number.

$ sudo -i
# xauth add <hostname>/unix:12 MIT-MAGIC-COOKIE-1 29b9ba4a72136095e43540119c8da384

Explanation

  1. xauth list being executed before sudoing to root to output the xauth authorization hex key
  2. after sudo as root, xauth add to add the authorization hex key from non-root into root