K
macOS 开发命令
创建于:2025-07-26 15:06:11
|
更新于:2026-05-26 10:08:00

本文收集mac常用文章。

系统设置

crontab

crontab 是 Linux 系统下的定时任务管理工具,macOS 系统下也有类似的功能。

crontab 文件位于: /var/at/tabs

crontab -e # 编辑定时任务
crontab -l # 查看定时任务
crontab -r # 删除定时任务

减少 dock-hover 延迟

defaults write http://com.apple.dock autohide-time-modifier -float 0.2
defaults write http://com.apple.dock autohide-delay -float 0
killall Dock

中英文切换延迟

hidutil property --set '{"CapsLockDelayOverride":0}'

网络相关命令

功能命令
查看本机IPifconfig | grep "inet " | grep -v 127.0.0.1
关闭网络服务networksetup -setairportpower en0 off
重置DNSsudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
重启网络服务sudo ifconfig en0 down && sudo ifconfig en0 up
查询网络端口对应的设备名称networksetup -listallhardwareports
网路连通性测试(telnet平替)nc -vz github.com 22
强制关闭 DNS 服务进程sudo killall -9 mDNSResponder
强制将指定的网络接口(en0)设置为通过 DHCP 自动获取 IP 地址sudo ipconfig set en0 DHCP
重置 DNS 为自动获取sudo networksetup -setdnsservers Wi-Fi Empty
查看当前 DNSnetworksetup -getdnsservers Wi-Fi

open-connect 连接失败

一般是缓存导致的,实在不行重启大法。

# 清除 DNS 缓存
sudo networksetup -setdnsservers Wi-Fi Empty
# 清除静态路由
sudo route delete 117.74.132.138
# 从 DHCP 获取了正确的 IP、网关和路由
sudo ipconfig set en0 DHCP
# 重新加载网络配置,刷新路由和网络状态
sudo killall -HUP configd

Shell相关命令

功能命令
查看当前shellecho $SHELL
查看当前shell的版本echo $SHELL_VERSION
查看当前shell的类型echo $SHELL_TYPE
同步时间sudo sntp -sS pool.ntp.org
下载最新的 macOS 安装程序softwareupdate --fetch-full-installer --full-installer-version 27.0

brew相关命令

功能命令
更新brewbrew update
更新brew包brew upgrade
清理brew缓存brew cleanup
查看依赖关系brew deps --installed
清理brew包缓存brew cleanup --prune=all
清理无用依赖brew autoremove

Git相关命令

功能命令
停止跟踪(保留本地文件)git rm --cached <file>
停止跟踪目录(保留本地文件)git rm -r --cached <dir>
彻底删除(暂存区+本地)git rm <file>
强制删除(文件有改动时)git rm -f <file>
只预览不执行git rm -n --cached <file>
文件不存在也不报错git rm --cached --ignore-unmatch <file>
取消暂存(不产生删除记录)git restore --staged <file>
取消暂存(旧写法)git reset HEAD <file>
忽略本地修改(仍跟踪)git update-index --skip-worktree <file>
恢复本地修改跟踪git update-index --no-skip-worktree <file>
临时忽略本地改动git update-index --assume-unchanged <file>
恢复临时忽略git update-index --no-assume-unchanged <file>
查看被忽略跟踪的文件git ls-files -v | grep ^S
查看暂存区状态git status
查看已跟踪文件列表git ls-files
查看某文件是否被跟踪git ls-files --error-unmatch <file>
提交停止跟踪的改动git commit -m "stop tracking <file>"

Github connnect error

git pull 时报错:

git pull
Connection closed by 127.0.0.1 port 6153
fatal: Could not read from remote repository.
 
Please make sure you have the correct access rights
and the repository exists.
 
# or
 
ssh: connect to host github.com port 22: Operation timed out
fatal: Could not read from remote repository.
 
Please make sure you have the correct access rights
and the repository exists.

这里本质上是当前网络环境无法访问 github 22 端口,可能是 22 端口被防火墙、网络运营商屏蔽了。

我们需要做的是,不在走 22 端口,而是走 443 端口。

是否被墙可以通过 telnet、nc 来判断: telnet github.com 22 或 nc -vz github.com 22

~/.ssh/config
Host github.com
Hostname ssh.github.com
Port 443

tar 相关命令

功能命令
压缩文件tar -czvf filename.tar.gz directory/
解压文件tar -xzvf filename.tar.gz
查看压缩文件内容tar -tzvf filename.tar.gz

涉及到大量文件压缩会很慢,可以考虑不压缩直接打包。

例如 node_modules,直接打包:

tar -cvf node_modules.tar node_modules

开启 sudo 的指纹认证

/etc/pam.d/sudo
+auth       sufficient     pam_tid.so
auth       required       pam_opendirectory.so
account    required       pam_opendirectory.so
password   required       pam_opendirectory.so
session    required       pam_opendirectory.so

npm 相关命令

功能命令
发布公开 npm 包npm publish --access public
登录 npm 账号npm login
查看全局 node_modules 安装目录npm root -g
查看当前登录的 npm 用户npm whoami
查看当前项目已安装依赖npm list
查看全局安装的包npm list -g --depth=0
查看包信息npm view <package>
设置 npm 源npm config set registry <registry-url>
查看 npm 缓存目录npm config get cache
清理 npm 缓存npm cache clean --force
我也是有底线的 🫠