溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux基礎(chǔ)知識(shí)點(diǎn)

發(fā)布時(shí)間:2020-11-18 14:07:40 來(lái)源:億速云 閱讀:115 作者:小新 欄目:建站服務(wù)器

了解Linux基礎(chǔ)知識(shí)點(diǎn)?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!

常用命令

alias(別名)

在Linux基礎(chǔ)知識(shí)系列之一中提到ls -l=ll,這個(gè)就是Linux中的別名,使用alias可以查看系統(tǒng)默認(rèn)的別名。

[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'

使用alias 別名=命令字符串就可以在當(dāng)前session會(huì)話中生效,如果你想要一直生效,請(qǐng)?jiān)?strong>環(huán)境變量文件末中增添上述命令,有關(guān)環(huán)境變量的請(qǐng)看下一節(jié)。

[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
環(huán)境變量
  1. 全局環(huán)境變量 Linux中/etc/profile就是全局變量,無(wú)論你用哪個(gè)用戶登錄都可以使用該文件里的所有變量。承接上一節(jié)如何在全局環(huán)境變量中設(shè)置alias,在文件末尾增添以下代碼。

#env alias ul='cd /usr/local'

當(dāng)然光增添代碼是不夠的,一定要使全局變量生效,使用以下命令均可

. /etc/profile 或者 source /etc/profile
  1. 個(gè)人環(huán)境變量 只針對(duì)個(gè)人用戶,存放的路徑就在 ~/.bash_profile 中,打開(kāi)該文件你會(huì)發(fā)現(xiàn)它其實(shí)還涉及到另一個(gè)文件 ~/.bashrc 。所以如果你要設(shè)置alias,也是在兩個(gè)文件末尾添加上述代碼。

# .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'

使其生效也是上述兩種。

. ~/.bash_profile    . ~/.bashrc 或者 source ~/.bash_profile    source ~/.bashrc
rm(刪除)

一般都是使用 rm -rf 文件名,這種方式會(huì)強(qiáng)制刪除文件或者文件夾,-f表示強(qiáng)制,-r表示可以文件夾。經(jīng)常聽(tīng)到就是 rm -rf /*,也就是刪庫(kù)跑路。當(dāng)然一般人都不會(huì)這樣直接運(yùn)行,但是可能會(huì)在shell腳本出現(xiàn)這種錯(cuò),以下場(chǎng)景就導(dǎo)致這種情況。

shell腳本可能會(huì)這樣 xxxpath=xxx/xx ...(邏輯部分) rm -rf $xxxpath/*    這里就是個(gè)坑 如果一空值賦予給了xxxpath,那么不就成了rm -rf /* 所以在生產(chǎn)上凡是碰見(jiàn)rm -rf強(qiáng)制刪除文件夾的,路徑一定先判斷存在不,不存在 就skip;就存在就rm
history(命令記錄)

history -c 就是清除命令記錄,當(dāng)然個(gè)人用戶登陸時(shí),~/.bash_history也會(huì)記錄命令,所以要清除的話,記得也把它給刪掉。

用戶/用戶組命令集合
  1. useradd 用戶名==>添加用戶,它的家目錄就在/home/用戶名

  2. id 用戶名==>顯示用戶和用戶組信息

[root@hadoop001 ~]# id dengdi uid=1001(dengdi) gid=1001(dengdi) groups=1001(dengdi) 用戶ID             主組ID           所有組
  1. cat /etc/passwd==>顯示所有用戶的信息

dengdi(用戶名):x:1001(用戶id):1001(主組id)::/home/dengdi(家目錄):/bin/bash(執(zhí)行解釋器)   如果/bin/bash變成/bin/false或者/sbin/nologin,這個(gè)用戶就不能登陸了
  1. userdel 用戶名==>刪除用戶 刪除用戶,會(huì)把/etc/passwd記錄刪除; 同時(shí)假如該組沒(méi)有其他用戶,則刪除該組 但是 家目錄還在,但是用戶和用戶組 發(fā)生變革

[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. 執(zhí)行userdel然后再useradd

[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

來(lái)看看系統(tǒng)提示的skel directory是什么,我們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就是.bash*所有的隱藏文件,嘗試將這些刪除然后切換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$

所以skel directory是決定你的[root@hadoop001 ~]還是-bash-4.2$ 6. groupadd 用戶組==>新增用戶組 usermod -a -G 用戶組 dengdi==>添加用戶組新成員為dengdi usermod -g 用戶組 dengdi==>修改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) 這里重新指定主組之后,會(huì)丟失原來(lái)的主組
  1. 重新指定用戶家目錄

usermod -d 路徑 用戶 或者 vi /etc/passwd
  1. 切換用戶

su ruoze   切換用戶 當(dāng)前路徑不會(huì)變,就是切換之前的路徑 su - ruoze 切換用戶 且切到該用戶的家目錄,且執(zhí)行環(huán)境變量文件生效
  1. passwd 用戶==>設(shè)置密碼或者重置密碼

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)Linux基礎(chǔ)知識(shí)點(diǎn)大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI