preface

In the previous article, how do I install normal applications on Linux? This article introduces how to install application programs in Linux, mainly for common application programs, and to install server programs in Linux, not only requires us to be able to install, but also requires us to be able to configure services. Therefore, this time I will take you to understand the basic method of server installation and configuration.

Before we do that, let’s take a look at some services-related knowledge to help us understand how Linux services work.

Linux runtime level

Linux has the following seven runlevels:

  1. System shutdown mode [0] : The default running level of the system cannot be set to 0; otherwise, the system cannot start normally
  2. Single user mode [1] : the user has the root permission and is used for system maintenance. Remote login is prohibited, similar to the secure mode in Windows
  3. Multi-user mode [2] : Without NFS network support, start XWindows
  4. Full multi-user text mode [3] : NFS is available, and the console command line mode is entered after login
  5. System not in use [4] : Reserved generally unused, in some special cases can be used to do some things, such as laptop battery exhausted, can switch to this mode to do some Settings
  6. Graphical mode [5] : Enter GUI mode after login and start XWindows system
  7. Restart mode [6] : The default level cannot be set to 6; otherwise, the machine will start and run repeatedlysudo init 6Command and the machine will restart

On the command line, type the following command to view the current runlevel:

runlevel
The current runlevel is 5, i.e. graphical mode
N 5
Copy the code

Type the following command to change the current runlevel:

sudo init [0 - 6]
Copy the code

Why do we need to know about runlevels? Because each runlevel has its own service that starts by default, if we want our service to start automatically, we simply place the executable file or link to it in the specified directory.

Services started by default for each runlevel

The default service for each runlevel on Linux is in /etc/rc$runlevel.d/. $runlevel = 5; $runlevel = 5; /etc/rc5.d/

ll /etc/rc5.d/
# the resultslrwxrwxrwx 1 root root 15 Oct 27 2016 S01acpid -> .. /init.d/acpid* lrwxrwxrwx 1 root root 17 Oct 27 2016 S01anacron -> .. /init.d/anacron* lrwxrwxrwx 1 root root 16 Oct 27 2016 K01apport -> .. /init.d/apport* ..Copy the code

Rc is short for Run Command.

You can see that in this directory all the directories start with S and K to point to.. /init.d/. These links point to services that need to be started and closed by default at the start level. The services starting with S(tart) need to be started, while the services starting with K(ILL) do not need to be started. The /etc/init.d/ directory, of course, houses all the server programs, which are called daemons.

So what if we write a daemon program and want it to start automatically after startup? In fact, it is very simple, just need the following 3 steps:

  1. The executable file of the programyou_appCopy to/etc/init.d/you_app
  2. Add a soft link to the program in the runlevel directory you want, for example in/etc/rc5.d/Under the increasingyou_appThe soft link and replace the program name asS[num]you_app, e.g.S02you_app. This indicates that the program is automatically started at the run level.
  3. runupdate-rc.dCommand to update system Settings in the/usr/sbin/update-rc.d

In addition, here are four categories you need to know:

  1. /etc/: Stores configuration files related to the system and programs
  2. /etc/init/: stores configuration files related to system startup
  3. /etc/init.d/: Stores the system daemon process
  4. /etc/rc[0 - 6].d: Stores the services that need to be started and shut down for each runlevel

The basic principles of understanding these can be, the following introduction to the installation of the server program. Why I always emphasize the principle, because I want you to read my article can learn how to take the initiative to think, take the initiative to ask questions, take the initiative to solve, because 90% of people’s progress is to rely on yourself, always rely on others will suffer.

Server program installation roadmap

The server programs described above are also called daemons, such as Linux’s famous init daemon, HTTP, HTTPD, FTP, and so on. Most of these non-native server installation methods can be installed using apt-get, but after installation, we need to manually configure, after all, it is the server, it needs to know how you want it to work.

Before configuring the server, we need to know where the configuration file of the server program is located. As mentioned above, the configuration file of the server program is stored. Again, we thought of using man. For example, if we want to know where the configuration file of the SSH service is, we can go directly to man SSH and locate the FILES field. Below this field is the path of the related file of the service.

FILES
	...
	/etc/ssh/ssh_config
		Systemwide configuration file.  The file format and configurationoptions are described in ssh_config(5).
	...
Copy the code

This file is the SSH system configuration file, using MAN can solve most of the problems, if using man does not work, then Google and StackOverflow can also help you, as for Baidu do not use.

The path of a server program is similar. If you are familiar with the configuration of a server program, you will know that most server programs are installed under /bin/ or /sbin/. You can find the mapping of these programs under /etc/init.d/.

Conf configuration file can be found in /etc/ssh/ssh_config. For example, /etc/ssh/ssh_config can be found in /etc/ssh/ssh_config. Other services are basically the same.

Commands to operate services

After installing the service, we need to start or shut down the service. The following two methods can be used:

Methods a

cd /etc/init.d/
service_name start/stop/restart/status
Copy the code

Method 2

service service_name start/stop/restart/status
Copy the code

The second method is a encapsulation of the first method. It is usually easier to use the second method. If one method does not start correctly, the other method can be used.

The following uses a Samba server installation as an example to practice installing the server program.

Exercise: Installing the Samba server

Samba is a service that allows Windows to access Linux files over the network. Learn to use it to easily share Windows and Linux files. I use Ubuntu to install, other Linux are more or less different, I can only tell you the basic method of installation, not all the problems listed, you need to learn to use Google search, so that you can practice your problem solving skills.

Uninstall the iptables

Uninstall the iptables firewall in Ubuntu:

sudo apt-get remove iptables
Copy the code

Install samba

Type the following command to install Samba:

sudo apt-get install samba
Copy the code

Configuring samba

View the Samba configuration file:

sudo vim /etc/samba/smb.conf
Copy the code

This file is a Samba configuration file that allows you to customize some of the properties that clients access to Samba. For example, you can add the following properties for root access:

The meaning of the # field is not introduced here, Google
[root]
	comment = root dir
	browseable = yes
	path = /
	writeable = yes
	valid users = root
Copy the code

Then set the password for root to access Samba:

smbpasswd -a root
Copy the code

If method 2 fails to start, use method 1 to start the service:

/etc/init.d/samba start
Copy the code

Enter your password, then check the status:

/etc/init.d/samba status
Copy the code

If the circle below is green, the startup is successful. Otherwise, the startup fails:

Low SMBD. Service - Samba SMB Daemon the Loaded: the Loaded (/ lib/systemd/system/SMBD. Service; enabled; vendor preset: enabled) Active: active (running) since Wed 2017-07-05 10:29:08 CST; 27min agoCopy the code

To restart, use the following command:

/etc/init.d/samba restart
Copy the code

Configure Windows

In order for Windows to be able to access Linux servers, the following two conditions must be met:

  1. The two are connected to the same network, such as the same wifi or directly connected with the network cable or connected to the same switch
  2. They can be reciprocalpingTong, this is the key

Generally, if the ping succeeds, the access probability is high. The ping operation is as follows:

First look at the IP of both:

# Type the following command under Windows CMD to view Windows IP
ipconfig
Example: 192.168.1.1

# Type the following command under Linux to view the Linux IP
ifconfig
Example: 192.168.1.2
Copy the code

Then ping each other’s IP addresses on the two machines to see if there is a reply:

# WindowsPing 192.168.1.2 instead# see if there is a reply

# LinuxPing 192.168.1.1# see if there is a reply
Copy the code

If yes, the connection is successful. Otherwise, you can disable the Windows and Linux firewalls.

Connect the Linux

If you can ping, you can access Linux from a browser in Windows and map Samba as a Network drive in Windows for later use.

conclusion

This article mainly introduces the idea and method of installing the server program on Linux, and gives the example of installing samba server as a practice. In addition, I recommend that you try to install other services, such as FTP, TFTP, NFS, etc., you need to check Google and StackOverflow to find the answer to the specific installation process. Most of the problems you encounter are already encountered by others. It depends on your ability to find that answer, wish you a happy study 🙂

This article was originally published in the wechat public number “cdeveloper”, programming, career, life, follow and reply to the keyword “Linux”, “machine learning” and so on to obtain free learning materials.