[关闭]
@torresdyl 2018-01-05T00:12:30.000000Z 字数 8998 阅读 2824

[Linux]Ubuntu/Redhat or CentOS 常用命令

linux ubuntu bash command


1. System commands

Commands Parameters/examples Usage
sudo sudo some command login as root and do something.
sudo -i prompt a login shell to login as root, and go back to /.
-s same as -i but doesn't change location. Stay where you are.
-E execute sudo and respect the environment variable set before using export. Useful when you set some variables with export and want to execute sudo with them.
apt-get/apt install some_app often with sudo. Install software from known sources.
update refresh sources to make them available for future installations. Must be executed after adding repositories or changing configurations.
upgrade some_app upgrade some app.
add-apt-repository ppa:webupd8team/atom add some ppa repository. After this, run apt(-get) update to refresh the source list.
pwd 显示当前工作目录
mount sudo mount -a mount all partitions as /etc/fstab indicate.
sudo mount mount-point target mount the filesystem in the mount-point. Mount point may be a directory under /media/username, and target may one of the filesystem under /dev with format like /dev/sda1 and so on.
whoami 显示你的用户名
blkid sudo blkid show blocked partitions and mount points. Must be run with sudo. 可以查看UUID等属性,写入/etc/fstab文件来进行开机的mount。
sudo blkid -L DATA show mount point of the partition with label "DATA"
gsettings set com.canonical.Unity.Launcher launcher-position Bottom Bottom/Left Move launcher to the bottom (requires 16.04, Unity 7)
cp cp some_file destiny copy file to some location
-i ask for confirmation before overriding
-r /dir/. /dir2/ copy all files under dir, including hidden ones, to /dir2/.
ldconfig configure dynamic linker run-time bindings 刷新动态链接库
find find . -name "xxx" find a file, recursively with current directory as starting point. . can be another directory. " " is necessary for recursive search. Without " ", is searching only this folder. With " ", if without wildcards, is exact searching. Wildcards are almost always needed to do a good search.
update-alternatives --display some_app display certain app's link in system variable
--remove absolute_path remove an app's alternative with this path.
--install path1 alia path2 priority add a link to path1 with the alia and with the priority, of the app in path2. path2 is a path ending with the linked app, not its parent folder, while path1 is the link, e.g., update-alternatives /usr/bin/java java /usr/local/java7/bin/java 0 installs a link in /usr/bin/java, of name java, with priority 0 (highest), of the executable /usr/local/java7/bin/java.
mv dir_or_file destination move some directory or file to another place.
rm some_file delete some file
-r, -R delete some directory and all its contained files.
xdg-open ./xdg-open file open this location in GUI file explorer
gnome-open ./nautilus . open file in GUI
ln create a link of a target. By default hard links(treated as original file, such as in Git: links are added and commited as the file changes.)
-s, --symbolic-link origen target create a symbolic/soft link.
getent group (not groups) get all group names in this machine
env check all system environment variables
rm -rf delete a folder even it is not empty. CAUTION!
rm -f delete a file without confirmation
kill, pkill kill kills with pid, and pkill with name. -9 is forced termination, may cause inconsistency. -15 is normal termination, allows release of resources. kill -15 pid or `pkill -15 name

2. System tools

1. vi/vim

Commands Parameters/examples Usage
vi/vim 启动vi/vim编辑器。在recovery模式下,只能启动vi而不能启动gedit。在gnome下,输入vi/vim都会启动vim。
(cmd) i 进入文档后按i来进入编辑模式。屏幕下方有提示,replace/insert表示了插入模式。
(cmd) :wq/ZZ 命令模式下使用:wq/ZZ来保存并退出。
visudo 专门用于编辑sudoer文件的vi编辑器。Run with sudo to open the /etc/sudoers file. Ubuntu/Red Hat.
-c 检查当前/etc/sudoers文件是否格式正确。
-q 打开当前/etc/sudoers文件进行编辑。更多信息可以参考 http://blog.sina.com.cn/s/blog_45d4d2c40100q70o.html

2. dpkg (Ubuntu)

Commands Parameters/examples Usage
dpkg .deb package installation/management. For Ubuntu, etc.
-i / --install some_deb_file install a package.
-r / --remove some_package remove a package.
-s / --status some_package check if some package is installed, and its details.
-S / --search some_file search a file, and what package includes this file. May find some files with same/similar name, but belonging to different packages.
-l / --list some_package list some info about this package. Not so many details like -s.
-L / --listfiles some_package list all files included in this package.

3. grep

Commands Parameters/examples Usage
grep option pattern location search text in files. Binary also.
-R "some text" /etc/apt/* search recursively "some text" in /etc/apt/.

4. samba

samba is for sharing files: build a server, etc.

https://www.tecmint.com/setup-samba-file-sharing-for-linux-windows-clients/

1. Installation

  1. yum update && yum install samba samba-client samba-common cifs-utils

2. Configuration

We add a folder to share, create a group and a user for sharing at system level. So, at last, because we grant rwx to the owner group, which is share, and not to other users(because 777 is not safe!), so we must make sure that the user belongs to group share.

  1. groupadd share # add a group share
  2. useradd win7 # add a user win7
  3. usermod -a -G share win7 # add win7 to group share
  4. mkdir /shareroot # create dir /shareroot
  5. chmod 0770 /shareroot # assign permission to user and group
  6. chgrp share /shareroot # change the group of /shareroot to share

3. Security

If we have SELinux and Firewall, we must do:

  1. setsebool -P samba_export_all_ro=1 samba_export_all_rw=1
  2. getsebool a | grep samba_export
  3. semanage fcontext at samba_share_t "/share(/.*)?"
  4. restorecon /finance
  5. firewall-cmd --permanent --add-service=samba
  6. firewall-cmd --reload

4. Samba config

Open /etc/samba/smb.conf, and add this part:

  1. [share] # the name of share
  2. comment=A simple description # whatever
  3. browsable=yes # whether the dir is browseable
  4. path=/shareroot # the dir to share. BETTER CREATE IT UNDER ROOT DIR!!!! to avoid permission problem (I created another under /home/westerngun/share and it doesn't work.)
  5. public=no # if is public, does not need auth
  6. valid users=@share # here @share means group. You can add samba user name like "user1, user2, ..."
  7. writeable=yes
  8. # keep it simple, no more!

After that, run testparm to test each section of samba config. Press Enter to get a dump(the parsing result of samba, i.e., how samba understands the file).

5. Create samba user

smbpasswd -a username add a user to samba database. The user must be created beforehand. And set password. For example:

  1. smbpasswd -a win7
  2. ... (password)

The name should be the same as the newly added user.

At last, restart samba service with:

  1. systemctl restart smb

6. Connect to share

In linux:

  1. smbclient -L <ip> -U <username>

To mount it, you can do:

  1. mount //ip/shareroot /media/samba/ -o username=win7

, if /media/samba exists.

To connect from Windows, we can map a net drive, or connect it in the address bar in the file explorer.

Enter "My Computer", choose "Map network drive". Choose a letter, enter the address:

  1. \\192.168.85.128\share

Here we must use the share's name share, not the dir name shareroot.

Enter win7 as username and its password, you are done!

If we pass file from windows to samba share, the file belongs to win7.

7. Misc

7.1 Samba Usermap file

https://www.samba.org/samba/docs/man/manpages-3/smb.conf.5.html#USERNAMEMAP

If you want to map Windows user name to Linux user name, you must edit smb.conf:

  1. [global]
  2. username map = </path/to/some/file>
  3. ...

In the file:

  1. <linux_username>=<windows_machine_name\username>
7.2 List samba users

sudo pdbedit -L -v to list all samba users. Use sudo.

7.3 Check share status

smbstatus checks how many shares are and their status.

7.4 Check which window manager you are using (CentOS or Ubuntu)

Use env to check system environment variables, and look for these two:

https://askubuntu.com/questions/72549/how-to-determine-which-window-manager-is-running#answer-72581

8. change host name

`/etc/sysconfig/network

  1. NETWORKING=yes
  2. HOSTNAME=new-name

Then:

  1. hostname new-name

and reboot.
...and it does not work!

The correct way is:

  1. hostnamectl set-hostname new-name
  2. reboot

3. 快捷键

Platform Commands Usage
Redhat Ctrl+Alt+Shift+r 录制屏幕为.webm视频,并保存在/home/username/video/文件夹下。
Ubuntu Ctrl+Alt+t Open terminal.
Redhat (any key combination you want) 在系统设置 - 键盘 - 快捷键中新建一个,名称随便,命令为gnome-terminal,点击保存后,左击选中的快捷键的右边,并按下你想要的组合键来设置。
CentOS Ctrl+Alt+F5~F7 打开命令行界面
CentOS Ctrl+Alt+F1 返回图形界面

3. Other tools

Commands Parameters/examples Usage
keytool -list -keystore path [-storetype type] list all root certificates in the keystore. If the keystore type is other than jks, -storetype is needed, for example, -storetype pkcs12.
-import -keystore path1 -file path2 import root certificates in a .crt file into a keystore. If the file is of .p12 type, will occur an Exception: not x.509 type.

Note:
p12 keys cannot be imported into java keystore, because it contains both private key and public key, but .crt only contains public key and can be imported into java keystore directly.

p12 or pks must be converted to .pem by OpenSSL, or by Internet Explorer. More info here.

When exporting from IE, we can choose contains private key and choose a password to protect it. If it has private key, it's exported as .pfx, if not, as .cer. When importing, you can also choose to import private key and public key, or only public key. If only the public key is imported, later it can be exported only as .crt. By default, for security reasons, it's imported only with public key. More info here.

Commands Parameters/examples Usage
smbstatus check samba status
systemctl reload/restart smb reload samba config files and restart samba

4. All the concurrent paths

full path What's there
/etc/apt/source.list* apt-get/apt source list
/etc/apt/apt.conf apt-get/apt config files. Proxy can be set here.
/etc/ssl/certs/ ca-certificate and all the other certificates
/etc/samba/smb.conf samba config files
/etc/group local file system groups, not including LDAP, Yellow Page, etc.
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注