macOS

macOs

Tips

# 查看可用的分辨率
system_profiler SPDisplaysDataType |grep Resolution
xrandr
# 统计 Chrome 用的内存
ps aux | grep -i chrome | awk '{sum += $4} END {print 4*1024 * (sum/100)}'
# 类似于 lsusb
ioreg -p IOUSB -l -w 0
# lsusb
brew update && brew tap jlhonora/lsusb && brew install lsusb
# 更新软件
softwareupdate -i -w
# 查看 OS X 系统版本信息
sw_vers
# 打开屏保
open -a ScreenSaverEngine
# 锁屏
/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend
# 阅读邮件
mail
# 删除所有邮件
# delete *
# q
# 查看硬件网络端口
networksetup -listallhardwareports
# 查看隐藏文件,需要重启 finder
defaults write com.apple.finder AppleShowAllFiles YES
# 关闭 finder
killall Finder /System/Library/CoreServices/Finder.app
# 关闭
defaults write com.apple.finder AppleShowAllFiles NO

打开取色器

on run {input, parameters}
choose color
return input
end run

文件目录结构

/Library
/Screen Savers # 屏保程序

Brew

Brew 是 OS X 上必不可少的软件包管理器, 具体使用可参考这里

移动磁盘格式选择

如果想要将移动磁盘与 PC 和 Mac 共用,最好选择 ExtFAT,支持大文件(>4G), 都能被识别和读写,在 Mac 上格式化的时候,需要选择使用主引导记录而不是 GUID, 否则 Windows 识别不了.

启动选项

启动键描述
Command-R启动为恢复模式
Alt/Option访问启动管理器
C从 CD, DVD, 或 USB 启动
NNetBoot
Shift安全启动
Command-V详细模式
Command-S单用户模式
Command-Option-P-R重置 NVRAM
T启动目标磁盘模式

证书

sudo security add-trusted-cert -d -r trustRoot -k "/Library/Keychains/System.keychain" cert.cer
sudo security remove-trusted-cert -d cert.cer

Finder

  • Finder 的主要问题
    • 不能复制当前地址
    • 不能新建文件
    • 不能剪切
  • xtrafinder
    • XtraFinder add Tabs and features to Mac Finder.
  • FinderPath
    • 地址栏

Install Xcode

xcode-select --install

资源下载

终端启用换行

不换行会被截断

# 禁用换行
tput rmam
# 启用换行
tput smam

pmset

# 查看当前配置
pmset -g
# Active Profiles:
# Battery Power 1
# AC Power -1*
# Currently in use:
# standbydelay 10800 写入休眠镜像到磁盘之前和停止给内存供电的延迟,秒
# standbydelaylow 10800 写休眠镜像的延时,秒
# standbydelayhigh 86400 写休眠镜像的延时,秒
# standby 1 是否让电源管理器自动休眠系统.
# highstandbythreshold 50
# womp 1 是否启用网络唤醒
# halfdim 1 display sleep will use an intermediate half-brightness state between full brightness and fully off
# hibernatefile /var/vm/sleepimage 休眠时转储的文件
# proximitywake 1 相同 iCloud ID 设备接近时唤醒
# powernap 1 是否启用 Power Nap
# gpuswitch 2
# networkoversleep 0
# disksleep 0 磁盘睡眠定时器
# sleep 1 系统睡眠定时器,分钟,0 禁用
# autopoweroffdelay 14400 进入自动停止电源模式的延迟,秒
# hibernatemode 3 休眠模式,分钟,0 禁用
# autopoweroff 1 是否自动停止电源
# ttyskeepawake 1 当任何 tty 处于'激活'的时候都使系统不会进入睡眠.当 tty 的空闲时间超过系统睡眠时间后便不再处于 '激活' 状态.
# displaysleep 10 显示器睡眠定时器,分钟,0 禁用
# acwake 0 wake the machine when power source (AC/battery) is changed
# lidwake 1 wake the machine when the laptop lid (or clamshell) is opened
# autorestart 是否在断电后自动重启
# ring - wake on modem ring
# lessbright - slightly turn down display brightness when switching to this power source
# sms - use Sudden Motion Sensor to park disk heads on sudden changes in G force
# networkoversleep - this setting affects how OS X networking presents shared network services during system sleep. This setting is not used by all platforms; changing its value is unsupported.
# destroyfvkeyonstandby - Destroy File Vault Key when going to standby mode. By default File vault keys are retained even when system goes to standby. If the keys are destroyed, user will be prompted to enter the password while coming out of standby mode.(value: 1 - Destroy, 0 - Retain)
# 避免待机
caffeinate
# 或
pmset noidel
# 其他参数
# boot 重启系统
# force 强制 PM(Power Management) 立即激活设置.不讲设置写到磁盘,可以使设置能够很容易被重写.当在特殊场景下 PM 未运行时很有帮助.
# touch PM 从新从磁盘读取现有配置
# noidle 避免空闲时进入睡眠模式,该命令已被废弃,使用 caffeinate 替代
# sleepnow 立即使系统进入睡眠
# restoredefaults 恢复节能程序到默认值
# displaysleepnow 使显示器立即进入睡眠
# resetdisplayambientparams 重置指定显示器的环境光参数
# 修改时指定范围
# -b 电池
# -c 链接了电源, UPS ( -u )
# -u UPS
# -a 所有
  • 休眠模式
    • 0 不会将内存持久化到存储.在断电时内存数据会丢失.
    • 3 会将内存拷贝到存储,当睡眠时也会给内存供电,系统会尝试从内存启动,如果断电会强制从磁盘恢复.
      • 默认为该选项
    • 25 会将内存拷贝到存储,并且停止给内存供电,启动时会从磁盘恢复内存.
      • 省电,电池寿命更久
      • 但睡眠和唤醒更慢
  • 如果系统支持 standby, 则在超过 standbydely 后就会写一个休眠镜像
  • 如果要完全禁止休眠,可将 hibernatemode, standby 和 autopoweroff 设置为 0

csrutil

Modify the System Integrity Protection configuration. All configuration changes apply to the entire machine.
Available commands:
clear
Clear the existing configuration.
disable
Disable the protection on the machine. Only available in Recovery OS.
enable
Enable the protection on the machine. Only available in Recovery OS.
status
Display the current configuration.
netboot
add <address>
Insert a new IPv4 address in the list of allowed NetBoot sources.
list
Print the list of allowed NetBoot sources.
remove <address>
Remove an IPv4 address from the list of allowed NetBoot sources.

/System /sbin /bin /usr * /Applications **

  • /usr is protected with the exception of /usr/local subdirectory, which is often used by tools like Homebrew

** /Applications is protected for apps that are pre-installed with Mac OS (Calendar, Photos, Safari, Terminal, Console, App Store, Notes, etc)

log show --predicate 'eventMessage contains "Previous shutdown cause"' --last 24h

FAQ

未进入休眠

  • 在 Console.app 中搜索 PreventUserIdleSystemSleep
  • pmset -g 确认当前的配置信息
    • 时间
    • sleep 是否有 prevent 信息
      • 常见阻碍线程
        • sharingd
        • backupd
        • AddressBookSourceSync
# 当前系统状态
# 主要是 PreventUserIdleSystemSleep
pmset -g assertions
# 查看状态变更日志
# InternalPreventSleep 和 PreventUserIdleSystemSleep
pmset -g assertionslog
# 查看唤起原因
log show --style syslog --start '2019-05-27 17:50:00' | grep "Wake reason"

Shutdown the computer, wait 30 seconds, restart the computer.

Disconnect all third-party peripherals. Resetting your Mac’s PRAM and NVRAM Reset the System Management Controller (SMC) Reset your Startup Disk and Sound preferences. Start the computer in Safe Mode. Test in safe mode to see if the problem persists, then restart normally. Use Apple Hardware Test to see if there is any hardware malfunction.

Repair the disk by booting from the Recovery HD. Immediately after the chime hold down the Command and R keys until the Utility Menu appears. Choose Disk Utility and click on the Continue button. Select the indented (usually, Macintosh HD) volume entry from the side list. Click on the First Aid button in the toolbar. Wait for the Done button to appear. Quit Disk Utility and return to the Utility Menu. Restart the computer from the Apple Menu.

Repair permissions on the Home folder: Resolve issues caused by changing the permissions of items in your home folder.

reset NVRAM https://support.apple.com/en-us/HT204063

Option, Command, P, and R. You can release the keys after about 20 seconds, during which your Mac might appear to restart.

On Mac computers that play a startup sound, you can release the keys after the second startup sound. On Mac computers that have the Apple T2 Security Chip, you can release the keys after the Apple logo appears and disappears for the second time.

https://support.apple.com/zh-cn/HT201295

有 T2 芯片的 Mac 笔记本电脑 先尝试以下操作:

选取苹果菜单 >“关机”。 在 Mac 关机后,按住电源按钮 10 秒钟。 松开电源按钮,然后等待几秒钟。 再次按下电源按钮以开启 Mac。 如果上述操作无法解决问题,请按照以下步骤操作:

选取苹果菜单 >“关机”。 在 Mac 关机后,按住右 Shift 键、左 Option 键和左 Control 键 7 秒钟。然后,在按住电源按钮的同时继续按住这些按键 7 秒钟。 松开所有三个按键和电源按钮,然后等待几秒钟。 再次按下电源按钮以开启 Mac。

Diskutil

# 格式化为 FAT32
diskutil eraseDisk FAT32 NAME MBRFormat /dev/disk2

BasicIPv6ValidationError

# 先禁用掉对应网卡的 IPv6
# networksetup -setv6off Wi-Fi
networksetup -setv6off Ethernet
# 然后通过命令行修改配置
networksetup -setmanual Ethernet 192.168.31.2 255.255.255.0 192.168.1.1
# 设置完成后也可以将 IPv6 设置为自动

NTFS 不能写

OS X 因为版权原因不支持 NTFS 的写操作,需要安装第三方软件才能进行写操作.

  • Tuxera NTFS

使用 USB 以太网

需要安装 HoRNDIS 来支持

制作安装盘

当下载好安装器后可直接使用安装器里附带的程序制作安装包

# 替换 MyVolume 为实际的挂载盘
# High Sierra
sudo /Applications/Install\ macOS\ High\ Sierra.app/Contents/Resources/createinstallmedia --volume /Volumes/MyVolume
# Sierra
sudo /Applications/Install\ macOS\ Sierra.app/Contents/Resources/createinstallmedia --applicationpath /Applications/Install\ macOS\ Sierra.app --volume /Volumes/MyVolume
# El Capitan
sudo /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/Resources/createinstallmedia --applicationpath /Applications/Install\ OS\ X\ El\ Capitan.app --volume /Volumes/MyVolume
# Yosemite
sudo /Applications/Install\ OS\ X\ Yosemite.app/Contents/Resources/createinstallmedia --applicationpath /Applications/Install\ OS\ X\ Yosemite.app --volume /Volumes/MyVolume
# Mavericks
sudo /Applications/Install\ OS\ X\ Mavericks.app/Contents/Resources/createinstallmedia --applicationpath /Applications/Install\ OS\ X\ Mavericks.app --volume /Volumes/MyVolume

在 x86 上安装

Installer can't verified 安装器不能被验证

使用旧的安装应用, 2016.2.14 之前,可能会由于证书过期导致无法使用,通过修改系统时间来规避

# 在安装前进入终端执行
date 0101010116

更新未能被验证

可参考该文章.

解决办法,前往 苹果组合更新下载页面, 下载需要的版本, 手动进行安装更新.

常见问题诊断

# Ubuntu
apt-get install hfsprogs
fdisk -l
# 检测磁盘是否有问题
fsck.hfsplus /dev/sda2
# 考虑将其挂载上然后把需要的数据拷贝出来
mount -t hfsplus -o force,rw /dev/sdb2 /media/mntpoint

清除 DNS 缓存

# OS X 12 (Sierra) and later
sudo killall -HUP mDNSResponder
sudo killall mDNSResponderHelper
sudo dscacheutil -flushcache
# OS X 11 (El Capitan) and OS X 12 (Sierra)
sudo killall -HUP mDNSResponder

网络

# 查看路由表
# -n 不做 resolve
netstat -nr

桥接

  • 对 wifi 支持不太好
sudo ifconfig bridge0 create
sudo ifconfig bridge0 addm en0 addm en1
sudo ifconfig bridge0 up
ifconfig tap0 192.168.0.20 netmask 255.255.255.0
ifconfig bridge0 addm tap0
man ifconfig

tuntap

brew cask install tuntap
# 避免 root 访问
sudo chown $USER:staff /dev/tap0
ll /dev/tun*
ll /dev/tap*
# 会创建失败
ifconfig tap0 create
# 在 root shell 中执行该命令会创建
# https://sourceforge.net/p/tuntaposx/mailman/message/30457237/
# The idea is that interfaces only get created when a program opens the corresponding /dev/tapX or /dev/tunX device.
# To try, do exec 3<>/dev/tap0 on a root shell.
exec 3<>/dev/tap0
ifconfig tap0 10.10.10.1 10.10.10.255
ifconfig tap0 up
ping -c1 10.10.10.1
ifconfig tap0 0.0.0.0
sudo ifconfig bridge0 addm tap0

转发

INTERFACE=en0
# ifup
sysctl -w net.inet.ip.forwarding=1
sysctl -w net.link.ether.inet.proxyall=1
sysctl -w net.inet.ip.fw.enable=1
ifconfig bridge0 create
ifconfig bridge0 addm $INTERFACE addm tap0
ifconfig bridge0 up
natd -interface $INTERFACE
ipfw add divert natd ip from any to any via $INTERFACE
# ifdown
sysctl -w net.inet.ip.forwarding=0
sysctl -w net.link.ether.inet.proxyall=0
sysctl -w net.inet.ip.fw.enable=1

路由

netstat -nr
route delete -host 10.3.2.1
route add -net 10.3 tun0
route add -host 9.8.7.6 tun0

防火墙

cat /etc/pf.conf
# 启用, 可能默认未启用
pfctl -ef /etc/pf.conf
# 语法检测
pfctl -v -n -f /etc/pf.conf
# 应用和重载
pfctl -f /etc/pf.conf

DocumentRevisions-V100

Migration

brew list > formulas.txt
brew fetch $(cat formulas.txt)
brew unlink $(cat formulas.txt)
brew link --overwrite $(cat formulas.txt)
brew reinstall $(cat formulas.txt)
brew doctor check_for_stray_headers
brew doctor check_for_stray_headers 2>&1 | grep /protobuf | xargs rm
brew doctor check_for_stray_headers 2>&1 | grep '^ /' | xargs rm
brew doctor check_for_stray_static_libs
brew doctor check_for_stray_static_libs 2>&1 | grep '^ /' | xargs rm
brew doctor check_for_stray_dylibs
brew doctor check_for_stray_dylibs 2>&1 | grep '^ /' | xargs rm
brew prune
brew missing

常见网络名称

  • lo0 = loopback
  • gif0 = Software Network Interface
  • stf0 = 6to4 tunnel interface
  • en0 = Ethernet 0
  • fw0 = Firewire
  • en1 = Ethernet 1
  • vmnet1 = Virtual Interface

Develope

security find-identity -v -p codesigning

命令行开启屏幕共享

# 开启所有服务,允许所有用户
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -off -restart -agent -privs -all -allowAccessFor -allUsers
# 只开启屏幕共享
sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist