目录

Linux问题汇总

bad interpreter: No such file or directory

在Windows环境下用Notepad++写了个shell脚本,上传到Linux平台后运行报错如下:

1
/bin/sh^M: bad interpreter: No such file or directory

经过查阅资料才知道,这是文件格式导致的问题:使用vi/vim进入该shell文件,按下:进入末行模式,输入set ff查询文件格式,结果如下:

1
fileformat=dos

不同的操作系统使用了不同的符号来换行,可以简单参考下下面的表格:

系统换行符
DOSCR/LF
UNIXLF
MACCR

如果通过Windows下的Git将文件提交到Linux上的服务器,也会收到换行符将被替换的消息通知。而解决方法也很简单,切换文件格式即可,如下:

通过vi/vim进入想要修改的文件,按下:进入末行模式,输入set fileformat=unix,接着按下ZZ或者按下shift + z + z或者输入:x或者:wq保存修改即可。

判断网络是否连通

ssh命令

有个通用命令,Windows和Linux都能使用,如下:

1
ssh ip -v -p port

-v表示调试模式,会打印出具体日志。-p表示端口号。如果网络连通则会打印出来连接已建立Connection established。如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
C:\Users\10191>ssh localhost -v -p 1313
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
debug1: Connecting to localhost [::1] port 1313.
debug1: connect to address ::1 port 1313: Connection refused
debug1: Connecting to localhost [127.0.0.1] port 1313.
debug1: Connection established.
debug1: identity file C:\\Users\\10191/.ssh/id_rsa type 0
debug1: identity file C:\\Users\\10191/.ssh/id_rsa-cert type -1
debug1: identity file C:\\Users\\10191/.ssh/id_dsa type -1
debug1: identity file C:\\Users\\10191/.ssh/id_dsa-cert type -1
debug1: identity file C:\\Users\\10191/.ssh/id_ecdsa type -1
debug1: identity file C:\\Users\\10191/.ssh/id_ecdsa-cert type -1
debug1: identity file C:\\Users\\10191/.ssh/id_ed25519 type -1
debug1: identity file C:\\Users\\10191/.ssh/id_ed25519-cert type -1
debug1: identity file C:\\Users\\10191/.ssh/id_xmss type -1
debug1: identity file C:\\Users\\10191/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1
debug1: kex_exchange_identification: banner line 0: HTTP/1.1 400 Bad Request
debug1: kex_exchange_identification: banner line 1: Content-Type: text/plain; charset=utf-8
debug1: kex_exchange_identification: banner line 2: Connection: close
debug1: kex_exchange_identification: banner line 3:
kex_exchange_identification: Connection closed by remote host

nc命令

如果服务器里无法使用ssh命令,可以使用下面的命令:

1
nc -vz -w 2 ip port

-v表示可视化,-z扫描时不发送数据,-w后面跟的数字表示超时几秒。用法如下:

1
2
3
4
5
6
7
// 端口能通
C:\Users\10191>nc -vz -w 2 1.2.3.70 8888
1.2.3.70 (1.2.3.70:8888) open

// 端口不能通
C:\Users\10191>nc -vz -w 2 1.2.3.70 8889
nc: 1.2.3.70 (1.2.3.70:8889): Operation timed out

查看端口是否启用

lsof命令

该命令用于列出系统已经打开的所有文件,在Linux中任何事物都以文件形式存在,通过文件可以访问常规数据、网络连接和硬件等。但该命令需要访问核心内存和各种文件,因此需要root用户执行。

1
2
3
4
lsof -i:port

// 查39007端口是否启用
lsof -i:39007

netstat命令

1
2
3
4
netstat -aptn

// 查39007端口是否启用
netstat -aptn | grep 39007

查看和修改Linux的时区

查看当前时区

1
date -R

修改设置Linux服务器时区

1
2
3
4
5
6
7
8
// Linux通用
tzselect

// 仅限于RedHat Linux 和 CentOS
timeconfig

// 适用于Debian
dpkg-reconfigure tzdata

复制相应的时区文件,替换系统时区文件;或者创建链接文件

1
2
3
4
cp /usr/share/zoneinfo/$主时区/$次时区 /etc/localtime

// demo:设置中国时区使用亚洲/上海(+8)
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

查看和修改Linux的时间

查看时间和日期

1
date

设置时间和日期

1
2
3
4
5
// 将系统日期设定成2009年11月3日的命令
date -s 11/03/2009

// 将系统时间设定成下午5点55分55秒的命令
date -s 17:55:55

将当前时间和日期写入BIOS,避免重启后失效

1
hwclock -w

附注

1
2
3
4
5
// 不加参数可以直接看到当前日期时间
date

// 不加参数可以直接看到本月月历
cal

vi/vim打开文件提示Found a swap file by the name

swap文件来源

在使用vi或vim命令打开一个文件后,就会产生一个.(filename).swp的文件。如果编辑完成之后,正常退出,那么这个swp文件就会被自动删除。但是如果在操作该文件时发生了异常中断(非正常退出),就会在当前目录下生成了一个对应的swp文件。

在Linux中,以.开头的文件都是隐藏文件,可以通过使用ll -a或者ls -a来查看。

而这种swp文件是隐藏文件,有两个作用:

  • 避免用多个程序编辑同一个文件时,产生两个不同的版本。
  • 非常规退出时,文件恢复。

删除swp文件

只要将swp文件删除,就不会再出现这个提示。可以通过rm命令来删除该文件。

禁止生成swp文件

如果想要禁止生成swp文件,可以通过修改vim的配置文件来实现。新建一个~/.vimrc文件,在文件中添加一行代码:

1
set noswapfile

这样该配置就只会对当前用户生效,你也可以直接修改/etc/vimrc文件,效果是一样的。

通过swp文件来恢复文件

swp文件可以用来恢复文件,假如你有一个swp文件.my.ini.swp,可以通过以下命令来恢复:

1
vi -r my.ini

恢复文件之后可以把swp文件删除,不然每次打开my.ini文件时都会提示。

Linux命令英文全称

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
suSwith user  切换用户,切换到root用户
cat: Concatenate  串联
uname: Unix name  系统名称
df: Disk free  空余硬盘
du: Disk usage 硬盘使用率
chown: Change owner 改变所有者
chgrp: Change group 改变用户组
psProcess Status  进程状态
tarTape archive 解压文件
chmod: Change mode 改变模式
umount: Unmount 卸载
lddList dynamic dependencies 列出动态相依
insmodInstall module 安装模块
rmmodRemove module 删除模块
lsmodList module 列表模块
alias :Create your own name for a command
bash :GNU Bourne-Again Shell  linux内核 
grep:global regular expression print
httpd :Start Apache
ipcalc :Calculate IP information for a host
ping :Send ICMP ECHO_Request to network hosts
reboot: Restart your computer
sudo:Superuser do

/bin = BINaries 
/dev = DEVices 
/etc = ETCetera 
/lib = LIBrary 
/proc = PROCesses 
/sbin = Superuser BINaries 
/tmp = TeMPorary 
/usr = Unix Shared Resources 
/var = VARiable ? 
FIFO = First In, First Out 
GRUB = GRand Unified Bootloader 
IFS = Internal Field Seperators 
LILO = LInux LOader 
PS = Prompt String 
Perl = "Pratical Extraction and Report Language" = "Pathologically Eclectic Rubbish Lister" 
Tcl = Tool Command Language 
Tk = ToolKit 
VT = Video Terminal 
YaST = Yet Another Setup Tool 
apt = Advanced Packaging Tool 
ar = archiver 
as = assembler 
bash = Bourne Again SHell 
bc = Basic (Better) Calculator 
bg = BackGround 
cal = CALendar 
cat = CATenate 
cd = Change Directory 
chgrp = CHange GRouP 
chmod = CHange MODe 
chown = CHange OWNer 
chsh = CHange SHell 
cmp = compare 
cobra = Common Object Request Broker Architecture 
comm = common 
cp = CoPy 
cpio = CoPy In and Out 
cpp = C Pre Processor 
cups = Common Unix Printing System 
cvs = Current Version System 
dc = Desk Calculator 
dd = Disk Dump 
df = Disk Free 
diff = DIFFerence 
dmesg = diagnostic message 
du = Disk Usage 
ed = editor 
egrep = Extended GREP 
elf = Extensible Linking Format 
elm = ELectronic Mail 
emacs = Editor MACroS 
eval = EVALuate 
ex = EXtended 
exec = EXECute 
fd = file descriptors 
fg = ForeGround 
fgrep = Fixed GREP 
fmt = format 
fsck = File System ChecK 
fstab = FileSystem TABle 
fvwm = F*** Virtual Window Manager 
gawk = GNU AWK 
gpg = GNU Privacy Guard 
groff = GNU troff 
hal = Hardware Abstraction Layer 
joe = Joe's Own Editor 
ksh = Korn SHell 
lame = Lame Ain't an MP3 Encoder 
lex = LEXical analyser 
lisp = LISt Processing = Lots of Irritating Superfluous Parentheses 
ln = LiNk 
lpr = Line PRint 
ls = list 
lsof = LiSt Open Files 
m4 = Macro processor Version 4 
man = MANual pages 
mawk = Mike Brennan's AWK 
mc = Midnight Commander 
mkfs = MaKe FileSystem 
mknod = MaKe NODe 
motd = Message of The Day 
mozilla = MOsaic GodZILLa 
mtab = Mount TABle 
mv = MoVe 
nano = Nano's ANOther editor 
nawk = New AWK 
nl = Number of Lines 
nm = names 
nohup = No HangUP 
nroff = New ROFF 
od = Octal Dump 
passwd = PASSWorD 
pg = pager 
pico = PIne's message COmposition editor 
pine = "Program for Internet News & Email" = "Pine is not Elm" 
ping =  Packet InterNet Grouper 
pirntcap = PRINTer CAPability 
popd = POP Directory 
pr = pre 
printf = PRINT Formatted 
ps = Processes Status 
pty = pseudo tty 
pushd = PUSH Directory 
pwd = Print Working Directory 
rc = runcom = run command, shell 
rev = REVerse 
rm = ReMove 
rn = Read News 
roff = RunOFF 
rpm = RPM Package Manager = RedHat Package Manager 
rsh, rlogin, = Remote 
rxvt = ouR XVT 
sed = Stream EDitor 
seq = SEQuence 
shar = SHell ARchive 
slrn = S-Lang rn 
ssh = Secure SHell 
ssl = Secure Sockets Layer 
stty = Set TTY 
su = Substitute User 
svn = SubVersioN 
tar = Tape ARchive 
tcsh = TENEX C shell 
telnet = TEminaL over Network 
termcap = terminal capability 
terminfo = terminal information 
tr = traslate 
troff = Typesetter new ROFF 
tsort = Topological SORT 
tty = TeleTypewriter 
twm = Tom's Window Manager 
tz = TimeZone 
udev = Userspace DEV 
ulimit = User's LIMIT 
umask = User's MASK 
uniq = UNIQue 
vi = VIsual = Very Inconvenient 
vim = Vi IMproved 
wall = write all 
wc = Word Count 
wine = WINE Is Not an Emulator 
xargs = eXtended ARGuments 
xdm = X Display Manager 
xlfd = X Logical Font Description 
xmms = X Multimedia System 
xrdb = X Resources DataBase 
xwd = X Window Dump 
yacc = yet another compiler compiler

参考链接

警告
本文最后更新于 December 11, 2022,文中内容可能已过时,请谨慎使用。