6.1 常见bash命令

sudo useradd username -g ftp -d /home/username -m username
root@test:~# sudo useradd username -g ftp -d /home/username -m username
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping
      --extrausers              Use the extra users database

system

uptime
last
date
history
# 重启网络服务
service network restart
# 重启apahce
service httpd restart
#测试服务器性能
wget -qO- git.io/superbench.sh | bash
# 当前操作系统版本信息
cat /proc/version 
# 查看当前操作系统版本发行版信息
cat /etc/issue
# 查看cpu相关信息,包括型号、主频、内核信息等
cat /proc/cpuinfo
# CPU运行在32 or 64模式下?
getconf LONG_BIT 
# linux主机信息
lsb_release -a
# 查看进程,
ps -aux 
#然后杀死此进程
kill -9  进程号        
# 查看服务器架构是多少位
arch
# 把结果扔进垃圾桶
ls &> /dev/null
$$ //当前脚本启动的进程
$? // 0 执行success ; others: failure
# 获取linux系统用户(linux系统用户都存在/etc/passwd) 
cat /etc/passwd | cut -d: -f1
# cut命令截取,-d(divide)指定分隔符,-f(filter)取哪个字段

user

# switch to root
sudo -i 
# switch to user
su - user
#新增用户,并添加到sudo组,并赋予某个文件夹特定权限
adduser xxx
usermod -aG sudo xxx
以xxx的用户登录
su - xxx
# We'll name ours 'ghost' in this example; you can use whatever you want
sudo mkdir -p /var/www/ghost
# Replace <user> with the name of your user who will own this directory
sudo chown <user>:<user> /var/www/ghost
# Set the correct permissions
sudo chmod 775 /var/www/ghost
# Then navigate into it
cd /var/www/ghost

file

# basic
cd 
mkdir [-p]
mv 
cp 

# zip unzip 有些服务器没有安装zip包执行不了zip命令,但基本上都可以用tar命令的
tar -zxvf xxx.zip
tar -zcvf /home/zdzlibs.tar.gz /home/zdz/java/zdzlibs/
# 压缩服务器上当前目录的内容为xxx.zip文件
zip -r xxx.zip ./*
# 解压zip文件到当前目录
unzip filename.zip
# 检测(下载)文件的完整性
md5sum filename

network

# linux
ip -a
# mac eno
ifconfig
# 查看tcp
netstat -tl
# 查看port被哪个应用占用
lsof -i:port
# 检测端口是否开放
telnet ip port
curl url
# 清空文本
echo '' > a.conf

#查看端口是否打开
netstat -an|grep 3306

#查看端口被哪个进程占用
sudo lsof -i:port

#curl url检测网络故障
curl http://68.183.96.16:2368/ghost/ 有数据则表示内网可以访问
curl http://ip:2368/ghost/ 没数据则表示,这个端口架有防火墙

process

# 查看进程, 可以看到column 2 是pid
ps -aux
# 查看所有(all)用户(user)的终端(x),过滤nginx(关键字)
ps aux|grep nginx 
# 在用awk提取进程id
ps -aux|grep nginx|awk '{print $2}'

# 由端口查进程pid
netstat -ap|grep port
lsof -i:port
# 由pid查看占用的端口
netstat -nap|grep pid
# kill
sudo kill -9 pid

chmod

chmod +x xx
# chmod 700 是cd不进去的
chmod 700 xx
chmod 600 xx
chmod 777 xx

#常用的linux文件权限如下:
444 r--r--r--
600 rw-------
644 rw-r--r--
666 rw-rw-rw-
700 rwx------
744 rwxr--r--
755 rwxr-xr-x
777 rwxrwxrwx
从左至右,先是3个数字,代表文件的权限
然后是9个字母(或者连字符)
其中 1-3位数字代表文件所有者的权限
4-6位数字代表同组用户的权限
7-9数字代表其他用户的权限

而具体的权限是由数字来表示的
读取的权限等于4,用r表示
写入的权限等于2,用w表示
执行的权限等于1,用x表示
通过4、2、1的组合,得到以下几种权限:
0(没有权限);4(读取权限);5(4+1 | 读取+执行);6(4+2 | 读取+写入);7(4+2+1 | 读取+写入+执行)
以754为例:

1-3位7等于4+2+1,rwx,所有者具有读取、写入、执行权限; 
4-6位5等于4+1+0,r-x,同组用户具有读取、执行权限但没有写入权限; 
7-9位4,其他用户具有读取但没有写入权限和执行权限

firewall:ufw

# ubuntu aws
sudo ufw --help
sudo ufw status | enable | disable | reset
sudo ufw allow port | app
sudo ufw delete allow port | app
# eg: sudo ufw allow 3306, sudo ufw allow OpenSSH
sudo ufw app list 
root@blog:~# sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

nginx

ps -ef|grep nginx
systemctl start|stop nginx
sudo service nginx status|start|stop|restart

others

查看当前网络 ip走向,0.0.0.0 表所有ipv4的ip,[::] 表ipv6的所有ip

results matching ""

    No results matching ""