Linux Basics Series II

Common Commands

alias (alias)

In the knowledge base Linux one series mentioned ls -l = ll, this is Linux alias, use the alias can view the system default alias.

[root@hadoop001 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

复制代码

Use alias alias = command string can take effect in the current session session, if you want to remain in effect, in the end environment variable file in to add the above command, see the next section on environment variables.

[root@hadoop001 ~]# alias ul='cd /usr/local'
[root@hadoop001 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias ul='cd /usr/local'   <-- 新增的
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@hadoop001 ~]# ul
[root@hadoop001 local]# pwd
/usr/local
复制代码

Environment Variables

  1. Global environment variable in Linux / etc / profile is a global variable, regardless of which user logs you can use with the file of all the variables. Undertake on how to set up an alias in the global environment variable, add the following code at the end of the file.
#env
alias ul='cd /usr/local'
复制代码

Of course, just add the code is not enough, be sure to make a global variable to take effect, use the following command can be

. /etc/profile
或者
source /etc/profile
复制代码
  1. Personal environment variables only for individual users, the path stored in ~ / .bash_profile , open the file you will find that it is actually also relates to another file ~ / .bashrc . So if you want to set the alias, the above code is added at the end of the two files.
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

#env
alias rc='cd /root/xxx'
复制代码

It is also the entry into force of the two.

. ~/.bash_profile    . ~/.bashrc
或者
source ~/.bash_profile    source ~/.bashrc
复制代码

rm (delete)

Generally use rm -rf file name, this approach will force the removal of files or folders, -f indicates a mandatory, -r represents the folder . Often hear that rm -rf / *, that is, delete the library on foot. Of course, most people will not run this directly, but this error may occur in the shell script, the following scenario has led to this situation.

shell脚本可能会这样
xxxpath=xxx/xx
...(逻辑部分)
rm -rf $xxxpath/*    这里就是个坑

如果一空值赋予给了xxxpath,那么不就成了rm -rf /*
所以在生产上凡是碰见rm -rf强制删除文件夹的,路径一定先判断存在不,不存在 就skip;就存在就rm
复制代码

history (command history)

history -c command record is clear, of course, when the individual user login, ~ / .bash_history will record command, so to remove it, but also remember to delete it.

User / Group command set

  1. useradd username ==> to add users, it's home directory in / home / username
  2. id user name ==> display user and group information
[root@hadoop001 ~]# id dengdi
uid=1001(dengdi) gid=1001(dengdi) groups=1001(dengdi)
用户ID             主组ID           所有组  
复制代码
  1. cat / etc / passwd ==> display information for all users
dengdi(用户名):x:1001(用户id):1001(主组id)::/home/dengdi(家目录):/bin/bash(执行解释器)  
如果/bin/bash变成/bin/false或者/sbin/nologin,这个用户就不能登陆了
复制代码
  1. userdel username ==> delete users delete users, will / etc / passwd delete records; at the same time if the group does not have other users, delete the group but still the home directory, but users and groups change occurs
[root@hadoop001 ~]# ll /home/
total 0
drwx------. 3 centos centos 70 Jun 28  2017 centos
drwx------  2   1001   1001 59 Jun 17 23:48 dengdi
复制代码
  1. And then execute useradd userdel
[root@hadoop001 ~]# userdel dengdi
[root@hadoop001 ~]# useradd dengdi
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
复制代码

What to look at the system prompt skel directory, we ll -a / home / dengdi

[root@hadoop001 ~]# ll -a /home/dengdi/
total 12
drwx------  2 dengdi dengdi  59 Jun 17 23:48 .
drwxr-xr-x. 4 root   root    32 Jun 17 23:48 ..
-rw-r--r--  1 dengdi dengdi  18 Apr 11  2018 .bash_logout
-rw-r--r--  1 dengdi dengdi 193 Apr 11  2018 .bash_profile
-rw-r--r--  1 dengdi dengdi 231 Apr 11  2018 .bashrc
复制代码

skel directory is .bash * all the hidden files, and then try to delete these users to switch dengdi

[root@hadoop001 ~]# ll -a /home/dengdi/
total 16
drwx------  2 dengdi dengdi  79 Jun 18 00:06 .
drwxr-xr-x. 4 root   root    32 Jun 17 23:48 ..
-rw-------  1 dengdi dengdi   5 Jun 18 00:06 .bash_history
-rw-r--r--  1 dengdi dengdi  18 Apr 11  2018 .bash_logout
-rw-r--r--  1 dengdi dengdi 193 Apr 11  2018 .bash_profile
-rw-r--r--  1 dengdi dengdi 231 Apr 11  2018 .bashrc
[root@hadoop001 ~]# rm -rf /home/dengdi/.*
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘/home/dengdi/.’
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘/home/dengdi/..’
[root@hadoop001 ~]# ll -a /home/dengdi/
total 0
drwx------  2 dengdi dengdi  6 Jun 18 00:08 .
drwxr-xr-x. 4 root   root   32 Jun 17 23:48 ..
[root@hadoop001 ~]# su - dengdi
Last login: Tue Jun 18 00:07:26 CST 2019 on pts/0
-bash-4.2$ 
复制代码

So skel directory is to determine your [root @ hadoop001 ~] or -bash-4.2 $ 6. groupadd User Group ==> new user group usermod -a -G user group dengdi ==> to add a new user group members dengdi usermod -g user group dengdi ==> modified based group xxx

[root@hadoop001 ~]# groupadd bigdata
[root@hadoop001 ~]# id ruoze
uid=501(ruoze) gid=501(ruoze) groups=501(ruoze)
[root@hadoop001 ~]# usermod -a -G bigdata ruoze
[root@hadoop001 ~]# id ruoze
uid=501(ruoze) gid=501(ruoze) groups=501(ruoze),502(bigdata)
[root@hadoop001 ~]# usermod -g bigdata ruoze
[root@hadoop001 ~]# id ruoze
uid=501(ruoze) gid=502(bigdata) groups=502(bigdata)
这里重新指定主组之后,会丢失原来的主组
复制代码
  1. Reassign the user's home directory
usermod -d 路径 用户
或者
vi /etc/passwd
复制代码
  1. Switch User
su ruoze   切换用户 当前路径不会变,就是切换之前的路径
su - ruoze 切换用户 且切到该用户的家目录,且执行环境变量文件生效
复制代码
  1. passwd user ==> set the password or password reset

Reproduced in: https: //juejin.im/post/5d08d161f265da1b957059b8

Guess you like

Origin blog.csdn.net/weixin_34114823/article/details/93180591