Linux usage tips and linux source code installation tips

Source: Internet
Author: User

Linux usage tips and linux source code installation tips

This article describes the Linux usage skills that have been accumulated in normal times.

The hidden property of the file.
  • lsattr: List hidden properties of Objects
  • chattr: Modify the hidden attributes of an object

    [root@www ~]# chattr [+-=][ASacdistu] FileName+ : Add one attribute- : Remove one attribute= : Set to be the only attributes that the files have

Important options

  • A: Only file content can be appended, but content cannot be modified or deleted.
  • I: A file cannot be deleted, renamed, or created as a link to it. You cannot write content to the file.
SUID/SGID/Sticky Bit

If a SUID or SGID bit is set for an executable file, the file will have the permissions of the file owner (set SUID) or group (set SGID.

For example, a common user cannot enable the httpd service because the httpd service requires port 80, while a port lower than 1024 can only be used by the root user. If we set the httpd executable file owner to root and set the SUID bit, normal users can also enable the httpd service.

If Sticky is set for a directory, only the owner of the file can delete the file. In Linux, the/tmp directory sets this bit by default:

drwxrwxrwt 12 root root 16384 Mar 6 09:04 tmp/

The main usage is as follows:

SUID

  • For Files: run with the permissions of the file owner
  • For Directory: SUID cannot be set for directory

Set SUID:

chmod u+s FILE chmod 4755 FILE

SGID

  • For Files: run with the permissions of the file group
  • For a directory: the files in the directory inherit the attributes of the directory.

Set SGID:

chmod g+s FILE/DIR chmod 2771 FILE/DIR

Sticky

  • For Files: the Sticky bit cannot be set for the file.
  • For directories: only the owner of files in this directory can delete them.

Set Sticky:

chmod o+t DIR chmod 1777 DIR

Set special permissions with letters:

u+s g+s o+t

Use numbers to indicate special permissions, which are:

4 for SUID2 for SGID1 for Sticky
Special permissions for scripts

Note that SUID bits cannot be set for shell, python, perl, and other script files, because they are actually interpreted and run by bash, python, and perl interpreters. To make the script file have a function similar to SUID, we need a shell. The SUID/SGID bit can be set for this shell. The script actually works in the shell.

For example, we have a script/home/jh/bin/myscript.shThe owner is a common user, but the operation in the script requires the root permission. Now we use the C language to write this layer of shell, the name istranseuid.c:

/** author: JH Gao <gaopenghigh@gmail.com>* Create Date: 2012-06-05* Function: transmit euid and egid to other scripts* since shell/python/... scripts can't get suid permission in Linux*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#define BUFFSIZE 1024/** usually euid is the uid who run the program* but when stick is setted to the program* euid is the uid or the program's owner*/int main(int argc, char *argv[]) {    char *cmd = "/home/jh/bin/myscript.sh";    char *pars[] = {"/home/jh/bin/myscript.sh", "par1", "par2"};    /* set uid and gid to euid and egid */    setuid(geteuid());    setgid(getegid());    if (execvp(cmd, pars)) {        printf("error");        free(cmd);        exit(1);    }    free(cmd);}

Compile the program, set the desired user for the program, and then set suid. Then, you can use this user's permissions to execute scripts or commands:

$ gcc -t transeuid transeuid.c$ sudo chown root transeuid$ sudo chmod +s transeuid$ ./transeuid ......DO SOMETHING

However, it should be noted that such tricks pose great security risks.

/Etc/fstab file Error

At this time, the system cannot be started normally. At this time, you can start the system to enter the single user mode, while the root directory "/" in the change mode is read-only, you can use the following command to remount "/" to "read/write ":

[root@linux]# mount -n -o remount,rw /-n : mount but do not change /etc/mtab-o : options
Partprobe-do not restart to use the new Partition Table

partprobe: Reinitializes the kernel in memory of the partition table. After you change the partition settings, the system prompts you to restart to change the partition table in the kernel. Use partprobe to avoid restart.

The ubuntu system switches between the two KDM instances.

If you have installed GNOME and KDE at the same time, you may need to switch between the following two methods:

sudo dpkg-reconfigure gdm
Increase swap space
  • Create a new partition or file
  • Use mkswap to write special tags
  • Add new records to/etc/fstab
  • Activate the swap partition. Run swapon-a or swapon [SWAPFILE].
  • Run the swapon-s command to check the status of the swap partition.

Example:

dd if=/dev/zero of=/swapfile bs=1M count=100mkswap /swapfilevi /etc/fstab ...swapon -a
One Nic is bound to multiple IP addresses

Example:

The system is CentOS and the NIC is eth0. If you need to set an additional IP address, create a file
/etc/sysconfig/network-scripts/ifcfg-eth0:0
, Set IP information in the file.

If you need to set an IP segment, create a fileifcfg-ethX-rangeX:

Ifcfg-eth0-range0:

DEVICE=eth0-range0BOOTPROTO=staticHWADDR=08:00:27:24:C2:72ONBOOT=yesIPADDR_START=192.168.56.20IPADDR_END=192.168.56.30NETMASK=255.255.255.0
Lsof

The lsof command can list all open files. The most common function of this command is to find the "lost" space.

For example, we can use the df command to see that the/home partition is only 1 GB left, but the result of using the du command should be 5 GB.
In this case, some files are often deleted, but the file handles of these deleted files have not been released.
Use commands

lsof | grep -i deleted

Check which files have been deleted, but the file handle has not been released. kill or restart the response process to retrieve the "lost" space.

Sometimes, lsof can restore objects that have been accidentally deleted. For more information, see google.

Directory Length

The directory length is never 0, because it always contains two items:. and. The length of a symbolic link refers to the number of characters contained in the path name. Because the path name contains at least one character, the length is not 0.

Create a folder named "-f"

To create a folder named "-f", run the following command:mkdir -fMust fail.mkdir -- -fIt can be created successfully.

The difference between su and su-switching between the two recently used directories using "cd -"

When su is executed, the new shell inherits the current shell environment, and the su-simulates the actual root login session.

Quickly clear history
export HISTSIZE=0
In bash $Related Parameters
  • $0-Indicates the current file name.
  • $*-Separate all parameters with spaces to form a string
  • $@-Separate all parameters with spaces to form a string combination. And$*When being referenced,"$*"Is a string, and"$@"Contains multiple strings.
  • $#-Number of parameters passed to the Process
  • $?-The execution result of the previous command. The value is 0 if no error occurs.
  • $$-PID of this command
Bash technique, which combines the content of a variable into the name of another variable

EXAMPLE:

A_B_C_D="something"t1="B"t2="_D"eval echo \$A_${t1}_C${t2};
Bash command line input skills

UseCtrl+RTo search for previously used commands
UseCtrl+WDelete current time
UseCtrl+UDelete current row

Xargs

Xargs is powerful and used-l{}You can specify the parameter location:

cat hosts | xargs -I{} ssh root@{} hostname
Write safe bash scripts

The most common ones are:

  • set -eWhen an error occurs, the script exits.
  • set -uExit when bash finds there are no initialized variables

For more information, see: Write a robust Bash script.

Tar package objects listed in the specified list
cat yourlist.lst/etc/fstab/home/admin/bin/somefile.sh/home/mysql/somefile...tar cvzf xxx.tar.gz -T yourlist.lst
Specify a DNS server to query domain name records
dig @8.8.8.8 www.google.com
The most important parameters of the sort command are-k and-s:
-s, --stable              stabilize sort by disabling last-resort comparison

Stable indicates that the final order depends on the original order.

$ cat a.txtaABb$ sort -f a.txtaAbB$ sort -f -s a.txtaABb

In this example,-f indicates case-insensitive, and-s indicates that the order of the original files depends on the order of the original files.

-k, --key=POS1[,POS2]              start a key at POS1 (origin 1), end it at POS2 (default end of line).

Therefore, the data is sorted by the second column and should be written as follows:

sort -k1,1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.