编程小贴士

给你的编程提供小点子


Linux中设置服务自启动的三种方式

从来没有对linux的开启自启动关心过,都是运维的操作,今天要好好的学习下了。

有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务

主要用三种方式进行这一操作:

ln -s                       在/etc/rc.d/rc*.d目录中建立/etc/init.d/服务的软链接(*代表0~6七个运行级别之一)

chkonfig                命令行运行级别设置

ntsysv                   伪图形运行级别设置

 

注意:1.这三种方式主要用于以redhat为基础的发行版

2.如果还不知道运行级别是什么,那么最好先看看相关资料再实验

 

第一种方式:ln -s 建立启动软连接

在Linux中有7种运行级别(可在/etc/inittab文件设置),每种运行级别分别对应着/etc/rc.d/rc[0~6].d这7个目录

tips1000_2014-07-14_05-46-063

Tips:/etc/rc[0~6].d其实是/etc/rc.d/rc[0~6].d的软连接,主要是为了保持和Unix的兼容性才做此策

 

这7个目录中,每个目录分别存放着对应运行级别加载时需要关闭或启动的服务

由详细信息可以知道,其实每个脚本文件都对应着/etc/init.d/目录下具体的服务

K开头的脚本文件代表运行级别加载时需要关闭的,S开头的代表需要执行

tips1000_2014-07-14_05-46-062

因此,当我们需要开机启动自己的脚本时,只需要将可执行脚本丢在/etc/init.d目录下,然后在/etc/rc.d/rc*.d中建立软链接即可

[root@localhost ~]# ln -s /etc/init.d/sshd /etc/rc.d/rc3.d/S100ssh

此处sshd是具体服务的脚本文件,S100ssh是其软链接,S开头代表加载时自启动

如果需要在多个运行级别下设置自启动,则需建立多个软链接

这种方式比较繁琐,适用于自定义的服务脚本

如果系统中已经存在某些服务(比如安装apache时就会有httpd服务项),可以使用下面的两种方式

 

第二种方式:chkconfig

tips1000_2014-07-14_05-46-064

如果需要自启动某些服务,只需使用chkconfig 服务名 on即可,若想关闭,将on改为off

在默认情况下,chkconfig会自启动2345这四个级别,如果想自定义可以加上–level选项

tips1000_2014-07-14_05-46-065

上面我们先将sshd服务的所有启动级别关闭,然后使用–level选项启动自定义级别

Tips:–list选项可查看指定服务的启动状态,chkconfig不带任何选项则查看所有服务状态

 

第三种方式:ntsysv 伪图形

ntsysvchkconfig其实是一样的,只不过加上了图形而已

启动ntsysv有两种方式,一是直接在命令行中输入ntsysv,二是使用setup命令,然后选择系统服务

tips1000_2014-07-14_05-46-06

默认情况下,当前运行级别为多少,在ntsysv中设置的启动服务的级别便是多少

比如,我当前的运行级别是3,那么我在伪图形界面中选择启动服务后,它的运行级别也会是3

如果想自定义运行级别可使用ntsysv –level方式

tips1000_2014-07-14_05-46-061

 

以上三种操作需要保证服务脚本文件可执行,并且要有root权限

其中,第一种方式多用于自定义脚本,第二、三种多用于系统已存在的服务

比如ftp、samba、ssh、httpd等等

并且,要做相关设置需要弄清楚运行级别的问题

 

Tips:如果想手动启动某服务,传统的方式是 /etc/init.d 服务名 start

实际上还可以这样,service 服务名 start

tips1000_2014-07-14_05-46-09

 

80 Responses to “ Linux中设置服务自启动的三种方式 ”

  1. i love pron说道:

    oUn3JD That is a really good tip particularly to those fresh to the blogosphere. Brief but very accurate information Appreciate your sharing this one. A must read post!

  2. Very nice post. I absolutely appreciate this website. Keep it up!

  3. Some really prize content on this website , saved to fav.

  4. Oluwadamilare说道:

    pretty helpful stuff, overall I consider this is well worth a bookmark, thanks

  5. wow, awesome blog article.Much thanks again. Cool.

  6. omlet arcade说道:

    I think other web-site proprietors should take this site as an model, very clean and excellent user friendly style and design, as well as the content. You are an expert in this topic!

  7. I truly appreciate this blog article. Keep writing.

  8. private Hire说道:

    There is evidently a bundle to identify about this. I consider you made some nice points in features also.

  9. This blog was how do I say it? Relevant!! Finally I have found something that helped me. Cheers!

  10. Major thankies for the blog post.Much thanks again. Cool.

  11. Thank You For Your Info. I like to browse in various places on the internet, often I will go to Stumble Upon and read and check stuff out

  12. I value the blog post.Much thanks again. Awesome.

  13. The Lost Ways说道:

    You have made some good points there. I checked on the net to learn more about the issue and found most people will go along with your views on this site.

  14. Really enjoyed this blog article.Really thank you!

  15. Nwokolo Collins说道:

    Really enjoyed this article post.Thanks Again. Will read on

  16. Lashawn说道:

    Right now it looks like WordPress is the best blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  17. Many thanks for sharing this fine write-up. Very interesting ideas! (as always, btw)

  18. Nwokolo说道:

    very nice submit, i certainly love this web site, carry on it

  19. cay thong noel说道:

    UVB Narrowband Treatment Is a computer science degree any good for computer forensics?

  20. klm voucher说道:

    Thanks a lot for the blog.Much thanks again. Great.

  21. Well I truly liked reading it. This tip provided by you is very useful for proper planning.

  22. Utterly composed articles, Really enjoyed reading through.

  23. visit说道:

    There is apparently a lot to identify about this. I assume you made various nice points in features also.

  24. thanks in part. Good quality early morning!

  25. Supermodel Elena说道:

    Yeah ! life is like riding a bicycle. You will not fall unless you stop pedaling!!

  26. visit website说道:

    I really liked your blog.Much thanks again. Will read on

  27. useful说道:

    In my view, if all site owners and bloggers made good content as you did, the web will be a lot more useful than ever before.

  28. girls说道:

    Pretty section of content. I just stumbled upon your weblog and in accession capital to claim that I get

  29. serais incapable avons enfin du les os du. Il reste trois parcours magnifique elle,

  30. Investment tips说道:

    What as up mates, you are sharing your opinion concerning blog Web optimization, I am also new user of web, so I am also getting more from it. Thanks to all.

  31. whoah this blog is excellent i love reading your articles. Keep up the good work! You know, a lot of people are looking around for this information, you can aid them greatly.

  32. When I saw this page was like wow. Thanks for putting your effort in publishing this article.

  33. Blusa Tejidas说道:

    Just Browsing While I was surfing today I noticed a excellent article concerning

  34. Kangerlussuaq说道:

    Really appreciate you sharing this blog post.Much thanks again. Really Great.

  35. blue nike说道:

    pretty practical material, overall I believe this is worthy of a bookmark, thanks

  36. value说道:

    your website and keep checking for new details about once per week.

  37. this blog loading? I am trying to determine if its a problem on my

  38. Wow, awesome blog format! How lengthy have you been running a blog for? you made blogging glance easy. The overall glance of your site is magnificent, let alone the content!

  39. Very neat article.Really thank you! Fantastic.

  40. Open heaven说道:

    Thanks so much for the article.Really looking forward to read more. Want more.

  41. tm objection说道:

    I used to be able to find good info from your content.|

  42. scary maze说道:

    The Silent Shard This can most likely be very practical for a few of the positions I decide to do not only with my website but

  43. Thanks again for the blog article.Really thank you! Much obliged.

  44. junior说道:

    Seriously, such a important online site.|

  45. resorts说道:

    Very interesting points you have remarked, thank you for posting.

  46. There may be noticeably a bundle to learn about this. I assume you made certain nice points in options also.

  47. happy wheels说道:

    Really enjoyed this article post.Really looking forward to read more.

  48. Say, you got a nice article post.Thanks Again. Really Great.

  49. Some really fantastic info , Glad I noticed this.

  50. very handful of websites that happen to be detailed below, from our point of view are undoubtedly properly really worth checking out

  51. personal injury说道:

    Some really prize content on this site, saved to fav.

  52. This blog was how do I say it? Relevant!! Finally I have found something that helped me. Kudos!

  53. I will right away clutch your rss feed as I can not find your e-mail subscription hyperlink or e-newsletter service. Do you ave any? Kindly let me recognize in order that I could subscribe. Thanks.

  54. Billy graham说道:

    Thank you very much! I took it for myself too. Will be useful!!

  55. well defined说道:

    I think this is a real great blog.Thanks Again. Will read on

  56. Thank you for your blog post.Much thanks again. Cool.

  57. for details说道:

    Thanks to my father who told me concerning this weblog,

  58. It as enormous that you are getting thoughts from this post

  59. very nice post, i certainly love this website, keep on it

  60. Say, you got a nice article post.Really looking forward to read more. Really Cool.

  61. Yay google is my king aided me to find this great internet site!

  62. see this website说道:

    Very informative blog.Really looking forward to read more. Really Cool.

  63. miller electric说道:

    it looks good. I ave bookmarked it in my google bookmarks.

  64. Whispering Misty So sorry you will pass up the workshop!

  65. I really liked your blog post.Really looking forward to read more. Will read on

  66. Find out more说道:

    Whoa! This blog looks just like my old one! It as on a entirely different topic but it has pretty much the same layout and design. Outstanding choice of colors!

  67. In the great I always visit your blog everyday to read new topics.,:~-~

  68. buy说道:

    Really enjoyed this post.Really thank you! Awesome.

  69. You have made some really good points there. I looked on the net for more info about the issue and found most individuals will go along with your views on this website.

  70. This is my first time go to see at here and i am really happy to read everthing at alone place.|

  71. Business news说道:

    Im obliged for the article post.Really looking forward to read more. Much obliged.

  72. Agen Joker123说道:

    They were why not look here permanently out. There was far too much fat on

  73. Really enjoyed this article post.Really thank you! Really Great.

  74. more info说道:

    Wow! This could be one particular of the most beneficial blogs We ave ever arrive across on this subject. Basically Magnificent. I am also an expert in this topic so I can understand your effort.

  75. magnificent post, very informative. I wonder why the other experts of this sector don at realize this. You should proceed your writing. I am sure, you have a huge readers a base already!

  76. Wow, great article.Much thanks again. Really Cool.

  77. Muchos Gracias for your article.Really looking forward to read more. Will read on

  78. good day说道:

    wants to find out about this topic. You realize a whole lot its almost tough to argue with you (not that I really will need toHaHa).

  79. Website worth visiting below you all find the link to some sites that we think you should visit

  80. read more说道:

    Right now it seems like WordPress is the best blogging platform available right now. (from what I ave read) Is that what you are using on your blog?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>