Skip to content

Instantly share code, notes, and snippets.

@ryuheechul
Last active January 27, 2023 04:45
Show Gist options
  • Save ryuheechul/3918366306f6b7f02c250dcb0cbee4ec to your computer and use it in GitHub Desktop.
Save ryuheechul/3918366306f6b7f02c250dcb0cbee4ec to your computer and use it in GitHub Desktop.
A note to enhance macOS experience

bootstrap

(almost) mouseless

key customizing

focus and break

terminal and editors

dev

text and font

Security

Enable Touch ID to replace password typing for sudo

SSH

Manage SSH Keys in the Secure Enclave

https://github.com/maxgoedjen/secretive

Prevent SSH Disconnection

both as a client or server

Turn this option on, Prevent automatic sleeping on power adapter when the display is off - https://apple.stackexchange.com/a/452273/368485

Multi Users

Screen Sharing to Another Account on Same Mac

Go to https://gist.github.com/ryuheechul/3918366306f6b7f02c250dcb0cbee4ec#file-self-vnc-md

Fast user switching

-- copied from https://apple.stackexchange.com/a/434000/368485 and edited

tell application "System Events"
    tell application process "Control Center"
        click (first menu bar item of menu bar 1 ¬
            whose value of attribute "AXIdentifier" is "com.apple.menuextra.user")
        click (first button of window "Control Center" whose name is "Target User Name")
    end tell
end tell
-- Copy and paste this into at Run AppleScript on Shortcuts and change the name for the target user

next> https://github.com/ryuheechul/my-gists

Screen Sharing to Another Account on Same Mac

After macOS Ventura for some reason, I experienced sluggish connection with non 5900 ports which is explained in this page. So I came up with a new way - https://github.com/ryuheechul/provision/tree/main/lima/screenshare which seems to work better.

Using SSH (simple)

discovered at https://youtu.be/WcytVLxoIkU from one terminal session, ssh localhost -L 5901:localhost:5900 from another, open 'vnc://localhost:5901'

https://apple.stackexchange.com/a/151160

#!/usr/bin/env bash

# let's say it's self-vnc.sh

target=5901
source=5900

bash -c "sleep 0.5; open 'vnc://localhost:${target}'" &
ncat --sh-exec "ncat 0.0.0.0 ${source}" -l "${target}" --keep-open

# and run `chmod +x self-vnc.sh; ./self-vnc.sh`
# `socat` equivalent if were to use `socat` instead of `ncat` - https://www.linuxshelltips.com/socat-command-examples/
socat TCP4-LISTEN:${target},fork,reuseaddr TCP4:localhost:${source}

More advanced example

Using the all method above we can create a function that can be sourced from either bash or zsh.

function _pf_w_ncat_or_ssh () {
    test -z "${target}" && echo '$target is not set' && exit 1
    test -z "${source}" && echo '$source is not set' && exit 1

    # favor `ncat` in case you have the binary installed
    command -v ncat >/dev/null && {
        echo '`ncat` detected; using `ncat`'
            ncat --sh-exec "ncat 0.0.0.0 ${source}" -l "${target}" --keep-open
    # fallback to `ssh` in case you don't have `ncat` installed
    } || {
        echo '`ncat` not detected; fallback to `ssh`'
        test -z "${host}" && echo '$host is not set' && exit 1

        echo "${target} port is being forwarded to ${host}:${source}"

        # `exec bash -c ...` is what "prevent to enter" the shell since that's not the reason that ssh is being used
        ssh "${host}" -L "${target}":localhost:"${source}" \
            "exec bash -c 'echo "CTRL + C to exit" && cat'"
    }
}

function self-vnc () {
    host=localhost # change this to something else especially if you have a convinient alias for localhost ~/.ssh/config
    target=5901 # whatever port you want to expose to
    source=5900 # this should not be changed

    # run this for the convinience
    bash -c "sleep 0.5; open 'vnc://localhost:${target}'" &

    # actual port-forwarding function
    _pf_w_ncat_or_ssh
}

# source these two functions from zsh or bash
# and run `self-vnc` to port-forward and open "Screen Sharing" app.

Another Way with Packet Filtering

The essence of it (without the launchctl)is like following.

  1. create the pf file. sudo touch /etc/pf-self-vnc.conf

  2. write the following to the file above (at least one line).

# 5901 to 5900
rdr pass inet proto tcp from 127.0.0.1 to 127.0.0.1 port 5901 -> 127.0.0.1 port 5900
# 5902 to 5900
rdr pass inet proto tcp from 127.0.0.1 to 127.0.0.1 port 5902 -> 127.0.0.1 port 5900
  1. run Packer Filter via the command, sudo pfctl -ef /etc/pf-self-vnc.conf
@ryuheechul
Copy link
Author

ryuheechul commented Feb 21, 2022

there is also a note for Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment