RE:137

时间是唯一致死的毒药。

Linux 小笔记

/usr meaning#

Unix System Resource

/bin, /sbin and /usr/bin, /usr/sbin#

Ref: Filesystem Hierarchy Standard - Wikipedia

  • /bin : For binaries usable before the /usr partition is mounted. This is used for trivial binaries used in the very early boot stage or ones that you need to have available in booting single-user mode. Think of binaries like catls, etc.
  • /sbin : Same, but for binaries with superuser (root) privileges required.
  • /usr/bin : Same as first, but for general system-wide binaries.
  • /usr/sbin : Same as above, but for binaries with superuser (root) privileges required.

List all files and directories by size#

du -sh * | sort -rh

For du:

  • -s: Display an entry for each specified file. (Equivalent to -d 0)
  • -d depth : Display an entry for all files and directories depth directories deep.
  • -h: “Human-readable” output. Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte based on powers of 1024.

For sort:

  • h, --human-numeric-sort, --sort=human-numeric: Sort by numerical value, but take into account the SI suffix, if present. Sort first by numeric sign (negative, zero, or positive); then by SI suffix (either empty, or ‘k’ or ‘K’, or one of ‘MGTPEZY’, in that order); and finally by numeric value. The SI suffix must immediately follow the number. For example, '12345K' sorts before '1M', because M is “larger” than K. This sort option is useful for sorting the output of a single invocation of ‘df’ command with -h or -H options (human-readable).
  • r, --reverse: Sort in reverse order.

Show welcome message when logging into the shell#

Modify the file: /etc/motd. If it doesn’t exist, then create one.

Count the number of files in a directory#

ls -l | grep '^-' | wc -l   # Counting the files
ls -l | grep '^d' | wc -l   # Counting the directories

Users and groups (add, modify, ID… )#

adduser [UserName]    # Adding user
userdel [Username]    # Delete user
passwd [UserName]     # Change pw
groupadd [GroupName]  # Create group
usermod -u [NewUserID] [UserName]     # Change user ID
groupmod -g [NewGroupID] [GroupName]  # Change group ID
usermod -g [GroupName] [UserName]     # Change user’s primary group
usermod -aG [GroupName] [UserName]    # Add user to a group
usermod -aG sudo [UserName]           # Add to sudo group

Sudo without password#

edit the file /etc/sudoers and append the following entry *to the end (important!)*:

[YourUserName] ALL=(ALL) NOPASSWD:ALL

Display information about the CPU#

  1. lscpu
  2. cat /proc/cpuinfo

Locale meanings#

Ref: Linux 的 locale、LC_ALL 和 LANG - LC_coding

Effect order: LANG < LC_* < LC_ALL (Most important!)

Easiest: export LC_ALL=C

For more detailed information, please check man locale

Python virtual environment#

  • Setup:
sudo apt install python3-venv
python -m venv venv-path
  • Activate:
source venv-path/bin/activate

Please refer to the official document.

Modify the host#

First change the name:

hostnamectl set-hostname server1

Then edit the /etc/hosts file. Find all references to old name and replace with new name except for the entries 127.0.0.1

SSH keygen#

SSH supports several public key algorithms for authentication keys. These include:

  • rsa - an old algorithm based on the difficulty of factoring large numbers. A key size of at least 2048 bits is recommended for RSA; 4096 bits is better. RSA is getting old and significant advances are being made in factoring. Choosing a different algorithm may be advisable. It is quite possible the RSA algorithm will become practically breakable in the foreseeable future. All SSH clients support this algorithm.
  • dsa - an old US government Digital Signature Algorithm. It is based on the difficulty of computing discrete logarithms. A key size of 1024 would normally be used with it. DSA in its original form is no longer recommended.
  • ecdsa - a new Digital Signature Algorithm standarized by the US government, using elliptic curves. This is probably a good algorithm for current applications. Only three key sizes are supported: 256, 384, and 521 (sic!) bits. We would recommend always using it with 521 bits, since the keys are still small and probably more secure than the smaller keys (even though they should be safe as well). Most SSH clients now support this algorithm.
  • ed25519 - this is a new algorithm added in OpenSSH. Support for it in clients is not yet universal. Thus its use in general purpose applications may not yet be advisable.

The algorithm is selected using the -t option and key size using the -b option. The following commands illustrate:

ssh-keygen -t rsa -b 4096
ssh-keygen -t dsa
ssh-keygen -t ecdsa -b 521
ssh-keygen -t ed25519

Input/Output Redirection in the Shell#

Input/Output Redirection in the Shell

Bash One-Liners Explained, Part III: All about redirections

See all packages manually added by apt#

apt list --manual-installed

Utilizing multi core for tar+gzip#

tar -c --use-compress-program=pigz -f tar.file dir_to_zip

Rename multiple files#

#!/bin/bash

for file_old in `ls | grep old`
do
    file_new=`echo "$file_old"|sed 's/old/new/g'`
    mv $file_old $file_new
done

Find files and execute mv command#

find [dirname] -name "file.name" -exec mv {} [destination] \;

Modify timezone#

timedatectl                                  # 查看时区信息
timedatectl list-timezones | grep -i Asia    # 查找可用时区
sudo timedatectl set-timezone Asia/Shanghai  # 修改时区

Ref: 如何设置或修改 Linux 时区

chmod -h [user]:[group] [softlink]

Set up password authentication with Nginx#

  • Use htpasswd to generate username and password:
sudo apt install apache2-utils

sudo htpasswd -c /etc/nginx/.htpasswd [username]  # For the first time
sudo htpasswd /etc/nginx/.htpasswd [another_user] # After
  • Add to Nginx config file:
server {
    ...
    location / {          # Add two lines here in location
        ...
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
}

Ref: How To Set Up Password Authentication with Nginx on Ubuntu 14.04

scp error "scp: realpath [FILEPATH]: No such file"#

This seems to be caused by OpenSSH version update (from 8 to 9). Add -O to solve the problem:

scp -r -O [FILEPATH] remote:[FILEPATH]