Ubuntu

Linux Ubuntu

Ubuntu Linux learning comes with notes record.

basic command

pwd 打印当前工作路径
ls 显示当前路径下的内容
mkdir 创建文件夹
touch 创建文件
cp 文件名 文件名 复制
rm 删除文件
rmdir 删除文件夹
rm -r filename # 删除文件夹(该文件夹下有内容时-r)
clear # 清空当前屏幕
man command or command --help # 帮助命令
wc filename # 打印出文件的行数-l,字数-w,字符数-m,最长一行的字符数-L
last # 列出最近的用户登录日志
lsblk # 列出所有block device -a 

User, Group, Permission

add user

useradd -d /home/username -m -s /bin/bash username 
# -d specify home dir
# -s create home dir if not exist
# -s specify bash path
# -g specify primary group (it is default)
# -G specify secondary group, could use comma sperate when specify multipal secondary group meantime
userdel -r username # -r remove home dir

set new user password

passwd username

Group

MaxNumber of group 65536
usermod -a -G groupname username
# -a append group rather than overwrite, or use comma like above

some command

id
groups [username]
members -a groupname

chown # change owner

chown username:groupname target
chown username target
chown :groupname target  # becareful colone symbal
chown -R username filename
chown -R username:groupname target # -R recursive

chmod # change permissions for file or directory

chmod permission-set target
chmod 666 filename
chmod -R 777 directoryname
chmod --reference=b.txt a.txt  # 将b.txt文件的权限属性作为模板,更改a.txt文件
chmod -c u+x b.txt # -c 显示更改详情

chmod g+w painting-of-Mordor.png  # Add group write permission to file
chmod u=x script.sh  # Only allow user execution permissionto file
chmod a-w do-not-edit.txt  # Remove all (ugo) write permissionto file

u: owner: file's owner
g: group: users who are members of the file's group
o: other: users who are neither the file's owner nor members of the file's group
a: all: all three of the above, same as ugo

+: adds the specified modes to the specified classes
-: removes the specified modes from the specified classes
=: the modes specified are to be made the exact modes for the specified classes

r: read: read a file or list a directory's  contents
w: write: write to a file or directory
x: execute: execute a file or recurse a directory tree

setfacl

setfacl -m g:groupname:rwx  /dir # 给文件夹添加多个组
setfacl -m u:username:rwx /dir # 给文件夹添加多个用户
setfacl -x  g:groupname /dir # cancel
setfacl -x u:username /dir # cancel
getfacl  /dir # 查看,因为默认只显示一个组

解决笔记本装完无法发现无线网络的问题:

sudo apt install bcmwl-kernel-source

sudo apt-get install linux-headers-generic build-essential dkms    
sudo apt-get update(optional maybe)    
sudp apt-get install linux-source    
sudo apt-get install --reinstall bcmwl-kernel-source #(有可能只需要执行该步骤即可成功)  
sudo modprobe wl    
    
sudo dpkg -i wireless-bcm43142-dkms_6.20.55.19-1_amd64.deb

图形界面下,开启登录到命令行

sudo systemctl set-default multi-user.target 
sudo systemctl start lightdm
systemctl set-default graphical.target 
# 默认进入图形界面

Sharing Write Permission

为了分享同一目录的多用户写入权限,把该目录的写入权限授予一个分组是必要的。
下面的例子把/var/www/html目录的写入权限授予”webmasters”分组。

sudo chgrp -R webmasters /var/www/html
sudo find /var/www/html -type d -exec chmod g=rwxs "{}" \;
sudo find /var/www/html -type f -exec chmod g=rw  "{}" \;

这些命令递归地设置组权限到/var/www/html下的所有文件和目录,以保证该组用户对该目录的读写权限。
也就是说,该目录下的文件和目录将从它们的父亲继承相应的组权限。
如果必须授权一个目录的访问到多个组,允许访问控制列表(ACLs)。

Ubuntu下apache2无法解析php文件,浏览器提示下载所要打开的php文件. 执行:

sudo apt-get install libapache2-mod-php5 
sudo a2enmod php5

如果显示为:

This module does not exist!

那就要彻底删除libapache2-mod-php5,然后重新安装它

sudo apt-get remove –purge libapache2-mod-php5 
sudo apt-get install libapache2-mod-php5

重启apache2

sudo /etc/init.d/apache2 restart

open PDF file on bash

xdg-open xxx.pdf
gio open xxx.pdf
okular xxx.pdf

查找进程,计算器,替换大小写

ps -ef | grep processname
who | tr [a-z] [A-Z] > /tmp/file.out
tr 'a-z' 'A-Z' > /tmp/file.out
echo {1..100} | tr ' ' '+' | bc

find /var/www/html -maxdepth 1 -name ".*" // 不迭代子文件夹,只搜索一级目录

find /etc -maxdepth 1 -name "s*" | head -n 10  // tail -n 10

find /etc -maxdepth 1 -type d -name "s*"

grep t[ae]st hello.txt // 匹配括号中的任何一个,且只有一个

grep [^g]oo hello.txt // 匹配不是以g开头的任何字符

grep '^A' test.txt // 表示输出以A开头的

grep '$A' test.txt // 表示输出以A结尾的行

grep '^$' test.txt // 找空行

rep 'f..' test.txt // 表示输出 f后面跟2个字节的行

grep 'f*' test.txt // 匹配f后面不限制字符

x\{m\}  #重复字符x,m次,如:'o\{5\}'匹配包含5个o的行。    
x\{m,\}  #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。    
x\{m,n\}  #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。

.*   #一起用代表任意字符。   
\(..\)  #标记匹配字符,如'\(love\)',love被标记为1。    
\<      #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。    
\>      #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。     
\w    #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。   
\W    #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。   
\b    #单词锁定符,如: '\bgrep\b'只匹配grep。


    . (dot) - a single character.
    ? - the preceding character matches 0 or 1 times only.
    * - the preceding character matches 0 or more times.
    + - the preceding character matches 1 or more times.
    {n} - the preceding character matches exactly n times.
    {n,m} - the preceding character matches at least n times and not more than m times.
    [agd] - the character is one of those included within the square brackets.
    [^agd] - the character is not one of those included within the square brackets.
    [c-f] - the dash within the square brackets operates as a range. In this case it means either the letters c, d, e or f.
    () - allows us to group several characters to behave as one.
    | (pipe symbol) - the logical OR operation.
    ^ - matches the beginning of the line.
    $ - matches the end of the line. 

Network

show

ip a # show all interfaces
ip addr # show all interfaces
ip addr show dev ens160 # show only ens160

temporary config

ip addr add 192.168.1.1/24 dev ens160
ip addr del 192.168.1.1/24 dev ens160

remove

ip addr flush dev ens160 # remove an IP before re-assignment

UP/DOWN NIC # just like enable/disable under the windows

ip link set dev ens160 up
ip link set dev ens160 down

Permanent config

sudo vim /etc/network/interfaces

auto ens160
iface ens160 inet static/dhcp/dynamic
    address 10.25.1.50
    netmask 255.255.255.0
    dns-nameservers 8.8.8.8 10.50.1.82
    gatway 192.168.1.1
    metric 0

Restart networking

systemctl restart networking

other

sudo lshw
sudo lshw -short
sudo lshw -short -class <type>
    memory
    network
    processor
    disk

arp # list ARP cache
arp -s <ipaddr> <macaddress> # add new to arp
arp -d <ipaddr>
arp -d <macaddress>

ethtool <interface> # list general hardware information
ethtool -i <interface> # list hardward device driver information

ping # check network connection
traceroute # trace packet journey
netstat #network connection information
    -a # print all
    -l # print which ports are listening
    -s # print connection statistics
    -t/-u # print only TCP/UDP connections
tcpdump #network packet information