Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

免 sudo 以 root 權限執行自編譯程式:設定 SUID 位元

編輯:關於 Linux

情境

程式使用原始套接字(raw socket)需 root 權限,但執行時無法透過 sudo 提權。

做法

  1. 以 root 編譯:sudo gcc -o myprog myprog.c
  2. 設定 SUID:sudo chmod u+s myprog

效果

一般使用者直接執行 ./myprog 即擁有 root 有效 UID,類似 ping 指令行為。

安全提醒

參考 ping 原始碼,建立 raw socket 後立即呼叫 setuid(getuid()) 降權,遵循最小權限原則。

結語

「root 編譯 + SUID」兩步驟缺一不可,讓自製程式免 sudo 即具 root 權限。

Copyright © Windows教程網 All Rights Reserved