Answers:
实际上,您可以在Mac OS 10.7 Lion和10.8 Mountain Lion上为Apple的VNC服务器切换默认端口。要更改端口,您需要编辑服务器的plist文件/System/Library/LaunchDaemons/com.apple.screensharing.plist
(该文件在10.7 Lion之前的系统中不存在)。
编辑文件需要root(sudo)特权。在终端中,如果您熟悉vi或vim,则可以输入:
sudo vim /System/Library/LaunchDaemons/com.apple.screensharing.plist
否则,最好使用nano:
sudo nano /System/Library/LaunchDaemons/com.apple.screensharing.plist
现在,您要做的就是将第34行(显示为<string>vnc-server</string>
)更改为<string>nnnn</string>
nnnn是您要使用的端口号。我知道将“ vnc-server”之类的名称更改为数字似乎很奇怪,但这就是您必须这样做的方式。我在下面提供了一个示例,以防万一。
要将默认端口更改为54321,您可以将plist文件编辑为如下所示:
...
<key>Sockets</key>
<dict>
<key>Listener</key>
<dict>
<key>Bonjour</key>
<string>rfb</string>
<key>SockServiceName</key>
<string>54321</string> <!-- Change this line! -->
</dict>
</dict>
<key>UserName</key>
<string>root</string>
<key>SHAuthorizationRight</key>
<string>system.preferences</string>
</dict>
</plist>
保存文件后,要使更改生效,请关闭“屏幕共享”,然后在“共享”首选项窗格中再次将其打开,或者使用以下命令卸载并重新加载服务:
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.screensharing.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.screensharing.plist
在通过Google找到此线程后,我可以确认/etc/services
对“ rfb”端口的编辑将更改所包含的VNC服务器的侦听端口。
我编辑了文件并重新启动(通常我会尝试重新启动服务或卸载launchdeamon,但是我也遇到了其他一些问题,并且没有打扰)。然后,iPad上的iTeleport在5900上无法连接,并在我选择的高特权端口上成功运行。
在apple.com和macosxhints.com的各种论坛上对此进行了讨论。简短的回答是“您无法更改”。
较长的答案提出了解决方法-三种可能性:
/etc/Services
可以解决问题。我尝试了一下(更改后甚至重新启动了Mac)都无济于事。再想一想,弄乱该文件甚至可能不是一个好主意,因为如果其他应用程序想要使用特定协议连接到某些第三方,则也可能会使用该文件来获取众所周知的端口号。(例如:更改该文件中的SSH端口似乎可行,但这是个坏主意。)
基于Greg在此线程中提供的信息,我编写了一个bash脚本,该脚本将自动执行更改系统的VNC侦听端口的过程。在我的测试中效果很好。让我知道是否有人有任何问题。
#!/bin/sh
#Created by Will D. on 04/10/2015
#If you find it useful (or have suggestions, feedback, etc.), shoot me an email at throwapenny@me.com.
#Requires Mac OS 10.7.x or later (tested up to and including 10.10.3)
#02/02/2016 - Updated Script to alert for SIP status
#Setting Static Variables
sourcepath="/System/Library/LaunchDaemons/"
filename="com.apple.screensharing.plist"
port=`less $sourcepath$filename | awk 'f{print $1;f=0} /SockServiceName/ {f=1}' | awk -F "<|>" '{print $3}'`
os_version=`sw_vers -productVersion`
os_version_aug=`sw_vers -productVersion | awk -F "." '{print $1$2}'`
sip_status=`csrutil status | awk '{print $5}'`
#Colors
nc='\033[0m'
light_red='\033[1;31m' #Light Red
yellow='\033[1;33m' #Yellow
clear
#Check the script is being run by root
if [ "$EUID" -ne 0 ];then
printf "${light_red}This Script Must Run As Root${nc}\n"
exit 0
fi
clear
printf ${yellow};echo "---------------------------------------------------------------"
echo "--- ---"
echo "--- This Script Will Change Your Systems VNC Listening Port ---"
echo "--- Hit Ctrl + c to exit at anytime ---"
echo "--- ---"
echo "---------------------------------------------------------------";printf "${nc}\n"
#Check System Version
sleep 1
if [ "${os_version_aug}" -lt "107" ]; then
echo ""
echo "System OS Must Be Greater Than 10.7.x. Aborting Script."
exit 0
else
echo ""
echo "System OS Version is" $os_version
echo "OS Requirement Met √"
echo "--------"
fi
if [ "${os_version_aug}" == "1011" ]; then
if [ "${sip_status}" == "enabled." ]; then
echo ""
printf "${light_red}••• System Integrity Protection is Enabled •••${nc}\n"
echo ""
echo "This script modifies /System/Library/LaunchDaemons/com.apple.screensharing.plist"
echo "Please Disable System Integrity Protection Before Running"
echo ""
exit 0
fi
fi
#Give Feedback on Current Port
sleep 1
if [ "${port}" == "vnc-server" ]; then
echo ""
echo "The System's VNC Port is Currently"
echo "Set to the System Default Port of 5900."
echo "--------"
elif [ "${port}" != "vnc-server" ]; then
echo ""
echo "The System's VNC Port is Currently"
echo "Set to a Non-default Port of" $port"."
echo "--------"
fi
#Updating Port
echo ""
printf "What Port Would You Like VNC to Listen On? "
read newport
echo ""
echo "The Following Action Requires an Admin Password."
echo "Note: Your Password Will Be Visible When You Type It"
echo ""
printf "Admin Password? "
read admin_pass
sleep 1
echo ""
echo "Created" $filename".bak."
sleep 1
echo ""
echo "Updating VNC Port to" $newport"..."
echo $admin_pass | sudo -S sed -i.bak -e "s|$port|$newport|g" $sourcepath$filename
sleep 1
echo "Done"
echo ""
sleep 1
#Restarting screensharing process
echo "Restarting Screen Sharing Service..."
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.screensharing.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.screensharing.plist
echo "Done"
sleep 1
echo ""
echo "Your System's VNC Port is Now Set to" $newport"."
echo ""
echo "Update Complete. All Done."
if [ "${os_version_aug}" == "1011" ]; then
echo ""
echo "Since you're running El Capitan"
echo "be sure to re-enable System Integrity Protection"
exit 0
fi
exit 0
要在不禁用系统完整性保护的情况下更改默认端口和/或绑定地址,您需要在中创建一个新的LaunchDaemon /Library
。
不幸的是,分配了其他标签后,屏幕共享代理将无法正常工作。这意味着守护程序必须使用相同的名称“遮蔽”原始文件。这会导致自身的问题,因为在重新启动时,系统将加载中的原始/System
版本,而忽略中的修改版本/Library
。
解决方案是禁用LaunchDaemon,并使用“启动器”守护程序,该守护程序将强制加载修改后的LaunchDaemon。但是,必须谨慎通过首选项激活“屏幕共享”,否则它将以“ 仅观察”模式结束。
执行
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
执行
sudo cp /System/Library/LaunchDaemons/com.apple.screensharing.plist /Library/LaunchDaemons/com.apple.screensharing.plist
在中/Library/LaunchDaemons/com.apple.screensharing.plist
,编辑“套接字”部分以查看所需的方式。例如听localhost:5901
:
<key>Sockets</key>
<dict>
<key>Listener</key>
<dict>
<key>SockNodeName</key>
<string>localhost</string>
<key>SockServiceName</key>
<string>5901</string>
</dict>
</dict>
创建/Library/LaunchDaemons/com.apple.screensharing.launcher.plist
具有以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.apple.screensharing.launcher</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/bin/launchctl</string>
<string>load</string>
<string>-F</string>
<string>/Library/LaunchDaemons/com.apple.screensharing.plist</string>
</array>
</dict>
</plist>
执行
sudo launchctl load -w /Library/LaunchDaemons/com.apple.screensharing.launcher.plist
此后,将正确设置“屏幕共享”权限,默认守护程序将不会自动加载,并且我们的启动器将强制启动我们的自定义守护程序。