<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MacOS &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/macos/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Tue, 24 Sep 2024 18:14:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>MacOS &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>MSAL: Caching Access Token</title>
		<link>https://myshittycode.com/2024/09/24/msal-caching-access-token/</link>
					<comments>https://myshittycode.com/2024/09/24/msal-caching-access-token/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 24 Sep 2024 18:13:59 +0000</pubDate>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[MSAL]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=2701</guid>

					<description><![CDATA[<p>Why Cache Access Token with MSAL? Building upon the previous post that performs delegated access authentication with MSAL, suppose your program uses this function to get the access token. In this case, it will always launch the browser to complete the authentication flow and retrieve the access token every time the program runs. While it [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/24/msal-caching-access-token/">MSAL: Caching Access Token</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#why-cache-access-token-with-msal">Why Cache Access Token with MSAL?</a></li><li><a href="#solution-a-use-custom-persistence-cache">Solution A: Use custom persistence cache</a></li><li><a href="#solution-b-use-the-os-platforms-encrypted-vault-preferred">Solution B: Use the OS platform&#8217;s encrypted vault (PREFERRED)</a></li></ul></nav></div>



<h2 class="wp-block-heading" id="why-cache-access-token-with-msal">Why Cache Access Token with MSAL?</h2>



<p>Building upon <a href="https://myshittycode.com/2024/09/23/msal-delegated-access-authentication/" data-type="post" data-id="2678">the previous post</a> that performs delegated access authentication with MSAL, suppose your program uses this function to get the access token.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
def get_access_token():
    app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
    result = app.acquire_token_interactive(scopes=SCOPES)
    access_token = result&#x5B;&#039;access_token&#039;]

    return access_token
</pre></div>


<p>In this case, it will always launch the browser to complete the authentication flow and retrieve the access token every time the program runs.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1650" height="1096" src="https://myshittycode.com/wp-content/uploads/2024/09/msal-browser.png?x45560" alt="" class="wp-image-2703" srcset="https://myshittycode.com/wp-content/uploads/2024/09/msal-browser.png 1650w, https://myshittycode.com/wp-content/uploads/2024/09/msal-browser-300x199.png 300w, https://myshittycode.com/wp-content/uploads/2024/09/msal-browser-1024x680.png 1024w, https://myshittycode.com/wp-content/uploads/2024/09/msal-browser-768x510.png 768w, https://myshittycode.com/wp-content/uploads/2024/09/msal-browser-1536x1020.png 1536w" sizes="(max-width: 1650px) 100vw, 1650px" /></figure>



<p>While it works, it introduces a slight latency to your program. Fortunately, MSAL provides the capability to cache the access token.</p>



<h2 class="wp-block-heading" id="solution-a-use-custom-persistence-cache">Solution A: Use custom persistence cache</h2>



<p>In this solution, we can introduce our custom persistence cache.</p>



<p>First, we can use these functions to load the access token from the cache file and save it to the cache file.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import os
import msal

...
CACHE_FILE = &#039;msal_token_cache.json&#039;

def load_cache():
    cache = msal.SerializableTokenCache()

    if os.path.exists(CACHE_FILE):
        with open(CACHE_FILE, &#039;rb&#039;) as f:
            cache.deserialize(f.read())

    return cache

def save_cache(cache):
    if cache.has_state_changed:
        with open(CACHE_FILE, &#039;wb&#039;) as f:
            f.write(cache.serialize().encode())
</pre></div>


<p>We can first attempt to get a valid access token from the cache. It will only get the token interactively via the browser if the access token doesn&#8217;t exist or has expired.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; highlight: [2,6,8,9,10,11,12,13,14,17]; title: ; notranslate">
def get_access_token():
    cache = load_cache()
    app = msal.PublicClientApplication(
        CLIENT_ID,
        authority=AUTHORITY,
        token_cache=cache,
    )
    result = None
    accounts = app.get_accounts()

    if accounts:
        result = app.acquire_token_silent(SCOPES, account=accounts&#x5B;0])

    if not result:
        result = app.acquire_token_interactive(scopes=SCOPES)

    save_cache(cache)
    access_token = result&#x5B;&#039;access_token&#039;]

    return access_token
</pre></div>


<p>While this solution works, the implementation code is very lengthy. Furthermore, the access token is stored in the cache file as clear text.</p>



<h2 class="wp-block-heading" id="solution-b-use-the-os-platforms-encrypted-vault-preferred">Solution B: Use the OS platform&#8217;s encrypted vault (PREFERRED)</h2>



<p>A Python package called <a href="https://github.com/AzureAD/microsoft-authentication-extensions-for-python" target="_blank" rel="noopener">msal-extensions</a> allows the access token to be stored securely in the OS platform&#8217;s encrypted vault. For example, If the program runs on MacOS, KeyChain will be used. Windows uses DPAPI, and Linux uses LibSecret.</p>



<p>To begin, install that Python package.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; highlight: [4]; title: ; notranslate">
# requirements.txt

msal
msal-extensions
</pre></div>


<p>The configuration is much simpler than Solution A.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; highlight: [2,5,11]; title: ; notranslate">
import msal
from msal_extensions import build_encrypted_persistence, PersistedTokenCache

...
CACHE_FILE = &#039;msal_token_cache.json&#039;

def get_access_token():
    app = msal.PublicClientApplication(
        CLIENT_ID,
        authority=AUTHORITY,
        token_cache=PersistedTokenCache(build_encrypted_persistence(CACHE_FILE)),
    )

    result = None
    accounts = app.get_accounts()

    if accounts:
        result = app.acquire_token_silent(SCOPES, account=accounts&#x5B;0])

    if not result:
        result = app.acquire_token_interactive(scopes=SCOPES)

    access_token = result&#x5B;&#039;access_token&#039;]

    return access_token
</pre></div>


<p>When running the program on MacOS, the access token is stored in the KeyChain.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="1940" height="1068" src="https://myshittycode.com/wp-content/uploads/2024/09/msal-keychain.png?x45560" alt="" class="wp-image-2704" srcset="https://myshittycode.com/wp-content/uploads/2024/09/msal-keychain.png 1940w, https://myshittycode.com/wp-content/uploads/2024/09/msal-keychain-300x165.png 300w, https://myshittycode.com/wp-content/uploads/2024/09/msal-keychain-1024x564.png 1024w, https://myshittycode.com/wp-content/uploads/2024/09/msal-keychain-768x423.png 768w, https://myshittycode.com/wp-content/uploads/2024/09/msal-keychain-1536x846.png 1536w" sizes="(max-width: 1940px) 100vw, 1940px" /></figure>



<p><strong>Note:</strong> An empty cache file named msal_token_cache.json has also been created. Unfortunately, there is no option in <code>msal-extensions==1.2.0</code> to disable the creation of this file at the moment.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/24/msal-caching-access-token/">MSAL: Caching Access Token</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2024/09/24/msal-caching-access-token/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2701</post-id>	</item>
		<item>
		<title>MSAL: Launching Specific Browser with Delegated Access Authentication</title>
		<link>https://myshittycode.com/2024/09/24/msal-launching-specific-browser-with-delegated-access-authentication/</link>
					<comments>https://myshittycode.com/2024/09/24/msal-launching-specific-browser-with-delegated-access-authentication/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 24 Sep 2024 15:16:59 +0000</pubDate>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[MSAL]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=2689</guid>

					<description><![CDATA[<p>Why Launch a Specific Browser with MSAL? Building upon the previous post that performs delegated access authentication with MSAL, your institution may sometimes allow you to access Microsoft 365 products only on the approved browsers. By default, MSAL launches the default browser on your machine to obtain the access token interactively. If your institution only [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/24/msal-launching-specific-browser-with-delegated-access-authentication/">MSAL: Launching Specific Browser with Delegated Access Authentication</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#why-launch-a-specific-browser-with-msal">Why Launch a Specific Browser with MSAL?</a></li><li><a href="#overriding-with-browser-environment-variable">Overriding with BROWSER environment variable</a></li><li><a href="#what-if-the-browser-environment-variables-predefined-value-doesnt-work">What if the BROWSER environment variable&#8217;s predefined value doesn&#8217;t work?</a></li></ul></nav></div>



<h2 class="wp-block-heading" id="why-launch-a-specific-browser-with-msal">Why Launch a Specific Browser with MSAL?</h2>



<p>Building upon <a href="https://myshittycode.com/2024/09/23/msal-delegated-access-authentication/" data-type="post" data-id="2678">the previous post</a> that performs delegated access authentication with MSAL, your institution may sometimes allow you to access Microsoft 365 products only on the approved browsers.</p>



<p>By default, MSAL launches the default browser on your machine to obtain the access token interactively. </p>



<p>If your institution only allows Chrome and your default browser is Firefox, you will get the following message:</p>



<figure class="wp-block-image aligncenter size-full"><img decoding="async" width="774" height="690" src="https://myshittycode.com/wp-content/uploads/2024/09/firefox-blocked.png?x45560" alt="msal" class="wp-image-2693" srcset="https://myshittycode.com/wp-content/uploads/2024/09/firefox-blocked.png 774w, https://myshittycode.com/wp-content/uploads/2024/09/firefox-blocked-300x267.png 300w, https://myshittycode.com/wp-content/uploads/2024/09/firefox-blocked-768x685.png 768w" sizes="(max-width: 774px) 100vw, 774px" /></figure>



<h2 class="wp-block-heading" id="overriding-with-browser-environment-variable">Overriding with BROWSER environment variable</h2>



<p>MSAL relies on Python&#8217;s built-in module called <a href="https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/331c16fdd803df6a629cb215a6372dbad93f0ba8/msal/oauth2cli/authcode.py#L68" target="_blank" rel="noopener">webbrowser</a> to launch the browser. This allows us to change the browser by setting the <a href="https://docs.python.org/3/library/webbrowser.html" target="_blank" rel="noopener">BROWSER environment variable</a> to the predefined values (&#8216;firefox&#8217;, &#8216;chrome&#8217;, etc.)</p>



<p>For example, to launch Firefox instead of the default browser, we can do this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; highlight: [5]; title: ; notranslate">
import os
import msal

# ... 
os.environ&#x5B;&#039;BROWSER&#039;] = &#039;firefox&#039;
app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
result = app.acquire_token_interactive(scopes=SCOPES)
</pre></div>


<h2 class="wp-block-heading" id="what-if-the-browser-environment-variables-predefined-value-doesnt-work">What if the BROWSER environment variable&#8217;s predefined value doesn&#8217;t work?</h2>



<p>I discovered that Python&#8217;s webbrowser failed to launch the correct browser, especially when dealing with Chromnium-based browsers.</p>



<p>For example, suppose my default browser is Brave, and I set the BROWSER environment variable to Chrome. In that case, it will still launch the Brave browser.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; highlight: [3]; title: ; notranslate">
# default browser = Brave

os.environ&#x5B;&#039;BROWSER&#039;] = &#039;chrome&#039;  # doesn&#039;t work
app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
result = app.acquire_token_interactive(scopes=SCOPES)
</pre></div>


<p>Fortunately, it is possible to <a href="https://github.com/AzureAD/microsoft-authentication-library-for-python/discussions/750#discussioncomment-10710600" target="_blank" rel="noopener">specify a full path ending with &#8216;%s&#8217;</a> instead:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; highlight: [3]; title: ; notranslate">
# default browser = Brave

os.environ&#x5B;&#039;BROWSER&#039;] = &#039;open -a /Applications/Google\\ Chrome.app %s&#039;  # works
app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
result = app.acquire_token_interactive(scopes=SCOPES)
</pre></div>


<p>If you are stuck on the &#8220;Approve sign in request&#8221; or<em> </em>&#8220;Set up your device to get access&#8221; screen indefinitely, consider clearing the browser cache and then try again.</p>



<p></p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/24/msal-launching-specific-browser-with-delegated-access-authentication/">MSAL: Launching Specific Browser with Delegated Access Authentication</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2024/09/24/msal-launching-specific-browser-with-delegated-access-authentication/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2689</post-id>	</item>
		<item>
		<title>Mac: Managing Startup/Shutdown Schedules on macOS Ventura</title>
		<link>https://myshittycode.com/2023/04/19/mac-managing-startup-shutdown-schedules-on-macos-ventura/</link>
					<comments>https://myshittycode.com/2023/04/19/mac-managing-startup-shutdown-schedules-on-macos-ventura/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 19 Apr 2023 14:09:56 +0000</pubDate>
				<category><![CDATA[Operating System]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[power management]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=2291</guid>

					<description><![CDATA[<p>PROBLEM Since upgrading to macOS Ventura, managing custom startup/shutdown schedules directly from the GUI (via Energy Saver) is no longer possible. SOLUTION While this feature is not accessible from the GUI anymore, it can still be accomplished using pmset command. To list all the existing schedules: By default, it is empty. To schedule a daily [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2023/04/19/mac-managing-startup-shutdown-schedules-on-macos-ventura/">Mac: Managing Startup/Shutdown Schedules on macOS Ventura</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Since upgrading to macOS Ventura, managing custom startup/shutdown schedules directly from the GUI (via Energy Saver) is no longer possible.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>While this feature is not accessible from the GUI anymore, it can still be accomplished using <strong>pmset</strong> command.</p>



<p>To list all the existing schedules:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
pmset -g sched
</pre></div>


<p>By default, it is empty.</p>



<p>To schedule a daily shutdown at 11 PM:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo pmset repeat shutdown MTWRFSU 23:00:00
</pre></div>


<p>While you can achieve a similar outcome by creating a cron job with <strong>shutdown -h now</strong>, you can pull off various power management actions using <strong>pmset</strong>, such as shutting down the machine when the UPS emergency is reached or control when the disk/screen should wake up or sleep. </p>



<p>To configure the machine to shut down at 11 PM daily and power on  at 1 AM Sunday:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo pmset repeat shutdown MTWRFSU 23:00:00 wakeorpoweron S 1:00:00
</pre></div>


<p>To show the new schedules:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&gt; pmset -g sched                                                                                                
Repeating power events:
  wakepoweron at 1:00AM Saturday
  shutdown at 11:00PM every day
</pre></div>


<p>If you are as forgetful as me, the above command ensures the machine doesn&#8217;t run constantly, and it wakes up at 1 AM every Sunday to perform the Time Machine backup to another server.</p>



<p>To learn more, <a href="http://x-man-page://pmset" data-type="URL" data-id="x-man-page://pmset">view <strong>pmset</strong> manual</a>.</p>



<p></p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2023/04/19/mac-managing-startup-shutdown-schedules-on-macos-ventura/">Mac: Managing Startup/Shutdown Schedules on macOS Ventura</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2023/04/19/mac-managing-startup-shutdown-schedules-on-macos-ventura/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2291</post-id>	</item>
		<item>
		<title>Wildcard Subdomains in /etc/hosts</title>
		<link>https://myshittycode.com/2022/03/29/wildcard-subdomains-in-etc-hosts/</link>
					<comments>https://myshittycode.com/2022/03/29/wildcard-subdomains-in-etc-hosts/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 29 Mar 2022 20:09:10 +0000</pubDate>
				<category><![CDATA[Operating System]]></category>
		<category><![CDATA[DNS]]></category>
		<category><![CDATA[dnsmasq]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[resolver]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=1409</guid>

					<description><![CDATA[<p>This post illustrates how you use a DNS forwarder to manage wildcard subdomains so that you don&#8217;t have to explicitly list each subdomain in /etc/host file. PROBLEM When trying to map multiple subdomains (ex: a.localhost, b.localhost, c.localhost, d.localhost) to the same IP, it is not possible to do the following in /etc/hosts: Rather, each subdomain [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2022/03/29/wildcard-subdomains-in-etc-hosts/">Wildcard Subdomains in /etc/hosts</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This post illustrates how you use a DNS forwarder to manage wildcard subdomains so that you don&#8217;t have to explicitly list each subdomain in <strong>/etc/host</strong> file.</p>



<h2 class="wp-block-heading">PROBLEM</h2>



<p>When trying to map multiple subdomains (ex: a.localhost, b.localhost, c.localhost, d.localhost) to the same IP, it is not possible to do the following in <strong>/etc/hosts</strong>:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
# /etc/hosts

1.2.3.4 *.localhost
</pre></div>


<p>Rather, each subdomain has to be explicitly defined:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
# /etc/hosts

1.2.3.4 a.localhost b.localhost c.localhost d.localhost
</pre></div>


<p>It requires you to babysit and manage these wildcard subdomains over time, but you do have a good job security.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<h3 class="wp-block-heading">Configuration</h3>



<p>Install a DNS forwarder using <a href="https://brew.sh/" target="_blank" rel="noopener">Homebrew</a>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
brew install dnsmasq
</pre></div>


<p>Create a configuration to map the wildcard subdomains to the same IP.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo bash -c \
  &#039;echo &quot;address=/localhost/1.2.3.4&quot; &gt; /usr/local/etc/dnsmasq.d/localhost.conf&#039;
</pre></div>


<p>Restart the service.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo brew services restart dnsmasq
</pre></div>


<p>Create <strong>/etc/resolver</strong> directory.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo mkdir -p /etc/resolver
</pre></div>


<p>Create a custom DNS resolver where the file name is the domain name.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo bash -c \
  &#039;echo &quot;nameserver 127.0.0.1&quot; &gt; /etc/resolver/localhost&#039;
</pre></div>


<h3 class="wp-block-heading">Verification</h3>



<p>Flush the DNS cache first.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo killall -HUP mDNSResponder
</pre></div>


<p>Verify that ping command on each subdomain resolves to the correct IP.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
$ ping -c 1 a.localhost
PING a.localhost (1.2.3.4): 56 data bytes

$ ping -c 1 b.localhost
PING b.localhost (1.2.3.4): 56 data bytes

$ ping -c 1 a.b.c.localhost
PING a.b.c.localhost (1.2.3.4): 56 data bytes
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2022/03/29/wildcard-subdomains-in-etc-hosts/">Wildcard Subdomains in /etc/hosts</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2022/03/29/wildcard-subdomains-in-etc-hosts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1409</post-id>	</item>
		<item>
		<title>GCP: Accessing GUI-Based App in GCE from Mac using X11</title>
		<link>https://myshittycode.com/2022/02/23/gcp-accessing-gui-based-software-in-gce-from-mac-using-x11/</link>
					<comments>https://myshittycode.com/2022/02/23/gcp-accessing-gui-based-software-in-gce-from-mac-using-x11/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 24 Feb 2022 00:59:49 +0000</pubDate>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Google Cloud Platform]]></category>
		<category><![CDATA[Google Compute Engine]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[x11]]></category>
		<category><![CDATA[xquartz]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=1330</guid>

					<description><![CDATA[<p>PROBLEM You want to access a GUI-based software that is installed in a GCE instance without using NoMachine. SOLUTION GCE Instance (One Time Configuration) Ensure X11Forwarding is enabled and set to yes. If not, change it. If a change is made to this file, restart the service. Mac (One Time Configuration) Apple no longer include [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2022/02/23/gcp-accessing-gui-based-software-in-gce-from-mac-using-x11/">GCP: Accessing GUI-Based App in GCE from Mac using X11</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading" id="problem">PROBLEM</h2>



<p>You want to access a GUI-based software that is installed in a GCE instance without using NoMachine.</p>



<h2 class="wp-block-heading" id="solution">SOLUTION</h2>



<h3 class="wp-block-heading" id="gce-instance-one-time-configuration">GCE Instance (One Time Configuration)</h3>



<p>Ensure <strong>X11Forwarding</strong> is enabled and set to <strong>yes</strong>. If not, change it.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
grep X11Forwarding /etc/ssh/sshd_config
</pre></div>


<p>If a change is made to this file, restart the service.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo systemctl restart sshd
</pre></div>


<h3 class="wp-block-heading" id="mac-one-time-configuration">Mac (One Time Configuration)</h3>



<p>Apple no longer include X11 since OS X 10.8 Mountain Lion. So, you need to install XQuartz, which is an open source version of X.</p>



<p>Using <a href="https://brew.sh/" target="_blank" rel="noopener">Homebrew</a>, install XQuartz.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
brew install xquartz
</pre></div>


<p>Reboot the machine. If you don&#8217;t reboot, it will not work.</p>



<p>Once rebooted, add the following line to <strong>~/.ssh/config</strong>. If this file does not exist, create it.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
ForwardX11 yes
</pre></div>


<p>This configuration prevents us from explicitly passing the <strong>-X</strong> option to the SSH command.</p>



<h3 class="wp-block-heading" id="accessing-gui-based-software-from-mac">Accessing GUI-based software from Mac </h3>



<p>Log into GCP.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
gcloud auth login
</pre></div>


<p>SSH into the GCE instance.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
gcloud compute ssh &#x5B;INSTANCE] --project &#x5B;PROJECT] --zone &#x5B;ZONE]
</pre></div>


<p>Once you are in the GCE instance, run any GUI-based software from the command line. </p>



<figure class="wp-block-image size-large"><a href="https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2.png?x45560"><img loading="lazy" decoding="async" width="1974" height="2300" src="https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2.png?x45560" alt="" class="wp-image-1342" srcset="https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2.png 1974w, https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2-257x300.png 257w, https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2-879x1024.png 879w, https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2-768x895.png 768w, https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2-1318x1536.png 1318w, https://myshittycode.com/wp-content/uploads/2022/02/screen_shot_2022-02-23_at_6_39_50_pm-2-1758x2048.png 1758w" sizes="auto, (max-width: 1974px) 100vw, 1974px" /></a></figure>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2022/02/23/gcp-accessing-gui-based-software-in-gce-from-mac-using-x11/">GCP: Accessing GUI-Based App in GCE from Mac using X11</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2022/02/23/gcp-accessing-gui-based-software-in-gce-from-mac-using-x11/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1330</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-20 03:46:22 by W3 Total Cache
-->