Linux常用命令

来源:互联网 发布:淘宝全球购直播怎么看 编辑:程序博客网 时间:2024/05/29 03:25

本文总结常用Linux命令(不断增加):


1 文件操作

文件复制
不同服务器之间文件拷贝
从本地到远程服务器
scp -P 23 [-r] localFile account@remoteIP:dir # r表示拷贝的是目录,-P指明端口号
从远程服务器到本地
scp [-r] account@remoteIP:remoteFile localDir
查看
ls命令只显示目录
ls -F | grep ‘/$’ #以/结尾的为目录
删除
删除除了文件Makefile2和目录hello.dir’之外的所有文件
ls | grep -v ‘Makefile2|hello.dir’ | xargs rm -rf
重定向

ls -a 1>stdout.txtls-a 2>err.txtls -a 1>out.txt 2>&1 #标准输出、标准错误都重定向到out.txt,&2表示2的输出通道ls -a 2>out.txt 1>&2#标准输出、标准错误都重定向到out.txt#上述例子中的1通常可以省略,>表示覆盖,>>表示追加ls mlog.txt mmemd.txt | tee 3.txt#屏幕上可以看到标准输出和标准错误,但是3.txt中只有标准输出,tee只重定向了标准输出

2 查看系统信息

uname -a 显示系统信息,包括内核名字、节点名字等
cat /proc/version
cat /etc/issue
free -m 查看内存信息

3 进程管理

查看使用某APP(如yum)的进程 ps -ef | grep yum
杀掉进程17412 kill -s 9 17412
screen

  • 常用命令
    screen -S test #创建一个名为test的会话
    screen -ls #列出所有会话
    screen -d test #卸载名为test的会话,但会话中的任务会继续执行
    screen -r test #恢复名为test的会话
    exit #在screen窗口中执行退出当前screen,且该screen将不再存在
  • 快捷键
    Ctrl+a c :在当前screen会话中创建窗口
    Ctrl+a d : 效果与screen -d相同,卸载当前会话
    Ctrl+a w :显示当前会话中的窗口列表,显示在标题栏中
    Ctrl+a n :切换到下一个窗口
    Ctrl+a p :切换到上一个窗口
    Ctrl+a 0-9 :在第0个窗口和第9个窗口之间切换

    注:这里的快捷键由三个键组成,如Ctrl+a c,可以按住Ctrl键,再依次按下a和c。也可以先按一次Ctrl+a,再按一次Ctrl+c。两种方法都是可行的。
    执行screen -r test时报错Cannot open your terminal '/dev/pts/0'
    解决方案:sudo chmod 777 /dev/pts/0

4 环境变量

PATH
先执行系统级别的配置再用户级别的。如在RedHat中,先执行 /etc/profile.d/*.sh /etc/bashrc /etc/profile 其后是 ~./.bashrc ~./.bash_profile,具体顺序应该参看脚本内容。

5 软件包管理

RedHat 系列(Redhat(RHEL)、Centos、Fedora、Oracle Linux等 )

1 软件包管理工具(安装、卸载、升级、查询验证),常见的安装包为rpm包
安装 rpm -ivh foo-1.0-1.i386.rpm
卸载 rpm -e foo #可以删除通过yum安装的软件
【注意这里使用软件包的名字foo,不是软件包文件的名字“foo-1.0-1.i386.rpm”.如果其它软件包依赖于要卸载的软件包,卸载时则会产生错误信息。如 rpm -e foo 报错:removing these packages would break dependencies:foo is needed by bar-1.0-1 若让RPM忽略这个错误继续卸载(依赖于该软件包的程序可能无法运行),使用–nodeps 命令行选项】
查询 rpm -q foo

2 包管理工具 yum ,基于RPM支持在线安装,不用手动解决包的依赖关系
故障一:yum安装软件包错误 HTTP Error 404 - Not Found Trying other mirror
解决办法yum clean all;yum update 或者
No more mirror类的错误还有可能是网络不通的问题
故障二:another app is currently holding the yum lock;waiting for it to exit
解决方案rm -f /var/run/yum.pid
注:yum remove XXX 可以自动将依赖于XXX的软件包一并卸载,而rpm -e XXX需要手动先卸载依赖XXX的包

Debian系列(Debian、Ubuntu等 )

1 常见的安装包格式 deb包,安装deb包的命令是“dpkg -参数”

2 包管理工具 apt-get

6 网络管理

本地域名解析 cat /etc/hosts
添加网关
route add default gw 172.16.1.10
netstat -rn
Kernel IP routing table

Destination Gateway Genmask Flags MSS Window irtt Iface 172.16.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ib0 0.0.0.0 172.16.1.10 0.0.0.0 UG 0 0 0 ib0

POSTROUTING
通过下面的命令可以使用本地vncserver跳过跳板机访问 172.16.1.40那台机器。
./ipset.sh 172.21.7.241 5901 172.16.1.40 5901

##ipset.shiptables -A INPUT -p tcp -d $1 --dport $2 -j ACCEPTiptables -A OUTPUT -p tcp -s $1 --sport $2 -j ACCEPTiptables -t nat -A PREROUTING -d $1 -p tcp --dport $2 -j DNAT --to-destination $3:$4iptables -t nat -A POSTROUTING -d $3 -p tcp --dport $4 -j SNAT --to 172.16.1.10iptables -A FORWARD -o ib0 -d $3 -p tcp --dport $4 -j ACCEPTiptables -A FORWARD -i ib0 -s $3 -p tcp --sport $4 -j ACCEPT

使用命令iptables-save查看

-A PREROUTING -d 172.21.7.241/32 -p tcp -m tcp --dport 5909 -j DNAT --to-destination 172.16.1.40:5901 

这样在本地vncserver中输入 172.21.7.241:5901就能连接到172.16.1.40的5901端口的vncserver服务上了。
通过跳板机(代理)上网
k2和k4通过ib0链接,k40可以联网

[user@k2]$ netstat -rnKernel IP routing tableDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface172.16.1.0      0.0.0.0         255.255.255.0   U         0 0          0 ib0172.20.104.0    0.0.0.0         255.255.255.0   U         0 0          0 ib010.1.1.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 ib0
[user@k4]# netstat -rnKernel IP routing tableDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface172.16.1.0      0.0.0.0         255.255.255.0   U         0 0          0 ib0172.20.104.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 ib00.0.0.0         172.20.104.254  0.0.0.0         UG        0 0          0 eth0

在k2上配置k4为默认路由

[user@k2]sudo route add default gw 172.16.1.20[root@k2 /]# netstat -rnKernel IP routing tableDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface172.16.1.0      0.0.0.0         255.255.255.0   U         0 0          0 ib0172.20.104.0    0.0.0.0         255.255.255.0   U         0 0          0 ib010.1.1.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 ib00.0.0.0         172.16.1.20     0.0.0.0         UG        0 0          0 ib0

k4执行:

echo "1" > /proc/sys/net/ipv4/ip_forwardiptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

7 打包工具

tar cvf caffe-master.tar caffe-master

8 帮助工具

man 【命令】

9 故障处理

忘记用户user登录密码,且root密码未知

以Ubuntu Kylin 14.04为例,
1.启动选项中选中Recovery模式,不要回车,按下【e】键,将“ro recovery nomodeset”替换为“quiet splash rw init=/bin/bash”,按下[F10]或者【Ctrl+x】进入root的命令行。
2.在root命令行下修改密码,passwd 回车,接着修改root密码,passwd user回车,修改密码。
3.重启系统
注释:步骤1是为了将系统由read-only状态改为编辑状态,否则进入root命令行改密码时出现passwd authentication token manipulation error,vi,cp进行文件写或复制时将提示不可写,为只读系统等问题。

SSH客户端中文乱码

将ssh客户端的编码 方式与服务器端的改成一致。
修改服务器端的编码方式为:export LANG=’zh_CN.UTF-8’ ,为了重启机器后修改不丢失要修改文件vi /etc/sysconfig/i18n

0 0
原创粉丝点击