When developing a website, you often need to configure your own Linux server. This document records the initial process for configuring the Linux server, that is, what to do next after the system is installed. This is my own summary and memo. if you have any omissions, please add them. Linux server
When developing a website, you often need to configure your own Linux server.
This document records the initial process for configuring the Linux server, that is, what to do next after the system is installed. This is my own summary and memo. if you have any omissions, please add them.
The following operations are applicable to Debian and Ubuntu systems. Other Linux systems are similar, that is, some commands are slightly different.
Step 1: log on as the root user
First, use the root user to log on to the remote host (assuming the IP address is 128.199.209.242 ).
- ssh root@128.199.209.242
At this time, the command line prompts a warning, indicating that this is a new address and there is a security risk. Type yes to accept. Then, you can successfully log on to the remote host.
Next, modify the password of the root user.
- passwd
Step 2: Create a user
First, add a user group (this is assumed to be the admin user group ).
- addgroup admin
Then, add a new user (assumed as bill ).
- useradd -d /home/bill -s /bin/bash -m bill
In the preceding command, the parameter d specifies the user's home directory, and the parameter s specifies the user's shell. the parameter m indicates that if the directory does not exist, the directory is created.
Next, set the password for the new user.
- passwd bill
Add a new user (bill) to the user group (admin ).
- usermod -a -G admin bill
Next, set sudo permissions for the new user.
- visudo
The cmddo command will open the sudo setting file/etc/sudoers and find the following line.
- root ALL=(ALL:ALL) ALL
Add another row under this row.
- root ALL=(ALL:ALL) ALL
- bill ALL=(ALL) NOPASSWD: ALL
The above NOPASSWD indicates that you do not need to enter a password when switching to sudo. it is easier for me to do so. For security reasons, you can also force a password.
- root ALL=(ALL:ALL) ALL
- bill ALL=(ALL:ALL) ALL
Then, log out of the root user and log in as a new user. check whether everything is normal until this step ends.
- exit
- ssh bill@128.199.209.242
Step 3: Set SSH
First, make sure that the local machine has an SSH public key (usually a file ~ /. Ssh/id_rsa.pub). If not, use the ssh-keygen command to generate one (refer to the SSH tutorial I wrote ).
Open another shell window on the local machine to copy the local public key to the authorized_keys file on the server.
- Cat ~ /. Ssh/id_rsa.pub | ssh bill@128.199.209.242 'mkdir-p. ssh & cat-> ~ /. Ssh/authorized_keys'
- # Or on the server side, run the following command
- Echo "ssh-rsa [your public key]"> ~ /. Ssh/authorized_keys
Then, go to the server and edit the SSH configuration file/etc/ssh/sshd_config.
- sudo cp /etc/ssh/sshd_config ~
- sudo nano /etc/ssh/sshd_config
In the configuration file, change the default SSH port 22 to any integer from 1025 to 65536 (this is assumed to be 25000 ).
- Port 25000
Then, check whether the settings are set as follows to ensure that the # above is removed.
- Protocol 2
- PermitRootLogin no
- PermitEmptyPasswords no
- PasswordAuthentication no
- RSAAuthentication yes
- PubkeyAuthentication yes
- AuthorizedKeysFile .ssh/authorized_keys
- UseDNS no
In the preceding section, root user logon is disabled and password logon is disabled.
Next, at the end of the configuration file, specify the user that is allowed to log on.
- AllowUsers bill
Save and exit file editing.
Then, change the permissions of the authorized_keys file.
- sudo chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh/
Then, restart SSHD.
- Sudo service ssh restart
- # Or
- Sudo/etc/init. d/ssh restart
The following steps are optional. On the local machine ~ /. Create the config file in the ssh folder. the content is as follows.
- Host s1
- HostName 128.199.209.242
- User bill
- Port 25000
Finally, open a shell window on the local machine to test whether SSH can log on smoothly.
- ssh s1
Step 4: configure the runtime environment
First, check the region settings of the server.
- locale
If the result is not a en_US.UTF-8, we recommend setting it to it.
- sudo locale-gen en_US en_US.UTF-8 en_CA.UTF-8
- sudo dpkg-reconfigure locales
Then, update the software.
- sudo apt-get update
- sudo apt-get upgrade
Finally, make some security settings as needed, such as building a firewall, closing ports other than HTTP, HTTPs, and SSH, and installing Fail2Ban, for details, refer to Securing a Linux Server.
Link: http://www.ruanyifeng.com/blog/2014/03/server_setup.html