ARTS Week 24

Algorithm

This week’s LeetCode problem is 69. Sqrt(x)

Given a non-negative integer x, compute and return the square root of x.

Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.

Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.

Dichotomy can be used for processing, because using multiplication to judge may overflow, so a better processing strategy is to use division for calculation. And the final result only needs to keep the integer part, so when mid+1 > x / (mid+1) and mid < x / mid, then the final result at this moment is mid.

Review

This week’s Review is for the following article: 3 ways I configure SSH for privacy

The author describes how to optimize the SSH experience and protect the server from unauthorized access. Here are the author’s specific recommendations

  1. Change the default port

The author had an SSH server default port TCP 22 over one cloud provider, and the average attacks per minute were 24. After changing the port to a much higher number, TCP 45678, the average of people connecting and guessing any username or password was two per day.

To change the default port for SSH, open /etc/ssh/sshd_config in your favorite text editor and change the value of the Port from 22 to some number greater than 1024.

Once you’ve changed the port and saved the file, restart the SSH server:

2. No more passwords

The author recommends using asymmetric keys for authentication, one needs to generate an SSH key pair first, the public key needs to be transferred to the server, and the other is private and shared with anyone. Use ssh-keygen to create a new key, and use the -t option to specify a good, up-to-date encryption library, such as ed25519:

Follow the prompts to enter the location and custom name of the key, and you can specify a password. Of course, you can also follow the default configuration all the way.

Next use the ssh-copy-id command to copy the key to the server, for example the following command copies the public key to a server named example.com.

Once you can log in without a password from the computer, edit the option PasswordAuthentication in sshd_config to allow password login to no

Finally, use sudo systemctl restart sshd to restart the ssh service.

3. Decide who can log in

Most Linux distributions do not allow root users to log in via SSH. Open the SSH configuration file sshd_config and add the following line:

This will only allow the three users jgarrido, jane, and tux to log in remotely to operate.

Apart from that, there are tons of useful functions and options in the sshd_config file, and you can also use applications like Fail2ban to further secure your SSH service.

Tip

The difference between the three comparison functions strcmp, strncmp, and memcmp in the C language, all three of them can be used to compare whether the strings are the same, when the return value is equal to 0, it means the same, and the return value is not 0 means not same. First compare according to their statement:

function name function declaration whether to pass in the length to be compared how to end the comparison strcmp int strcmp(const char *s1, const char *s2); not required ends s1 and s2 with \0 strncmp int strncmp(const char *s1, const char *s2, size_t n); Required Use the incoming parameter n as the comparison length and \0 as the end of s1 and s2 memcmp int memcmp(const void *s1, const void *s2, size_t n); Required Use the incoming parameter n as the comparison length

It should be noted that strncmp() will have two conditions to judge the end, one is the incoming length, the other is the end of the string \0, the content after the string \0 and will not be compared.

Share

This week, of course, I want to share a separate article that I have been dragging for about half a year. The following three articles mainly introduce how to use C language to read and write CSV files. I am very happy to hear your thoughts and suggestions :)

// Note: These essays are not published yet

Reading and Writing CSV files in C language — Part 1. Basics

Reading and Writing CSV files in C language — Part 2. Advanced — writing CSV

Reading and Writing CSV files in C language — Part 3. Advanced — reading CSV

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KeepNewbie_yan

KeepNewbie_yan

5 Followers

A programmer. Share knowledge of programming, operating system, Linux kernel, and reading, thinking etc. Let us maintain a beginner mend and grow together!