收集了一些经常用到的Linux命令,以免忘记了。后续会持续增加。
grant permission of directory to specificed user - link¶
setfacl -m u:username:rwx myfolder
create user¶
useradd -r -m -s /bin/bash <USERNAME>passwd <USERNAME>chage -d0 <USERNAME>force expires user password, user must change their password once login
add user to sudoers¶
- show sudoers:
getent group sudo | cut -d: -f4- How could I list all super users? sudo adduser <username> sudo- How can I add a new user as sudoer using the command line?
remove user from sudoers¶
sudo deluser <USERNAME> sudo
disable user¶
sudo usermod --lock --expiredate 1970-01-02 <USERNAME>
re-enable user¶
sudo usermod --unlock --expiredate '' <USERNAME>
add user to group¶
sudo usermod -a -G groupName userName
change hostname of Ubuntu¶
https://linuxize.com/post/how-to-change-hostname-on-ubuntu-18-04/
- sudo hostnamectl set-hostname linuxize
- modify hostname in /etc/hosts
apt¶
apt-cache search <keyword>- to search package
io speed¶
io_test() {
(LANG=C dd if=/dev/zero of=test_$$ bs=64k count=16k conv=fdatasync && rm -f test_$$ ) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
}
setup swapfile¶
fallocate -l 16G /swapfile
chmod 600 /swapfile
ls -lh /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show
# Add this line to /etc/fstab to mount swap at boot
/swapfile swap swap defaults 0 0
swapoff /swapfile
unzip .xz¶
xz -dc < file.tar.xz > file.tar
Run only Specific Commands with sudo in Linux¶
https://kifarunix.com/run-only-specific-commands-with-sudo-in-linux/
- Fix sudoers file corruption https://askubuntu.com/questions/799669/etc-sudoers-file-corrupted-and-i-cant-run-pkexec-visudo-over-ssh
I ran into this problem also and with some digging, found a working solution. The original solution is from this github issue for NixOS by EstalillaJ.
Open two ssh sessions to the target server.
In the first session, get the PID of bash by running:
`echo $$`
In the second session, start the authentication agent with:
`pkttyagent --process (pid from step 2)`
Back in the first session, run:
`pkexec visudo`
In the second session, you will get the password prompt. visudo will start in the first session.
shutdown¶
sudo shutdown -h 19:50 # shutdown at 19:50
screen¶
pstree -p $PID
# It will show how many processes holding/using the filesystem.
> fuser -cu /local/mnt/
local/mnt: 1725e(root) 5645c(shasankarora)
ps -ef | grep 1725 <--> ps -ef | grep <pid>
kill -9 pid
# show PWD of $PID
pwdx $PID
# List all screens
# List process tree of each screen
# List processes' PWD