告别繁琐运维!Codex 一键安装脚本

📅 2026/8/20 11:02:36
告别繁琐运维!Codex 一键安装脚本
使用 Codex CLI 提升服务器运维效率在服务器运维工作中日志查看、部署操作和问题排查等任务往往比较繁琐。Codex CLI 作为一款命令行工具能够显著简化这些流程提升工作效率。本文将介绍如何通过自动化脚本在多台服务器上快速安装和配置 Codex CLI。脚本效果展示我编写了一个自动化安装脚本下面是执行效果示例脚本主要功能一、三种安装方式脚本支持三种安装方式直接安装从官方源直接下载安装Node.js/npm 安装通过 npm 包管理器安装Snap Store 安装通过 Snap 包管理器安装二、卸载功能脚本提供完整的卸载功能可以清理不同安装方式下的 Codex 文件同时保留用户配置和会话数据。三、API Key 配置安装完成后自动配置 API Key方便快速开始使用。安装与使用安装步骤# 添加执行权限 chmod x install_codex.sh 运行安装脚本 ./install_codex.sh基本使用命令# 正常启动 Codex codex 恢复上次对话 codex resume 以超级权限启动谨慎使用 codex --yolo完整脚本代码将以下代码保存为install_codex.sh运行不起来的请下载脚本文件codex教程与脚本下载请点击#!/bin/bash set -o pipefail 配置区域 请在此处填写您的配置信息 您的 OpenAI API Key从官方平台获取 API_KEY请在此处填写您的 API Key 官方 API 地址使用 OpenAI 官方地址 BASE_URLhttps://api.openai.com/v1 echo 开始执行 Codex CLI 自动化配置脚本... INSTALL_SCRIPT_URLhttps://chatgpt.com/codex/install.sh NETWORK_TEST_URLhttps://example.com/ CURL_CONNECT_TIMEOUT30 CURL_MAX_TIME60 INSTALL_DEBUG${INSTALL_DEBUG:-0} INSTALL_PROGRESS${INSTALL_PROGRESS:-1} test_network_access() { local test_name$1 local test_url$2 local test_result local test_exit_code echo gt;gt;gt; 正在测试${test_name}... test_result$(curl -fsSL --connect-timeout $CURL_CONNECT_TIMEOUT --max-time $CURL_MAX_TIME --output /dev/null --write-out HTTP%{http_code}; remote_ip%{remote_ip}; dns%{time_namelookup}s; connect%{time_connect}s; tls%{time_appconnect}s; total%{time_total}s $test_url) test_exit_code$? if [[ $test_exit_code -ne 0 ]]; then echo gt;gt;gt; 网络连接异常${test_name}失败。 echo gt;gt;gt; 检测结果${test_result:-HTTP000}; curl_exit${test_exit_code} return 1 fi echo gt;gt;gt; ${test_name}成功${test_result} } enable_install_progress() { local curl_bin [[ $INSTALL_PROGRESS 1 ]] || return 0 curl_bin$(command -v curl) || return 1 INSTALL_PROGRESS_DIR$(mktemp -d) || return 1 INSTALL_ORIGINAL_PATH$PATH cat gt; $INSTALL_PROGRESS_DIR/curl lt;lt; EOF #!/bin/sh exec $curl_bin $ --progress-bar EOF chmod x $INSTALL_PROGRESS_DIR/curl export PATH$INSTALL_PROGRESS_DIR:$PATH echo 已启用下载进度显示。 } disable_install_progress() { if [[ -n ${INSTALL_ORIGINAL_PATH:-} ]]; then export PATH$INSTALL_ORIGINAL_PATH fi if [[ -n ${INSTALL_PROGRESS_DIR:-} ]]; then rm -rf $INSTALL_PROGRESS_DIR fi } install_official() { local -a curl_args(-fsSL --connect-timeout $CURL_CONNECT_TIMEOUT --max-time $CURL_MAX_TIME) local install_script_file local install_exit_code if ! test_network_access 网络连接测试 $NETWORK_TEST_URL; then echo gt;gt;gt; 请检查网络连接。 return 1 fi if ! test_network_access 安装地址访问测试 $INSTALL_SCRIPT_URL; then echo gt;gt;gt; 无法访问安装地址请检查网络。 return 1 fi install_script_file$(mktemp) || return 1 if ! curl ${curl_args[]} --output $install_script_file $INSTALL_SCRIPT_URL; then rm -f $install_script_file return 1 fi if [[ $INSTALL_DEBUG 1 ]]; then echo gt;gt;gt; 已启用安装诊断模式将显示官方安装脚本执行的下载命令。 fi if ! enable_install_progress; then echo gt;gt;gt; 警告无法启用下载进度显示将继续安装。 fi echo gt;gt;gt; 正在执行官方安装器下载和安装阶段将显示进度... echo gt;gt;gt; 安装完成后将由本脚本写入 API Key不立即启动 Codex。 if [[ $INSTALL_DEBUG 1 ]]; then printf n\n | sh -x $install_script_file else printf n\n | sh $install_script_file fi install_exit_code$? disable_install_progress rm -f $install_script_file return $install_exit_code } install_with_node() { if ! command -v npm /dev/null; then echo 未检测到 npm尝试安装 nodejs/npm... if command -v apt-get /dev/null; then apt-get update apt-get install -y nodejs npm elif command -v yum /dev/null; then yum install -y nodejs npm elif command -v dnf /dev/null; then dnf install -y nodejs npm else echo 错误未检测到 npm且无法自动识别包管理器请先手动安装 Node.js/npm。 return 1 fi fi if ! command -v npm amp;gt; /dev/null; then echo gt;gt;gt; 错误npm 安装失败请先手动安装 Node.js return 1 fi echo gt;gt;gt; 正在使用 npm 安装 Codex... npm install -g openai/codex } install_with_snap() { if ! command -v snap /dev/null; then echo 错误未检测到 snap请先安装 snapd。 return 1 fi echo gt;gt;gt; 正在通过 Snap Store 安装 Codex... snap install codex } ensure_codex_on_path() { local codex_dir hash -r 2gt;/dev/null || true for codex_dir in $HOME/.local/bin /usr/local/bin /snap/bin; do if [[ -x $codex_dir/codex ]]; then PATH$codex_dir:$PATH export PATH return 0 fi done command -v codex amp;gt; /dev/null } remove_codex() { local removed_anything0 read -r -p gt;gt;gt; 将卸载 Snap/npm/官方安装器中的 Codex但保留 ~/.codex 配置与会话。确认删除[y/N] confirm_remove case $confirm_remove in y|Y|yes|YES) ;; *) echo gt;gt;gt; 已取消删除。 return 0 ;; esac if command -v snap amp;gt; /dev/null amp;amp; snap list codex amp;gt; /dev/null; then echo gt;gt;gt; 正在卸载 Snap Codex... snap remove codex || return 1 removed_anything1 fi if command -v npm amp;gt; /dev/null amp;amp; npm ls -g --depth0 openai/codex amp;gt; /dev/null; then echo gt;gt;gt; 正在卸载 npm Codex... npm uninstall -g openai/codex || return 1 removed_anything1 fi if [[ -e $HOME/.local/bin/codex ]]; then echo gt;gt;gt; 正在删除官方安装器的 Codex 命令... rm -f $HOME/.local/bin/codex removed_anything1 fi if [[ -d $HOME/.codex/packages/standalone ]]; then echo gt;gt;gt; 正在删除官方安装器的 standalone 安装包... rm -rf $HOME/.codex/packages/standalone removed_anything1 fi if [[ -f $HOME/.bashrc ]] amp;amp; grep -qxF export PATH$HOME/.local/bin:$PATH $HOME/.bashrc; then echo gt;gt;gt; 正在清理 .bashrc 中的官方 Codex PATH 配置... sed -i |^export PATH$HOME/.local/bin:$PATH$|d $HOME/.bashrc removed_anything1 fi hash -r if [[ $removed_anything -eq 0 ]]; then echo gt;gt;gt; 未发现可删除的 Codex 安装。 else echo gt;gt;gt; Codex 已删除已保留 ~/.codex 配置与会话。 fi } configure_codex() { echo 开始配置 API Key 与 Base URL... mkdir -p ~/.codex cat gt; ~/.codex/config.toml lt;lt; EOF model_provider openai model gpt-4 model_reasoning_effort medium disable_response_storage false [model_providers.openai] name openai base_url ${BASE_URL} wire_api responses requires_openai_auth true EOF echo gt;gt;gt; config.toml 配置文件已写入。 echo ${API_KEY} | codex login --with-api-key echo gt;gt;gt; 正在验证登录状态... codex login status } install_selected_source() { local selected$1 local install_status0 case $selected in 1) install_official || install_status$? ;; 2) install_with_node || install_status$? ;; 3) install_with_snap || install_status$? ;; *) echo gt;gt;gt; 错误无效安装选项。; return 1 ;; esac ensure_codex_on_path || return $install_status return $install_status } choose_install_source() { echo 1. 直接安装 Codex echo 2. 使用 Node.js/npm 安装 Codex echo 3. 使用 Snap Store 安装 Codex read -r -p 请输入安装方式 [1-3] selected_source install_selected_source $selected_source } 安装或更新认证配置 if ensure_codex_on_path; then echo 检测到 Codex CLI 已安装。 echo 1. 重新填写 API Key 与 Base URL echo 2. 删除 Codex echo 3. 切换 Codex 安装方式 echo 4. 退出 read -r -p 请输入选项 [1-4] installed_mode case $installed_mode in if ! configure_codex; then echo 错误API Key 或 Base URL 配置失败。 exit 1 fi ;; 2) remove_codex exit $? ;; 3) echo 切换安装方式前将删除现有 Snap/npm/官方安装文件保留 ~/.codex 配置。 remove_codex || exit $? if ! ensure_codex_on_path; then choose_install_source || exit $? ensure_codex_on_path || exit 1 configure_codex || exit 1 fi ;; *) echo 保留现有 Codex 与配置脚本结束。 ;; esac else echo 未检测到 Codex CLI请选择安装方式 choose_install_source install_command_exit_code$? if [[ $install_command_exit_code -ne 0 ]]; then exit $install_command_exit_code; fi if ! ensure_codex_on_path; then echo gt;gt;gt; 错误Codex CLI 安装失败请检查网络或手动排查。 exit 1 fi if [[ $install_command_exit_code -ne 0 ]]; then echo gt;gt;gt; 警告安装器返回了非零状态但 Codex CLI 已成功安装将继续配置。 fi echo gt;gt;gt; Codex CLI 安装成功 if ! configure_codex; then echo gt;gt;gt; 错误API Key 或 Base URL 配置失败。 exit 1 fi fi echo 脚本执行完毕你可以直接运行 codex 开始使用。 if [[ -x $HOME/.local/bin/codex $PATH ! $HOME/.local/bin ]]; then echo 当前终端请先执行export PATH$HOME/.local/bin:$PATH hash