95 Tips for Linux

Source: Internet
Author: User
Tags aliases auth i18n disk usage dmesg rdesktop

95 Tips for Linux

by Web full stack engineer on March 27, 2012

Here's a summary of some of the tips for using Linux

1. Automatic disk repair for Redhat abnormal shutdown

Log in to the server first, and then add a file autofsck in the/etc/sysconfig, as follows:
Autofsck_def_check=yes
Prompt=yes

2. Change the last modified time of the file or directory (into the current time)
Execution format: Touch name can be a file or directory name. )

3. How to set up welcome information after login

Modify the/ETC/MOTD and write the text inside.

4. How to set the Welcome interface before login

Modify/etc/issue or issue.net to write text inside.
The issue content is present in the native login user interface, while the issue.net is present when the user Telnet through the network.

5, how to modify the network card MAC address

You must first turn off the network card device, otherwise you will report that the system is busy and cannot be changed.
The command is:/sbin/ifconfig eth0 down
Modify the MAC address, which is simpler than the modifications in Windows.
The command is:/sbin/ifconfig eth0 hw ether 00:aa:bb:cc:dd:ee
Re-enable NIC/sbin/ifconfig eht0 up
The MAC address of the NIC is changed to complete.

6. Create Aliases/delete aliases

Alias cp= ' Cp-i '
Unalias CP

7, AddUser m-g cvsroot-s/bin/false

Add user m, parameter-s/bin/false indicates that the user is not allowed to log on directly to the server
ID m
Displays the UID and GID number of the M user.
8. Force unload RPM Package

Rpm-e–nodeps Package Name
#个别不正常情况下:
Rm-f/var/lib/rpm/__*
Rpm–rebuilddb

9, deny other users except the root user login

Touch/etc/nologin
can also be added in/etc/passwd! Restrict login to specified user

10. Allow only one IP login, deny all other IPs

Write in/etc/hosts.allow:
sshd:1.2.3.4
Write in/etc/hosts.deny:
Sshd:all
You can also use iptables:
Iptables-i input-p tcp–dport 22-j DROP
Iptables-i input-p tcp–dport 22-s 1.2.3.4-j ACCEPT

11. Prohibit a user from SSH login

Add in/etc/ssh/sshd_conf
Allowusers User Name
Or
Allowgroups Group Name
Or
Denyusers User Name

12. Set Login blacklist

Vi/etc/pam.d/sshd
Increase
Auth required/lib/security/pam_listfile.so item=user Sense=deny file=/etc/sshd_user_deny_list onerr=succeed
All users inside the/etc/sshd_user_deny_list are denied SSH login

13. Check the name of the group you belong to

Execution format: Groups

14. Modify files/folders to the user group (Support-R)

Chown. Group name File name (Note: Do not forget the group name ".", ":" Also available)
You can also chgrp the group name file name
Chown user name. Group name File name (also modify the owning user and user group)

15. Use the Fuser command to see which processes are using the files on this partition:

Fuser–v–m/usr
If there are no important processes, use the following command to stop them:
Fuser-k–v–m/usr
You can then re-mount these file systems.

16. Network Wake-up host

Ether-wake Target Nic Mac

17. How to find files between 500K and 1000K size

Find/-type f-size +500k-and-size-1000k

18, let the host does not respond to ping

Echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all
If you want to recover, use
echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all
#必须是用命令改, cannot be VI modified

19. Automatic logout of root

Edit your configuration file "Vi/etc/profile" and add the following line after "histsize=":
tmout=300
#300, which means 300 seconds.

20, LS only lists the directory

LS-LF | grep ^d
LS-LF | grep/$
Ls-f | grep/$

21, let cron task does not give feedback information

* * * * * cmd >/dev/null 2>&1

22. lsof (list open files) usage

Lsof-i: XX
Lsof Abc.txt shows the process of opening the file Abc.txt
Lsof-i: 22 Know what program 22 port is running now
Lsof-c NSD Show NSD process now open file
Lsof-g GID shows the process of attribution to GID

23, change the sshd port

Add a row to/etc/ssh/sshd_config: Port 2222,/etc/init.d/sshd Restart Restart daemon

24. Prevent anyone from using the SU command to become root

VI/ETC/PAM.D/SU, add the following two lines at the beginning:
Auth sufficient/lib/security/pam_rootok.so
Auth required/lib/security/pam_wheel.so Group=wheel
Then add the user to the "wheel" group: CHMOD-G10 Usernam

25, how to let SSH only allow the specified user login

Method 1: Add the/etc/pam.d/sshd file
Auth Required pam_listfile.so item=user sense=allow file=/etc/sshusers onerr=fail
Then set up the Sshusers file under/etc, join the username that allows the SSH service (a single line per user name), and restart the sshd

26. Using SSH to copy files

1. Copy b from a (push over) #scp-rp/path/filename [email Protected]:/path
2, copy from B to a (pull over) #scp-RP [email Protected]:/path/filename/path

27. Linux machine mounts shared files on Windows

Windows ip:192.168.1.1
Mount-t Smbfs-o Username=massky,password=massky//192.168.1.1/dbf/mnt/share
If you want to automatically mount the machine restart, Vi/etc/fstab finally added:
192.168.1.1/dbf/mnt/share Smbfs Defaults,auto,username=massky,password=massky 0 0

28. Customizing the Linux prompt

The prompt in bash is specified by an environment variable $PS1. Use export $PS 1 to view the current values, compare straight
The common cue can be set to export ps1= "[\[email protected]\h \w]\$". Where \u represents the user name, \h represents the host
Name, \w represents the last level of the current working directory, and if it is a normal user \$ displays the $,root user display #.

29. Empty Files

echo > FileName

30. DNS-related

Host-a domain.com #显示相关资讯都列出来
Host domain.com 202.106.0.20 #用202.106.0.20 This DNS server query domain.com

31, front and back task related

Jobs lists the processes that belong to the current user
BG moves the process to the background (Background)
FG moves the process to the foreground (Foreground)
In case you forget to use "&" when running the program, and do not want to re-execute. You can use CTRL + Z to suspend the program, and then typed the BG command, so the program continues to run in the background.

32, find the current directory seven days before the file, and delete

Find./-mtime +7-type f-exec rm {} \;

33. Produce files of the specified size (bs*count)

DD If=/dev/zero of=filename bs=1000000 count=10

34. Locate the file in the current directory and change the extension

Change all. ss files to. aa
# Find/-name "*.SS"-exec rename. ss. aa ' {} ';

35. Modify the system time

Date-s "2005-6-4 17:26″

36. Let the server automatically synchronize the time

0 1 * * */usr/sbin/ntpdate 210.72.145.44
or 0 1 * * * rdate-s time.nist.gov

37. Solve the problem of too many open files

Set the process file descriptor limit in the etc/security/limits.conf configuration file:
* Soft Nofile 2048
* Hard Nofile 4096
System level file descriptor limit and timeout time modification, add the following two lines to the/etc/rc.d/rc.local startup script:
# increase System-wide file descriptor limit.
echo 65536 >/proc/sys/fs/file-max
echo >/proc/sys/net/ipv4/tcp_fin_timeout
#一般情况下, the maximum number of open files is reasonably set to 256 per 4 m of physical memory, such as 1G memory can be set to 65536,
#而最大的使用的i节点的数目应该是最大打开文件数目的3倍到4倍

38. How to package a directory with tar, remove some subdirectories or specify files

Add parameter –exclude, can add file name or directory name, can write more
Tar cvf–exclude {dirname,filename} #dirname不要加/

39. Modify the server time zone under Terminal

/usr/sbin/timeconfig
or direct #/etc/sysconfig/clock

40. Low memory 256M Prompt when startup is off

#vi/etc/rc.sysinit #把最后六行注释掉
or #vi/var/lib/supportinfo
Turn down the value of the minram:256m.

41. Find a specified "string" in a multi-level directory

grep string-r/etc/sysconfig/
Find./pathname/-name ' * ' | Xargs grep ' string '

42, a command to occupy the CPU

#yes string #有时候测试用得上. Crazy CPU

43. Kill related

Kill-stop [PID]
Send Sigstop (17,19,23) to stop a process without destroying it.
Kill-cont [PID]
Send Sigcont (19,18,25) to restart a stopped process.
Kill-kill [PID]
Send Sigkill (9) forces the process to stop immediately and does not implement a cleanup operation.
Kill-9-1
Stop all the processes you have.

44, in the current directory to build a Bak directory, and then CP * BAK, will be prompted to skip Bak, there are other ways to exclude the specified file (clip)?

Ls-f|grep-v \/|xargs-i cp {} bak #推荐
or find! -name "./bak"

45. Show process number according to process name

# pidof HTTPD
1846 1845 1844 1843 1842 1841 1840 1839 1820

46, E2fsck

Check whether the partition of the Linux ext2 file system is working properly, check if the/dev/hda5 is normal, fix it automatically if there is an exception, and set the answer [YES] if you have questions and answers:
E2fsck-a-y/dev/hda5

47. Reverse Output

Rev reverse output (in behavioral units)
TAC Reverse output (full text)

48. Display Terminal number

Tty

49, the number of file lines/Word Count

Wc–l File Line Count
Wc-w number of words in file calculation files
Wc-c number of characters in file calculation files

50, out of each line 5th to 9th characters

cut-b5-9 file.txt

51. Delete the columns appearing in the text file

Uniq

52. Return to the path where the file is located

Dirname/bin/tux #将返回/bin

53, FCITX in English environment under normal use

#vi ~/.BASHRC
Xport lc_ctype= "ZH_CN. Utf-8″
Export xmodifiers= "@im =FCITX"
Export XIM=FCITX
Export XIM_PROGRAM=FCITX
#gnome-session-properties can add Fctix to log in and start from

54, Split split merge file

split-b1440k a_whopping_big_file Chunk #拆
Cat chunk* > A_whopping_big_file #合
55. How to know what library files a command uses

For example, to know what library files The LS uses, you can use:
$ ldd/bin/ls

56. How to make a user process still running after the user exits the system

Use Nohup command &, for example: Nohup wget-c Ftp://test.com/test.iso
#这样即使用户退出系统, the wget process continues to run until the Test.iso download is complete

57, how to limit the user's minimum password length

Modify the value of the Pass_min_len inside the/etc/login.defs. For example, limit user Minimum password length is 8:
Pass_min_len 8

58. How to remove the root command history for added security

In order to set the system not to log the commands executed by each person, it is set in/etc/profile:
Histfilesize=0
Histsize=0
Or:
Ln-s/dev/null ~/.bash_history

59. How to test HDD performance

Using Hdparm-t-T/DEV/HDX, you can test the hard disk's buffer-cache reads and buffered disk reads two data, which can be used as a reference for hard disk performance.
The HDPARM-C3/DEV/HDAX can also be used to set the hard drive to 32bit transmission to speed up the data transfer.

60, how to list the space occupied by a directory

Du or Du-s or du-k
Du-s | Sort-n can quickly find out that the directory is the largest.
With DF, you can see the size of the installed file system and the amount of space remaining.
Quota-v View the user's disk space information if you use quota to limit the size of the user space.

61, how to make a new user after the first login force change password

#useradd-P ' testuser; chage-d 0 TestUser

62, in Linux sometimes boot does not automatically check the new hardware, the newly installed network card can not be found. How can i solve it?

A: The service that automatically checks for new hardware is kudzu, and the user can start the service with the "NTSYSV" command. The next time you restart, you will find the user's new NIC.

63, how to make the system password and Samba password consistent, and allow users to modify their own password.

Use the Web interface to synchronize changes to system passwd and samba password
Download http://changepassword.sourceforge.net/
installation is possible. Read the Readme first.
Additional:
Pour the system users into Samba users in batches.
less/etc/passwd | Mksmbpasswd.sh >; /etc/samba/smbpasswd

64. Change the GUI or character interface for Linux startup

Cd/etc
VI Inittab
Id:5:initdefault: where 5 represents the default graphical interface
Change Id:3: Initdefault:3 denotes character interface

65. Configure which IPs can be used by SMB.

Cd/etc/samba
Vi smb.conf
Find the hosts allow = 192.168.1. 192.168.2.127.
Modify which machines are used, note that the IP is separated by commas
Example:
Hosts Allow =192.168.1.110,192.168.1.120

67, prohibit the use of ctrl-alt-delete in the background to restart the machine

Cd/etc/inittab
VI Inittab find the following line in the file
# Trap Ctrl-alt-delete
Ca::ctrlaltdel:/sbin/shutdown-t3-r Now (comment out this line)
such as: # Trap Ctrl-alt-delete
#ca:: Ctrlaltdel:/sbin/shutdown-t3-r now

68. Modify Host Name

Vi/etc/sysconfig/network
Modify the hostname line to hostname= host name

69. Check the hardware of boot detection

DMESG | More

70. View hard disk usage

Df–m

71. View the size of the directory

Du–sh dirname

72, unzip the small whole

Tar xvfj lichuanhua.tar.bz2
Tar Xvfz lichuanhua.tar.gz
Tar Xvfz lichuanhua.tgz
Tar xvf Lichuanhua.tar
Unzip Lichuanhua.zip
Note: Compress tar cvfz FileName.tar.gz DirName

73. Show memory usage

Free–m

74. Show how long the system has been running

Uptime

75. Display the Post content command

Dmesg

76. Detailed List of ports

/etc/services

77. View physical Information

Lspci

78, text section of the Chinese language support

RH 9.0 Self-installing package zhcon_0.2.3_1.rh9.i386.rpm
After the installation is complete, execute: Zhcon can support Chinese.

79. Linux Control Windows

(1) with RH9.0 himself with Rdesktop, version is 1.2.0
Command: Rdesktop–u user–f 192.168.1.70 Color default is 8 bit
(2) to reach 16 colors, the new version must be downloaded 1.3.0
Rdesktop–a 16–u lichuanhua–g 800*600 192.168.1.70

80. Do not let the monitor hibernate

Setterm–blank 0

81. Show the last user logged in to the system

Last

82. Check the last login time of all accounts

Lastlog/var/log/lastlog

83. Check the login time and location of all users since the system was opened.

Cat/var/log/secure

84, display the current user's information

Id

85. How to know the number of Apache connections

Ps-ef|grep httpd|wc-l #其它服务可以类推
Netstat-nat|grep-i "80″|wc-l # minus 1 of the results.

86, delete the user account at the same time, the user's home directory is also deleted

Userdel-r User Name

87, modify the information of existing users

usermod [parameter] user name
Parameters:-C,-D,-M,-G,-G,-S,-u and-O are the same as AddUser parameters
New parameter:-L new user name (Specify a new account, change the original user name to a new username)

88. Change the system language/character set of Redhat

Change/etc/sysconfig/i18n files, such as
Lang= "en_US", Xwindow will display the English interface,
Lang= "ZH_CN. Gb18030″,xwindow will display the Chinese interface.
There's another way.
cp/etc/sysconfig/i18n $HOME/.i18n
Modify the $HOME/.i18n file, as
Lang= "en_US", Xwindow will display the English interface,
Lang= "ZH_CN. Gb18030″,xwindow will display the Chinese interface.
This allows you to change your personal interface language without affecting other users.
VI. BASHRC
Export LANG=ZH_CN. GB2312
Export LC_ALL=ZH_CN. GB2312

89, CD-ROM made into ISO file

Cp/dev/cdrom Xxxx.iso

90. Quickly watch the boot hardware detection

DMESG | More

91, check the use of hard disk

Df-k is displayed in K units
Df-h in the human unit display, can be b,k,m,g,t.

92. View the size of the directory

Du-sh dirname
-S displays totals only
-H with K, M, G as the unit, improve the readability of information. KB, MB, GB are converted in 1024 units, and-H is converted to 1000 units.

93. Find or delete a process that is using a file

Fuser filename
Fuser-k filename

94, Linux to let the user's password must have a certain length, and meet the complexity of

Vi/etc/login.defs, Change Pass_min_len

95. Run the program as a different user

Su–username-c "/path/to/command"
Sometimes you need to run a special identity program so that SU can do

95 Tips for Linux

Related Article

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.