Ubuntu开机启动项的添加

本文介绍如何在Linux系统中管理各种启动项,包括依赖于图形系统的GUI设置和不依赖于图形系统的初始化配置。涵盖init进程、runlevel切换、配置文件修改等关键操作。

依赖于图形系统

GUI 设置

点击菜单:System->Preferences ->Sessions,中文为:系统->首选项->启动应用程序.这里设置的是启动图形界面以后自动运行的程序

修改配置文件

进入 ~/.config/autostart 目录 ,创建以名字加.desktop的文件,如: thunderbird.desktop


不依赖于图形系统的

init

Linux 内核启动 init ,init进程ID是1,是所有进程的父进程,所有进程由它控制。

/etc/event.d

Ubuntu 的启动由upstart控制,自9.10后不再使用/etc/event.d目录的配置文件,改为/etc/init。


runlevel

查看当前的运行级别,Ubuntu 桌面默认是2。

runlevel

Ubuntu 的系统运行级别:

0        系统停机状态
1        单用户或系统维护状态
2~5      多用户状态
6        重新启动 
S

切换运行级别,执行命令:

init [0123456Ss]

即在 init 命令后跟一个参数,此参数是要切换到的运行级的运行级代号,如:用 init 0 命令关机;用 init 6 命令重新启动。

/etc/rc2.d

该目录为runlevel=2环境(就是Ubuntu默认情况)的启动项

qii@ubuntu:/etc/rc2.d$ ls
README             S20nginx              S20xinetd      S50rsync      S90binfmt-support  S99grub-common
S19postgresql-8.4  S20speech-dispatcher  S25bluetooth   S50saned      S91apache2         S99ondemand
S20fancontrol      S20wicd               S50cups        S70dns-clean  S91lighttpd        S99rc.local
S20kerneloops      S20winbind            S50pulseaudio  S70pppd-dns   S99acpi-support

其中S表示启动,随后的数字表示启动的顺序。

手动的话将S重命名为K,运行

sudo update-rc.d script defaults

可以禁用某个服务,不过比较麻烦,方便的方法是sysv-rc-conf

runlevel下的增/删

[编辑]sysv-rc-conf

查看/修改不同运行级别的启动项,可以发现Ubuntu(其实是Debian)2-5之间的runlevel效果是一样的。

sudo apt-get install sysv-rc-conf 
sudo sysv-rc-conf 

[编辑]update-rc.d

添加启动项,例如mysql

qii@ubuntu:/etc/rc2.d$ sudo update-rc.d mysql defaults 
update-rc.d: warning: /etc/init.d/mysql missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql -> ../init.d/mysql
   /etc/rc1.d/K20mysql -> ../init.d/mysql
   /etc/rc6.d/K20mysql -> ../init.d/mysql
   /etc/rc2.d/S20mysql -> ../init.d/mysql
   /etc/rc3.d/S20mysql -> ../init.d/mysql
   /etc/rc4.d/S20mysql -> ../init.d/mysql
   /etc/rc5.d/S20mysql -> ../init.d/mysql

删除启动项

qii@ubuntu:/etc/rc2.d$ sudo update-rc.d -f mysql remove 
 Removing any system startup links for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql
   /etc/rc1.d/K20mysql
   /etc/rc2.d/S20mysql
   /etc/rc3.d/S20mysql
   /etc/rc4.d/S20mysql
   /etc/rc5.d/S20mysql
   /etc/rc6.d/K20mysql

rcconf

查看,修改当前运行级别的启动项

sudo apt-get install rcconf
sudo rcconf

[编辑]bashrc

Ubuntu默认shell为bash,.bashrc即为配置文件,在用户输入完用户名和密码后生效,可添加启动项目。

[编辑].profile和.xprofile

被KDM读取

/etc/profile

查看KDM读取配置

cat /etc/kde4/kdm/Xsession
[ -f /etc/profile ] && . /etc/profile
    if [ -f $HOME/.bash_profile ]; then
      . $HOME/.bash_profile
    elif [ -f $HOME/.bash_login ]; then
      . $HOME/.bash_login
    elif [ -f $HOME/.profile ]; then
      . $HOME/.profile
    fi
    ;;

查看GDM读取配置

cat /etc/gdm/Xsession
# First read /etc/profile and .profile
test -f /etc/profile && . /etc/profile
test -f "$HOME/.profile" && . "$HOME/.profile"
# Second read /etc/xprofile and .xprofile for X specific setup
test -f /etc/xprofile && . /etc/xprofile
test -f "$HOME/.xprofile" && . "$HOME/.xprofile"

可见GDM会读取.profile和.xprofile配置文件。


autostart

/etc/xdg/autostart
/usr/share/autostart
/usr/share/gdm/autostart
/usr/share/gnome/autostart
~/.config/autostart
~/.kde/share/autostart
~/.local/share/autostart

这些目录下的启动项目是这样的文件(举例)

Launchy.desktop

KDE SC 4系统设置中设定的启动项便是

~/.config/autostart

只在KDE中自动启动,就是设定.desktop文件的字段

OnlyShowIn=KDE;

<think>我们正在讨论Ubuntu系统中查看和管理开机启动项的方法。Ubuntu使用不同的机制来管理开机启动项,包括传统的init系统和现代的systemd。以下是一些常用的方法: ### 1. 使用`systemd`(Ubuntu 15.04及以后版本默认使用) `systemd`是较新Ubuntu版本的初始化系统,可以使用以下命令管理服务: - 查看所有开机启动的服务: ```bash systemctl list-unit-files --type=service --state=enabled ``` - 禁用某个服务开机启动: ```bash sudo systemctl disable <service_name> ``` - 启用某个服务开机启动: ```bash sudo systemctl enable <service_name> ``` ### 2. 使用`sysv-rc-conf`工具(适用于SysVinit脚本) 对于旧版本或兼容SysVinit的脚本,可以使用`sysv-rc-conf`: - 安装: ```bash sudo apt install sysv-rc-conf ``` - 运行: ```bash sudo sysv-rc-conf ``` 在图形界面中,用空格键启用或禁用服务。 ### 3. 检查`/etc/init.d/`目录 一些服务会在这里放置启动脚本,但通常这些脚本会被链接到`/etc/rcX.d/`目录(X为运行级别)。 ### 4. 使用`update-rc.d`(管理SysVinit启动链接) - 禁用服务: ```bash sudo update-rc.d <service_name> disable ``` - 启用服务: ```bash sudo update-rc.d <service_name> enable ``` ### 5. 图形界面工具 - `gnome-session-properties`:用于管理用户级别的启动应用程序。 在终端输入: ```bash gnome-session-properties ``` 或者通过图形界面:搜索“启动应用程序”。 ### 6. 检查用户级别的启动项 用户级别的启动项通常位于: - `~/.config/autostart/` - `/etc/xdg/autostart/` ### 注意事项: - 在修改启动项前,请确保了解服务的作用,避免禁用系统关键服务。 - 使用`systemd`的系统,服务配置文件通常位于`/lib/systemd/system/`和`/etc/systemd/system/`。 ### 示例:禁用Apache服务开机启动 ```bash sudo systemctl disable apache2 ``` §§相关问题§§ 1. 如何查看`systemd`服务的运行状态? 2. 如何添加自定义服务到`systemd`? 3. Ubuntu中如何查看系统启动日志? 4. 如何创建和管理用户级别的启动应用程序?</think>### Ubuntu 查看和管理开机启动项指南 在 Ubuntu 中管理开机启动项主要有以下几种方法,适用于不同级别的启动项管理: #### 1. **系统服务启动项 (systemd 管理)** 适用于系统级服务,使用 `systemctl` 命令: ```bash # 查看所有已启用的系统服务 systemctl list-unit-files --type=service --state=enabled # 禁用服务 (如禁用 apache) sudo systemctl disable apache2.service # 启用服务 sudo systemctl enable nginx.service # 查看服务状态 systemctl status ssh.service ``` #### 2. **用户图形界面启动项** 管理图形界面登录后自动启动的应用程序: ```bash # 打开 GUI 管理工具 gnome-session-properties ``` 或通过菜单:`应用程序 → 首选项 → 启动应用程序`[^1] #### 3. **终端配置文件启动项** 检查以下配置文件: - `/etc/rc.local`:系统级启动脚本(需执行权限) - `~/.bashrc` 和 `~/.profile`:用户级终端启动命令 - `cron` 任务:`@reboot` 定时任务 ```bash # 检查 cron 启动任务 crontab -l | grep "@reboot" ``` #### 4. **SysVinit 兼容管理 (旧系统)** ```bash # 安装管理工具 sudo apt install sysv-rc-conf # 交互式管理界面 sudo sysv-rc-conf ``` #### 5. **查看启动耗时** 分析系统启动性能: ```bash systemd-analyze blame # 查看各服务启动耗时 systemd-analyze plot > boot.svg # 生成启动时序图 ``` ### 最佳实践 1. **优先级建议**: - 系统服务 → 使用 `systemctl` - 图形应用 → 使用 `gnome-session-properties` - 脚本任务 → 使用 `/etc/rc.local` 或 `cron @reboot` 2. **安全提示**: - 修改系统级启动项需要 `sudo` 权限 - 禁用未知服务可能导致系统不稳定 - 备份配置文件:`sudo cp /etc/rc.local /etc/rc.local.bak` > 示例:禁用蓝牙开机启动 > `sudo systemctl disable bluetooth.service`
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值