{"id":126,"date":"2025-05-11T16:50:35","date_gmt":"2025-05-11T16:50:35","guid":{"rendered":"https:\/\/haco.club\/?p=126"},"modified":"2025-05-11T16:50:35","modified_gmt":"2025-05-11T16:50:35","slug":"lima-ubuntu-vm-setup-on-mac","status":"publish","type":"post","link":"https:\/\/haco.club\/?p=126","title":{"rendered":"Lima Ubuntu VM setup on Mac"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Homebrew<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/gitee.com\/cunkai\/HomebrewCN\/blob\/master\/error.md\">https:\/\/gitee.com\/cunkai\/HomebrewCN\/blob\/master\/error.md<\/a><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Installation<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/bin\/zsh -c \"$(curl -fsSL https:\/\/gitee.com\/cunkai\/HomebrewCN\/raw\/master\/Homebrew.sh)\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Uninstallation<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/bin\/zsh -c \"$(curl -fsSL https:\/\/gitee.com\/cunkai\/HomebrewCN\/raw\/master\/HomebrewUninstall.sh)\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Lima<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/lima-vm.io\/\">Lima<\/a>\u00a0launches Linux virtual machines with automatic file sharing and port forwarding (similar to WSL2).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Install\nbrew install lima\n\n# Create VM\nlimactl create --cpus=8 --memory=8 --name=default template:\/\/ubuntu\nlimactl list\n\n# Start &amp;&amp; Enter\nlimactl start default\nlimactl shell default\n\n\n# Stop\nlimactl stop default\n# Delete\nlimactl delete default\n# Protect &amp;&amp; Unprotect\nlimactl protect default\nlimactl unprotect default\n\n# Copy\n# Host -> VM\nlimactl cp ~\/Downloads\/test.png default:\/tmp\n# VM -> Host\nlimactl cp default:\/tmp\/test.png ~\/Downloads<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Port Fowarding(SSH)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">eg. Forward traffic from 0.0.0.0:2222 to 127.0.0.1:60022<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Local Port Forwarding on the Same Machine<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># socat\nbrew install socat\nsocat TCP-LISTEN:2222,bind=0.0.0.0,reuseaddr,fork TCP:127.0.0.1:60022<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Remote Port Forwarding via SSH<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Access a service on the remote server that\u2019s only bound to\u00a0<code>127.0.0.1<\/code>\u00a0(not exposed to the internet).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -N -L 2222:127.0.0.1:60022 user@remote-server<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>-N<\/code><\/strong>: Tells SSH not to execute any remote commands (no shell session; just port forwarding).<\/li>\n\n\n\n<li><strong><code>-L 2222:127.0.0.1:60022<\/code><\/strong>: Sets up\u00a0<strong>local port forwarding<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Local Port<\/strong>:\u00a0<code>2222<\/code>\u00a0(on your machine).<\/li>\n\n\n\n<li><strong>Remote Target<\/strong>:\u00a0<code>127.0.0.1:60022<\/code>\u00a0(a service running on the remote server\u2019s localhost, accessible only from the server itself).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When you connect to\u00a0<code>localhost:2222<\/code>\u00a0on your machine, the traffic is encrypted and sent through the SSH tunnel to\u00a0<code>127.0.0.1:60022<\/code>\u00a0<strong>on the remote server<\/strong>.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\"><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Firewall Rules (Linux\/macOS)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For advanced setups, use firewall rules to forward traffic:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Linux (iptables)<\/strong>:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to-destination 127.0.0.1:60022\nsudo iptables -t nat -A OUTPUT -p tcp --dport 2222 -j DNAT --to-destination 127.0.0.1:60022<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>macOS (pfctl)<\/strong>:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Edit\u00a0<code>\/etc\/pf.conf<\/code> and Apply the rules:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\"><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>rdr pass on en0 proto tcp from any to any port 2222 -> 127.0.0.1 port 60022\n# Replace en0 with your network interface.\n\nsudo pfctl -f \/etc\/pf.conf\nsudo pfctl -e<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">VS Code Remote SSH<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>copy ~\/.lima\/_config\/user on Mac to ~\/.ssh\/lima-user on local machine<\/li>\n\n\n\n<li>chmod 600 ~\/.ssh\/lima-user<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>Host lima-default\n  IdentityFile \"\/home\/haco\/.ssh\/lima-user\"\n  StrictHostKeyChecking no\n  UserKnownHostsFile \/dev\/null\n  NoHostAuthenticationForLocalhost yes\n  GSSAPIAuthentication no\n  PreferredAuthentications publickey\n  Compression no\n  BatchMode yes\n  IdentitiesOnly yes\n  Ciphers \"^aes128-gcm@openssh.com,aes256-gcm@openssh.com\"\n  User haco\n  # ControlMaster auto\n  # ControlPath \"\/Users\/haco\/.lima\/default\/ssh.sock\"\n  # ControlPersist yes\n  Hostname 127.0.0.1\n  Port 2222<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Homebrew https:\/\/gitee.com\/cunkai\/HomebrewCN\/blob\/master\/error.md Installation Uninstallation Lima Lima\u00a0launches Linux virtual machines with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[19,20,10],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-tutotial","tag-lima","tag-mac","tag-toolchain"],"_links":{"self":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=126"}],"version-history":[{"count":1,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}