0%

MacOS使用技巧

MacOS使用技巧


恢复u盘实际容量

装机系统启动盘恢复

1
2
3
4
5
6
7
# 查看要回复的 U盘
diskutil list

#sudo diskutil umountDisk /dev/disk2

# 这里假设是 disk2
diskutil eraseDisk JHFS+ Secure_HDD disk2

log 命令

查看过去一小时日志
1
log show --info --start "$(date -u -v-1H +%Y-%m-%d\ %H:%M:%S)"
查看 sshd 进程最后10条记录
1
log show --predicate 'process == "sshd"' --info --last 10

命令行操作远程登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看远程登录是否开启
sudo systemsetup -getremotelogin

# 开启远程登录
sudo systemsetup -setremotelogin on

# 关闭远程登录
sudo systemsetup -setremotelogin off

# 也可以通过 launchctl 来启动和关闭远程登录
# 开启远程登录
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
# 关闭远程登录
sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
sudo launchctl list | grep sshd

命令行使用 typora 打开 markdown 文件

1
2
3
echo 'alias typora="open -a typora"' >> ~/.zshrc
source ~/.zshrc
typora README.md

iPhone、iPad 备份位置

1
ls -lah ~/Library/Application\ Support/MobileSync/Backup/

远程桌面管理

访达 中键入 ⌘ + K 输入 vnc://192.168.36.47

命令行方式开启远程管理

开启
1
2
3
4
5
# 允许指定用户
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -restart -agent -privs -all -users codezm

# 允许所有用户
# sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -restart -agent -privs -all -allowAccessFor -allUsers
关闭
1
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -deactivate -configure -access -off

Mac 如何清除 dns 缓存

  • 10.11.5及以上

    1
    sudo killall mDNSResponder
  • 10.10.4

    1
    sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder;say cache flushed

tar 打包时存在多余 _ 文件

1
2
find . -name "._*" | xargs rm -f
find . -name ".DS_Store" | xargs rm -f
1
COPYFILE_DISABLE=1 tar -cvf inws.tar inws

Mac 开启远程登录

  1. 系统偏好设置
  2. 共享
  3. 勾选允许远程登陆

xcode 无法升级 - 一直提示”无法连接到服务器”

访问官方网站 https://developer.apple.com/download/more 下载xcode最新包.

刚下载的 xx.app 运行时提示软件已损坏,无法打开

  1. 设置允许任何来源

    • 系统偏好设置 -> 安全性与隐私 -> 任何来源

    • macOS Sierra 10.12系统需要执行以下命令:

      1
      sudo spctl --master-disable
  2. 修复 xxx.app

    1
    sudo xattr -rd com.apple.quarantine /Applications/VLC.app

git diff 时出现的 ^M

1
2
3
4
# 去除git diff 时出现的 ^M(只是git不再提醒了,内容并未变化)
git config --global core.whitespace cr-at-eol
# 还原git diff 时出现的 ^M(只是git恢复提醒了,内容并未变化)
git config --global core.whitespace crlr-at-eol

brew install Error: Your Command Line Tools are too outdated

1
2
3
4
5
$ softwareupdate --list
Software Update Tool

Finding available software
No new software available.
1
2
3
4
5
$ sudo rm -rf /Library/Developer/CommandLineTools
$ sudo xcode-select --install
$ xcodebuild -version
Xcode 12.5
Build version 12E262

brew 切换至清华大学源。

https://juejin.cn/post/6844904069471928328

在执行 brew install 命令时,自动升级旧版软件

1
2
3
4
# 防止升级所有软件
export HOMEBREW_NO_AUTO_UPDATE=1
# 防止自动清理
export HOMEBREW_NO_INSTALL_CLEANUP=1

切换至 root 用户

1
sudo su

mount_hfs: Operation not permitted?

起因:iMac 默认配置是机械硬盘,为了提速换成了USB外接固态硬盘。并将硬盘划分了两块分区,第一块分区做系统盘,第二块分区做代码数据盘(区分大小写—为了与线上环境保持一致),第二块分区在开机未登录的时候是未挂载到系统上了,在用户登录后会自动进行挂载。目前一些文件做了软链至第二块分区,这就导致自启动脚本需依赖挂载第二块分区后才能正常运行。

本来是想放到 launchctl 自启动脚本中的,但尝试了各种办法未实现,但 crontab 确可以。

mount 命令需要 sudo 权限,下面方法也是为了解决在使用 sudo 时不需要输入密码。

参考:https://apple.stackexchange.com/questions/117148/how-do-i-get-my-launchagent-to-run-as-root/288322#288322

  1. 创建 mount-disk.sh 脚本

    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/bash

    if [ -z "$(mount | grep 'work')" ]; then
    echo $(date '+%F %H:%M:%S')": try mount..."
    mkdir /Volumes/work || mount -t hfs /dev/disk1s3 /Volumes/work
    else
    echo $(date '+%F %H:%M:%S')": mounted..."
    fi
1
2
# 添加执行权限
chmod 755 mount-disk.sh
  1. crontab -e 添加 crontab 任务

    1
    * * * * * /usr/bin/sudo /Users/codezm/intranet-penetration/mount-disk.sh >> /tmp/crontab-root.log 2>&1
  2. sudo 取消密码输入

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    sudo visudo

    # 将以下内容
    # root and users in group wheel can run anything on any machine as any user
    root ALL = (ALL) ALL
    %admin ALL = (ALL) ALL

    # 替换为
    # root and users in group wheel can run anything on any machine as any user
    root ALL = (ALL) ALL
    %admin ALL = (ALL) ALL
    codezm ALL=(ALL) NOPASSWD: /Users/codezm/intranet-penetration/mount-disk.sh

Crontab: Operation not permitted?

受Mac系统保护的影响,crontab 默认仅允许在用户主目录下执行脚本。

但脚本不在用户主目录下,则需要将 cron 添加到 完整磁盘访问权限 中,否则就会出现 Operation not permitted 错误。

image-20210825095316669

具体操作步骤:

  1. 系统偏好设置 -> 安全与隐私 -> 隐私 -> 完全磁盘访问权限
  2. 点击 点按锁按钮以进行更改。 ,输入账号及密码。
  3. 点击 ,使用 shift+command+G 快捷键,输入 /usr/bin/cron,点击 打开 按钮。
  4. 点击 点按锁按钮以防再次更改。

查看crontab任务未执行原因:

1
2
3
4
5
6
7
$ sudo launchctl list | grep 'cron'
307 0 com.vix.cron
$ locate com.vix.cron
/System/Library/LaunchDaemons/com.apple.locate.plist

# 查看邮件内容,这里的 codezm 为用户名
$ tail -f /var/mail/codezm

参考:

Mac 网络配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看路由表
netstat -nr
# 查看所有网络链接方式
networksetup -listallnetworkservices

# 设置静态IP
networksetup -setmanual Ethernet 192.168.36.47 255.255.254.0 192.168.36.254
# 设置DHCP
networksetup -setdhcp Ethernet
# 设置DHCP(手动设定地址)
networksetup -setmanualwithdhcprouter Ethernet 192.168.36.47
networksetup -setdnsservers Ethernet 223.5.5.5 114.114.114.114 8.8.8.8

# 设置DNS
networksetup -setdnsservers Ethernet 192.168.36.254
# 清空DNS缓存
dscacheutil -flushcache

networksetup -setnetworkserviceenabled Ethernet off
networksetup -setnetworkserviceenabled Ethernet on

networksetup -setdhcp Ethernet
networksetup -setmanualwithdhcprouter Ethernet 192.168.36.47

Mac 平台 tar 打包后,在其他平台解压时出现多余 ._ 文件

设置 COPYFILE_DISABLE 变量,使其在压缩时避免生成 ._ 文件。

解决方案
1
2
3
4
5
6
tar --disable-copyfile -zcvf files
# COPYFILE_DISABLE=1 tar -zcvf files.tar.gz files

git diff e6bd106649ef0..69e2fc98b17 --name-only | grep -E "statics|phpcms|caches/caches_commons/caches_data/poster_template_1.cache.php" | xargs -I {} tar --disable-copyfile -rvf "phpcms-$(date +%Y-%m-%d).tar.gz" {}
# 查看压缩包文件列表 --不解压
tar -tvf phpcms-2021-04-30.tar.gz
1
2
3
4
# 创建包文件
git show 9660d86 --name-only --oneline | grep "wap" | xargs -I {} tar cvf vip-fixed.tar.gz {}
# 追加包文件
git show 9660d86 --name-only --oneline | grep "wap" | xargs -I {} tar rvf vip-fixed.tar.gz {}

tar 中 -z 是压缩,-c 是归档,凡使用这两个参数创建的压缩包文件,则不能向其再追加文件。

mysql 更改存储路径

更改配置文件

vim /usr/local/opt/mysql/homebrew.mxcl.mysql.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
将默认路径 `/usr/local/var/mysql` 更换为自定义路径 `/Volumes/work/web/databases/3306`

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>homebrew.mxcl.mysql</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/mysql/bin/mysqld_safe</string>
<string>--datadir=/Volumes/work/web/databases/3306</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/Volumes/work/web/databases/3306</string>
</dict>
</plist>

vim /usr/local/opt/mysql@5.7/homebrew.mxcl.mysql@5.7.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
将默认路径 `/usr/local/var/mysql` 更换为自定义路径 `/Volumes/work/web/databases/3307`


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>homebrew.mxcl.mysql@5.7</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/mysql@5.7/bin/mysqld_safe</string>
<string>--datadir=/Volumes/work/web/databases/3307 --defaults-file=/usr/local/etc/mysql@5.7/my.cnf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/Volumes/work/web/databases/3307</string>
</dict>
</plist>
删除默认数据存储目录
1
rm -rf /usr/local/var/mysql
初始化数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/usr/local/Cellar/mysql/8.0.23/bin/mysqld --datadir=/Volumes/work/web/databases/3306 --initialize-insecure

# 相对于 mysql@5.7 版本,需要创建 /usr/local/etc/my@5.7.cnf 配置文件,否则将与 mysql@8.0 版本配置文件冲突
vim /usr/local/etc/my@5.7.cnf

[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
port=3307
datadir=/Volumes/work/web/databases/3307
socket=/Volumes/work/web/databases/3307/mysql.sock
pid-file=/Volumes/work/web/databases/3307/mysql.pid

/usr/local/Cellar/mysql@5.7/5.7.32/bin/mysqld --defaults-file=/usr/local/etc/mysql@5.7/my.cnf --datadir=/Volumes/work/web/databases/3307 --initialize-insecure --explicit_defaults_for_timestamp
迁移数据

Got a packet bigger than 'max_allowed_packet' bytes

1
2
# 默认:4M = 4194304,此处调整为 100M。
max_allowed_packet=104857600

brew

在使用 brew list | grep mysql 命令时,提示 Error: Cask 'java' is unreadable: undefined method 'undent' for #<String:0x00007f8685373738> 错误信息。
1
find "$(brew --prefix)/Caskroom/"*'/.metadata' -type f -name '*.rb' | xargs grep 'EOS.undent' --files-with-matches | xargs sed -i '' 's/EOS.undent/EOS/'

详见

在 Mac 上压缩含有中文名的文件后,再在 Windows 上解压时乱码

原因:Mac、Linux 默认使用的是 UTF-8 编码,而 Windows 使用的是 GBK 编码。

安装并使用 Keka 压缩软件,压缩格式设置为 7z

Keka 是一款免费且 开源 的文件压缩、解压软件。

禁止在移动存储设备上创建 .Trashes、.Spotlight-V100、.fseventsd 目录

1
2
3
4
5
6
7
# 关闭索引创建
mdutil -i off /Volumes/yourUSBstick
cd /Volumes/yourUSBstick
rm -rf .{,_.}{fseventsd,Spotlight-V*,Trashes}
mkdir .fseventsd
touch .fseventsd/no_log .metadata_never_index .Trashes
cd -

显示通知

脚本运行完后,来个提醒

1
osascript -e 'display notification "hello world!"'
参考

更新系统时间

1
sudo sntp -sS time.apple.com

Mac 备份盘

分区格式:Mac OS 扩展(日志式)

image-20210123124020534

Mac 端重启 sshd 服务

1
2
3
sudo kill -SIGHUP $(ps aux | grep sshd | grep priv | awk '{print $2}')

sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist

iTunes 在 Mac 上的备份

1
cd ~/Library/Application Support/MobileSync/Backup

Finder 访达中显示/隐藏 . 前缀的隐藏文件快捷键

Shift + CMD + .

访达

跳转目录

Command + shift + g 输入目录路径。

刷新网页、强制刷新网页快捷键

  • 刷新网页:CMD + r
  • 强制刷新网页:Shift + CMD + r

查看文件权限、附加权限

普通文件可通过 ls -l 命令,查看文件所属用户、用户组。但部分文件增加了 @ 符,是 Mac OS 追加的附加权限,可通过 xattr -l 命令查看。

如何更改 Mac 文件的默认打开方式

更改某一文件的默认打开方式
  1. 右键单击该文件
  2. 按住 option 键,在 始终以此方式打开 列表中选择要打开此文件的应用。

以后再鼠标双击打开该文件时将使用配置的应用打开,但其他同类型文件则使用默认应用。

更改同类型文件的默认打开方式
  1. 右键单击该文件
  2. 选择 显示简介 选项。
  3. 打开方式 中,选择此后缀文件默认打开的应用。
  4. 点击 全部更改 按钮。

以后所有此后缀文件均有该应用打开,要想复原可再次通过以上步骤操作。

如何禁止创建 .DS_Store 文件

并未实践操作证明此项配置生效

1
defaults write com.apple.desktopservices DSDontWriteNetworkStores true

~/Libaray/Developer 占用大量存储空间

1
$ du -d 1 -h

修改主机名:hostname

1
2
sudo scutil --set HostName codezm-Cicada
hostname

MacBook Pro 2015款

随航❌:https://support.apple.com/zh-cn/HT210380

随航:可以将 iPad 用作显示屏,从而扩展或镜像 Mac 桌面。

通用控制❌:https://support.apple.com/zh-cn/HT212757

接力✅:https://support.apple.com/zh-cn/HT209455

Mac、iPhone、iPad、iPod touch 和 Apple Watch 上“连续互通”的系统要求

参考